From 7bc91470e7f1c96e2556ceda5c2adf49903cc222 Mon Sep 17 00:00:00 2001 From: tibordigana Date: Sat, 5 Mar 2022 20:56:43 +0100 Subject: [PATCH 1/3] Support junit-platform-runner and junit-platform-suite-engine in plugin dependencies --- .../plugin/surefire/AbstractSurefireMojo.java | 5 +- .../surefire/AbstractSurefireMojoTest.java | 48 +++++++++++ .../site/apt/examples/junit-platform.apt.vm | 47 +++++++++++ .../surefire/its/JUnitPlatformEnginesIT.java | 11 +-- .../its/jiras/Surefire1787JUnit5IT.java | 11 +++ .../src/test/resources/junit5-runner/pom.xml | 4 +- .../src/test/resources/junit5-suite/pom.xml | 80 +++++++++++++++++++ .../src/test/java/pkg/JUnit5Tests.java | 29 +++++++ .../src/test/java/pkg/domain/AxTest.java | 30 +++++++ .../src/test/java/pkg/domain/BxTest.java | 30 +++++++ 10 files changed, 286 insertions(+), 9 deletions(-) create mode 100644 surefire-its/src/test/resources/junit5-suite/pom.xml create mode 100644 surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/JUnit5Tests.java create mode 100644 surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/AxTest.java create mode 100644 surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/BxTest.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 ed946b63ac..d0998e622f 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 @@ -2385,7 +2385,8 @@ private Artifact getJunitDepArtifact() private Artifact getJUnit5Artifact() { - if ( getProjectArtifactMap().get( "org.junit.platform:junit-platform-runner" ) != null ) + if ( getProjectArtifactMap().containsKey( "org.junit.platform:junit-platform-runner" ) + || getPluginArtifactMap().containsKey( "org.junit.platform:junit-platform-runner" ) ) { return null; } @@ -2393,7 +2394,7 @@ private Artifact getJUnit5Artifact() Artifact artifact = getPluginArtifactMap().get( "org.junit.platform:junit-platform-engine" ); if ( artifact == null ) { - return getProjectArtifactMap().get( "org.junit.platform:junit-platform-commons" ); + artifact = getProjectArtifactMap().get( "org.junit.platform:junit-platform-commons" ); } return artifact; diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java index 4cce707ace..2810764f74 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java @@ -774,6 +774,54 @@ public void shouldExistTmpDirectory() throws IOException .isDirectory(); } + @Test + public void shouldNotBeJunit5ArtifactInPluginDeps() throws Exception + { + Artifact testClasspathJupiter = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine", + createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact testClasspathPlatformEng = new DefaultArtifact( "org.junit.platform", "junit-platform-engine", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + setProjectDepedenciesToMojo( testClasspathJupiter, testClasspathPlatformEng, testClasspathCommons ); + + Artifact pluginDep1 = new DefaultArtifact( "org.junit.platform", "junit-platform-runner", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact pluginDep2 = new DefaultArtifact( "org.junit.platform", "junit-platform-commons", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + addPluginDependencies( pluginDep1, pluginDep2 ); + + Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" ); + assertThat( junitPlatformArtifact ).isNull(); + } + + @Test + public void shouldNotBeJunit5ArtifactInProjectDeps() throws Exception + { + Artifact testClasspathJupiter = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine", + createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact testClasspathPlatformEng = new DefaultArtifact( "org.junit.platform", "junit-platform-engine", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + Artifact testClasspathRunner = new DefaultArtifact( "org.junit.platform", "junit-platform-runner", + createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) ); + + setProjectDepedenciesToMojo( testClasspathJupiter, testClasspathPlatformEng, testClasspathCommons, + testClasspathRunner ); + + Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" ); + assertThat( junitPlatformArtifact ).isNull(); + } + @Test public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception { diff --git a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm index e1426a680a..d4e1317e94 100644 --- a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm +++ b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm @@ -411,6 +411,53 @@ Using JUnit 5 Platform +---+ +** JUnit5 Suite + + For more information see this + {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-suite}example}}. + ++---+ + + + org.junit.jupiter + junit-jupiter-api + 5.8.0 + test + + + org.junit.platform + junit-platform-suite-api + 1.8.0 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + JUnit5Tests + + + + + org.junit.jupiter + junit-jupiter-engine + 5.8.2 + + + org.junit.platform + junit-platform-suite-engine + 1.8.2 + + + + + ++---+ + * Provider Selection If nothing is configured, Surefire detects which JUnit version to use by the following algorithm: diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java index 02c2706382..c176862418 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java @@ -77,13 +77,14 @@ public static Iterable artifactVersions() { ArrayList args = new ArrayList<>(); args.add( new Object[] {"1.0.3", "5.0.3", "1.0.0", "1.0.0"} ); - args.add( new Object[] {"1.1.1", "5.1.1", "1.0.0", "1.0.0"} ); - args.add( new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"} ); + //args.add( new Object[] {"1.1.1", "5.1.1", "1.0.0", "1.0.0"} ); + //args.add( new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"} ); args.add( new Object[] {"1.3.2", "5.3.2", "1.1.1", "1.0.0"} ); - args.add( new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"} ); - args.add( new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"} ); + //args.add( new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"} ); + //args.add( new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"} ); args.add( new Object[] {"1.6.2", "5.6.2", "1.2.0", "1.1.0"} ); - //args.add( new Object[] { "1.6.0-SNAPSHOT", "5.6.0-SNAPSHOT", "1.2.0", "1.1.0" } ); + //args.add( new Object[] {"1.7.2", "5.7.2", "1.2.0", "1.1.0"} ); + args.add( new Object[] {"1.8.2", "5.8.2", "1.2.0", "1.1.2"} ); return args; } diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1787JUnit5IT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1787JUnit5IT.java index 3e7e056b53..5df769b277 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1787JUnit5IT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1787JUnit5IT.java @@ -129,4 +129,15 @@ public void junit4Runner() .verifyTextInLog( "Running pkg.JUnit5Tests" ) .verifyTextInLog( "Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider" ); } + + @Test + public void junit5Suite() + { + unpack( "junit5-suite" ) + .executeTest() + .verifyErrorFree( 1 ) + .verifyTextInLog( "Running pkg.JUnit5Test" ) + .verifyTextInLog( + "Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider" ); + } } diff --git a/surefire-its/src/test/resources/junit5-runner/pom.xml b/surefire-its/src/test/resources/junit5-runner/pom.xml index c667e030f6..09b530028e 100644 --- a/surefire-its/src/test/resources/junit5-runner/pom.xml +++ b/surefire-its/src/test/resources/junit5-runner/pom.xml @@ -37,13 +37,13 @@ org.junit.jupiter junit-jupiter-engine - 5.6.2 + 5.8.2 test org.junit.platform junit-platform-runner - 1.6.2 + 1.8.2 test diff --git a/surefire-its/src/test/resources/junit5-suite/pom.xml b/surefire-its/src/test/resources/junit5-suite/pom.xml new file mode 100644 index 0000000000..ee88bf55c5 --- /dev/null +++ b/surefire-its/src/test/resources/junit5-suite/pom.xml @@ -0,0 +1,80 @@ + + + + + 4.0.0 + + org.example + junit5-suite + 1.0-SNAPSHOT + + + UTF-8 + ${java.specification.version} + ${java.specification.version} + + + + + org.junit.jupiter + junit-jupiter-api + 5.8.0 + test + + + org.junit.platform + junit-platform-suite-api + 1.8.0 + test + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + JUnit5Tests + + + + + org.junit.jupiter + junit-jupiter-engine + 5.8.2 + + + org.junit.platform + junit-platform-suite-engine + 1.8.2 + + + + + + + + diff --git a/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/JUnit5Tests.java b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/JUnit5Tests.java new file mode 100644 index 0000000000..a9cb8d63f6 --- /dev/null +++ b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/JUnit5Tests.java @@ -0,0 +1,29 @@ +package pkg; + +/* + * 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.platform.suite.api.SelectClasses; +import org.junit.platform.suite.api.Suite; + +@Suite +@SelectClasses({pkg.domain.AxTest.class}) +public class JUnit5Tests +{ +} diff --git a/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/AxTest.java b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/AxTest.java new file mode 100644 index 0000000000..ca7c26c807 --- /dev/null +++ b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/AxTest.java @@ -0,0 +1,30 @@ +package pkg.domain; + +/* + * 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 AxTest +{ + @Test + void test() + { + } +} diff --git a/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/BxTest.java b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/BxTest.java new file mode 100644 index 0000000000..c1e1a16b7d --- /dev/null +++ b/surefire-its/src/test/resources/junit5-suite/src/test/java/pkg/domain/BxTest.java @@ -0,0 +1,30 @@ +package pkg.domain; + +/* + * 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 BxTest +{ + @Test + void test() + { + } +} From fe95deb329f0258a096c575aa22d02f06ebcef42 Mon Sep 17 00:00:00 2001 From: tibordigana Date: Sat, 5 Mar 2022 22:21:19 +0100 Subject: [PATCH 2/3] according to Slawomir's hints --- .../site/apt/examples/junit-platform.apt.vm | 20 ++++--------------- .../surefire/its/JUnitPlatformEnginesIT.java | 5 ----- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm index d4e1317e94..bef651ad54 100644 --- a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm +++ b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm @@ -420,14 +420,14 @@ Using JUnit 5 Platform org.junit.jupiter - junit-jupiter-api - 5.8.0 + junit-jupiter-engine + 5.8.2 test org.junit.platform - junit-platform-suite-api - 1.8.0 + junit-platform-suite-engine + 1.8.2 test @@ -441,18 +441,6 @@ Using JUnit 5 Platform JUnit5Tests - - - org.junit.jupiter - junit-jupiter-engine - 5.8.2 - - - org.junit.platform - junit-platform-suite-engine - 1.8.2 - - diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java index c176862418..ffebb8c069 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformEnginesIT.java @@ -77,13 +77,8 @@ public static Iterable artifactVersions() { ArrayList args = new ArrayList<>(); args.add( new Object[] {"1.0.3", "5.0.3", "1.0.0", "1.0.0"} ); - //args.add( new Object[] {"1.1.1", "5.1.1", "1.0.0", "1.0.0"} ); - //args.add( new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"} ); args.add( new Object[] {"1.3.2", "5.3.2", "1.1.1", "1.0.0"} ); - //args.add( new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"} ); - //args.add( new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"} ); args.add( new Object[] {"1.6.2", "5.6.2", "1.2.0", "1.1.0"} ); - //args.add( new Object[] {"1.7.2", "5.7.2", "1.2.0", "1.1.0"} ); args.add( new Object[] {"1.8.2", "5.8.2", "1.2.0", "1.1.2"} ); return args; } From 3a1de7a711c7894e4c2559b8136625f59f054272 Mon Sep 17 00:00:00 2001 From: tibordigana Date: Sat, 5 Mar 2022 22:23:55 +0100 Subject: [PATCH 3/3] added the link https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine --- .../src/site/apt/examples/junit-platform.apt.vm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm index bef651ad54..3467ea368f 100644 --- a/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm +++ b/maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm @@ -414,7 +414,8 @@ Using JUnit 5 Platform ** JUnit5 Suite For more information see this - {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-suite}example}}. + {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-suite}example}} + and the {{{https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine}tutorial}}. +---+