1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.aether;
31
32 import java.io.File;
33 import java.util.Arrays;
34 import org.apache.maven.artifact.Artifact;
35 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
36 import org.apache.maven.execution.MavenSession;
37 import org.apache.maven.model.Dependency;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
40 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
41 import org.apache.maven.shared.dependency.graph.DependencyNode;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Ignore;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.TemporaryFolder;
48 import org.mockito.Mockito;
49
50
51
52
53
54
55 @SuppressWarnings("unchecked")
56 public final class MavenClasspathTest {
57
58
59
60
61
62 @Rule
63 public final transient TemporaryFolder temp = new TemporaryFolder();
64
65
66
67
68
69 @Test
70 public void buildsClasspath() throws Exception {
71 final Dependency dep = new Dependency();
72 final String group = "junit";
73 dep.setGroupId(group);
74 dep.setArtifactId(group);
75 dep.setVersion("4.10");
76 dep.setScope("test");
77 final String jar = "junit-4.10.jar";
78 final DependencyGraphBuilder builder = this.builder(jar);
79 final MavenSession session = Mockito.mock(MavenSession.class);
80 final MavenProject project = this.project(dep);
81 Mockito.when(session.getCurrentProject()).thenReturn(project);
82 MatcherAssert.assertThat(
83 new MavenClasspath(builder, session, MavenClasspath.TEST_SCOPE),
84 Matchers.<File>hasItems(
85 Matchers.hasToString(
86 Matchers.endsWith(
87 String.format(
88 "%sas%<sdirectory",
89 System.getProperty("file.separator")
90 )
91 )
92 ),
93 Matchers.hasToString(Matchers.endsWith(jar))
94 )
95 );
96 }
97
98
99
100
101
102
103 @Test
104 @Ignore
105 public void hasToStringWithBrokenDependency() throws Exception {
106 final Dependency dep = new Dependency();
107 dep.setGroupId("junit-broken");
108 dep.setArtifactId("junit-absent");
109 dep.setVersion("1.0");
110 dep.setScope(MavenClasspath.TEST_SCOPE);
111 final DependencyGraphBuilder builder =
112 Mockito.mock(DependencyGraphBuilder.class);
113 final MavenSession session = Mockito.mock(MavenSession.class);
114 final MavenProject project = this.project(dep);
115 Mockito.when(session.getCurrentProject()).thenReturn(project);
116 final MavenClasspath classpath = new MavenClasspath(
117 builder, session, MavenClasspath.TEST_SCOPE
118 );
119 MatcherAssert.assertThat(
120 classpath.toString(),
121 Matchers.containsString(
122 "failed to load 'junit-broken:junit-absent:jar:1.0 (compile)'"
123 )
124 );
125 }
126
127
128
129
130
131 @Test
132 public void comparesToAnotherClasspath() throws Exception {
133 final Dependency dep = new Dependency();
134 dep.setGroupId("org.apache.commons");
135 dep.setArtifactId("commons-lang3-absent");
136 dep.setVersion("3.0");
137 dep.setScope(MavenClasspath.COMPILE_SCOPE);
138 final DependencyGraphBuilder builder =
139 Mockito.mock(DependencyGraphBuilder.class);
140 final MavenSession session = Mockito.mock(MavenSession.class);
141 final MavenProject project = this.project(dep);
142 Mockito.when(session.getCurrentProject()).thenReturn(project);
143 final MavenClasspath classpath = new MavenClasspath(
144 builder, session, MavenClasspath.TEST_SCOPE
145 );
146 MatcherAssert.assertThat(classpath, Matchers.equalTo(classpath));
147 MatcherAssert.assertThat(
148 classpath.canEqual(classpath),
149 Matchers.is(true)
150 );
151 }
152
153
154
155
156
157
158 private DependencyGraphBuilder builder(final String location) {
159 final DependencyGraphBuilder builder = Mockito
160 .mock(DependencyGraphBuilder.class);
161 final DependencyNode node = Mockito.mock(DependencyNode.class);
162 try {
163 Mockito.when(
164 builder.buildDependencyGraph(
165 Mockito.any(MavenProject.class),
166 Mockito.any(ArtifactFilter.class)
167 )
168 )
169 .thenReturn(node);
170 } catch (final DependencyGraphBuilderException ex) {
171 throw new IllegalStateException(ex);
172 }
173 final Artifact artifact = Mockito.mock(Artifact.class);
174 Mockito.when(artifact.getFile()).thenReturn(new File(location));
175 Mockito.when(node.getArtifact()).thenReturn(artifact);
176 return builder;
177 }
178
179
180
181
182
183
184
185 private MavenProject project(final Dependency dep) throws Exception {
186 final MavenProject project = Mockito.mock(MavenProject.class);
187 Mockito.doReturn(Arrays.asList("/some/path/as/directory"))
188 .when(project).getTestClasspathElements();
189 Mockito.doReturn(Arrays.asList(dep)).when(project).getDependencies();
190 return project;
191 }
192
193 }