1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }