From d7f4dbb7cc99a08a911066afbab3e01273dbbdc0 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 23 Sep 2024 17:18:32 +0200 Subject: [PATCH] [SUREFIRE-2270] Use JUnit5 in surefire-shadefire --- .../plugin/surefire/AbstractSurefireMojo.java | 49 +++++++++++++++++-- .../Surefire2006ShadefireTransformersIT.java | 18 +++++-- .../pom.xml | 16 +++++- .../src/test/java/JUnit4Test.java | 26 ++++++++++ .../src/test/java/JUnit5Test.java | 26 ++++++++++ .../src/test/java/PojoTest.java | 3 ++ surefire-shadefire/pom.xml | 5 +- ...ven.surefire.api.provider.SurefireProvider | 2 +- 8 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit4Test.java create mode 100644 surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit5Test.java diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 6e0de47307..67e0cb60f3 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -1154,7 +1154,9 @@ protected List createProviders(TestClassPath testClasspath) throws new TestNgProviderInfo(getTestNgArtifact()), new JUnitCoreProviderInfo(getJunitArtifact(), junitDepArtifact), new JUnit4ProviderInfo(getJunitArtifact(), junitDepArtifact), - new JUnit3ProviderInfo()); + new JUnit3ProviderInfo(), + new JUnitPlatformProviderShadefireInfo( + getJUnitPlatformRunnerArtifact(), getJUnit5Artifact(), testClasspath)); } SurefireProperties setupProperties() { @@ -3034,7 +3036,7 @@ public Set getProviderClasspath() throws MojoExecutionException { } } - final class JUnitPlatformProviderInfo implements ProviderInfo { + class JUnitPlatformProviderInfo implements ProviderInfo { private static final String PROVIDER_DEP_GID = "org.junit.platform"; private static final String PROVIDER_DEP_AID = "junit-platform-launcher"; @@ -3057,6 +3059,10 @@ public String getProviderName() { return "org.apache.maven.surefire.junitplatform.JUnitPlatformProvider"; } + protected String getProviderArtifactName() { + return "surefire-junit-platform"; + } + @Override public boolean isApplicable() { return junitPlatformRunnerArtifact == null && junitPlatformArtifact != null; @@ -3082,7 +3088,7 @@ public Set getProviderClasspath() throws MojoExecutionException { Map providerArtifacts = surefireDependencyResolver.getProviderClasspathAsMap( session.getRepositorySession(), project.getRemotePluginRepositories(), - "surefire-junit-platform", + getProviderArtifactName(), surefireVersion); Map testDeps = testClasspath.getTestDependencies(); @@ -3162,7 +3168,7 @@ private void narrowDependencies( providerArtifacts.keySet().removeAll(testDependencies.keySet()); } - private void alignProviderVersions(Map providerArtifacts) throws MojoExecutionException { + protected void alignProviderVersions(Map providerArtifacts) throws MojoExecutionException { String version = junitPlatformArtifact.getBaseVersion(); for (Artifact launcherArtifact : resolve(PROVIDER_DEP_GID, PROVIDER_DEP_AID, version, null, "jar")) { String key = launcherArtifact.getGroupId() + ":" + launcherArtifact.getArtifactId(); @@ -3197,6 +3203,41 @@ private boolean hasDependencyPlatformEngine(Map dependencies) } } + final class JUnitPlatformProviderShadefireInfo extends JUnitPlatformProviderInfo { + + JUnitPlatformProviderShadefireInfo( + Artifact junitPlatformRunnerArtifact, + Artifact junitPlatformArtifact, + @Nonnull TestClassPath testClasspath) { + super(junitPlatformRunnerArtifact, junitPlatformArtifact, testClasspath); + } + + @Override + public boolean isApplicable() { + // newer discover this provider automatically + return false; + } + + @Override + @Nonnull + public String getProviderName() { + return "org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider"; + } + + @Override + protected String getProviderArtifactName() { + return "surefire-shadefire"; + } + + @Override + protected void alignProviderVersions(Map providerArtifacts) throws MojoExecutionException { + // shadefire is used as booter we can not provide additional dependencies, + // so we need add a launcher here + providerArtifacts.put("org.junit.platform:junit-platform-launcher", null); + super.alignProviderVersions(providerArtifacts); + } + } + final class JUnitCoreProviderInfo implements ProviderInfo { private final Artifact junitArtifact; diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2006ShadefireTransformersIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2006ShadefireTransformersIT.java index 7cbfca546a..f91f1d6b0e 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2006ShadefireTransformersIT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2006ShadefireTransformersIT.java @@ -18,9 +18,13 @@ */ package org.apache.maven.surefire.its.jiras; +import org.apache.maven.shared.verifier.VerificationException; import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; import org.junit.Test; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.core.StringContains.containsString; + /** * Test for shadefire usage * @@ -28,11 +32,17 @@ */ public class Surefire2006ShadefireTransformersIT extends SurefireJUnit4IntegrationTestCase { @Test - public void shadefireShouldBeUsed() { + public void shadefireShouldBeUsed() throws VerificationException { unpack("surefire-2006-shadefire-transformers") + .debugLogging() .executeTest() - .assertTestSuiteResults(1, 0, 0, 0) - .verifyTextInLog( - "[INFO] Using configured provider org.apache.maven.shadefire.surefire.junit.JUnit3Provider"); + .assertTestSuiteResults(2, 0, 0, 0) + .assertThatLogLine( + containsString( + "[INFO] Using configured provider org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider"), + is(1)) + .assertThatLogLine(containsString("[INFO] Running PojoTest"), is(0)) + .assertThatLogLine(containsString("[INFO] Running JUnit4Test"), is(1)) + .assertThatLogLine(containsString("[INFO] Running JUnit5Test"), is(1)); } } diff --git a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/pom.xml b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/pom.xml index 68ea2607e4..25dde03292 100644 --- a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/pom.xml +++ b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/pom.xml @@ -33,7 +33,21 @@ ${java.specification.version} - + + + + org.junit.jupiter + junit-jupiter-api + 5.11.0 + test + + + junit + junit + 4.13.2 + test + + diff --git a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit4Test.java b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit4Test.java new file mode 100644 index 0000000000..fb7cfacddb --- /dev/null +++ b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit4Test.java @@ -0,0 +1,26 @@ +/* + * 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.Test; + +public class JUnit4Test { + + @Test + public void success() {} +} diff --git a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit5Test.java b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit5Test.java new file mode 100644 index 0000000000..aceecd37c5 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/JUnit5Test.java @@ -0,0 +1,26 @@ +/* + * 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.Test; + +public class JUnit5Test { + + @Test + public void success() {} +} diff --git a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/PojoTest.java b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/PojoTest.java index 1638c64667..78c701d1b1 100644 --- a/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/PojoTest.java +++ b/surefire-its/src/test/resources/surefire-2006-shadefire-transformers/src/test/java/PojoTest.java @@ -17,6 +17,9 @@ * under the License. */ +/** + * This test will be not executed ... + */ public class PojoTest { public void testSuccess() diff --git a/surefire-shadefire/pom.xml b/surefire-shadefire/pom.xml index f7d39ff17e..f96d0e1748 100644 --- a/surefire-shadefire/pom.xml +++ b/surefire-shadefire/pom.xml @@ -44,7 +44,7 @@ org.apache.maven.surefire - surefire-junit3 + surefire-junit-platform ${project.version} runtime @@ -81,8 +81,7 @@ org.apache.maven.surefire:surefire-extensions-spi org.apache.maven.surefire:surefire-booter org.apache.maven.surefire:common-java5 - org.apache.maven.surefire:common-junit3 - org.apache.maven.surefire:surefire-junit3 + org.apache.maven.surefire:surefire-junit-platform diff --git a/surefire-shadefire/src/main/resources/META-INF/services/org.apache.maven.surefire.api.provider.SurefireProvider b/surefire-shadefire/src/main/resources/META-INF/services/org.apache.maven.surefire.api.provider.SurefireProvider index 1445853c50..b82fb1d4e3 100644 --- a/surefire-shadefire/src/main/resources/META-INF/services/org.apache.maven.surefire.api.provider.SurefireProvider +++ b/surefire-shadefire/src/main/resources/META-INF/services/org.apache.maven.surefire.api.provider.SurefireProvider @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.maven.shadefire.surefire.junit.JUnit3Provider +org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider \ No newline at end of file