Skip to content

Commit 0097a41

Browse files
[SUREFIRE-1962] Unit test for ProviderInfo#isApplicable
1 parent d3dafe4 commit 0097a41

File tree

3 files changed

+306
-0
lines changed

3 files changed

+306
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.apache.maven.plugin.surefire;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.Arrays;
23+
import java.util.Collection;
24+
25+
import org.apache.maven.artifact.Artifact;
26+
import org.apache.maven.artifact.DefaultArtifact;
27+
import org.apache.maven.surefire.providerapi.ProviderInfo;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.Parameterized;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.Mockito.spy;
34+
import static org.mockito.Mockito.when;
35+
36+
/**
37+
* Testing JUnitCoreProviderInfo applicable behavior.
38+
*/
39+
@RunWith( Parameterized.class )
40+
public class AbstractSurefireMojoJunitCoreProvidersInfoTest
41+
{
42+
private final Artifact junitArtifact;
43+
private final Artifact junitDepArtifact;
44+
private final boolean isParallel;
45+
private final boolean hasGroups;
46+
47+
private final boolean isApplicable;
48+
49+
50+
@Parameterized.Parameters(
51+
name = "{index}: junit={0}, junitDep={1}, parallel={2}, groups={3} then isApplicable={4}" )
52+
public static Collection<Object[]> data()
53+
{
54+
return Arrays.asList( new Object[][] {
55+
// junit and junitDep are null
56+
{null, null, false, false, false},
57+
{null, null, true, false, false},
58+
{null, null, false, true, false},
59+
{null, null, true, true, false},
60+
61+
// only junit artifact
62+
// without parallel and groups
63+
{"4.5", null, false, false, false},
64+
{"4.7", null, false, false, false},
65+
66+
// with parallel
67+
{"4.5", null, true, false, false},
68+
{"4.7", null, true, false, true},
69+
70+
// with groups
71+
{"4.5", null, false, true, false},
72+
{"4.7", null, false, true, true},
73+
74+
// only junitDep artifact
75+
// without parallel and groups
76+
{null, "4.5", false, false, false},
77+
{null, "4.7", false, false, false},
78+
79+
// with parallel
80+
{null, "4.5", true, false, false},
81+
{null, "4.7", true, false, true},
82+
83+
// with groups
84+
{null, "4.5", false, true, false},
85+
{null, "4.7", false, true, true}
86+
} );
87+
}
88+
89+
public AbstractSurefireMojoJunitCoreProvidersInfoTest( String junitVersion, String junitDepVersion,
90+
boolean isParallel, boolean hasGroups,
91+
boolean isApplicable )
92+
{
93+
this.junitArtifact = junitVersion != null ? aArtifact( junitVersion ) : null;
94+
this.junitDepArtifact = junitDepVersion != null ? aArtifact( junitDepVersion ) : null;
95+
this.isParallel = isParallel;
96+
this.hasGroups = hasGroups;
97+
this.isApplicable = isApplicable;
98+
}
99+
100+
private Artifact aArtifact( String version )
101+
{
102+
return new DefaultArtifact( "test", "test", version, "test", "jar", "", null );
103+
}
104+
105+
@Test
106+
public void test()
107+
{
108+
AbstractSurefireMojo mojo = spy( AbstractSurefireMojo.class );
109+
110+
when( mojo.isAnyConcurrencySelected() ).thenReturn( isParallel );
111+
when( mojo.isAnyGroupsSelected() ).thenReturn( hasGroups );
112+
113+
ProviderInfo providerInfo = mojo.new JUnitCoreProviderInfo( junitArtifact, junitDepArtifact );
114+
115+
assertThat( providerInfo.isApplicable() ).isEqualTo( isApplicable );
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package org.apache.maven.plugin.surefire;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.artifact.Artifact;
23+
import org.apache.maven.artifact.DefaultArtifact;
24+
import org.apache.maven.surefire.providerapi.ProviderInfo;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.Spy;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verifyNoMoreInteractions;
33+
34+
/**
35+
* Testing providerInfo applicable behavior.
36+
*/
37+
@RunWith( MockitoJUnitRunner.class )
38+
public class AbstractSurefireMojoProvidersInfoTest
39+
{
40+
41+
@Spy
42+
private AbstractSurefireMojo mojo;
43+
44+
@Test
45+
public void defaultProviderAreAlwaysAvailable()
46+
{
47+
ProviderInfo providerInfo = mojo.new JUnit3ProviderInfo();
48+
assertThat( providerInfo.isApplicable() ).isTrue();
49+
}
50+
51+
@Test
52+
public void dynamicProviderAreAlwaysApplicable()
53+
{
54+
ProviderInfo providerInfo = mojo.new DynamicProviderInfo( "test" );
55+
assertThat( providerInfo.isApplicable() ).isTrue();
56+
}
57+
58+
@Test
59+
public void testNgProviderApplicable()
60+
{
61+
Artifact testNg = mock( Artifact.class );
62+
ProviderInfo providerInfo = mojo.new TestNgProviderInfo( testNg );
63+
64+
assertThat( providerInfo.isApplicable() ).isTrue();
65+
66+
// no interaction with artifact only reference are checked
67+
verifyNoMoreInteractions( testNg );
68+
}
69+
70+
@Test
71+
public void testNgProviderNotApplicable()
72+
{
73+
ProviderInfo providerInfo = mojo.new TestNgProviderInfo( null );
74+
assertThat( providerInfo.isApplicable() ).isFalse();
75+
}
76+
77+
//surefire-junit-platform
78+
79+
@Test
80+
public void jUnitPlatformProviderApplicable()
81+
{
82+
Artifact junitPlatform = mock( Artifact.class );
83+
ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo( null, junitPlatform, aTestClassPath() );
84+
85+
assertThat( providerInfo.isApplicable() ).isTrue();
86+
87+
// no interaction with artifact only reference are checked
88+
verifyNoMoreInteractions( junitPlatform );
89+
}
90+
91+
@Test
92+
public void jUnitPlatformProviderNotApplicable()
93+
{
94+
ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo( null, null, aTestClassPath() );
95+
assertThat( providerInfo.isApplicable() ).isFalse();
96+
}
97+
98+
@Test
99+
public void jUnitPlatformProviderNotApplicableForPlatformRunner()
100+
{
101+
// not applicable if junit-platform-runner is on classpath
102+
Artifact junitPlatformRunnerArtifact = mock( Artifact.class );
103+
ProviderInfo providerInfo = mojo.new JUnitPlatformProviderInfo(
104+
junitPlatformRunnerArtifact, null, aTestClassPath() );
105+
assertThat( providerInfo.isApplicable() ).isFalse();
106+
107+
// no interaction with artifact only reference are checked
108+
verifyNoMoreInteractions( junitPlatformRunnerArtifact );
109+
}
110+
111+
// surefire-junit4
112+
113+
@Test
114+
public void jUnit4ProviderNullArtifacts()
115+
{
116+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( null, null );
117+
assertThat( providerInfo.isApplicable() ).isFalse();
118+
}
119+
120+
@Test
121+
public void jUnit4ProviderOnlyJunitDepArtifact()
122+
{
123+
Artifact junitDep = mock( Artifact.class );
124+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( null, junitDep );
125+
126+
assertThat( providerInfo.isApplicable() ).isTrue();
127+
128+
// no interaction with artifact only reference are checked
129+
verifyNoMoreInteractions( junitDep );
130+
}
131+
132+
133+
@Test
134+
public void jUnit4ProviderJunit3WithJDepArtifact()
135+
{
136+
Artifact junit = aArtifact( "3.8.1" );
137+
Artifact junitDep = mock( Artifact.class );
138+
139+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, junitDep );
140+
141+
// ??? only existing for junitDep in any version is checked
142+
assertThat( providerInfo.isApplicable() ).isTrue();
143+
144+
// no interaction with artifact only reference are checked
145+
verifyNoMoreInteractions( junitDep );
146+
}
147+
148+
@Test
149+
public void jUnit4ProviderJunit3AsDependencyArtifact()
150+
{
151+
Artifact junit = aArtifact( "3.8.1" );
152+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null );
153+
assertThat( providerInfo.isApplicable() ).isFalse();
154+
}
155+
156+
@Test
157+
public void jUnit4ProviderJunit45AsDependencyArtifact()
158+
{
159+
Artifact junit = aArtifact( "4.5" );
160+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null );
161+
assertThat( providerInfo.isApplicable() ).isTrue();
162+
}
163+
164+
@Test
165+
public void jUnit4ProviderJunit47AsDependencyArtifact()
166+
{
167+
Artifact junit = aArtifact( "4.7" );
168+
ProviderInfo providerInfo = mojo.new JUnit4ProviderInfo( junit, null );
169+
// ??? it is ok for 4.7 ???
170+
assertThat( providerInfo.isApplicable() ).isTrue();
171+
}
172+
173+
174+
private TestClassPath aTestClassPath()
175+
{
176+
return new TestClassPath( null, null, null, null );
177+
}
178+
179+
private Artifact aArtifact( String version )
180+
{
181+
return new DefaultArtifact( "test", "test", version, "test", "jar", "", null );
182+
}
183+
184+
185+
}

maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import junit.framework.TestCase;
2525
import junit.framework.TestSuite;
2626
import org.apache.maven.plugin.surefire.AbstractSurefireMojoJava7PlusTest;
27+
import org.apache.maven.plugin.surefire.AbstractSurefireMojoJunitCoreProvidersInfoTest;
28+
import org.apache.maven.plugin.surefire.AbstractSurefireMojoProvidersInfoTest;
2729
import org.apache.maven.plugin.surefire.AbstractSurefireMojoTest;
2830
import org.apache.maven.plugin.surefire.AbstractSurefireMojoToolchainsTest;
2931
import org.apache.maven.plugin.surefire.CommonReflectorTest;
@@ -120,6 +122,8 @@ public static Test suite()
120122
suite.addTest( new JUnit4TestAdapter( EventDecoderTest.class ) );
121123
suite.addTest( new JUnit4TestAdapter( EventConsumerThreadTest.class ) );
122124
suite.addTest( new JUnit4TestAdapter( ChecksumCalculatorTest.class ) );
125+
suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoJunitCoreProvidersInfoTest.class ) );
126+
suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoProvidersInfoTest.class ) );
123127
return suite;
124128
}
125129
}

0 commit comments

Comments
 (0)