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