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.shared.artifact.filter;
20  
21  import org.apache.maven.artifact.Artifact;
22  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  import org.apache.maven.plugin.testing.ArtifactStubFactory;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertFalse;
28  import static org.junit.Assert.assertTrue;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.when;
31  
32  public class ScopeArtifactFilterTest {
33  
34      @Test
35      public void testExcludedArtifactWithRangeShouldNotCauseNPE() throws Exception {
36          ArtifactStubFactory factory = new ArtifactStubFactory();
37  
38          Artifact excluded = factory.createArtifact(
39                  "group",
40                  "artifact",
41                  VersionRange.createFromVersionSpec("[1.2.3]"),
42                  Artifact.SCOPE_PROVIDED,
43                  "jar",
44                  null,
45                  false);
46  
47          ArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
48  
49          assertFalse(filter.include(excluded));
50      }
51  
52      @Test
53      public void testNullScopeDisabled() {
54          ScopeArtifactFilter filter = new ScopeArtifactFilter();
55          filter.setIncludeNullScope(false);
56  
57          verifyExcluded(filter, null);
58      }
59  
60      @Test
61      public void testFineGrained_IncludeOnlyScopesThatWereEnabled_TestScope() {
62          ScopeArtifactFilter filter = new ScopeArtifactFilter();
63          filter.setIncludeTestScope(true);
64  
65          verifyExcluded(filter, Artifact.SCOPE_COMPILE);
66          verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
67          verifyExcluded(filter, Artifact.SCOPE_RUNTIME);
68          verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
69          verifyIncluded(filter, Artifact.SCOPE_TEST);
70          verifyIncluded(filter, null);
71      }
72  
73      @Test
74      public void testFineGrained_IncludeOnlyScopesThatWereEnabled_CompileScope() {
75          ScopeArtifactFilter filter = new ScopeArtifactFilter();
76          filter.setIncludeCompileScope(true);
77  
78          verifyIncluded(filter, Artifact.SCOPE_COMPILE);
79          verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
80          verifyExcluded(filter, Artifact.SCOPE_RUNTIME);
81          verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
82          verifyExcluded(filter, Artifact.SCOPE_TEST);
83          verifyIncluded(filter, null);
84      }
85  
86      @Test
87      public void testFineGrained_IncludeOnlyScopesThatWereEnabled_RuntimeScope() {
88          ScopeArtifactFilter filter = new ScopeArtifactFilter();
89          filter.setIncludeRuntimeScope(true);
90  
91          verifyExcluded(filter, Artifact.SCOPE_COMPILE);
92          verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
93          verifyIncluded(filter, Artifact.SCOPE_RUNTIME);
94          verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
95          verifyExcluded(filter, Artifact.SCOPE_TEST);
96          verifyIncluded(filter, null);
97      }
98  
99      @Test
100     public void testFineGrained_IncludeOnlyScopesThatWereEnabled_ProvidedScope() {
101         ScopeArtifactFilter filter = new ScopeArtifactFilter();
102         filter.setIncludeProvidedScope(true);
103 
104         verifyExcluded(filter, Artifact.SCOPE_COMPILE);
105         verifyIncluded(filter, Artifact.SCOPE_PROVIDED);
106         verifyExcluded(filter, Artifact.SCOPE_RUNTIME);
107         verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
108         verifyExcluded(filter, Artifact.SCOPE_TEST);
109         verifyIncluded(filter, null);
110     }
111 
112     @Test
113     public void testFineGrained_IncludeOnlyScopesThatWereEnabled_SystemScope() {
114         ScopeArtifactFilter filter = new ScopeArtifactFilter();
115         filter.setIncludeSystemScope(true);
116 
117         verifyExcluded(filter, Artifact.SCOPE_COMPILE);
118         verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
119         verifyExcluded(filter, Artifact.SCOPE_RUNTIME);
120         verifyIncluded(filter, Artifact.SCOPE_SYSTEM);
121         verifyExcluded(filter, Artifact.SCOPE_TEST);
122         verifyIncluded(filter, null);
123     }
124 
125     @Test
126     public void testFineGrained_IncludeOnlyScopesThatWereEnabled_ProvidedAndRuntimeScopes() {
127         ScopeArtifactFilter filter = new ScopeArtifactFilter();
128         filter.setIncludeRuntimeScope(true);
129         filter.setIncludeProvidedScope(true);
130 
131         verifyExcluded(filter, Artifact.SCOPE_COMPILE);
132         verifyIncluded(filter, Artifact.SCOPE_PROVIDED);
133         verifyIncluded(filter, Artifact.SCOPE_RUNTIME);
134         verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
135         verifyExcluded(filter, Artifact.SCOPE_TEST);
136         verifyIncluded(filter, null);
137     }
138 
139     @Test
140     public void testFineGrained_IncludeOnlyScopesThatWereEnabled_SystemAndRuntimeScopes() {
141         ScopeArtifactFilter filter = new ScopeArtifactFilter();
142         filter.setIncludeRuntimeScope(true);
143         filter.setIncludeSystemScope(true);
144 
145         verifyExcluded(filter, Artifact.SCOPE_COMPILE);
146         verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
147         verifyIncluded(filter, Artifact.SCOPE_RUNTIME);
148         verifyIncluded(filter, Artifact.SCOPE_SYSTEM);
149         verifyExcluded(filter, Artifact.SCOPE_TEST);
150         verifyIncluded(filter, null);
151     }
152 
153     @Test
154     public void
155             testFineGrainedWithImplications_CompileScopeShouldIncludeOnlyArtifactsWithNullSystemProvidedOrCompileScopes() {
156         ScopeArtifactFilter filter = new ScopeArtifactFilter();
157         filter.setIncludeCompileScopeWithImplications(true);
158 
159         verifyIncluded(filter, null);
160         verifyIncluded(filter, Artifact.SCOPE_COMPILE);
161         verifyIncluded(filter, Artifact.SCOPE_PROVIDED);
162         verifyIncluded(filter, Artifact.SCOPE_SYSTEM);
163 
164         verifyExcluded(filter, Artifact.SCOPE_RUNTIME);
165         verifyExcluded(filter, Artifact.SCOPE_TEST);
166     }
167 
168     @Test
169     public void testFineGrainedWithImplications_RuntimeScopeShouldIncludeOnlyArtifactsWithNullRuntimeOrCompileScopes() {
170         ScopeArtifactFilter filter = new ScopeArtifactFilter();
171         filter.setIncludeRuntimeScopeWithImplications(true);
172 
173         verifyIncluded(filter, null);
174         verifyIncluded(filter, Artifact.SCOPE_COMPILE);
175         verifyIncluded(filter, Artifact.SCOPE_RUNTIME);
176 
177         verifyExcluded(filter, Artifact.SCOPE_PROVIDED);
178         verifyExcluded(filter, Artifact.SCOPE_SYSTEM);
179         verifyExcluded(filter, Artifact.SCOPE_TEST);
180     }
181 
182     @Test
183     public void testFineGrainedWithImplications_TestScopeShouldIncludeAllScopes() {
184         ScopeArtifactFilter filter = new ScopeArtifactFilter();
185         filter.setIncludeTestScopeWithImplications(true);
186 
187         verifyIncluded(filter, null);
188         verifyIncluded(filter, Artifact.SCOPE_COMPILE);
189         verifyIncluded(filter, Artifact.SCOPE_RUNTIME);
190 
191         verifyIncluded(filter, Artifact.SCOPE_PROVIDED);
192         verifyIncluded(filter, Artifact.SCOPE_SYSTEM);
193         verifyIncluded(filter, Artifact.SCOPE_TEST);
194     }
195 
196     @Test
197     public void testScopesShouldIncludeArtifactWithSameScope() {
198         verifyIncluded(Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE);
199         verifyIncluded(Artifact.SCOPE_PROVIDED, Artifact.SCOPE_PROVIDED);
200         verifyIncluded(Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME);
201         verifyIncluded(Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM);
202         verifyIncluded(Artifact.SCOPE_TEST, Artifact.SCOPE_TEST);
203         verifyIncluded((String) null, null);
204     }
205 
206     @Test
207     public void testCompileScopeShouldIncludeOnlyArtifactsWithNullSystemProvidedOrCompileScopes() {
208         String scope = Artifact.SCOPE_COMPILE;
209 
210         verifyIncluded(scope, null);
211         verifyIncluded(scope, Artifact.SCOPE_COMPILE);
212         verifyIncluded(scope, Artifact.SCOPE_PROVIDED);
213         verifyIncluded(scope, Artifact.SCOPE_SYSTEM);
214 
215         verifyExcluded(scope, Artifact.SCOPE_RUNTIME);
216         verifyExcluded(scope, Artifact.SCOPE_TEST);
217     }
218 
219     @Test
220     public void testRuntimeScopeShouldIncludeOnlyArtifactsWithNullRuntimeOrCompileScopes() {
221         String scope = Artifact.SCOPE_RUNTIME;
222 
223         verifyIncluded(scope, null);
224         verifyIncluded(scope, Artifact.SCOPE_COMPILE);
225         verifyIncluded(scope, Artifact.SCOPE_RUNTIME);
226 
227         verifyExcluded(scope, Artifact.SCOPE_PROVIDED);
228         verifyExcluded(scope, Artifact.SCOPE_SYSTEM);
229         verifyExcluded(scope, Artifact.SCOPE_TEST);
230     }
231 
232     @Test
233     public void testTestScopeShouldIncludeAllScopes() {
234         String scope = Artifact.SCOPE_TEST;
235 
236         verifyIncluded(scope, null);
237         verifyIncluded(scope, Artifact.SCOPE_COMPILE);
238         verifyIncluded(scope, Artifact.SCOPE_RUNTIME);
239 
240         verifyIncluded(scope, Artifact.SCOPE_PROVIDED);
241         verifyIncluded(scope, Artifact.SCOPE_SYSTEM);
242         verifyIncluded(scope, Artifact.SCOPE_TEST);
243     }
244 
245     @Test
246     public void testProvidedScopeShouldIncludeOnlyArtifactsWithNullOrProvidedScopes() {
247         String scope = Artifact.SCOPE_PROVIDED;
248 
249         verifyIncluded(scope, null);
250         verifyExcluded(scope, Artifact.SCOPE_COMPILE);
251         verifyExcluded(scope, Artifact.SCOPE_RUNTIME);
252 
253         verifyIncluded(scope, Artifact.SCOPE_PROVIDED);
254 
255         verifyExcluded(scope, Artifact.SCOPE_SYSTEM);
256         verifyExcluded(scope, Artifact.SCOPE_TEST);
257     }
258 
259     @Test
260     public void testSystemScopeShouldIncludeOnlyArtifactsWithNullOrSystemScopes() {
261         String scope = Artifact.SCOPE_SYSTEM;
262 
263         verifyIncluded(scope, null);
264         verifyExcluded(scope, Artifact.SCOPE_COMPILE);
265         verifyExcluded(scope, Artifact.SCOPE_RUNTIME);
266         verifyExcluded(scope, Artifact.SCOPE_PROVIDED);
267 
268         verifyIncluded(scope, Artifact.SCOPE_SYSTEM);
269 
270         verifyExcluded(scope, Artifact.SCOPE_TEST);
271     }
272 
273     private void verifyIncluded(String filterScope, String artifactScope) {
274         Artifact artifact = createMockArtifact(artifactScope);
275 
276         ArtifactFilter filter = new ScopeArtifactFilter(filterScope);
277 
278         assertTrue(
279                 "Artifact scope: " + artifactScope + " NOT included using filter scope: " + filterScope,
280                 filter.include(artifact));
281     }
282 
283     private void verifyExcluded(String filterScope, String artifactScope) {
284         Artifact artifact = createMockArtifact(artifactScope);
285 
286         ArtifactFilter filter = new ScopeArtifactFilter(filterScope);
287 
288         assertFalse(
289                 "Artifact scope: " + artifactScope + " NOT excluded using filter scope: " + filterScope,
290                 filter.include(artifact));
291     }
292 
293     private void verifyIncluded(ScopeArtifactFilter filter, String artifactScope) {
294         Artifact artifact = createMockArtifact(artifactScope);
295 
296         assertTrue("Artifact scope: " + artifactScope + " SHOULD BE included", filter.include(artifact));
297     }
298 
299     private void verifyExcluded(ScopeArtifactFilter filter, String artifactScope) {
300         Artifact artifact = createMockArtifact(artifactScope);
301 
302         assertFalse("Artifact scope: " + artifactScope + " SHOULD BE excluded", filter.include(artifact));
303     }
304 
305     private Artifact createMockArtifact(String scope) {
306         Artifact artifact = mock(Artifact.class);
307 
308         when(artifact.getScope()).thenReturn(scope);
309         when(artifact.getId()).thenReturn("group:artifact:type:version");
310 
311         return artifact;
312     }
313 }