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