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

Add flakes to the failsafe-summary.xml. Fix a bug in the equals metho… #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -64,6 +64,7 @@ public final class FailsafeSummaryXmlUtils {
+ " <errors>%d</errors>\n"
+ " <failures>%d</failures>\n"
+ " <skipped>%d</skipped>\n"
+ " <flakes>%d</flakes>\n"
+ " %s\n"
+ "</failsafe-summary>";

Expand All @@ -84,12 +85,14 @@ public static RunResult toRunResult(File failsafeSummaryXml) throws Exception {
String skipped = xpath.evaluate("/failsafe-summary/skipped", root);
String failureMessage = xpath.evaluate("/failsafe-summary/failureMessage", root);
String timeout = xpath.evaluate("/failsafe-summary/@timeout", root);
String flakes = xpath.evaluate("/failsafe-summary/flakes", root);

Choose a reason for hiding this comment

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

Probably not a problem:

What happens when you don't put flakes in the xml string? Does this fail well?

Copy link
Author

Choose a reason for hiding this comment

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

https://github.com/atlassian-forks/maven-surefire/pull/1/files#diff-5bf0b71acbf01d365bb28569f502903f8cb33d96ec98b0c1c53bab5a5aa7ee5fR95

Yeah; this function doesn't throw when the field isn't present. It will just return a blank string (and I'm handling that case in the linked line of code). We'll set flakes = 0 when the flakes XML element isn't present.


return new RunResult(
parseInt(completed),
parseInt(errors),
parseInt(failures),
parseInt(skipped),
isBlank(flakes) ? 0 : parseInt(flakes),
isBlank(failureMessage) ? null : unescapeXml(failureMessage),
parseBoolean(timeout));
}
Expand All @@ -107,6 +110,7 @@ public static void fromRunResultToFile(RunResult fromRunResult, File toFailsafeS
fromRunResult.getErrors(),
fromRunResult.getFailures(),
fromRunResult.getSkipped(),
fromRunResult.getFlakes(),
msg);

Files.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
package org.apache.maven.plugin.failsafe;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Locale;

import org.apache.maven.plugin.failsafe.util.FailsafeSummaryXmlUtils;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.util.SureFireFileManager;
import org.junit.Test;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -64,6 +69,49 @@ public void testSkipped() throws Exception {
writeReadCheck(new RunResult(3, 2, 1, 0, null, true));
}

@Test
public void testFlakes() throws Exception {
writeReadCheck(new RunResult(3, 2, 1, 0, 2, null, true));
}

@Test
public void testLegacyDeserialization() throws Exception {
Copy link
Author

Choose a reason for hiding this comment

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

I added this test to check for backwards compatibility. It ensures that failsafe-summary.xml files produced by older versions of the plugin (i.e without the flakes field) can still be read into memory. In these cases, we simply set flakes to 0.

Choose a reason for hiding this comment

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

Love your work.

File legacySummary = SureFireFileManager.createTempFile("failsafe", "test");
String legacyFailsafeSummaryXmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<failsafe-summary xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xsi:noNamespaceSchemaLocation=\"https://maven.apache.org/surefire/maven-surefire-plugin/xsd/failsafe-summary.xsd\""
+ " result=\"%s\" timeout=\"%s\">\n"
+ " <completed>%d</completed>\n"
+ " <errors>%d</errors>\n"
+ " <failures>%d</failures>\n"
+ " <skipped>%d</skipped>\n"
+ " %s\n"
+ "</failsafe-summary>";
String xml = format(Locale.ROOT, legacyFailsafeSummaryXmlTemplate, 0, false, 3, 2, 1, 0, "msg");
Files.write(
legacySummary.toPath(),
xml.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE);

// When the failsafe-summary.xml does not contain the <flakes> element, it should be considered as 0.
RunResult expected = new RunResult(3, 2, 1, 0, 0, null, false);
RunResult actual = FailsafeSummaryXmlUtils.toRunResult(legacySummary);

assertThat(actual.getCompletedCount()).isEqualTo(expected.getCompletedCount());

assertThat(actual.getErrors()).isEqualTo(expected.getErrors());

assertThat(actual.getFailures()).isEqualTo(expected.getFailures());

assertThat(actual.getSkipped()).isEqualTo(expected.getSkipped());

assertThat(actual.getFlakes()).isEqualTo(expected.getFlakes());

Copy link
Member

Choose a reason for hiding this comment

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

nit: unnecessary empty lines

Copy link
Author

Choose a reason for hiding this comment

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

I only did this to be consistent with the person who wrote the other test in this file 😆

assertThat(actual).isEqualTo(expected);
}

@Test
public void testAppendSerialization() throws Exception {
RunResult simpleAggregate = getSimpleAggregate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!--~ 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.--><xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="failsafe-summary"> <xsd:complexType> <xsd:sequence> <xsd:element name="completed" type="xsd:int"/> <xsd:element name="errors" type="xsd:int"/> <xsd:element name="failures" type="xsd:int"/> <xsd:element name="skipped" type="xsd:int"/> <xsd:element name="failureMessage" type="xsd:string" nillable="true"/> </xsd:sequence> <xsd:attribute name="result" type="errorType" use="optional"/> <xsd:attribute name="timeout" type="xsd:boolean" use="required"/> </xsd:complexType> </xsd:element> <xsd:simpleType name="errorType"> <xsd:restriction base="xsd:string"> <xsd:enumeration id="FAILURE" value="255"/> <xsd:enumeration id="NO_TESTS" value="254"/> </xsd:restriction> </xsd:simpleType></xsd:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!--~ 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.--><xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="failsafe-summary"> <xsd:complexType> <xsd:sequence> <xsd:element name="completed" type="xsd:int"/> <xsd:element name="errors" type="xsd:int"/> <xsd:element name="failures" type="xsd:int"/> <xsd:element name="skipped" type="xsd:int"/> <xsd:element name="flakes" type="xsd:int" nillable="true"/> <xsd:element name="failureMessage" type="xsd:string" nillable="true"/> </xsd:sequence> <xsd:attribute name="result" type="errorType" use="optional"/> <xsd:attribute name="timeout" type="xsd:boolean" use="required"/> </xsd:complexType> </xsd:element> <xsd:simpleType name="errorType"> <xsd:restriction base="xsd:string"> <xsd:enumeration id="FAILURE" value="255"/> <xsd:enumeration id="NO_TESTS" value="254"/> </xsd:restriction> </xsd:simpleType></xsd:schema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public boolean equals(Object o) {
if (skipped != runResult.skipped) {
return false;
}
if (flakes != runResult.flakes) {
return false;
}
if (timeout != runResult.timeout) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,45 @@ public void testJupiterEngineWithFailureInTestTemplateProvider() {
.assertContainsText("Encountered failure in TestTemplate provideTestTemplateInvocationContexts()");
}

@Test
public void testJupiterEngineWithTestTemplateNotClassifiedAsFlake() {
unpack("junit5-testtemplate-bug", "-" + jupiter)
.setTestToRun("FieldSettingTest")
.sysProp("junit5.version", jupiter)
.maven()
.withFailure()
.executeTest()
.verifyTextInLog("AssertionFailedError")
.assertTestSuiteResults(2, 0, 1, 0, 0);

unpack("junit5-testtemplate-bug", "-" + jupiter)
.debugLogging()
.setTestToRun("FieldSettingTest")
.sysProp("junit5.version", jupiter)
// The tests are failing deterministically, so rerunning them should not change the result
.sysProp("surefire.rerunFailingTestsCount", "1")
.maven()
.withFailure()
.executeTest()
.verifyTextInLog("AssertionFailedError")
.assertTestSuiteResults(2, 0, 1, 0, 0);
}

@Test
public void testJupiterEngineWithParameterizedTestsNotClassifiedAsFlake() {
unpack("junit5-testtemplate-bug", "-" + jupiter)
.debugLogging()
.setTestToRun("ParamsContextTest")
.sysProp("junit5.version", jupiter)
// The tests are failing deterministically, so rerunning them should not change the result
.sysProp("surefire.rerunFailingTestsCount", "1")
.maven()
.withFailure()
.executeTest()
.verifyTextInLog("AssertionFailedError")
.assertTestSuiteResults(2, 0, 1, 0, 0);
}

@Test
public void testJupiterEngineWithAssertionsFailNoParameters() {
// `Assertions.fail()` not supported until 5.2.0
Expand Down
49 changes: 49 additions & 0 deletions surefire-its/src/test/resources/junit5-testtemplate-bug/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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>surefire-junit-testtemplate-retry-bug</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Test for JUnit 5 TestTemplate with retries</name>

<properties>
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<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.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
</plugins>
</build>

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

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class FieldSettingTest {
private int testValue = 42;

// We're calling this in the provider underneath
public void setTestValue(int testValue) {
this.testValue = testValue;
}

@TestTemplate
@ExtendWith(FieldSettingContextProvider.class)
public void testTemplatePartiallyFails() {
assertEquals(42, testValue);
}
}


class FieldSettingContextProvider implements TestTemplateInvocationContextProvider {
@Override
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
return true;
}

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) {
return Stream.of(context(0), context(42));
}

private TestTemplateInvocationContext context(int parameter) {
return new TestTemplateInvocationContext() {
@Override
public String getDisplayName(int invocationIndex) {
return "[%d] %s".formatted(invocationIndex, parameter);
}

@Override
public List<Extension> getAdditionalExtensions() {
return getBeforeEachCallbacks(parameter);
}
};
}

private List<Extension> getBeforeEachCallbacks(int value) {
return List.of(((BeforeEachCallback) ctx ->
((FieldSettingTest) ctx.getRequiredTestInstance()).setTestValue(value)
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pkg;

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParamsContextTest {

@TestTemplate
@ExtendWith(ParamsContextProvider.class)
void testTemplatePartiallyFails(Integer value) {
assertEquals(42, value);
}
}

class ParamsContextProvider implements TestTemplateInvocationContextProvider {

@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
return Stream.of(invocationContext(0), invocationContext(42));
}

private TestTemplateInvocationContext invocationContext(int parameter) {
return new TestTemplateInvocationContext() {
@Override
public String getDisplayName(int invocationIndex) {
return "[%d] %s".formatted(invocationIndex, parameter);
}

@Override
public List<Extension> getAdditionalExtensions() {
return Collections.singletonList(new ParameterResolver() {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType().equals(Integer.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameter;
}
});
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,14 @@ private String[] toClassMethodName(TestIdentifier testIdentifier) {
boolean needsSpaceSeparator = isNotBlank(parentDisplay) && !display.startsWith("[");
String methodDisplay = parentDisplay + (needsSpaceSeparator ? " " : "") + display;

boolean isParameterized = isNotBlank(methodSource.getMethodParameterTypes());
boolean hasParameterizedParent = collectAllTestIdentifiersInHierarchy(testIdentifier)
.filter(identifier -> !identifier.getSource().isPresent())
.map(TestIdentifier::getLegacyReportingName)
.anyMatch(legacyReportingName -> legacyReportingName.matches("^\\[.+]$"));
boolean isTestTemplate = testIdentifier.getLegacyReportingName().matches("^.*\\[\\d+]$");

boolean parameterized = isNotBlank(methodSource.getMethodParameterTypes()) || hasParameterizedParent;
boolean parameterized = isParameterized || hasParameterizedParent || isTestTemplate;
String methodName = methodSource.getMethodName();
String description = testIdentifier.getLegacyReportingName();
boolean equalDescriptions = methodDisplay.equals(description);
Expand Down