From 0097a41879983b4790299ce1663edd669f321400 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 21 Nov 2021 18:07:42 +0100 Subject: [PATCH] [SUREFIRE-1962] Unit test for ProviderInfo#isApplicable --- ...urefireMojoJunitCoreProvidersInfoTest.java | 117 +++++++++++ ...AbstractSurefireMojoProvidersInfoTest.java | 185 ++++++++++++++++++ .../maven/surefire/JUnit4SuiteTest.java | 4 + 3 files changed, 306 insertions(+) create mode 100644 maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoJunitCoreProvidersInfoTest.java create mode 100644 maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoProvidersInfoTest.java diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoJunitCoreProvidersInfoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoJunitCoreProvidersInfoTest.java new file mode 100644 index 0000000000..190d8b3eb0 --- /dev/null +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoJunitCoreProvidersInfoTest.java @@ -0,0 +1,117 @@ +package org.apache.maven.plugin.surefire; + +/* + * 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 java.util.Arrays; +import java.util.Collection; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.surefire.providerapi.ProviderInfo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +/** + * Testing JUnitCoreProviderInfo applicable behavior. + */ +@RunWith( Parameterized.class ) +public class AbstractSurefireMojoJunitCoreProvidersInfoTest +{ + private final Artifact junitArtifact; + private final Artifact junitDepArtifact; + private final boolean isParallel; + private final boolean hasGroups; + + private final boolean isApplicable; + + + @Parameterized.Parameters( + name = "{index}: junit={0}, junitDep={1}, parallel={2}, groups={3} then isApplicable={4}" ) + public static Collection data() + { + return Arrays.asList( new Object[][] { + // junit and junitDep are null + {null, null, false, false, false}, + {null, null, true, false, false}, + {null, null, false, true, false}, + {null, null, true, true, false}, + + // only junit artifact + // without parallel and groups + {"4.5", null, false, false, false}, + {"4.7", null, false, false, false}, + + // with parallel + {"4.5", null, true, false, false}, + {"4.7", null, true, false, true}, + + // with groups + {"4.5", null, false, true, false}, + {"4.7", null, false, true, true}, + + // only junitDep artifact + // without parallel and groups + {null, "4.5", false, false, false}, + {null, "4.7", false, false, false}, + + // with parallel + {null, "4.5", true, false, false}, + {null, "4.7", true, false, true}, + + // with groups + {null, "4.5", false, true, false}, + {null, "4.7", false, true, true} + } ); + } + + public AbstractSurefireMojoJunitCoreProvidersInfoTest( String junitVersion, String junitDepVersion, + boolean isParallel, boolean hasGroups, + boolean isApplicable ) + { + this.junitArtifact = junitVersion != null ? aArtifact( junitVersion ) : null; + this.junitDepArtifact = junitDepVersion != null ? aArtifact( junitDepVersion ) : null; + this.isParallel = isParallel; + this.hasGroups = hasGroups; + this.isApplicable = isApplicable; + } + + private Artifact aArtifact( String version ) + { + return new DefaultArtifact( "test", "test", version, "test", "jar", "", null ); + } + + @Test + public void test() + { + AbstractSurefireMojo mojo = spy( AbstractSurefireMojo.class ); + + when( mojo.isAnyConcurrencySelected() ).thenReturn( isParallel ); + when( mojo.isAnyGroupsSelected() ).thenReturn( hasGroups ); + + ProviderInfo providerInfo = mojo.new JUnitCoreProviderInfo( junitArtifact, junitDepArtifact ); + + assertThat( providerInfo.isApplicable() ).isEqualTo( isApplicable ); + } +} diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoProvidersInfoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoProvidersInfoTest.java new file mode 100644 index 0000000000..1429428509 --- /dev/null +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoProvidersInfoTest.java @@ -0,0 +1,185 @@ +package org.apache.maven.plugin.surefire; + +/* + * 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.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.surefire.providerapi.ProviderInfo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +/** + * Testing providerInfo applicable behavior. + */ +@RunWith( MockitoJUnitRunner.class ) +public class AbstractSurefireMojoProvidersInfoTest +{ + + @Spy + private AbstractSurefireMojo mojo; + + @Test + public void defaultProviderAreAlwaysAvailable() + { + ProviderInfo providerInfo = mojo.new JUnit3ProviderInfo(); + assertThat( providerInfo.isApplicable() ).isTrue(); + } + + @Test + public void dynamicProviderAreAlwaysApplicable() + { + ProviderInfo providerInfo = mojo.new DynamicProviderInfo( "test" ); + assertThat( providerInfo.isApplicable() ).isTrue(); + } + + @Test + public void testNgProviderApplicable() + { + Artifact testNg = mock( Artifact.class ); + ProviderInfo providerInfo = mojo.new TestNgProviderInfo( testNg ); + + assertThat( providerInfo.isApplicable() ).isTrue(); + + // no interaction with artifact only reference are checked + verifyNoMoreInteractions( testNg ); + } + + @Test + public void testNgProviderNotApplicable() + { + ProviderInfo providerInfo = mojo.new TestNgProviderInfo( null ); + assertThat( providerInfo.isApplicable() ).isFalse(); + } + + //surefire-junit-platform + + @Test + public void jUnitPlatformProviderApplicable() + { + Artifact junitPlatform = mock( Artifact.class ); + ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo( null, junitPlatform, aTestClassPath() ); + + assertThat( providerInfo.isApplicable() ).isTrue(); + + // no interaction with artifact only reference are checked + verifyNoMoreInteractions( junitPlatform ); + } + + @Test + public void jUnitPlatformProviderNotApplicable() + { + ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo( null, null, aTestClassPath() ); + assertThat( providerInfo.isApplicable() ).isFalse(); + } + + @Test + public void jUnitPlatformProviderNotApplicableForPlatformRunner() + { + // not applicable if junit-platform-runner is on classpath + Artifact junitPlatformRunnerArtifact = mock( Artifact.class ); + ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo( + junitPlatformRunnerArtifact, null, aTestClassPath() ); + assertThat( providerInfo.isApplicable() ).isFalse(); + + // no interaction with artifact only reference are checked + verifyNoMoreInteractions( junitPlatformRunnerArtifact ); + } + + // surefire-junit4 + + @Test + public void jUnit4ProviderNullArtifacts() + { + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( null, null ); + assertThat( providerInfo.isApplicable() ).isFalse(); + } + + @Test + public void jUnit4ProviderOnlyJunitDepArtifact() + { + Artifact junitDep = mock( Artifact.class ); + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( null, junitDep ); + + assertThat( providerInfo.isApplicable() ).isTrue(); + + // no interaction with artifact only reference are checked + verifyNoMoreInteractions( junitDep ); + } + + + @Test + public void jUnit4ProviderJunit3WithJDepArtifact() + { + Artifact junit = aArtifact( "3.8.1" ); + Artifact junitDep = mock( Artifact.class ); + + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, junitDep ); + + // ??? only existing for junitDep in any version is checked + assertThat( providerInfo.isApplicable() ).isTrue(); + + // no interaction with artifact only reference are checked + verifyNoMoreInteractions( junitDep ); + } + + @Test + public void jUnit4ProviderJunit3AsDependencyArtifact() + { + Artifact junit = aArtifact( "3.8.1" ); + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null ); + assertThat( providerInfo.isApplicable() ).isFalse(); + } + + @Test + public void jUnit4ProviderJunit45AsDependencyArtifact() + { + Artifact junit = aArtifact( "4.5" ); + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null ); + assertThat( providerInfo.isApplicable() ).isTrue(); + } + + @Test + public void jUnit4ProviderJunit47AsDependencyArtifact() + { + Artifact junit = aArtifact( "4.7" ); + ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null ); + // ??? it is ok for 4.7 ??? + assertThat( providerInfo.isApplicable() ).isTrue(); + } + + + private TestClassPath aTestClassPath() + { + return new TestClassPath( null, null, null, null ); + } + + private Artifact aArtifact( String version ) + { + return new DefaultArtifact( "test", "test", version, "test", "jar", "", null ); + } + + +} diff --git a/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java b/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java index fa85b9f448..509cc67fc1 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java @@ -24,6 +24,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.maven.plugin.surefire.AbstractSurefireMojoJava7PlusTest; +import org.apache.maven.plugin.surefire.AbstractSurefireMojoJunitCoreProvidersInfoTest; +import org.apache.maven.plugin.surefire.AbstractSurefireMojoProvidersInfoTest; import org.apache.maven.plugin.surefire.AbstractSurefireMojoTest; import org.apache.maven.plugin.surefire.AbstractSurefireMojoToolchainsTest; import org.apache.maven.plugin.surefire.CommonReflectorTest; @@ -120,6 +122,8 @@ public static Test suite() suite.addTest( new JUnit4TestAdapter( EventDecoderTest.class ) ); suite.addTest( new JUnit4TestAdapter( EventConsumerThreadTest.class ) ); suite.addTest( new JUnit4TestAdapter( ChecksumCalculatorTest.class ) ); + suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoJunitCoreProvidersInfoTest.class ) ); + suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoProvidersInfoTest.class ) ); return suite; } }