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