Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-2298] fix xml output with junit 5 nested classes #828

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
import org.apache.maven.surefire.api.report.SafeThrowable;
import org.apache.maven.surefire.extensions.StatelessReportEventListener;
import org.apache.maven.surefire.shared.utils.StringUtils;
import org.apache.maven.surefire.shared.utils.xml.PrettyPrintXMLWriter;
import org.apache.maven.surefire.shared.utils.xml.XMLWriter;

Expand Down Expand Up @@ -414,9 +415,14 @@ private void startTestElement(XMLWriter ppw, WrappedReportEntry report) throws I
ppw.addAttribute("group", report.getGroup());
}

String className = phrasedClassName
? report.getReportSourceName(reportNameSuffix)
: report.getSourceName(reportNameSuffix);
String className;
if (StringUtils.isBlank(report.getSourceText())) {
className = phrasedClassName
? report.getReportSourceName(reportNameSuffix)
: report.getSourceName(reportNameSuffix);
} else {
className = report.getSourceText();
}
if (className != null) {
ppw.addAttribute("classname", extraEscapeAttribute(className));
}
Expand Down
5 changes: 5 additions & 0 deletions surefire-its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/
package org.apache.maven.surefire.its.jiras;

import org.apache.maven.surefire.its.fixture.OutputValidator;
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
import org.apache.maven.surefire.its.fixture.TestFile;
import org.hamcrest.collection.IsIterableWithSize;
import org.junit.Test;
import org.w3c.dom.Node;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;

import static org.hamcrest.MatcherAssert.assertThat;

/**
*
*/
@SuppressWarnings("checkstyle:magicnumber")
public class Surefire2298IT extends SurefireJUnit4IntegrationTestCase {
@Test
public void shouldNotCreateFilesForNested() throws Exception {
OutputValidator outputValidator = unpack("surefire-2298-nested-class")
.maven()
.executeTest()
.verifyTextInLog("BUILD SUCCESS")
.assertTestSuiteResults(4, 0, 0, 0);

outputValidator
.getSurefireReportsXmlFile("io.olamy.BaseNestedTest$Inner.txt")
.assertFileNotExists();

TestFile xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-io.olamy.FirstNestedTest.xml");
xmlReportFile.assertFileExists();

/**
* <testsuite version="3.0.2" name="FirstNestedTest" time="0.019" tests="1" errors="0" skipped="0" failures="0">
* <testcase name="outerTest" classname="FirstNestedTest" time="0.007"/>
* <testcase name="innerTest" classname="BaseNestedTest$Inner" time="0.001"/>
* </testsuite>
**/
Iterable<Node> ite = new JAXPXPathEngine()
.selectNodes(
"//testcase", Input.fromFile(xmlReportFile.getFile()).build());
assertThat(ite, IsIterableWithSize.iterableWithSize(2));
ite = new JAXPXPathEngine()
.selectNodes(
"//testcase[@classname='FirstNestedTest']",
Input.fromFile(xmlReportFile.getFile()).build());
assertThat(ite, IsIterableWithSize.iterableWithSize(1));
ite = new JAXPXPathEngine()
.selectNodes(
"//testcase[@classname='BaseNestedTest$Inner']",
Input.fromFile(xmlReportFile.getFile()).build());
assertThat(ite, IsIterableWithSize.iterableWithSize(1));

xmlReportFile = outputValidator.getSurefireReportsXmlFile("TEST-io.olamy.SecondNestedTest.xml");
/**
* <testsuite name="SecondNestedTest" time="0.003" tests="1" errors="0" skipped="0" failures="0">
* <testcase name="outerTest" classname="SecondNestedTest" time="0.001"/>
* <testcase name="innerTest" classname="BaseNestedTest$Inner" time="0.0"/>
* </testsuite>
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assertions missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha you're too fast :)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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>junit-platform-1.0.0</artifactId>
<artifactId>junit-platform-1.0.0-surefire-1643</artifactId>
<version>1.0</version>
<name>[SUREFIRE-1643] JUnit 5: Parallel Test mixed</name>

Expand Down
73 changes: 73 additions & 0 deletions surefire-its/src/test/resources/surefire-2298-nested-class/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>junit-platform-1.0.0-surefire-2298</artifactId>
<version>1.0</version>
<name>[SUREFIRE-2298] JUnit 5: Nested class in report</name>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<junit.jupiter.execution.parallel.config.fixed.parallelism>2</junit.jupiter.execution.parallel.config.fixed.parallelism>
<junit.jupiter.execution.parallel.config.strategy>fixed</junit.jupiter.execution.parallel.config.strategy>
<junit.jupiter.execution.parallel.enabled>true</junit.jupiter.execution.parallel.enabled>
<junit.jupiter.execution.parallel.mode.classes.default>concurrent</junit.jupiter.execution.parallel.mode.classes.default>
<junit.jupiter.execution.parallel.mode.default>same_thread</junit.jupiter.execution.parallel.mode.default>
<junit.jupiter.extensions.autodetection.enabled>true</junit.jupiter.extensions.autodetection.enabled>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.12.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.olamy;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.olamy;

import org.junit.jupiter.api.*;

abstract class BaseNestedTest {
@Test
void outerTest() {
}

@Nested
class Inner {

@Test
void innerTest() {
}
}
}

class FirstNestedTest extends BaseNestedTest {
}

class SecondNestedTest extends BaseNestedTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.maven.surefire.report.RunModeSetter;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.TestExecutionListener;
Expand Down Expand Up @@ -216,11 +217,25 @@ private SimpleReportEntry createReportEntry(
String reason,
Integer elapsedTime) {
String[] classMethodName = toClassMethodName(testIdentifier);
String className = classMethodName[0];

Optional<UniqueId.Segment> classSegment = testIdentifier.getUniqueIdObject().getSegments().stream()
.filter(segment -> "class".equals(segment.getType()))
.findFirst();
Comment on lines +221 to +223
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't parse the UniqueId because it's considered to be an implementation detail. Instead, you should use testPlan.getParent(testIdentifier) to find the right level. If this should only support class-based engines, finding the last one with a TestSource of type ClassSource should work. If this should also work for file-based engines like Cucumber, you might want to use a differeny heuristic instead. Maybe always use the level immediate below the root (engine descriptor)? If it has a ClassSource, use that; otherwise, use the display name (similar to what toClassMethodName does)?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Cucumber was also broken by the last patch, so the alternative heuristic should be used.


String className;
if (classSegment.isPresent()) {
className = classSegment.get().getValue();
} else {
className = classMethodName[0];
}

String classText = classMethodName[1];
if (Objects.equals(className, classText)) {
classText = null;
}

classText = classMethodName[0]; // testIdentifier.getLegacyReportingName();

boolean failed = testExecutionResult == null || testExecutionResult.getStatus() == FAILED;
String methodName = failed || testIdentifier.isTest() ? classMethodName[2] : null;
String methodText = failed || testIdentifier.isTest() ? classMethodName[3] : null;
Expand Down
Loading