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.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugin.testing.stubs.ArtifactStub;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.shared.artifact.filter.resolve.Node;
33  import org.mockito.ArgumentCaptor;
34  
35  import static java.util.Collections.singletonList;
36  import static org.mockito.Mockito.mock;
37  import static org.mockito.Mockito.verify;
38  import static org.mockito.Mockito.when;
39  
40  public class ExcludeReactorProjectsDependencyFilterTest extends AbstractDependencyMojoTestCase {
41      public void testReject() {
42          final Artifact artifact1 = new ArtifactStub();
43          artifact1.setGroupId("org.apache.maven.plugins");
44          artifact1.setArtifactId("maven-dependency-plugin-dummy");
45          artifact1.setVersion("1.0");
46  
47          Artifact artifact2 = new ArtifactStub();
48          artifact2.setGroupId("org.apache.maven.plugins");
49          artifact2.setArtifactId("maven-dependency-plugin-other-dummy");
50          artifact2.setVersion("1.0");
51  
52          Set<Artifact> artifacts = new HashSet<>();
53          artifacts.add(artifact1);
54          artifacts.add(artifact2);
55  
56          MavenProject project = new MavenProjectStub();
57          project.setArtifact(artifact1);
58  
59          Log log = mock(Log.class);
60          when(log.isDebugEnabled()).thenReturn(false);
61  
62          ExcludeReactorProjectsDependencyFilter filter =
63                  new ExcludeReactorProjectsDependencyFilter(singletonList(project), log);
64  
65          Node node = new Node() {
66              @Override
67              public Dependency getDependency() {
68                  final Dependency result = new Dependency();
69                  result.setGroupId(artifact1.getGroupId());
70                  result.setArtifactId(artifact1.getArtifactId());
71                  result.setVersion(artifact1.getVersion());
72                  return result;
73              }
74          };
75  
76          assertFalse(filter.accept(node, Collections.<Node>emptyList()));
77      }
78  
79      public void testRejectWithLogging() {
80          final Artifact artifact1 = new ArtifactStub();
81          artifact1.setGroupId("org.apache.maven.plugins");
82          artifact1.setArtifactId("maven-dependency-plugin-dummy");
83          artifact1.setVersion("1.0");
84  
85          Artifact artifact2 = new ArtifactStub();
86          artifact2.setGroupId("org.apache.maven.plugins");
87          artifact2.setArtifactId("maven-dependency-plugin-other-dummy");
88          artifact2.setVersion("1.0");
89  
90          Set<Artifact> artifacts = new HashSet<>();
91          artifacts.add(artifact1);
92          artifacts.add(artifact2);
93  
94          MavenProject project = new MavenProjectStub();
95          project.setArtifact(artifact1);
96  
97          Log log = mock(Log.class);
98          when(log.isDebugEnabled()).thenReturn(true);
99  
100         ExcludeReactorProjectsDependencyFilter filter =
101                 new ExcludeReactorProjectsDependencyFilter(singletonList(project), log);
102 
103         Node node = new Node() {
104             @Override
105             public Dependency getDependency() {
106                 final Dependency result = new Dependency();
107                 result.setGroupId(artifact1.getGroupId());
108                 result.setArtifactId(artifact1.getArtifactId());
109                 result.setVersion(artifact1.getVersion());
110                 return result;
111             }
112         };
113 
114         filter.accept(node, Collections.<Node>emptyList());
115 
116         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
117         verify(log).debug(captor.capture());
118         assertTrue(captor.getValue().contains("Skipped dependency"));
119     }
120 
121     public void testAccept() {
122         final Artifact artifact1 = new ArtifactStub();
123         artifact1.setGroupId("org.apache.maven.plugins");
124         artifact1.setArtifactId("maven-dependency-plugin-dummy");
125         artifact1.setVersion("1.0");
126 
127         Artifact artifact2 = new ArtifactStub();
128         artifact2.setGroupId("org.apache.maven.plugins");
129         artifact2.setArtifactId("maven-dependency-plugin-other-dummy");
130         artifact2.setVersion("1.0");
131 
132         Set<Artifact> artifacts = new HashSet<>();
133         artifacts.add(artifact1);
134         artifacts.add(artifact2);
135 
136         MavenProject project = new MavenProjectStub();
137         project.setArtifact(artifact1);
138 
139         Log log = mock(Log.class);
140         when(log.isDebugEnabled()).thenReturn(false);
141 
142         ExcludeReactorProjectsDependencyFilter filter =
143                 new ExcludeReactorProjectsDependencyFilter(singletonList(project), log);
144 
145         Node node = new Node() {
146             @Override
147             public Dependency getDependency() {
148                 final Dependency result = new Dependency();
149                 result.setGroupId("something-else");
150                 result.setArtifactId(artifact1.getArtifactId());
151                 result.setVersion(artifact1.getVersion());
152                 return result;
153             }
154         };
155 
156         assertTrue(filter.accept(node, Collections.<Node>emptyList()));
157     }
158 }