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