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