View Javadoc
1   package org.apache.maven.plugins.assembly.utils;
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.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.Set;
34  
35  import org.apache.maven.artifact.Artifact;
36  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
37  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
38  import org.apache.maven.project.MavenProject;
39  import org.codehaus.plexus.logging.Logger;
40  import org.codehaus.plexus.logging.console.ConsoleLogger;
41  import org.hamcrest.Matchers;
42  import org.junit.Assert;
43  import org.junit.Before;
44  import org.junit.Test;
45  import org.junit.runner.RunWith;
46  import org.mockito.junit.MockitoJUnitRunner;
47  
48  @RunWith( MockitoJUnitRunner.class )
49  public class FilterUtilsTest
50  {
51      private Logger logger;
52  
53      @Before
54      public void setUp()
55      {
56          logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
57      }
58  
59      @Test
60      public void testFilterArtifacts_ShouldThrowExceptionUsingStrictModeWithUnmatchedInclude()
61      {
62          final Artifact artifact = mock( Artifact.class );
63          when( artifact.getGroupId() ).thenReturn( "group" );
64          when( artifact.getArtifactId() ).thenReturn( "artifact" );
65          when( artifact.getId() ).thenReturn( "group:artifact:type:version" );
66          when( artifact.getDependencyConflictId() ).thenReturn( "group:artifact:type" );
67  
68          final List<String> includes = new ArrayList<>();
69  
70          includes.add( "other.group:other-artifact:type:version" );
71  
72          final List<String> excludes = Collections.emptyList();
73  
74          final Set<Artifact> artifacts = new HashSet<>();
75          artifacts.add( artifact );
76  
77          try
78          {
79              FilterUtils.filterArtifacts( artifacts, includes, excludes, true, false, logger );
80  
81              fail( "Should fail because of unmatched include." );
82          }
83          catch ( final InvalidAssemblerConfigurationException e )
84          {
85              // expected.
86          }
87      }
88  
89      @Test
90      public void testFilterArtifacts_ShouldNotRemoveArtifactDirectlyIncluded()
91          throws Exception
92      {
93          verifyArtifactInclusion( "group", "artifact", "group:artifact", null, null, null );
94          verifyArtifactInclusion( "group", "artifact", "group:artifact:jar", null, null, null );
95      }
96  
97      @Test
98      public void testFilterArtifacts_ShouldNotRemoveArtifactTransitivelyIncluded()
99          throws Exception
100     {
101         verifyArtifactInclusion( "group", "artifact", "group:dependentArtifact", null,
102                                  Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ),
103                                  null );
104     }
105 
106     @Test
107     public void testFilterArtifacts_ShouldRemoveArtifactTransitivelyExcluded()
108         throws Exception
109     {
110         verifyArtifactExclusion( "group", "artifact", null, "group:dependentArtifact",
111                                  Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ),
112                                  null );
113     }
114 
115     @Test
116     public void testFilterArtifacts_ShouldRemoveArtifactDirectlyExcluded()
117         throws Exception
118     {
119         verifyArtifactExclusion( "group", "artifact", null, "group:artifact", null, null );
120         verifyArtifactExclusion( "group", "artifact", null, "group:artifact:jar", null, null );
121     }
122 
123     @Test
124     public void testFilterArtifacts_ShouldNotRemoveArtifactNotIncludedAndNotExcluded()
125         throws Exception
126     {
127         verifyArtifactInclusion( "group", "artifact", null, null, null, null );
128         verifyArtifactInclusion( "group", "artifact", null, null, null, null );
129     }
130 
131     @Test
132     public void testFilterArtifacts_ShouldRemoveArtifactExcludedByAdditionalFilter()
133         throws Exception
134     {
135         final ArtifactFilter filter = new ArtifactFilter()
136         {
137 
138             public boolean include( final Artifact artifact )
139             {
140                 return false;
141             }
142 
143         };
144 
145         verifyArtifactExclusion( "group", "artifact", "fail:fail", null, null, filter );
146     }
147 
148     @Test
149     public void testFilterProjects_ShouldNotRemoveProjectDirectlyIncluded()
150     {
151         verifyProjectInclusion( "group", "artifact", "group:artifact", null, null );
152         verifyProjectInclusion( "group", "artifact", "group:artifact:jar", null, null );
153     }
154 
155     @Test
156     public void testFilterProjects_ShouldNotRemoveProjectTransitivelyIncluded()
157     {
158         verifyProjectInclusion( "group", "artifact", "group:dependentArtifact", null,
159                                 Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ) );
160     }
161 
162     @Test
163     public void testFilterProjects_ShouldRemoveProjectTransitivelyExcluded()
164     {
165         verifyProjectExclusion( "group", "artifact", null, "group:dependentArtifact",
166                                 Arrays.asList( "current:project:jar:1.0", "group:dependentArtifact:jar:version" ) );
167     }
168 
169     @Test
170     public void testFilterProjects_ShouldRemoveProjectDirectlyExcluded()
171     {
172         verifyProjectExclusion( "group", "artifact", null, "group:artifact", null );
173         verifyProjectExclusion( "group", "artifact", null, "group:artifact:jar", null );
174     }
175 
176     @Test
177     public void testFilterProjects_ShouldNotRemoveProjectNotIncludedAndNotExcluded()
178     {
179         verifyProjectInclusion( "group", "artifact", null, null, null );
180         verifyProjectInclusion( "group", "artifact", null, null, null );
181     }
182 
183     @Test
184     public void testTransitiveScopes()
185     {
186         Assert.assertThat( FilterUtils.newScopeFilter( "compile" ).getIncluded(),
187                            Matchers.containsInAnyOrder( "compile", "provided", "system" ) );
188 
189         Assert.assertThat( FilterUtils.newScopeFilter( "provided" ).getIncluded(),
190                            Matchers.containsInAnyOrder( "provided" ) );
191 
192         Assert.assertThat( FilterUtils.newScopeFilter( "system" ).getIncluded(),
193                            Matchers.containsInAnyOrder( "system" ) );
194 
195         Assert.assertThat( FilterUtils.newScopeFilter( "runtime" ).getIncluded(),
196                            Matchers.containsInAnyOrder( "compile", "runtime" ) );
197 
198         Assert.assertThat( FilterUtils.newScopeFilter( "test" ).getIncluded(),
199                            Matchers.containsInAnyOrder( "compile", "provided", "runtime", "system", "test" ) );
200 
201     }
202     
203     private void verifyArtifactInclusion( final String groupId, final String artifactId, final String inclusionPattern,
204                                           final String exclusionPattern, final List<String> depTrail,
205                                           final ArtifactFilter additionalFilter )
206         throws InvalidAssemblerConfigurationException
207     {
208         verifyArtifactFiltering( groupId, artifactId, inclusionPattern, exclusionPattern, depTrail, true,
209                                  additionalFilter );
210     }
211 
212     private void verifyArtifactExclusion( final String groupId, final String artifactId, final String inclusionPattern,
213                                           final String exclusionPattern, final List<String> depTrail,
214                                           final ArtifactFilter additionalFilter )
215         throws InvalidAssemblerConfigurationException
216     {
217         verifyArtifactFiltering( groupId, artifactId, inclusionPattern, exclusionPattern, depTrail, false,
218                                  additionalFilter );
219     }
220 
221     private void verifyArtifactFiltering( final String groupId, final String artifactId, final String inclusionPattern,
222                                           final String exclusionPattern, final List<String> depTrail,
223                                           final boolean verifyInclusion, final ArtifactFilter additionalFilter )
224         throws InvalidAssemblerConfigurationException
225     {
226         Artifact artifact = mock( Artifact.class );
227 
228         // this is always enabled, for verification purposes.
229         when( artifact.getDependencyConflictId() ).thenReturn( groupId + ":" + artifactId + ":jar" );
230         when( artifact.getGroupId() ).thenReturn( groupId );
231         when( artifact.getArtifactId() ).thenReturn( artifactId );
232         when( artifact.getId() ).thenReturn( groupId + ":" + artifactId + ":version:null:jar" );
233 
234         if ( depTrail != null )
235         {
236             when( artifact.getDependencyTrail() ).thenReturn( depTrail );
237         }
238 
239         List<String> inclusions;
240         if ( inclusionPattern != null )
241         {
242             inclusions = Collections.singletonList( inclusionPattern );
243         }
244         else
245         {
246             inclusions = Collections.emptyList();
247         }
248 
249         List<String> exclusions;
250         if ( exclusionPattern != null )
251         {
252             exclusions = Collections.singletonList( exclusionPattern );
253         }
254         else
255         {
256             exclusions = Collections.emptyList();
257         }
258 
259         final Set<Artifact> artifacts = new HashSet<>( Collections.singleton( artifact ) );
260 
261         FilterUtils.filterArtifacts( artifacts, inclusions, exclusions, false, depTrail != null, logger,
262                                      additionalFilter );
263 
264         if ( verifyInclusion )
265         {
266             assertEquals( 1, artifacts.size() );
267             assertEquals( artifact.getDependencyConflictId(),
268                           artifacts.iterator().next().getDependencyConflictId() );
269         }
270         else
271         {
272             // just make sure this trips, to meet the mock's expectations.
273             artifact.getDependencyConflictId();
274 
275             assertTrue( artifacts.isEmpty() );
276         }
277     }
278 
279     private void verifyProjectInclusion( final String groupId, final String artifactId, final String inclusionPattern,
280                                          final String exclusionPattern, final List<String> depTrail )
281     {
282         verifyProjectFiltering( groupId, artifactId, inclusionPattern, exclusionPattern, depTrail, true );
283     }
284 
285     private void verifyProjectExclusion( final String groupId, final String artifactId, final String inclusionPattern,
286                                          final String exclusionPattern, final List<String> depTrail )
287     {
288         verifyProjectFiltering( groupId, artifactId, inclusionPattern, exclusionPattern, depTrail, false );
289     }
290 
291     private void verifyProjectFiltering( final String groupId, final String artifactId, final String inclusionPattern,
292                                          final String exclusionPattern, final List<String> depTrail,
293                                          final boolean verifyInclusion )
294     {
295         final Artifact artifact = mock( Artifact.class );
296 
297         // this is always enabled, for verification purposes.
298         when( artifact.getDependencyConflictId() ).thenReturn( groupId + ":" + artifactId + ":jar" );
299         when( artifact.getGroupId() ).thenReturn( groupId );
300         when( artifact.getArtifactId() ).thenReturn( artifactId );
301         when( artifact.getId() ).thenReturn( groupId + ":" + artifactId + ":version:null:jar" );
302 
303         if ( depTrail != null )
304         {
305             when( artifact.getDependencyTrail() ).thenReturn( depTrail );
306         }
307 
308         MavenProject project = mock( MavenProject.class );
309         when( project.getId() ).thenReturn( "group:artifact:jar:1.0" );
310         when( project.getArtifact() ).thenReturn( artifact );
311 
312         final Set<MavenProject> projects = new HashSet<>();
313         projects.add( project );
314 
315         List<String> inclusions;
316         if ( inclusionPattern != null )
317         {
318             inclusions = Collections.singletonList( inclusionPattern );
319         }
320         else
321         {
322             inclusions = Collections.emptyList();
323         }
324 
325         List<String> exclusions;
326         if ( exclusionPattern != null )
327         {
328             exclusions = Collections.singletonList( exclusionPattern );
329         }
330         else
331         {
332             exclusions = Collections.emptyList();
333         }
334 
335         final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
336 
337         Set<MavenProject> result =
338             FilterUtils.filterProjects( projects, inclusions, exclusions, depTrail != null, logger );
339         
340         if ( verifyInclusion )
341         {
342             assertEquals( 1, result.size() );
343             assertEquals( project.getId(), result.iterator().next().getId() );
344         }
345         else
346         {
347             assertTrue( result.isEmpty() );
348         }
349     }
350 }