You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/01/24 18:05:28 UTC

[maven-surefire] 01/01: [SUREFIRE-1962] Unit test for ProviderInfo#isApplicable

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch SUREFIRE-1962
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit e054b900eb74185e7c89cc65e8845b23be2fe25f
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sun Nov 21 18:07:42 2021 +0100

    [SUREFIRE-1962] Unit test for ProviderInfo#isApplicable
---
 ...ractSurefireMojoJunitCoreProvidersInfoTest.java | 117 ++++++++++++++
 .../AbstractSurefireMojoProvidersInfoTest.java     | 172 +++++++++++++++++++++
 .../org/apache/maven/surefire/JUnit4SuiteTest.java |   4 +
 3 files changed, 293 insertions(+)

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 0000000..080c7c9
--- /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.fest.assertions.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<Object[]> 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 0000000..c43df71
--- /dev/null
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoProvidersInfoTest.java
@@ -0,0 +1,172 @@
+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.fest.assertions.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( 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, aTestClassPath() );
+        assertThat( providerInfo.isApplicable() ).isFalse();
+    }
+
+    // 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 fa85b9f..509cc67 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.Test;
 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 class JUnit4SuiteTest extends TestCase
         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;
     }
 }