Skip to content

Commit 6c30bf1

Browse files
authored
[SUREFIRE-2298] fix xml output with junit 5 nested classes (#828)
* [SUREFIRE-2298] fix xml output with junit 5 nested classes Signed-off-by: Olivier Lamy <[email protected]>
1 parent 9f49866 commit 6c30bf1

File tree

28 files changed

+811
-114
lines changed

28 files changed

+811
-114
lines changed

Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ properties(
2424
buildDiscarder(logRotator(artifactNumToKeepStr: env.BRANCH_NAME == 'master' ? '15' : '5',
2525
daysToKeepStr: env.BRANCH_NAME == 'master' ? '30' : '14',
2626
numToKeepStr: env.BRANCH_NAME == 'master' ? '20' : '10')
27-
)//,
28-
//disableConcurrentBuilds()
27+
),
28+
disableConcurrentBuilds(abortPrevious: true)
2929
]
3030
)
3131
// final def oses = ['linux':'ubuntu && maven', 'windows':'windows-he']

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public class StatelessXmlReporter implements StatelessReportEventListener<Wrappe
120120

121121
private final boolean enablePropertiesElement;
122122

123+
@Deprecated
123124
public StatelessXmlReporter(
124125
File reportsDirectory,
125126
String reportNameSuffix,
@@ -205,17 +206,10 @@ private Map<String, Map<String, List<WrappedReportEntry>>> arrangeMethodStatisti
205206
Map<String, Map<String, List<WrappedReportEntry>>> classMethodStatistics = new LinkedHashMap<>();
206207
for (WrappedReportEntry methodEntry : aggregateCacheFromMultipleReruns(testSetReportEntry, testSetStats)) {
207208
String testClassName = methodEntry.getSourceName();
208-
Map<String, List<WrappedReportEntry>> stats = classMethodStatistics.get(testClassName);
209-
if (stats == null) {
210-
stats = new LinkedHashMap<>();
211-
classMethodStatistics.put(testClassName, stats);
212-
}
209+
Map<String, List<WrappedReportEntry>> stats =
210+
classMethodStatistics.computeIfAbsent(testClassName, k -> new LinkedHashMap<>());
213211
String methodName = methodEntry.getName();
214-
List<WrappedReportEntry> methodRuns = stats.get(methodName);
215-
if (methodRuns == null) {
216-
methodRuns = new ArrayList<>();
217-
stats.put(methodName, methodRuns);
218-
}
212+
List<WrappedReportEntry> methodRuns = stats.computeIfAbsent(methodName, k -> new ArrayList<>());
219213
methodRuns.add(methodEntry);
220214
}
221215
return classMethodStatistics;
@@ -416,7 +410,7 @@ private void startTestElement(XMLWriter ppw, WrappedReportEntry report) throws I
416410

417411
String className = phrasedClassName
418412
? report.getReportSourceName(reportNameSuffix)
419-
: report.getSourceName(reportNameSuffix);
413+
: report.getSourceText() != null ? report.getSourceText() : report.getSourceName(reportNameSuffix);
420414
if (className != null) {
421415
ppw.addAttribute("classname", extraEscapeAttribute(className));
422416
}

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static String concatenateWithTestGroup(MessageBuilder builder, ReportEntry repor
252252
if (phrasedClassName && report.getReportNameWithGroup() != null) {
253253
return builder.strong(report.getReportNameWithGroup()).toString();
254254
} else {
255-
String testClass = report.getNameWithGroup();
255+
String testClass = report.getSourceText() != null ? report.getSourceText() : report.getNameWithGroup();
256256
int indexOfGroup = testClass.indexOf(GROUP_PREFIX);
257257
int delimiter = testClass.lastIndexOf('.', indexOfGroup == -1 ? testClass.length() : indexOfGroup);
258258
String pkg = testClass.substring(0, 1 + delimiter);

maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/TestSetStatsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ public class TestSetStatsTest {
4444

4545
@Test
4646
public void shouldConcatenateWithTestGroup() {
47+
when(reportEntry.getSourceText()).thenReturn(null);
4748
when(reportEntry.getNameWithGroup()).thenReturn("pkg.MyTest (my group)");
4849
String actual = TestSetStats.concatenateWithTestGroup(buffer(), reportEntry, false);
50+
verify(reportEntry, times(1)).getSourceText();
4951
verify(reportEntry, times(1)).getNameWithGroup();
5052
verifyNoMoreInteractions(reportEntry);
5153
String expected = buffer().a("pkg.").strong("MyTest (my group)").toString();
@@ -54,6 +56,7 @@ public void shouldConcatenateWithTestGroup() {
5456

5557
@Test
5658
public void shouldConcatenateWithJUnit5TestGroup() {
59+
when(reportEntry.getSourceText()).thenReturn(null);
5760
when(reportEntry.getReportNameWithGroup()).thenReturn("pkg.MyTest (my group)");
5861
String actual = TestSetStats.concatenateWithTestGroup(buffer(), reportEntry, true);
5962
verify(reportEntry, atLeastOnce()).getReportNameWithGroup();
@@ -64,11 +67,13 @@ public void shouldConcatenateWithJUnit5TestGroup() {
6467

6568
@Test
6669
public void shouldFallBackToTestGroupIfJUnit5TestGroupIsNull() {
70+
when(reportEntry.getSourceText()).thenReturn(null);
6771
when(reportEntry.getReportNameWithGroup()).thenReturn(null);
6872
when(reportEntry.getNameWithGroup()).thenReturn("pkg.MyTest (my group)");
6973
String actual = TestSetStats.concatenateWithTestGroup(buffer(), reportEntry, true);
7074
verify(reportEntry, atLeastOnce()).getReportNameWithGroup();
7175
verify(reportEntry, atLeastOnce()).getNameWithGroup();
76+
verify(reportEntry, atLeastOnce()).getSourceText();
7277
verifyNoMoreInteractions(reportEntry);
7378
String expected = buffer().a("pkg.").strong("MyTest (my group)").toString();
7479
assertThat(actual).isEqualTo(expected);

surefire-its/pom.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@
3333

3434
<properties>
3535
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
36-
<its.forkCount>1C</its.forkCount>
36+
<surefire.rerunFailingTestsCount>0</surefire.rerunFailingTestsCount>
37+
<its.forkCount>0.5C</its.forkCount>
3738
<its.threadCount>1</its.threadCount>
39+
<maven.deploy.skip>true</maven.deploy.skip>
40+
<maven.install.skip>true</maven.install.skip>
41+
<maven.javadoc.skip>true</maven.javadoc.skip>
3842
</properties>
3943

4044
<dependencies>
@@ -110,6 +114,11 @@
110114
<artifactId>assertj-core</artifactId>
111115
<scope>test</scope>
112116
</dependency>
117+
<dependency>
118+
<groupId>org.xmlunit</groupId>
119+
<artifactId>xmlunit-core</artifactId>
120+
<scope>test</scope>
121+
</dependency>
113122
</dependencies>
114123

115124
<build>
@@ -189,18 +198,6 @@
189198
</execution>
190199
</executions>
191200
</plugin>
192-
<plugin>
193-
<artifactId>maven-install-plugin</artifactId>
194-
<configuration>
195-
<skip>true</skip>
196-
</configuration>
197-
</plugin>
198-
<plugin>
199-
<artifactId>maven-deploy-plugin</artifactId>
200-
<configuration>
201-
<skip>true</skip>
202-
</configuration>
203-
</plugin>
204201
</plugins>
205202
</build>
206203
<profiles>
@@ -217,7 +214,6 @@
217214
<runOrder>alphabetical</runOrder>
218215
<forkCount>${its.forkCount}</forkCount>
219216
<threadCount>${its.threadCount}</threadCount>
220-
<rerunFailingTestsCount>1</rerunFailingTestsCount>
221217
<perCoreThreadCount>false</perCoreThreadCount>
222218
<argLine>-server -Xmx64m -XX:+UseG1GC -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Djava.awt.headless=true -Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
223219
<!-- Pass current surefire version to the main suite so that it -->
@@ -251,6 +247,7 @@
251247
<properties>
252248
<its.forkCount>0.4C</its.forkCount>
253249
<its.threadCount>1</its.threadCount>
250+
<surefire.rerunFailingTestsCount>1</surefire.rerunFailingTestsCount>
254251
</properties>
255252
</profile>
256253
<profile>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.surefire.its;
20+
21+
import javax.xml.transform.Source;
22+
23+
import org.apache.maven.surefire.its.fixture.OutputValidator;
24+
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25+
import org.apache.maven.surefire.its.fixture.TestFile;
26+
import org.hamcrest.collection.IsIterableWithSize;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.w3c.dom.Node;
30+
import org.xmlunit.builder.Input;
31+
import org.xmlunit.xpath.JAXPXPathEngine;
32+
33+
import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaVersion;
34+
import static org.hamcrest.MatcherAssert.assertThat;
35+
36+
/**
37+
*
38+
*/
39+
@SuppressWarnings("checkstyle:magicnumber")
40+
public class ArchUnitIT extends SurefireJUnit4IntegrationTestCase {
41+
42+
@Before
43+
public void setUp() {
44+
assumeJavaVersion(17);
45+
}
46+
47+
@Test
48+
public void simpleCucumberUsage() throws Exception {
49+
OutputValidator outputValidator = unpack("archunit")
50+
.maven()
51+
.withFailure()
52+
.executeTest()
53+
.verifyTextInLog("BUILD FAILURE")
54+
.verifyTextInLog("classes that have simple name ending with 'DTO'")
55+
.assertTestSuiteResults(1, 0, 1, 0);
56+
57+
TestFile xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-custom.ArchUnitTest.xml");
58+
xmlReportFile.assertFileExists();
59+
60+
Source source = Input.fromFile(xmlReportFile.getFile()).build();
61+
62+
Iterable<Node> ite = new JAXPXPathEngine().selectNodes("//testcase", source);
63+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
64+
ite = new JAXPXPathEngine().selectNodes("//testcase[@classname='ArchUnitTest']", source);
65+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
66+
67+
ite = new JAXPXPathEngine().selectNodes("//testcase[@name='DTO_IN_PACKAGE_DTO']", source);
68+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
69+
}
70+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.surefire.its;
20+
21+
import javax.xml.transform.Source;
22+
23+
import org.apache.maven.surefire.its.fixture.OutputValidator;
24+
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25+
import org.apache.maven.surefire.its.fixture.TestFile;
26+
import org.hamcrest.collection.IsIterableWithSize;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.w3c.dom.Node;
30+
import org.xmlunit.builder.Input;
31+
import org.xmlunit.xpath.JAXPXPathEngine;
32+
33+
import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaVersion;
34+
import static org.hamcrest.MatcherAssert.assertThat;
35+
36+
/**
37+
*
38+
*/
39+
@SuppressWarnings("checkstyle:magicnumber")
40+
public class CucumberIT extends SurefireJUnit4IntegrationTestCase {
41+
42+
@Before
43+
public void setUp() {
44+
assumeJavaVersion(17);
45+
}
46+
47+
@Test
48+
public void simpleCucumberUsage() throws Exception {
49+
OutputValidator outputValidator = unpack("cucumber-tests")
50+
.maven()
51+
.withFailure()
52+
.executeTest()
53+
.verifyTextInLog("BUILD FAILURE")
54+
.verifyTextInLog("expected: <11> but was: <15>")
55+
.assertTestSuiteResults(3, 0, 1, 0);
56+
57+
TestFile xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-cz.fafejta.SimpleTest.xml");
58+
xmlReportFile.assertFileExists();
59+
60+
Source source = Input.fromFile(xmlReportFile.getFile()).build();
61+
62+
Iterable<Node> ite = new JAXPXPathEngine().selectNodes("//testcase", source);
63+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
64+
ite = new JAXPXPathEngine().selectNodes("//testcase[@classname='cz.fafejta.SimpleTest']", source);
65+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
66+
67+
xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-cz.fafejta.RunCucumberTest.xml");
68+
xmlReportFile.assertFileExists();
69+
70+
source = Input.fromFile(xmlReportFile.getFile()).build();
71+
72+
ite = new JAXPXPathEngine().selectNodes("//testcase", source);
73+
assertThat(ite, IsIterableWithSize.iterableWithSize(2));
74+
75+
ite = new JAXPXPathEngine().selectNodes("//testcase[@classname='RunCucumberTest']", source);
76+
assertThat(ite, IsIterableWithSize.iterableWithSize(2));
77+
78+
ite = new JAXPXPathEngine().selectNodes("//testcase[@name='Sum test - Valid test']", source);
79+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
80+
81+
ite = new JAXPXPathEngine().selectNodes("//testcase[@name='Sum test - Invalid test']", source);
82+
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
83+
}
84+
}

surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,10 @@ public class JUnitPlatformEnginesIT extends SurefireJUnit4IntegrationTestCase {
7373
@Parameters(name = "{0}")
7474
public static Iterable<Object[]> artifactVersions() {
7575
List<Object[]> args = new ArrayList<>();
76-
args.add(new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"});
77-
args.add(new Object[] {"1.3.2", "5.3.2", "1.1.1", "1.0.0"});
78-
args.add(new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"});
79-
args.add(new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"});
80-
args.add(new Object[] {"1.6.3", "5.6.3", "1.2.0", "1.1.0"});
81-
args.add(new Object[] {"1.7.2", "5.7.2", "1.2.0", "1.1.0"});
8276
args.add(new Object[] {"1.8.2", "5.8.2", "1.2.0", "1.1.2"});
8377
args.add(new Object[] {"1.9.1", "5.9.1", "1.2.0", "1.1.2"});
8478
args.add(new Object[] {"1.10.2", "5.10.2", "1.3.0", "1.1.2"});
79+
args.add(new Object[] {"1.13.4", "5.13.4", "1.3.0", "1.1.2"});
8580
return args;
8681
}
8782

surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformIT.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,11 @@ public class JUnitPlatformIT extends SurefireJUnit4IntegrationTestCase {
4848
@Parameters(name = "{0}")
4949
public static Iterable<Object[]> artifactVersions() {
5050
List<Object[]> args = new ArrayList<>();
51-
args.add(new Object[] {"5.2.0", "0.8.0"});
52-
args.add(new Object[] {"5.3.2", "0.9.0"});
53-
args.add(new Object[] {"5.4.2", "1.0.0"});
54-
args.add(new Object[] {"5.5.2", "0.8.15"});
55-
args.add(new Object[] {"5.6.2", "1.3.5"});
56-
args.add(new Object[] {"5.7.2", "1.5.5"});
5751
args.add(new Object[] {"5.8.2", "1.6.5"});
5852
args.add(new Object[] {"5.9.1", "1.7.1"});
5953
args.add(new Object[] {"5.10.2", "1.8.5"});
60-
args.add(new Object[] {"5.11.0-M2", "1.8.5"});
54+
args.add(new Object[] {"5.11.4", "1.8.5"});
55+
args.add(new Object[] {"5.13.4", "1.8.5"});
6156
return args;
6257
}
6358

surefire-its/src/test/java/org/apache/maven/surefire/its/fixture/SurefireLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private List<String> getInitialGoals() {
8686
jacocoAgent = jacocoAgent.replace("\"", "");
8787
goals.add("-Djacoco.agent=" + jacocoAgent);
8888
goals.add("-nsu");
89-
89+
goals.add("-Dmaven.build.cache.enabled=false");
9090
return goals;
9191
}
9292

0 commit comments

Comments
 (0)