Skip to content

Commit

Permalink
[SUREFIRE-1962] Unit test for ProviderInfo#isApplicable
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jan 3, 2023
1 parent d3dafe4 commit 0097a41
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<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 );
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0097a41

Please sign in to comment.