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 java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.DefaultArtifact;
27  import org.apache.maven.artifact.handler.ArtifactHandler;
28  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  
31  import junit.framework.AssertionFailedError;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  
37  /**
38   * Tests subclasses of <code>AbstractStrictPatternArtifactFilter</code>.
39   * 
40   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
41   * @see AbstractStrictPatternArtifactFilter
42   */
43  public abstract class AbstractStrictPatternArtifactFilterTest
44  {
45      protected Artifact artifact;
46  
47      @Before
48      public void setUp()
49      {
50          artifact = createArtifact( "groupId", "artifactId", "type", "version" );
51      }
52  
53      @Test
54      public void testExactIncluded()
55      {
56          assertIncluded( "groupId:artifactId" );
57      }
58  
59      @Test
60      public void testExactExcluded()
61      {
62          assertExcluded( "differentGroupId:differentArtifactId" );
63      }
64  
65      @Test
66      public void testGroupIdIncluded()
67      {
68          assertIncluded( "groupId" );
69      }
70  
71      @Test
72      public void testGroupIdExcluded()
73      {
74          assertExcluded( "differentGroupId" );
75      }
76  
77      @Test
78      public void testGroupIdWildcardIncluded()
79      {
80          assertIncluded( "*" );
81      }
82  
83      @Test
84      public void testGroupIdImplicitWildcardIncluded()
85      {
86          assertIncluded( "" );
87      }
88  
89      @Test
90      public void testGroupIdStartsWithWildcardIncluded()
91      {
92          assertIncluded( "groupId*" );
93      }
94  
95      @Test
96      public void testGroupIdStartsWithPartialWildcardIncluded()
97      {
98          assertIncluded( "group*" );
99      }
100 
101     @Test
102     public void testGroupIdStartsWithWildcardExcluded()
103     {
104         assertExcluded( "different*" );
105     }
106 
107     @Test
108     public void testGroupIdEndsWithWildcardIncluded()
109     {
110         assertIncluded( "*groupId" );
111     }
112 
113     @Test
114     public void testGroupIdEndsWithPartialWildcardIncluded()
115     {
116         assertIncluded( "*Id" );
117     }
118 
119     @Test
120     public void testGroupIdEndsWithWildcardExcluded()
121     {
122         assertExcluded( "*different" );
123     }
124 
125     @Test
126     public void testGroupIdContainsWildcardIncluded()
127     {
128         assertIncluded( "*oup*" );
129     }
130 
131     @Test
132     public void testGroupIdContainsWildcardExcluded()
133     {
134         assertExcluded( "*different*" );
135     }
136 
137     @Test
138     public void testArtifactIdIncluded()
139     {
140         assertIncluded( ":artifactId" );
141     }
142 
143     @Test
144     public void testArtifactIdExcluded()
145     {
146         assertExcluded( ":differentArtifactId" );
147     }
148 
149     @Test
150     public void testArtifactIdWildcardIncluded()
151     {
152         assertIncluded( ":*" );
153     }
154 
155     @Test
156     public void testArtifactIdImplicitWildcardIncluded()
157     {
158         assertIncluded( ":" );
159     }
160 
161     @Test
162     public void testArtifactIdStartsWithWildcardIncluded()
163     {
164         assertIncluded( ":artifactId*" );
165     }
166 
167     @Test
168     public void testArtifactIdStartsWithPartialWildcardIncluded()
169     {
170         assertIncluded( ":artifact*" );
171     }
172 
173     @Test
174     public void testArtifactIdStartsWithWildcardExcluded()
175     {
176         assertExcluded( ":different*" );
177     }
178 
179     @Test
180     public void testArtifactIdEndsWithWildcardIncluded()
181     {
182         assertIncluded( ":*artifactId" );
183     }
184 
185     @Test
186     public void testArtifactIdEndsWithPartialWildcardIncluded()
187     {
188         assertIncluded( ":*Id" );
189     }
190 
191     @Test
192     public void testArtifactIdEndsWithWildcardExcluded()
193     {
194         assertExcluded( ":*different" );
195     }
196 
197     @Test
198     public void testArtifactIdContainsWildcardIncluded()
199     {
200         assertIncluded( ":*fact*" );
201     }
202 
203     @Test
204     public void testArtifactIdContainsWildcardExcluded()
205     {
206         assertExcluded( ":*different*" );
207     }
208 
209     @Test
210     public void testTypeIncluded()
211     {
212         assertIncluded( "::type" );
213     }
214 
215     @Test
216     public void testTypeExcluded()
217     {
218         assertExcluded( "::differentType" );
219     }
220 
221     @Test
222     public void testTypeWildcardIncluded()
223     {
224         assertIncluded( "::*" );
225     }
226 
227     @Test
228     public void testTypeImplicitWildcardIncluded()
229     {
230         assertIncluded( "::" );
231     }
232 
233     @Test
234     public void testTypeStartsWithWildcardIncluded()
235     {
236         assertIncluded( "::type*" );
237     }
238 
239     @Test
240     public void testTypeStartsWithPartialWildcardIncluded()
241     {
242         assertIncluded( "::t*" );
243     }
244 
245     @Test
246     public void testTypeStartsWithWildcardExcluded()
247     {
248         assertExcluded( "::different*" );
249     }
250 
251     @Test
252     public void testTypeEndsWithWildcardIncluded()
253     {
254         assertIncluded( "::*type" );
255     }
256 
257     @Test
258     public void testTypeEndsWithPartialWildcardIncluded()
259     {
260         assertIncluded( "::*e" );
261     }
262 
263     @Test
264     public void testTypeEndsWithWildcardExcluded()
265     {
266         assertExcluded( "::*different" );
267     }
268 
269     @Test
270     public void testTypeContainsWildcardIncluded()
271     {
272         assertIncluded( "::*yp*" );
273     }
274 
275     @Test
276     public void testTypeContainsWildcardExcluded()
277     {
278         assertExcluded( "::*different*" );
279     }
280 
281     @Test
282     public void testVersionIncluded()
283     {
284         assertIncluded( ":::version" );
285     }
286 
287     @Test
288     public void testVersionExcluded()
289     {
290         assertExcluded( ":::differentVersion" );
291     }
292 
293     @Test
294     public void testVersionWildcardIncluded()
295     {
296         assertIncluded( ":::*" );
297     }
298 
299     @Test
300     public void testVersionImplicitWildcardIncluded()
301     {
302         assertIncluded( ":::" );
303     }
304 
305     @Test
306     public void testVersionStartsWithWildcardIncluded()
307     {
308         assertIncluded( ":::version*" );
309     }
310 
311     @Test
312     public void testVersionStartsWithPartialWildcardIncluded()
313     {
314         assertIncluded( ":::ver*" );
315     }
316 
317     @Test
318     public void testVersionStartsWithWildcardExcluded()
319     {
320         assertExcluded( ":::different*" );
321     }
322 
323     @Test
324     public void testVersionEndsWithWildcardIncluded()
325     {
326         assertIncluded( ":::*version" );
327     }
328 
329     @Test
330     public void testVersionEndsWithPartialWildcardIncluded()
331     {
332         assertIncluded( ":::*ion" );
333     }
334 
335     @Test
336     public void testVersionEndsWithWildcardExcluded()
337     {
338         assertExcluded( ":::*different" );
339     }
340 
341     @Test
342     public void testVersionContainsWildcardIncluded()
343     {
344         assertIncluded( ":::*si*" );
345     }
346 
347     @Test
348     public void testVersionContainsWildcardExcluded()
349     {
350         assertExcluded( ":::*different*" );
351     }
352 
353     @Test
354     public void testComplex()
355     {
356         assertIncluded( "group*:*Id:*:version" );
357     }
358 
359     @Test
360     public void testSnapshotVersion()
361     {
362         artifact = createArtifact( "groupId", "artifactId", "type", "version-12345678.123456-1" );
363 
364         assertIncluded( ":::*-SNAPSHOT" );
365     }
366     
367     @Test
368     public void testRangeVersion()
369     {
370         artifact = createArtifact( "groupId", "artifactId", "type", "1.0.1" );
371         assertIncluded( "groupId:artifactId:type:[1.0.1]");
372         assertIncluded( "groupId:artifactId:type:[1.0,1.1)");
373         
374         assertExcluded( "groupId:artifactId:type:[1.5,)");
375         assertExcluded( "groupId:artifactId:type:(,1.0],[1.2,)");
376         assertExcluded( "groupId:artifactId:type:(,1.0],[1.2,)");
377     }
378 
379     @Test
380     public void testWildcardsWithRangeVersion()
381     {
382         artifact = createArtifact( "groupId", "artifactId", "type", "1.0.1" );
383         assertIncluded( ":::[1.0.1]");
384         assertIncluded( ":artifact*:*:[1.0,1.1)");
385         
386         assertExcluded( "*group*:*:t*e:[1.5,)");
387 
388         artifact = createArtifact( "test", "uf", "jar", "0.2.0" );
389         assertIncluded( "test:*:*:[0.0.2,)" );
390     	
391     }
392     
393     // protected methods ------------------------------------------------------
394 
395     /**
396      * Creates an artifact with the specified attributes.
397      * 
398      * @param groupId
399      *            the group id for the new artifact
400      * @param artifactId
401      *            the artifact id for the new artifact
402      * @param type
403      *            the type for the new artifact
404      * @param version
405      *            the version for the new artifact
406      * @return the artifact
407      */
408     protected Artifact createArtifact( String groupId, String artifactId, String type, String version )
409     {
410         VersionRange versionRange = VersionRange.createFromVersion( version );
411         ArtifactHandler handler = new DefaultArtifactHandler();
412 
413         return new DefaultArtifact( groupId, artifactId, versionRange, null, type, null, handler );
414     }
415 
416     /**
417      * Asserts that the specified pattern is included by the filter being tested.
418      * 
419      * @param pattern
420      *            the pattern to test for inclusion
421      * @throws AssertionFailedError
422      *             if the assertion fails
423      */
424     protected void assertIncluded( String pattern )
425     {
426         assertFilter( true, pattern );
427     }
428 
429     /**
430      * Asserts that the specified pattern is excluded by the filter being tested.
431      * 
432      * @param pattern
433      *            the pattern to test for exclusion
434      * @throws AssertionFailedError
435      *             if the assertion fails
436      */
437     protected void assertExcluded( String pattern )
438     {
439         assertFilter( false, pattern );
440     }
441 
442     /**
443      * Asserts that the filter being tested returns the specified result for the specified pattern.
444      * 
445      * @param expected
446      *            the result expected from the filter
447      * @param pattern
448      *            the pattern to test
449      * @throws AssertionFailedError
450      *             if the assertion fails
451      */
452     protected void assertFilter( boolean expected, String pattern )
453     {
454         List<String> patterns = Collections.singletonList( pattern );
455         AbstractStrictPatternArtifactFilter filter = createFilter( patterns );
456 
457         assertEquals( expected, filter.include( artifact ) );
458     }
459 
460     /**
461      * Creates the strict pattern artifact filter to test for the specified patterns.
462      * 
463      * @param patterns
464      *            the list of artifact patterns that the filter should match
465      * @return the filter to test
466      */
467     protected abstract AbstractStrictPatternArtifactFilter createFilter( List<String> patterns );
468 }