Skip to content

Commit 5eead11

Browse files
authored
fix failure related to blank string parameters in junit 5 (fixes #697, via #698)
1 parent 233d699 commit 5eead11

File tree

6 files changed

+115
-15
lines changed

6 files changed

+115
-15
lines changed

allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.ArrayList;
5151
import java.util.Arrays;
5252
import java.util.Collections;
53+
import java.util.HashMap;
5354
import java.util.HashSet;
5455
import java.util.List;
5556
import java.util.Map;
@@ -90,6 +91,9 @@
9091
})
9192
public class AllureJunitPlatform implements TestExecutionListener {
9293

94+
public static final String ALLURE_REPORT_ENTRY_BLANK_PREFIX
95+
= "ALLURE_REPORT_ENTRY_BLANK_PREFIX__";
96+
9397
public static final String ALLURE_PARAMETER = "allure.parameter";
9498
public static final String ALLURE_PARAMETER_VALUE_KEY = "value";
9599
public static final String ALLURE_PARAMETER_MODE_KEY = "mode";
@@ -195,11 +199,11 @@ public void executionSkipped(final TestIdentifier testIdentifier,
195199
);
196200
}
197201

198-
@SuppressWarnings({"ReturnCount", "PMD.NcssCount", "CyclomaticComplexity"})
202+
@SuppressWarnings({"ReturnCount", "PMD.NcssCount", "CyclomaticComplexity" })
199203
@Override
200204
public void reportingEntryPublished(final TestIdentifier testIdentifier,
201205
final ReportEntry entry) {
202-
final Map<String, String> keyValuePairs = entry.getKeyValuePairs();
206+
final Map<String, String> keyValuePairs = unwrap(entry.getKeyValuePairs());
203207
if (keyValuePairs.containsKey(ALLURE_FIXTURE)) {
204208
processFixtureEvent(testIdentifier, keyValuePairs);
205209
return;
@@ -220,11 +224,33 @@ public void reportingEntryPublished(final TestIdentifier testIdentifier,
220224

221225
}
222226

227+
private Map<String, String> unwrap(final Map<String, String> data) {
228+
final Map<String, String> res = new HashMap<>();
229+
data.forEach((key, value) -> {
230+
if (Objects.nonNull(value)
231+
&& value.trim().isEmpty()
232+
&& value.startsWith(ALLURE_REPORT_ENTRY_BLANK_PREFIX)) {
233+
res.put(key, value.substring(ALLURE_REPORT_ENTRY_BLANK_PREFIX.length()));
234+
} else {
235+
res.put(key, value);
236+
}
237+
}
238+
);
239+
return res;
240+
}
241+
223242
private void processParameterEvent(final Map<String, String> keyValuePairs) {
224243
final String name = keyValuePairs.get(ALLURE_PARAMETER);
225244
final String value = keyValuePairs.get(ALLURE_PARAMETER_VALUE_KEY);
226-
227-
final Parameter parameter = ResultsUtils.createParameter(name, value);
245+
final Parameter parameter;
246+
if (Objects.nonNull(value) && value.startsWith(ALLURE_REPORT_ENTRY_BLANK_PREFIX)) {
247+
parameter = ResultsUtils.createParameter(
248+
name,
249+
value.substring(ALLURE_REPORT_ENTRY_BLANK_PREFIX.length())
250+
);
251+
} else {
252+
parameter = ResultsUtils.createParameter(name, value);
253+
}
228254
if (keyValuePairs.containsKey(ALLURE_PARAMETER_MODE_KEY)) {
229255
final String modeString = keyValuePairs.get(ALLURE_PARAMETER_MODE_KEY);
230256
Stream.of(Parameter.Mode.values())
@@ -244,7 +270,7 @@ private void processParameterEvent(final Map<String, String> keyValuePairs) {
244270
);
245271
}
246272

247-
@SuppressWarnings({"ReturnCount"})
273+
@SuppressWarnings({"ReturnCount" })
248274
private void processFixtureEvent(final TestIdentifier testIdentifier,
249275
final Map<String, String> keyValuePairs) {
250276
final String type = keyValuePairs.get(ALLURE_FIXTURE);

allure-junit5/src/main/java/io/qameta/allure/junit5/AllureJunit5.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.lang.reflect.Parameter;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.Objects;
3132
import java.util.Optional;
3233
import java.util.UUID;
3334
import java.util.stream.Stream;
@@ -37,6 +38,7 @@
3738
import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_EXCLUDED_KEY;
3839
import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_MODE_KEY;
3940
import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_VALUE_KEY;
41+
import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_REPORT_ENTRY_BLANK_PREFIX;
4042
import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_FAILURE;
4143
import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_START;
4244
import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_STOP;
@@ -86,7 +88,7 @@ private void sendParameterEvent(final ReflectiveInvocationContext<Method> invoca
8688
map.put(ALLURE_PARAMETER_EXCLUDED_KEY, Boolean.toString(param.excluded()));
8789
});
8890

89-
extensionContext.publishReportEntry(map);
91+
extensionContext.publishReportEntry(wrap(map));
9092
}
9193
}
9294

@@ -128,22 +130,22 @@ protected void processFixture(final String type,
128130
final ExtensionContext extensionContext) throws Throwable {
129131
final String uuid = UUID.randomUUID().toString();
130132
try {
131-
extensionContext.publishReportEntry(buildStartEvent(
133+
extensionContext.publishReportEntry(wrap(buildStartEvent(
132134
type,
133135
uuid,
134136
invocationContext.getExecutable()
135-
));
137+
)));
136138
invocation.proceed();
137-
extensionContext.publishReportEntry(buildStopEvent(
139+
extensionContext.publishReportEntry(wrap(buildStopEvent(
138140
type,
139141
uuid
140-
));
142+
)));
141143
} catch (Throwable throwable) {
142-
extensionContext.publishReportEntry(buildFailureEvent(
144+
extensionContext.publishReportEntry(wrap(buildFailureEvent(
143145
type,
144146
uuid,
145147
throwable
146-
));
148+
)));
147149
throw throwable;
148150
}
149151
}
@@ -184,4 +186,17 @@ public Map<String, String> buildFailureEvent(final String type,
184186
maybeDetails.map(StatusDetails::getTrace).ifPresent(trace -> map.put("trace", trace));
185187
return map;
186188
}
189+
190+
public Map<String, String> wrap(final Map<String, String> data) {
191+
final Map<String, String> res = new HashMap<>();
192+
data.forEach((key, value) -> {
193+
if (Objects.isNull(value) || value.trim().isEmpty()) {
194+
res.put(key, ALLURE_REPORT_ENTRY_BLANK_PREFIX + value);
195+
} else {
196+
res.put(key, value);
197+
}
198+
}
199+
);
200+
return res;
201+
}
187202
}

allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import io.qameta.allure.Allure;
1919
import io.qameta.allure.AllureLifecycle;
20+
import io.qameta.allure.Issue;
2021
import io.qameta.allure.Step;
2122
import io.qameta.allure.aspects.AttachmentsAspects;
2223
import io.qameta.allure.aspects.StepsAspects;
2324
import io.qameta.allure.junit5.features.AfterEachFixtureFailureSupport;
2425
import io.qameta.allure.junit5.features.AllFixtureSupport;
2526
import io.qameta.allure.junit5.features.BeforeEachFixtureFailureSupport;
2627
import io.qameta.allure.junit5.features.EachFixtureSupport;
28+
import io.qameta.allure.junit5.features.ParameterisedBlankParameterValueTests;
2729
import io.qameta.allure.junit5.features.ParameterisedPrimitivesTests;
2830
import io.qameta.allure.junit5.features.ParameterisedTests;
2931
import io.qameta.allure.junit5.features.SkipOtherInjectables;
@@ -57,6 +59,20 @@
5759
@AllureFeatures.Fixtures
5860
class AllureJunit5Test {
5961

62+
@Issue("697")
63+
@Test
64+
void shouldSupportEmptyStringParameters() {
65+
final AllureResults results = runClasses(ParameterisedBlankParameterValueTests.class);
66+
67+
assertThat(results.getTestResults())
68+
.extracting(TestResult::getName, tr -> tr.getParameters().size())
69+
.containsExactlyInAnyOrder(
70+
tuple("first(String) [1] value=", 2),
71+
tuple("first(String) [2] value= ", 2),
72+
tuple("first(String) [3] value=null", 2)
73+
);
74+
}
75+
6076
@Test
6177
void shouldSupportPrimitiveTypeParameters() {
6278
final AllureResults results = runClasses(ParameterisedPrimitivesTests.class);
@@ -82,7 +98,7 @@ void shouldSupportPrimitiveTypeParameters() {
8298
tuple("chars(char) [3] value=c", 2),
8399
tuple("longs(long) [1] value=0", 2),
84100
tuple("longs(long) [2] value=1", 2),
85-
tuple("nullMethodSource(String) [1] value=null", 2),
101+
tuple("nullMethodSource(String, Long) [1] stringValue=null, longValue=null", 3),
86102
tuple("doubles(double) [1] value=0.1", 2),
87103
tuple("doubles(double) [2] value=0.01", 2),
88104
tuple("booleans(boolean) [1] value=true", 2),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.junit5.features;
17+
18+
import org.junit.jupiter.params.ParameterizedTest;
19+
import org.junit.jupiter.params.provider.MethodSource;
20+
21+
import java.util.stream.Stream;
22+
23+
/**
24+
* @author charlie (Dmitry Baev).
25+
*/
26+
public class ParameterisedBlankParameterValueTests {
27+
28+
@ParameterizedTest
29+
@MethodSource("arguments")
30+
void first(String value) {
31+
}
32+
33+
static Stream<String> arguments() {
34+
return Stream.of(
35+
"",
36+
" ",
37+
null
38+
);
39+
}
40+
41+
}

allure-junit5/src/test/java/io/qameta/allure/junit5/features/ParameterisedPrimitivesTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void booleansMethodSource(final boolean a, final boolean b) {
7474

7575
@ParameterizedTest
7676
@MethodSource("nulls")
77-
void nullMethodSource(final String value) {
77+
void nullMethodSource(final String stringValue,
78+
final Long longValue) {
7879
}
7980

8081
static Stream<Arguments> arguments() {
@@ -88,7 +89,7 @@ static Stream<Arguments> arguments() {
8889

8990
static Stream<Arguments> nulls() {
9091
return Stream.of(
91-
Arguments.of((String) null)
92+
Arguments.of(null, null)
9293
);
9394
}
9495

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
allure.results.directory=build/allure-results
22
allure.label.epic=#project.description#
3+
allure.link.issue.pattern=https://github.com/allure-framework/allure-java/issues/{}

0 commit comments

Comments
 (0)