Skip to content

Commit 4f4ac97

Browse files
andpabslawekjaranowski
authored andcommitted
[SUREFIRE-2117] Include package + outer classes in XML report of @nested
1 parent 7ade5cc commit 4f4ac97

File tree

6 files changed

+391
-1
lines changed

6 files changed

+391
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.apache.maven.surefire.its.jiras;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import static java.nio.charset.StandardCharsets.UTF_8;
23+
24+
import java.util.Arrays;
25+
26+
import org.apache.maven.surefire.its.fixture.OutputValidator;
27+
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.Parameterized;
31+
import org.junit.runners.Parameterized.Parameter;
32+
import org.junit.runners.Parameterized.Parameters;
33+
34+
/**
35+
* Integration Test for SUREFIRE-2117
36+
*/
37+
@RunWith( Parameterized.class )
38+
@SuppressWarnings( "checkstyle:magicnumber" )
39+
public class Surefire2117XmlReportingNestedIT extends SurefireJUnit4IntegrationTestCase
40+
{
41+
@Parameter
42+
@SuppressWarnings( "checkstyle:visibilitymodifier" )
43+
public String jupiterVersion;
44+
45+
@Parameters( name = "{0}" )
46+
public static Iterable<?> junitJupiterVersions()
47+
{
48+
return Arrays.asList( "5.2.0", "5.8.2", "5.9.1" );
49+
}
50+
51+
@Test
52+
public void testXmlReport()
53+
{
54+
OutputValidator validator = unpack( "surefire-2117-xml-reporting-nested", "-" + jupiterVersion )
55+
.sysProp( "junit5.version", jupiterVersion )
56+
.executeTest()
57+
.verifyErrorFree( 9 );
58+
59+
validator.getSurefireReportsFile( "TEST-jira2117.NestedJupiterTest$A.xml", UTF_8 )
60+
.assertContainsText( "<testcase name=\"level1_test\" "
61+
+ "classname=\"jira2117.NestedJupiterTest$A\"" );
62+
63+
validator.getSurefireReportsFile( "TEST-jira2117.NestedJupiterTest$B$C.xml", UTF_8 )
64+
.assertContainsText( "<testcase name=\"level2_test_nonparameterized\" "
65+
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" )
66+
.assertContainsText( "<testcase name=\"level2_test_parameterized(String)[1] paramValue1\" "
67+
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" )
68+
.assertContainsText( "<testcase name=\"level2_test_parameterized(String)[2] paramValue2\" "
69+
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" );
70+
71+
String expectedDisplayNameForNestedClassA = String.join( " ",
72+
"Display name of the main test class",
73+
"Display name of level 1 nested class A" );
74+
75+
validator.getSurefireReportsFile( "TEST-jira2117.NestedDisplayNameTest$A.xml", UTF_8 )
76+
.assertContainsText( "<testcase name=\"level1_test_without_display_name\" "
77+
+ "classname=\"" + expectedDisplayNameForNestedClassA + "\"" )
78+
.assertContainsText( "<testcase name=\"Display name of level 1 test method\" "
79+
+ "classname=\"" + expectedDisplayNameForNestedClassA + "\"" );
80+
81+
String expectedDisplayNameForNestedClassC = String.join( " ",
82+
"Display name of the main test class",
83+
"Display name of level 1 nested class B",
84+
"Display name of level 2 nested class C" );
85+
86+
validator.getSurefireReportsFile( "TEST-jira2117.NestedDisplayNameTest$B$C.xml", UTF_8 )
87+
.assertContainsText(
88+
"<testcase name=\"Display name of non-parameterized level 2 test method\" "
89+
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" )
90+
.assertContainsText(
91+
"<testcase name=\"Display name of parameterized level 2 test method[1] paramValue1\" "
92+
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" )
93+
.assertContainsText(
94+
"<testcase name=\"Display name of parameterized level 2 test method[2] paramValue2\" "
95+
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" );
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins.surefire</groupId>
27+
<artifactId>surefire-2117-xml-reporting-nested</artifactId>
28+
<version>1.0</version>
29+
<name>Test for: JUnit5Xml30StatelessReporter and Nested</name>
30+
31+
<properties>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
35+
</properties>
36+
37+
<!--
38+
Declare "junit-jupiter-engine" dependency because the
39+
Jupiter Engine is needed at test runtime. Artifacts
40+
needed for test compilation, like "junit-jupiter-api",
41+
are pulled-in via transitive dependency resolution.
42+
-->
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter-engine</artifactId>
47+
<version>${junit5.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter-params</artifactId>
53+
<version>${junit5.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<version>3.8.0</version>
63+
<configuration>
64+
<encoding>UTF-8</encoding>
65+
</configuration>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-surefire-plugin</artifactId>
70+
<version>${surefire.version}</version>
71+
<configuration>
72+
<forkCount>1.0C</forkCount>
73+
<redirectTestOutputToFile>true</redirectTestOutputToFile>
74+
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
75+
<disable>false</disable>
76+
<version>3.0</version>
77+
<usePhrasedFileName>false</usePhrasedFileName>
78+
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
79+
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
80+
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
81+
</statelessTestsetReporter>
82+
<consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
83+
<disable>false</disable>
84+
<encoding>UTF-8</encoding>
85+
<usePhrasedFileName>false</usePhrasedFileName>
86+
</consoleOutputReporter>
87+
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
88+
<disable>false</disable>
89+
<usePhrasedFileName>false</usePhrasedFileName>
90+
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
91+
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
92+
</statelessTestsetInfoReporter>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
98+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package jira2117;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.junit.jupiter.api.DisplayName;
23+
import org.junit.jupiter.api.Nested;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
27+
28+
@DisplayName( "Display name of the main test class" )
29+
class NestedDisplayNameTest
30+
{
31+
@Nested
32+
@DisplayName( "Display name of level 1 nested class A" )
33+
class A
34+
{
35+
@Test
36+
void level1_test_without_display_name()
37+
{
38+
}
39+
40+
@Test
41+
@DisplayName( "Display name of level 1 test method" )
42+
void level1_test_with_display_name()
43+
{
44+
}
45+
}
46+
47+
@Nested
48+
@DisplayName( "Display name of level 1 nested class B" )
49+
class B
50+
{
51+
@Nested
52+
@DisplayName( "Display name of level 2 nested class C" )
53+
class C
54+
{
55+
@Test
56+
@DisplayName( "Display name of non-parameterized level 2 test method" )
57+
void level2_test_nonparameterized()
58+
{
59+
}
60+
61+
@ParameterizedTest
62+
@ValueSource(strings = {"paramValue1", "paramValue2"})
63+
@DisplayName( "Display name of parameterized level 2 test method" )
64+
void level2_test_parameterized(String paramValue)
65+
{
66+
}
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package jira2117;
2+
3+
import org.junit.jupiter.api.Nested;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.ValueSource;
7+
8+
/*
9+
* Licensed to the Apache Software Foundation (ASF) under one
10+
* or more contributor license agreements. See the NOTICE file
11+
* distributed with this work for additional information
12+
* regarding copyright ownership. The ASF licenses this file
13+
* to you under the Apache License, Version 2.0 (the
14+
* "License"); you may not use this file except in compliance
15+
* with the License. You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing,
20+
* software distributed under the License is distributed on an
21+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22+
* KIND, either express or implied. See the License for the
23+
* specific language governing permissions and limitations
24+
* under the License.
25+
*/
26+
27+
class NestedJupiterTest
28+
{
29+
@Nested
30+
class A
31+
{
32+
@Test
33+
void level1_test()
34+
{
35+
}
36+
}
37+
38+
@Nested
39+
class B
40+
{
41+
@Nested
42+
class C
43+
{
44+
@Test
45+
void level2_test_nonparameterized()
46+
{
47+
}
48+
49+
@ParameterizedTest
50+
@ValueSource(strings = {"paramValue1", "paramValue2"})
51+
void level2_test_parameterized(String paramValue)
52+
{
53+
}
54+
}
55+
}
56+
}

surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
import static java.util.Collections.emptyMap;
2323
import static java.util.stream.Collectors.joining;
24+
import static java.util.stream.Collectors.toList;
2425
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
2526
import static org.apache.maven.surefire.shared.lang3.StringUtils.isNotBlank;
2627
import static org.junit.platform.engine.TestExecutionResult.Status.FAILED;
2728

29+
import java.util.Collections;
30+
import java.util.List;
2831
import java.util.Map;
2932
import java.util.Objects;
3033
import java.util.Optional;
@@ -356,10 +359,20 @@ private String[] toClassMethodName( TestIdentifier testIdentifier )
356359
}
357360
else if ( testSource.filter( ClassSource.class::isInstance ).isPresent() )
358361
{
362+
List<String> parentClassDisplays =
363+
collectAllTestIdentifiersInHierarchy( testIdentifier )
364+
.filter( identifier -> identifier.getSource().filter( ClassSource.class::isInstance ).isPresent() )
365+
.map( TestIdentifier::getDisplayName )
366+
.collect( toList() );
367+
368+
Collections.reverse( parentClassDisplays );
369+
String classDisplay = Stream.concat( parentClassDisplays.stream(), Stream.of( display ) )
370+
.collect( joining( " " ) );
371+
359372
ClassSource classSource = testSource.map( ClassSource.class::cast ).get();
360373
String className = classSource.getClassName();
361374
String simpleClassName = className.substring( 1 + className.lastIndexOf( '.' ) );
362-
String source = display.equals( simpleClassName ) ? className : display;
375+
String source = classDisplay.replace( ' ', '$' ).equals( simpleClassName ) ? className : classDisplay;
363376
return new String[] {className, source, null, null};
364377
}
365378
else

0 commit comments

Comments
 (0)