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