Skip to content

Commit

Permalink
[SUREFIRE-2117] Include package + outer classes in XML report of @nested
Browse files Browse the repository at this point in the history
  • Loading branch information
andpab authored and slawekjaranowski committed Dec 28, 2022
1 parent 7ade5cc commit 4f4ac97
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.apache.maven.surefire.its.jiras;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.Arrays;

import org.apache.maven.surefire.its.fixture.OutputValidator;
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

/**
* Integration Test for SUREFIRE-2117
*/
@RunWith( Parameterized.class )
@SuppressWarnings( "checkstyle:magicnumber" )
public class Surefire2117XmlReportingNestedIT extends SurefireJUnit4IntegrationTestCase
{
@Parameter
@SuppressWarnings( "checkstyle:visibilitymodifier" )
public String jupiterVersion;

@Parameters( name = "{0}" )
public static Iterable<?> junitJupiterVersions()
{
return Arrays.asList( "5.2.0", "5.8.2", "5.9.1" );
}

@Test
public void testXmlReport()
{
OutputValidator validator = unpack( "surefire-2117-xml-reporting-nested", "-" + jupiterVersion )
.sysProp( "junit5.version", jupiterVersion )
.executeTest()
.verifyErrorFree( 9 );

validator.getSurefireReportsFile( "TEST-jira2117.NestedJupiterTest$A.xml", UTF_8 )
.assertContainsText( "<testcase name=\"level1_test\" "
+ "classname=\"jira2117.NestedJupiterTest$A\"" );

validator.getSurefireReportsFile( "TEST-jira2117.NestedJupiterTest$B$C.xml", UTF_8 )
.assertContainsText( "<testcase name=\"level2_test_nonparameterized\" "
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" )
.assertContainsText( "<testcase name=\"level2_test_parameterized(String)[1] paramValue1\" "
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" )
.assertContainsText( "<testcase name=\"level2_test_parameterized(String)[2] paramValue2\" "
+ "classname=\"jira2117.NestedJupiterTest$B$C\"" );

String expectedDisplayNameForNestedClassA = String.join( " ",
"Display name of the main test class",
"Display name of level 1 nested class A" );

validator.getSurefireReportsFile( "TEST-jira2117.NestedDisplayNameTest$A.xml", UTF_8 )
.assertContainsText( "<testcase name=\"level1_test_without_display_name\" "
+ "classname=\"" + expectedDisplayNameForNestedClassA + "\"" )
.assertContainsText( "<testcase name=\"Display name of level 1 test method\" "
+ "classname=\"" + expectedDisplayNameForNestedClassA + "\"" );

String expectedDisplayNameForNestedClassC = String.join( " ",
"Display name of the main test class",
"Display name of level 1 nested class B",
"Display name of level 2 nested class C" );

validator.getSurefireReportsFile( "TEST-jira2117.NestedDisplayNameTest$B$C.xml", UTF_8 )
.assertContainsText(
"<testcase name=\"Display name of non-parameterized level 2 test method\" "
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" )
.assertContainsText(
"<testcase name=\"Display name of parameterized level 2 test method[1] paramValue1\" "
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" )
.assertContainsText(
"<testcase name=\"Display name of parameterized level 2 test method[2] paramValue2\" "
+ "classname=\"" + expectedDisplayNameForNestedClassC + "\"" );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.surefire</groupId>
<artifactId>surefire-2117-xml-reporting-nested</artifactId>
<version>1.0</version>
<name>Test for: JUnit5Xml30StatelessReporter and Nested</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<!--
Declare "junit-jupiter-engine" dependency because the
Jupiter Engine is needed at test runtime. Artifacts
needed for test compilation, like "junit-jupiter-api",
are pulled-in via transitive dependency resolution.
-->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<forkCount>1.0C</forkCount>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<disable>false</disable>
<version>3.0</version>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
<consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
<disable>false</disable>
<encoding>UTF-8</encoding>
<usePhrasedFileName>false</usePhrasedFileName>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
<disable>false</disable>
<usePhrasedFileName>false</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package jira2117;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@DisplayName( "Display name of the main test class" )
class NestedDisplayNameTest
{
@Nested
@DisplayName( "Display name of level 1 nested class A" )
class A
{
@Test
void level1_test_without_display_name()
{
}

@Test
@DisplayName( "Display name of level 1 test method" )
void level1_test_with_display_name()
{
}
}

@Nested
@DisplayName( "Display name of level 1 nested class B" )
class B
{
@Nested
@DisplayName( "Display name of level 2 nested class C" )
class C
{
@Test
@DisplayName( "Display name of non-parameterized level 2 test method" )
void level2_test_nonparameterized()
{
}

@ParameterizedTest
@ValueSource(strings = {"paramValue1", "paramValue2"})
@DisplayName( "Display name of parameterized level 2 test method" )
void level2_test_parameterized(String paramValue)
{
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package jira2117;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class NestedJupiterTest
{
@Nested
class A
{
@Test
void level1_test()
{
}
}

@Nested
class B
{
@Nested
class C
{
@Test
void level2_test_nonparameterized()
{
}

@ParameterizedTest
@ValueSource(strings = {"paramValue1", "paramValue2"})
void level2_test_parameterized(String paramValue)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isNotBlank;
import static org.junit.platform.engine.TestExecutionResult.Status.FAILED;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -356,10 +359,20 @@ private String[] toClassMethodName( TestIdentifier testIdentifier )
}
else if ( testSource.filter( ClassSource.class::isInstance ).isPresent() )
{
List<String> parentClassDisplays =
collectAllTestIdentifiersInHierarchy( testIdentifier )
.filter( identifier -> identifier.getSource().filter( ClassSource.class::isInstance ).isPresent() )
.map( TestIdentifier::getDisplayName )
.collect( toList() );

Collections.reverse( parentClassDisplays );
String classDisplay = Stream.concat( parentClassDisplays.stream(), Stream.of( display ) )
.collect( joining( " " ) );

ClassSource classSource = testSource.map( ClassSource.class::cast ).get();
String className = classSource.getClassName();
String simpleClassName = className.substring( 1 + className.lastIndexOf( '.' ) );
String source = display.equals( simpleClassName ) ? className : display;
String source = classDisplay.replace( ' ', '$' ).equals( simpleClassName ) ? className : classDisplay;
return new String[] {className, source, null, null};
}
else
Expand Down
Loading

0 comments on commit 4f4ac97

Please sign in to comment.