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.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
31  
32  import static org.easymock.EasyMock.*;
33  
34  public abstract class AbstractPatternArtifactFilterTest
35      extends TestCase
36  {
37  
38      protected abstract ArtifactFilter createFilter( List<String> patterns );
39  
40      protected abstract ArtifactFilter createFilter( List<String> patterns, boolean actTransitively );
41  
42      protected abstract boolean isInclusionExpected();
43  
44      public void testShouldTriggerBothPatternsWithWildcards()
45      {
46          final String groupId1 = "group";
47          final String artifactId1 = "artifact";
48  
49          final String groupId2 = "group2";
50          final String artifactId2 = "artifact2";
51  
52          final ArtifactMockAndControl mac1 = new ArtifactMockAndControl( groupId1, artifactId1 );
53          final ArtifactMockAndControl mac2 = new ArtifactMockAndControl( groupId2, artifactId2 );
54  
55          replay( mac1.getMock(), mac2.getMock() );
56  
57          final List<String> patterns = new ArrayList<String>();
58          patterns.add( groupId1 + ":" + artifactId1 + ":*" );
59          patterns.add( groupId2 + ":" + artifactId2 + ":*" );
60  
61          final ArtifactFilter filter = createFilter( patterns );
62  
63          if ( !isInclusionExpected() )
64          {
65              assertFalse( filter.include( mac1.artifact ) );
66              assertFalse( filter.include( mac2.artifact ) );
67          }
68          else
69          {
70              assertTrue( filter.include( mac1.artifact ) );
71              assertTrue( filter.include( mac2.artifact ) );
72          }
73  
74          verify( mac1.getMock(), mac2.getMock() );
75      }
76  
77      public void testShouldTriggerBothPatternsWithNonColonWildcards()
78      {
79          final String groupId1 = "group";
80          final String artifactId1 = "artifact";
81  
82          final String groupId2 = "group2";
83          final String artifactId2 = "artifact2";
84  
85          final ArtifactMockAndControl mac1 = new ArtifactMockAndControl( groupId1, artifactId1 );
86          final ArtifactMockAndControl mac2 = new ArtifactMockAndControl( groupId2, artifactId2 );
87  
88          replay( mac1.getMock(), mac2.getMock() );
89  
90          final List<String> patterns = new ArrayList<String>();
91          patterns.add( groupId1 + "*" );
92          patterns.add( groupId2 + "*" );
93  
94          final ArtifactFilter filter = createFilter( patterns );
95  
96          if ( !isInclusionExpected() )
97          {
98              assertFalse( filter.include( mac1.artifact ) );
99              assertFalse( filter.include( mac2.artifact ) );
100         }
101         else
102         {
103             assertTrue( filter.include( mac1.artifact ) );
104             assertTrue( filter.include( mac2.artifact ) );
105         }
106 
107         verify( mac1.getMock(), mac2.getMock() );
108     }
109 
110     public void testShouldIncludeDirectlyMatchedArtifactByGroupIdArtifactId()
111     {
112         final String groupId = "group";
113         final String artifactId = "artifact";
114 
115         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
116 
117         replay( mac.getMock() );
118 
119         final ArtifactFilter filter = createFilter( Collections.singletonList( groupId + ":" + artifactId ) );
120 
121         if ( !isInclusionExpected() )
122         {
123             assertFalse( filter.include( mac.artifact ) );
124         }
125         else
126         {
127             assertTrue( filter.include( mac.artifact ) );
128         }
129 
130         verify( mac.getMock() );
131     }
132 
133     public void testShouldIncludeDirectlyMatchedArtifactByDependencyConflictId()
134     {
135         final String groupId = "group";
136         final String artifactId = "artifact";
137 
138         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
139 
140         replay( mac.getMock() );
141 
142         final ArtifactFilter filter = createFilter( Collections.singletonList( groupId + ":" + artifactId + ":jar" ) );
143 
144         if ( !isInclusionExpected() )
145         {
146             assertFalse( filter.include( mac.artifact ) );
147         }
148         else
149         {
150             assertTrue( filter.include( mac.artifact ) );
151         }
152 
153         verify( mac.getMock() );
154     }
155 
156     public void testShouldNotIncludeWhenGroupIdDiffers()
157     {
158         final String groupId = "group";
159         final String artifactId = "artifact";
160 
161         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
162 
163         replay( mac.getMock() );
164         final List<String> patterns = new ArrayList<String>();
165 
166         patterns.add( "otherGroup:" + artifactId + ":jar" );
167         patterns.add( "otherGroup:" + artifactId );
168 
169         final ArtifactFilter filter = createFilter( patterns );
170 
171         if ( !isInclusionExpected() )
172         {
173             assertTrue( filter.include( mac.artifact ) );
174         }
175         else
176         {
177             assertFalse( filter.include( mac.artifact ) );
178         }
179 
180         verify( mac.getMock() );
181     }
182 
183     public void testShouldNotIncludeWhenArtifactIdDiffers()
184     {
185         final String groupId = "group";
186         final String artifactId = "artifact";
187 
188         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
189 
190         replay( mac.getMock() );
191 
192         final List<String> patterns = new ArrayList<String>();
193 
194         patterns.add( groupId + "otherArtifact:jar" );
195         patterns.add( groupId + "otherArtifact" );
196 
197         final ArtifactFilter filter = createFilter( patterns );
198 
199         if ( !isInclusionExpected() )
200         {
201             assertTrue( filter.include( mac.artifact ) );
202         }
203         else
204         {
205             assertFalse( filter.include( mac.artifact ) );
206         }
207 
208         verify( mac.getMock() );
209     }
210 
211     public void testShouldNotIncludeWhenBothIdElementsDiffer()
212     {
213         final String groupId = "group";
214         final String artifactId = "artifact";
215 
216         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
217 
218         replay( mac.getMock() );
219 
220         final List<String> patterns = new ArrayList<String>();
221 
222         patterns.add( "otherGroup:otherArtifact:jar" );
223         patterns.add( "otherGroup:otherArtifact" );
224 
225         final ArtifactFilter filter = createFilter( patterns );
226 
227         if ( !isInclusionExpected() )
228         {
229             assertTrue( filter.include( mac.artifact ) );
230         }
231         else
232         {
233             assertFalse( filter.include( mac.artifact ) );
234         }
235 
236         verify( mac.getMock() );
237     }
238 
239     public void testShouldIncludeWhenPatternMatchesDependencyTrailAndTransitivityIsEnabled()
240     {
241         final String groupId = "group";
242         final String artifactId = "artifact";
243 
244         final String rootDepTrailItem = "current:project:jar:1.0";
245         final String depTrailItem = "otherGroup:otherArtifact";
246 
247         final List<String> depTrail = Arrays.asList( new String[] { rootDepTrailItem, depTrailItem + ":jar:1.0" } );
248         final List<String> patterns = Collections.singletonList( depTrailItem );
249 
250         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, depTrail );
251 
252         replay( mac.getMock() );
253 
254         final ArtifactFilter filter = createFilter( patterns, true );
255 
256         if ( !isInclusionExpected() )
257         {
258             assertFalse( filter.include( mac.artifact ) );
259         }
260         else
261         {
262             assertTrue( filter.include( mac.artifact ) );
263         }
264 
265         verify( mac.getMock() );
266     }
267 
268     public void testIncludeWhenPatternMatchesDepTrailWithTransitivityUsingNonColonWildcard()
269     {
270         final String groupId = "group";
271         final String artifactId = "artifact";
272 
273         final String rootDepTrailItem = "current:project:jar:1.0";
274         final String depTrailItem = "otherGroup:otherArtifact";
275 
276         final List<String> depTrail = Arrays.asList( new String[] { rootDepTrailItem, depTrailItem + ":jar:1.0" } );
277         final List<String> patterns = Collections.singletonList( "otherGroup*" );
278 
279         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, depTrail );
280 
281         replay( mac.getMock() );
282 
283         final ArtifactFilter filter = createFilter( patterns, true );
284 
285         if ( !isInclusionExpected() )
286         {
287             assertFalse( filter.include( mac.artifact ) );
288         }
289         else
290         {
291             assertTrue( filter.include( mac.artifact ) );
292         }
293 
294         verify( mac.getMock() );
295     }
296 
297     public void testShouldNotIncludeWhenNegativeMatch()
298     {
299         final String groupId = "group";
300         final String artifactId = "artifact";
301 
302         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
303 
304         replay( mac.getMock() );
305 
306         final List<String> patterns = new ArrayList<String>();
307 
308         patterns.add( "!group:artifact:jar" );
309 
310         final ArtifactFilter filter = createFilter( patterns );
311 
312         if ( !isInclusionExpected() )
313         {
314             assertTrue( filter.include( mac.artifact ) );
315         }
316         else
317         {
318             assertFalse( filter.include( mac.artifact ) );
319         }
320 
321         verify( mac.getMock() );
322     }
323 
324     public void testShouldIncludeWhenWildcardMatchesInsideSequence()
325     {
326         final String groupId = "group";
327         final String artifactId = "artifact";
328 
329         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
330 
331         replay( mac.getMock() );
332 
333         final List<String> patterns = new ArrayList<String>();
334 
335         patterns.add( "group:*:jar" );
336 
337         final ArtifactFilter filter = createFilter( patterns );
338 
339         if ( !isInclusionExpected() )
340         {
341             assertFalse( filter.include( mac.artifact ) );
342         }
343         else
344         {
345             assertTrue( filter.include( mac.artifact ) );
346         }
347 
348         verify( mac.getMock() );
349     }
350 
351     public void testShouldIncludeWhenWildcardMatchesOutsideSequence()
352     {
353         final String groupId = "group";
354         final String artifactId = "artifact";
355 
356         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
357 
358         replay( mac.getMock() );
359 
360         final List<String> patterns = new ArrayList<String>();
361 
362         patterns.add( "*:artifact:*" );
363 
364         final ArtifactFilter filter = createFilter( patterns );
365 
366         if ( !isInclusionExpected() )
367         {
368             assertFalse( filter.include( mac.artifact ) );
369         }
370         else
371         {
372             assertTrue( filter.include( mac.artifact ) );
373         }
374 
375         verify( mac.getMock() );
376     }
377 
378     public void testShouldIncludeWhenWildcardMatchesMiddleOfArtifactId()
379     {
380         final String groupId = "group";
381         final String artifactId = "some-artifact-id";
382 
383         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
384 
385         replay( mac.getMock() );
386 
387         final List<String> patterns = new ArrayList<String>();
388 
389         patterns.add( "group:some-*-id" );
390 
391         final ArtifactFilter filter = createFilter( patterns );
392 
393         if ( !isInclusionExpected() )
394         {
395             assertFalse( filter.include( mac.artifact ) );
396         }
397         else
398         {
399             assertTrue( filter.include( mac.artifact ) );
400         }
401 
402         verify( mac.getMock() );
403     }
404 
405     public void testShouldIncludeWhenWildcardCoversPartOfGroupIdAndEverythingElse()
406     {
407         final String groupId = "some.group.id";
408         final String artifactId = "some-artifact-id";
409 
410         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
411 
412         replay( mac.getMock() );
413 
414         final List<String> patterns = new ArrayList<String>();
415 
416         patterns.add( "some.group*" );
417 
418         final ArtifactFilter filter = createFilter( patterns );
419 
420         if ( !isInclusionExpected() )
421         {
422             assertFalse( filter.include( mac.artifact ) );
423         }
424         else
425         {
426             assertTrue( filter.include( mac.artifact ) );
427         }
428 
429         verify( mac.getMock() );
430     }
431 
432     public void testShouldIncludeTransitiveDependencyWhenWildcardMatchesButDoesntMatchParent()
433     {
434         final String groupId = "group";
435         final String artifactId = "artifact";
436 
437         final String otherGroup = "otherGroup";
438         final String otherArtifact = "otherArtifact";
439         final String otherType = "ejb";
440 
441         final String depTrailItem = otherGroup + ":" + otherArtifact + ":" + otherType + ":version";
442         final List<String> depTrail = Collections.singletonList( depTrailItem );
443         final List<String> patterns = Collections.singletonList( "*:jar:*" );
444 
445         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, "jar", depTrail );
446         final ArtifactMockAndControl otherMac =
447             new ArtifactMockAndControl( otherGroup, otherArtifact, otherType, Collections.<String> emptyList() );
448 
449         replay( mac.getMock(), otherMac.getMock() );
450 
451         final ArtifactFilter filter = createFilter( patterns, true );
452 
453         if ( !isInclusionExpected() )
454         {
455             assertTrue( filter.include( otherMac.artifact ) );
456             assertFalse( filter.include( mac.artifact ) );
457         }
458         else
459         {
460             assertFalse( filter.include( otherMac.artifact ) );
461             assertTrue( filter.include( mac.artifact ) );
462         }
463 
464         verify( mac.getMock(), otherMac.getMock() );
465     }
466     
467     public void testShouldIncludeJarsWithAndWithoutClassifier()
468     {
469         final String groupId = "com.mycompany.myproject";
470         final String artifactId = "some-artifact-id";
471 
472         final ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
473 
474         replay( mac.getMock() );
475 
476         final List<String> patterns = new ArrayList<String>();
477 
478         patterns.add( "com.mycompany.*:*:jar:*:*" );
479 
480         final ArtifactFilter filter = createFilter( patterns );
481 
482         if ( !isInclusionExpected() )
483         {
484             assertFalse( filter.include( mac.artifact ) );
485         }
486         else
487         {
488             assertTrue( filter.include( mac.artifact ) );
489         }
490 
491         verify( mac.getMock() );
492     }
493 
494     // FIXME: Not sure what this is even trying to test.
495     // public void testShouldIncludeDirectDependencyWhenInvertedWildcardMatchesButDoesntMatchTransitiveChild(
496     // boolean reverse )
497     // {
498     // String groupId = "group";
499     // String artifactId = "artifact";
500     //
501     // String otherGroup = "otherGroup";
502     // String otherArtifact = "otherArtifact";
503     // String otherType = "ejb";
504     //
505     // String depTrailItem = otherGroup + ":" + otherArtifact + ":" + otherType + ":version";
506     // List depTrail = Collections.singletonList( depTrailItem );
507     // List patterns = Collections.singletonList( "!*:ejb:*" );
508     //
509     // ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, "jar", depTrail );
510     // ArtifactMockAndControl otherMac = new ArtifactMockAndControl( otherGroup, otherArtifact, otherType, null );
511     //
512     // mockManager.replayAll();
513     //
514     // ArtifactFilter filter = createFilter( patterns, true );
515     //
516     // if ( isInclusionExpected() )
517     // {
518     // assertTrue( filter.include( otherMac.artifact ) );
519     // assertFalse( filter.include( mac.artifact ) );
520     // }
521     // else
522     // {
523     // assertFalse( filter.include( otherMac.artifact ) );
524     // assertFalse( filter.include( mac.artifact ) );
525     // }
526     //
527     // mockManager.verifyAll();
528     // }
529 
530     private final class ArtifactMockAndControl
531     {
532         Artifact artifact;
533 
534         String groupId;
535 
536         String artifactId;
537 
538         String version;
539 
540         List<String> dependencyTrail;
541 
542         String type;
543 
544         ArtifactMockAndControl( final String groupId, final String artifactId, final List<String> depTrail )
545         {
546             this( groupId, artifactId, "jar", depTrail );
547         }
548 
549         ArtifactMockAndControl( final String groupId, final String artifactId, final String type,
550                                 final List<String> dependencyTrail )
551         {
552             this.groupId = groupId;
553             this.artifactId = artifactId;
554             this.dependencyTrail = dependencyTrail;
555             this.type = type;
556 
557             artifact = createNiceMock( Artifact.class );
558 
559             enableGetDependencyConflictId();
560             enableGetGroupIdArtifactIdAndVersion();
561             enableGetId();
562 
563             if ( dependencyTrail != null )
564             {
565                 enableGetDependencyTrail();
566             }
567         }
568 
569         ArtifactMockAndControl( final String groupId, final String artifactId )
570         {
571             this( groupId, artifactId, "jar", null );
572         }
573 
574         Artifact getMock()
575         {
576             return artifact;
577         }
578 
579         void enableGetId()
580         {
581             expect( artifact.getId() ).andReturn( groupId + ":" + artifactId + ":" + type + ":version" ).anyTimes();
582         }
583 
584         void enableGetDependencyTrail()
585         {
586             expect( artifact.getDependencyTrail() ).andReturn( dependencyTrail ).anyTimes();
587         }
588 
589         void enableGetDependencyConflictId()
590         {
591             expect( artifact.getDependencyConflictId() ).andReturn( groupId + ":" + artifactId + ":" + type ).atLeastOnce();
592         }
593 
594         void enableGetGroupIdArtifactIdAndVersion()
595         {
596             expect( artifact.getGroupId() ).andReturn( groupId ).atLeastOnce();
597             expect( artifact.getArtifactId() ).andReturn( artifactId ).atLeastOnce();
598             expect( artifact.getVersion() ).andReturn( version ).anyTimes();
599         }
600     }
601 
602 }