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