1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.artifact.filter;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
29  import org.apache.maven.shared.tools.easymock.MockManager;
30  import org.easymock.MockControl;
31  
32  public abstract class PatternArtifactFilterTCK
33      extends TestCase
34  {
35  
36      private MockManager mockManager = new MockManager();
37  
38      protected abstract ArtifactFilter createFilter( List patterns );
39  
40      protected abstract ArtifactFilter createFilter( List patterns, boolean actTransitively );
41  
42      public void testShouldTriggerBothPatternsWithWildcards( boolean reverse )
43      {
44          String groupId1 = "group";
45          String artifactId1 = "artifact";
46  
47          String groupId2 = "group2";
48          String artifactId2 = "artifact2";
49  
50          ArtifactMockAndControl mac1 = new ArtifactMockAndControl( groupId1, artifactId1 );
51          ArtifactMockAndControl mac2 = new ArtifactMockAndControl( groupId2, artifactId2 );
52  
53          mockManager.replayAll();
54  
55          List patterns = new ArrayList();
56          patterns.add( groupId1 + ":" + artifactId1 + ":*" );
57          patterns.add( groupId2 + ":" + artifactId2 + ":*" );
58  
59          ArtifactFilter filter = createFilter( patterns );
60  
61          if ( reverse )
62          {
63              assertFalse( filter.include( mac1.artifact ) );
64              assertFalse( filter.include( mac2.artifact ) );
65          }
66          else
67          {
68              assertTrue( filter.include( mac1.artifact ) );
69              assertTrue( filter.include( mac2.artifact ) );
70          }
71  
72          mockManager.verifyAll();
73      }
74  
75      public void testShouldIncludeDirectlyMatchedArtifactByGroupIdArtifactId( boolean reverse )
76      {
77          String groupId = "group";
78          String artifactId = "artifact";
79  
80          ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
81  
82          mockManager.replayAll();
83  
84          ArtifactFilter filter = createFilter( Collections.singletonList( groupId + ":" + artifactId ) );
85  
86          if ( reverse )
87          {
88              assertFalse( filter.include( mac.artifact ) );
89          }
90          else
91          {
92              assertTrue( filter.include( mac.artifact ) );
93          }
94  
95          mockManager.verifyAll();
96      }
97  
98      public void testShouldIncludeDirectlyMatchedArtifactByDependencyConflictId( boolean reverse )
99      {
100         String groupId = "group";
101         String artifactId = "artifact";
102 
103         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
104 
105         mockManager.replayAll();
106 
107         ArtifactFilter filter = createFilter( Collections.singletonList( groupId + ":" + artifactId + ":jar" ) );
108 
109         if ( reverse )
110         {
111             assertFalse( filter.include( mac.artifact ) );
112         }
113         else
114         {
115             assertTrue( filter.include( mac.artifact ) );
116         }
117 
118         mockManager.verifyAll();
119     }
120 
121     public void testShouldNotIncludeWhenGroupIdDiffers( boolean reverse )
122     {
123         String groupId = "group";
124         String artifactId = "artifact";
125 
126         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
127 
128         mockManager.replayAll();
129         List patterns = new ArrayList();
130 
131         patterns.add( "otherGroup:" + artifactId + ":jar" );
132         patterns.add( "otherGroup:" + artifactId );
133 
134         ArtifactFilter filter = createFilter( patterns );
135 
136         if ( reverse )
137         {
138             assertTrue( filter.include( mac.artifact ) );
139         }
140         else
141         {
142             assertFalse( filter.include( mac.artifact ) );
143         }
144 
145         mockManager.verifyAll();
146     }
147 
148     public void testShouldNotIncludeWhenArtifactIdDiffers( boolean reverse )
149     {
150         String groupId = "group";
151         String artifactId = "artifact";
152 
153         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
154 
155         mockManager.replayAll();
156 
157         List patterns = new ArrayList();
158 
159         patterns.add( groupId + "otherArtifact:jar" );
160         patterns.add( groupId + "otherArtifact" );
161 
162         ArtifactFilter filter = createFilter( patterns );
163 
164         if ( reverse )
165         {
166             assertTrue( filter.include( mac.artifact ) );
167         }
168         else
169         {
170             assertFalse( filter.include( mac.artifact ) );
171         }
172 
173         mockManager.verifyAll();
174     }
175 
176     public void testShouldNotIncludeWhenBothIdElementsDiffer( boolean reverse )
177     {
178         String groupId = "group";
179         String artifactId = "artifact";
180 
181         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
182 
183         mockManager.replayAll();
184 
185         List patterns = new ArrayList();
186 
187         patterns.add( "otherGroup:otherArtifact:jar" );
188         patterns.add( "otherGroup:otherArtifact" );
189 
190         ArtifactFilter filter = createFilter( patterns );
191 
192         if ( reverse )
193         {
194             assertTrue( filter.include( mac.artifact ) );
195         }
196         else
197         {
198             assertFalse( filter.include( mac.artifact ) );
199         }
200 
201         mockManager.verifyAll();
202     }
203 
204     public void testShouldIncludeWhenPatternMatchesDependencyTrailAndTransitivityIsEnabled( boolean reverse )
205     {
206         String groupId = "group";
207         String artifactId = "artifact";
208 
209         String depTrailItem = "otherGroup:otherArtifact";
210         List depTrail = Collections.singletonList( depTrailItem + ":jar:1.0" );
211         List patterns = Collections.singletonList( depTrailItem );
212 
213         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, depTrail );
214 
215         mockManager.replayAll();
216 
217         ArtifactFilter filter = createFilter( patterns, true );
218 
219         if ( reverse )
220         {
221             assertFalse( filter.include( mac.artifact ) );
222         }
223         else
224         {
225             assertTrue( filter.include( mac.artifact ) );
226         }
227 
228         mockManager.verifyAll();
229     }
230 
231     public void testShouldNotIncludeWhenNegativeMatch( boolean reverse )
232     {
233         String groupId = "group";
234         String artifactId = "artifact";
235 
236         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
237 
238         mockManager.replayAll();
239 
240         List patterns = new ArrayList();
241 
242         patterns.add( "!group:artifact:jar" );
243 
244         ArtifactFilter filter = createFilter( patterns );
245 
246         if ( reverse )
247         {
248             assertTrue( filter.include( mac.artifact ) );
249         }
250         else
251         {
252             assertFalse( filter.include( mac.artifact ) );
253         }
254 
255         mockManager.verifyAll();
256     }
257 
258     public void testShouldIncludeWhenWildcardMatchesInsideSequence( boolean reverse )
259     {
260         String groupId = "group";
261         String artifactId = "artifact";
262 
263         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
264 
265         mockManager.replayAll();
266 
267         List patterns = new ArrayList();
268 
269         patterns.add( "group:*:jar" );
270 
271         ArtifactFilter filter = createFilter( patterns );
272 
273         if ( reverse )
274         {
275             assertFalse( filter.include( mac.artifact ) );
276         }
277         else
278         {
279             assertTrue( filter.include( mac.artifact ) );
280         }
281 
282         mockManager.verifyAll();
283     }
284 
285     public void testShouldIncludeWhenWildcardMatchesOutsideSequence( boolean reverse )
286     {
287         String groupId = "group";
288         String artifactId = "artifact";
289 
290         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId );
291 
292         mockManager.replayAll();
293 
294         List patterns = new ArrayList();
295 
296         patterns.add( "*:artifact:*" );
297 
298         ArtifactFilter filter = createFilter( patterns );
299 
300         if ( reverse )
301         {
302             assertFalse( filter.include( mac.artifact ) );
303         }
304         else
305         {
306             assertTrue( filter.include( mac.artifact ) );
307         }
308 
309         mockManager.verifyAll();
310     }
311 
312     public void testShouldIncludeTransitiveDependencyWhenWildcardMatchesButDoesntMatchParent( boolean reverse )
313     {
314         String groupId = "group";
315         String artifactId = "artifact";
316 
317         String otherGroup = "otherGroup";
318         String otherArtifact = "otherArtifact";
319         String otherType = "ejb";
320 
321         String depTrailItem = otherGroup + ":" + otherArtifact + ":" + otherType + ":version";
322         List depTrail = Collections.singletonList( depTrailItem );
323         List patterns = Collections.singletonList( "*:jar:*" );
324 
325         ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, "jar", depTrail );
326         ArtifactMockAndControl otherMac = new ArtifactMockAndControl( otherGroup, otherArtifact, otherType,
327                                                                       Collections.EMPTY_LIST );
328 
329         mockManager.replayAll();
330 
331         ArtifactFilter filter = createFilter( patterns, true );
332 
333         if ( reverse )
334         {
335             assertTrue( filter.include( otherMac.artifact ) );
336             assertFalse( filter.include( mac.artifact ) );
337         }
338         else
339         {
340             assertFalse( filter.include( otherMac.artifact ) );
341             assertTrue( filter.include( mac.artifact ) );
342         }
343 
344         mockManager.verifyAll();
345     }
346 
347     // FIXME: Not sure what this is even trying to test.
348 //    public void testShouldIncludeDirectDependencyWhenInvertedWildcardMatchesButDoesntMatchTransitiveChild(
349 //                                                                                                           boolean reverse )
350 //    {
351 //        String groupId = "group";
352 //        String artifactId = "artifact";
353 //
354 //        String otherGroup = "otherGroup";
355 //        String otherArtifact = "otherArtifact";
356 //        String otherType = "ejb";
357 //
358 //        String depTrailItem = otherGroup + ":" + otherArtifact + ":" + otherType + ":version";
359 //        List depTrail = Collections.singletonList( depTrailItem );
360 //        List patterns = Collections.singletonList( "!*:ejb:*" );
361 //
362 //        ArtifactMockAndControl mac = new ArtifactMockAndControl( groupId, artifactId, "jar", depTrail );
363 //        ArtifactMockAndControl otherMac = new ArtifactMockAndControl( otherGroup, otherArtifact, otherType, null );
364 //
365 //        mockManager.replayAll();
366 //
367 //        ArtifactFilter filter = createFilter( patterns, true );
368 //
369 //        if ( reverse )
370 //        {
371 //            assertTrue( filter.include( otherMac.artifact ) );
372 //            assertFalse( filter.include( mac.artifact ) );
373 //        }
374 //        else
375 //        {
376 //            assertFalse( filter.include( otherMac.artifact ) );
377 //            assertFalse( filter.include( mac.artifact ) );
378 //        }
379 //
380 //        mockManager.verifyAll();
381 //    }
382 
383     private final class ArtifactMockAndControl
384     {
385         MockControl control;
386 
387         Artifact artifact;
388 
389         String groupId;
390 
391         String artifactId;
392 
393         String version;
394         
395         List dependencyTrail;
396 
397         String type;
398 
399         ArtifactMockAndControl( String groupId, String artifactId, List depTrail )
400         {
401             this( groupId, artifactId, "jar", depTrail );
402         }
403 
404         ArtifactMockAndControl( String groupId, String artifactId, String type, List dependencyTrail )
405         {
406             this.groupId = groupId;
407             this.artifactId = artifactId;
408             this.dependencyTrail = dependencyTrail;
409             this.type = type;
410 
411             control = MockControl.createControl( Artifact.class );
412             mockManager.add( control );
413 
414             artifact = (Artifact) control.getMock();
415 
416             enableGetDependencyConflictId();
417             enableGetGroupIdArtifactIdAndVersion();
418             enableGetId();
419 
420             if ( dependencyTrail != null )
421             {
422                 enableGetDependencyTrail();
423             }
424         }
425 
426         public ArtifactMockAndControl( String groupId, String artifactId )
427         {
428             this( groupId, artifactId, "jar", null );
429         }
430 
431         public ArtifactMockAndControl( String groupId, String artifactId, String type )
432         {
433             this( groupId, artifactId, type, null );
434         }
435 
436         void enableGetId()
437         {
438             artifact.getId();
439             control.setReturnValue( groupId + ":" + artifactId + ":" + type + ":version", MockControl.ZERO_OR_MORE );
440         }
441 
442         void enableGetDependencyTrail()
443         {
444             artifact.getDependencyTrail();
445             control.setReturnValue( dependencyTrail, MockControl.ZERO_OR_MORE );
446         }
447 
448         void enableGetDependencyConflictId()
449         {
450             artifact.getDependencyConflictId();
451             control.setReturnValue( groupId + ":" + artifactId + ":" + type, MockControl.ONE_OR_MORE );
452         }
453 
454         void enableGetGroupIdArtifactIdAndVersion() 
455         {
456             artifact.getGroupId();
457             control.setReturnValue( groupId, MockControl.ONE_OR_MORE );
458 
459             artifact.getArtifactId();
460             control.setReturnValue( artifactId, MockControl.ONE_OR_MORE );
461 
462             artifact.getVersion();
463             control.setReturnValue( version , MockControl.ZERO_OR_MORE );
464             
465         }
466     }
467 
468 }