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