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