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 java.util.List;
35 import org.apache.maven.model.Dependency;
36 import org.apache.maven.project.MavenProject;
37 import org.hamcrest.MatcherAssert;
38 import org.hamcrest.Matchers;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.mockito.Mockito;
43 import org.sonatype.aether.repository.RemoteRepository;
44 import org.sonatype.aether.util.artifact.JavaScopes;
45
46
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public final class ClasspathTest {
53
54
55
56
57 private static final String GROUP = "junit";
58
59
60
61
62
63 @Rule
64 public final transient TemporaryFolder temp = new TemporaryFolder();
65
66
67
68
69
70 @Test
71 public void buildsClasspath() throws Exception {
72 MatcherAssert.assertThat(
73 new Classpath(
74 this.project(
75 this.dependency(
76 ClasspathTest.GROUP, ClasspathTest.GROUP, "4.10"
77 )
78 ), this.temp.newFolder(), JavaScopes.TEST
79 ),
80 Matchers.<File>hasItems(
81 Matchers.hasToString(
82 Matchers.endsWith(
83 String.format(
84
85 "%sas%<sdirectory",
86 System.getProperty("file.separator")
87 )
88 )
89 ),
90 Matchers.hasToString(Matchers.endsWith("junit-4.10.jar")),
91 Matchers.hasToString(Matchers.endsWith("hamcrest-core-1.1.jar"))
92 )
93 );
94 }
95
96
97
98
99
100 @Test
101 public void returnsArtifactWithHighestVersionNumber() throws Exception {
102 MatcherAssert.assertThat(
103 new Classpath(
104 this.project(
105 this.dependency(
106 ClasspathTest.GROUP, ClasspathTest.GROUP, "4.8"
107 ),
108 this.dependency(
109 ClasspathTest.GROUP, ClasspathTest.GROUP, "4.8.2"
110 )
111 ), this.temp.newFolder(), JavaScopes.TEST
112 ),
113 Matchers.<File>hasItems(
114 Matchers.hasToString(
115 Matchers.endsWith(
116 String.format(
117
118 "%sas%<sdirectory",
119 System.getProperty("file.separator")
120 )
121 )
122 ),
123 Matchers.hasToString(Matchers.endsWith("junit-4.8.2.jar"))
124 )
125 );
126 }
127
128
129
130
131
132 @Test
133 public void buildsClasspathWithoutOptionalArtifacts() throws Exception {
134 final Dependency dep = new Dependency();
135
136 dep.setGroupId("commons-validator");
137 dep.setArtifactId("commons-validator");
138 dep.setVersion("1.3.1");
139 dep.setScope(JavaScopes.COMPILE);
140 MatcherAssert.assertThat(
141 new Classpath(
142 this.project(dep), this.temp.newFolder(), JavaScopes.COMPILE
143 ),
144 Matchers.not(
145 Matchers.<File>hasItems(
146 Matchers.hasToString(
147 Matchers.endsWith("oro-2.0.8.jar")
148 )
149 )
150 )
151 );
152 }
153
154
155
156
157
158 @Test
159 public void hasToStringWithBrokenDependency() throws Exception {
160 final Dependency dep = new Dependency();
161 dep.setGroupId("junit-broken");
162 dep.setArtifactId("junit-absent");
163 dep.setVersion("1.0");
164 dep.setScope(JavaScopes.TEST);
165 final Classpath classpath = new Classpath(
166 this.project(dep), this.temp.newFolder(), JavaScopes.TEST
167 );
168 MatcherAssert.assertThat(
169 classpath.toString(),
170 Matchers.containsString(
171 "failed to load 'junit-broken:junit-absent:jar:1.0 (compile)'"
172 )
173 );
174 }
175
176
177
178
179
180 @Test
181 public void comparesToAnotherClasspath() throws Exception {
182 final Dependency dep = new Dependency();
183 dep.setGroupId("org.apache.commons");
184 dep.setArtifactId("commons-lang3-absent");
185 dep.setVersion("3.0");
186 dep.setScope(JavaScopes.COMPILE);
187 final Classpath classpath = new Classpath(
188 this.project(dep), this.temp.newFolder(), JavaScopes.TEST
189 );
190 MatcherAssert.assertThat(classpath, Matchers.equalTo(classpath));
191 MatcherAssert.assertThat(
192 classpath.canEqual(classpath),
193 Matchers.is(true)
194 );
195 }
196
197
198
199
200
201
202
203
204 private Dependency dependency(final String group, final String artifact,
205 final String version) {
206 final Dependency dep = new Dependency();
207 dep.setGroupId(group);
208 dep.setArtifactId(artifact);
209 dep.setVersion(version);
210 dep.setScope(JavaScopes.TEST);
211 return dep;
212 }
213
214
215
216
217
218
219
220 private MavenProject project(final Dependency... dep) throws Exception {
221 final MavenProject project = Mockito.mock(MavenProject.class);
222 Mockito.doReturn(Arrays.asList("/some/path/as/directory"))
223 .when(project).getTestClasspathElements();
224 Mockito.doReturn(Arrays.asList(dep)).when(project).getDependencies();
225 final List<RemoteRepository> repos = Arrays.asList(
226 new RemoteRepository(
227 "maven-central",
228 "default",
229 "http://repo1.maven.org/maven2/"
230 )
231 );
232 Mockito.doReturn(repos).when(project).getRemoteProjectRepositories();
233 return project;
234 }
235
236 }