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