View Javadoc
1   package org.apache.maven.plugins.dependency.resolvers;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.logging.Log;
24  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
25  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
26  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
29  import org.mockito.ArgumentCaptor;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import static java.util.Collections.singleton;
35  import static java.util.Collections.singletonList;
36  import static org.mockito.ArgumentMatchers.any;
37  import static org.mockito.Mockito.mock;
38  import static org.mockito.Mockito.never;
39  import static org.mockito.Mockito.verify;
40  import static org.mockito.Mockito.when;
41  
42  public class ExcludeReactorProjectsArtifactFilterTest
43          extends AbstractDependencyMojoTestCase
44  {
45  
46      public void testFilter()
47              throws ArtifactFilterException
48      {
49          Artifact artifact1 = new ArtifactStub();
50          artifact1.setGroupId("org.apache.maven.plugins");
51          artifact1.setArtifactId("maven-dependency-plugin-dummy");
52          artifact1.setVersion("1.0");
53  
54          Artifact artifact2 = new ArtifactStub();
55          artifact2.setGroupId("org.apache.maven.plugins");
56          artifact2.setArtifactId("maven-dependency-plugin-other-dummy");
57          artifact2.setVersion("1.0");
58  
59          Set<Artifact> artifacts = new HashSet<>();
60          artifacts.add( artifact1 );
61          artifacts.add( artifact2 );
62  
63          MavenProject project = new MavenProjectStub();
64          project.setArtifact(artifact1);
65  
66          Log log = mock( Log.class );
67          when( log.isDebugEnabled() ).thenReturn( false );
68  
69          ExcludeReactorProjectsArtifactFilter filter = new ExcludeReactorProjectsArtifactFilter(
70                  singletonList( project ), log );
71  
72          Set<Artifact> result = filter.filter( artifacts );
73  
74          assertEquals( 1, result.size() );
75          verify( log, never() ).debug( any( String.class ) );
76      }
77  
78      public void testFilterWithLogging()
79              throws ArtifactFilterException
80      {
81          Artifact artifact = new ArtifactStub();
82          artifact.setGroupId("org.apache.maven.plugins");
83          artifact.setArtifactId("maven-dependency-plugin-dummy");
84          artifact.setVersion("1.0");
85  
86          MavenProject project = new MavenProjectStub();
87          project.setArtifact(artifact);
88  
89          Log log = mock( Log.class );
90          when( log.isDebugEnabled() ).thenReturn( true );
91  
92          ExcludeReactorProjectsArtifactFilter filter = new ExcludeReactorProjectsArtifactFilter(
93                  singletonList( project ), log );
94  
95          filter.filter( singleton( artifact ) );
96  
97          ArgumentCaptor<String> captor = ArgumentCaptor.forClass( String.class );
98          verify( log ).debug( captor.capture() );
99          assertTrue( captor.getValue().contains( "Skipped artifact" ) );
100     }
101 }