1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.legacy;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.DefaultArtifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.metadata.ArtifactMetadata;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
32 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
33 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
34 import org.apache.maven.artifact.versioning.VersionRange;
35 import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
36 import org.apache.maven.wagon.ResourceDoesNotExistException;
37 import org.apache.maven.wagon.TransferFailedException;
38 import org.apache.maven.wagon.UnsupportedProtocolException;
39 import org.apache.maven.wagon.Wagon;
40 import org.apache.maven.wagon.authorization.AuthorizationException;
41 import org.apache.maven.wagon.events.TransferEvent;
42 import org.apache.maven.wagon.events.TransferListener;
43 import org.apache.maven.wagon.observers.AbstractTransferListener;
44 import org.apache.maven.wagon.observers.Debug;
45 import org.codehaus.plexus.PlexusTestCase;
46 import org.codehaus.plexus.util.FileUtils;
47
48
49
50
51 public class DefaultWagonManagerTest extends PlexusTestCase {
52 private DefaultWagonManager wagonManager;
53
54 private TransferListener transferListener = new Debug();
55
56 private ArtifactFactory artifactFactory;
57
58 private ArtifactRepositoryFactory artifactRepositoryFactory;
59
60 protected void setUp() throws Exception {
61 super.setUp();
62 wagonManager = (DefaultWagonManager) lookup(WagonManager.class);
63 artifactFactory = lookup(ArtifactFactory.class);
64 artifactRepositoryFactory = lookup(ArtifactRepositoryFactory.class);
65 }
66
67 @Override
68 protected void tearDown() throws Exception {
69 wagonManager = null;
70 artifactFactory = null;
71 super.tearDown();
72 }
73
74 public void testUnnecessaryRepositoryLookup() throws Exception {
75 Artifact artifact = createTestPomArtifact("target/test-data/get-missing-pom");
76
77 List<ArtifactRepository> repos = new ArrayList<>();
78 repos.add(artifactRepositoryFactory.createArtifactRepository(
79 "repo1", "string://url1", new ArtifactRepositoryLayoutStub(), null, null));
80 repos.add(artifactRepositoryFactory.createArtifactRepository(
81 "repo2", "string://url2", new ArtifactRepositoryLayoutStub(), null, null));
82
83 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
84 wagon.addExpectedContent(repos.get(0).getLayout().pathOf(artifact), "expected");
85 wagon.addExpectedContent(repos.get(1).getLayout().pathOf(artifact), "expected");
86
87 class TransferListener extends AbstractTransferListener {
88 public List<TransferEvent> events = new ArrayList<>();
89
90 @Override
91 public void transferInitiated(TransferEvent transferEvent) {
92 events.add(transferEvent);
93 }
94 }
95
96 TransferListener listener = new TransferListener();
97 wagonManager.getArtifact(artifact, repos, listener, false);
98 assertEquals(1, listener.events.size());
99 }
100
101 public void testGetMissingJar() throws TransferFailedException, UnsupportedProtocolException, IOException {
102 Artifact artifact = createTestArtifact("target/test-data/get-missing-jar", "jar");
103
104 ArtifactRepository repo = createStringRepo();
105
106 try {
107 wagonManager.getArtifact(artifact, repo, null, false);
108
109 fail();
110 } catch (ResourceDoesNotExistException e) {
111 assertTrue(true);
112 }
113
114 assertFalse(artifact.getFile().exists());
115 }
116
117 public void testGetMissingJarForced() throws TransferFailedException, UnsupportedProtocolException, IOException {
118 Artifact artifact = createTestArtifact("target/test-data/get-missing-jar", "jar");
119
120 ArtifactRepository repo = createStringRepo();
121
122 try {
123 wagonManager.getArtifact(artifact, repo, null, true);
124
125 fail();
126 } catch (ResourceDoesNotExistException e) {
127 assertTrue(true);
128 }
129
130 assertFalse(artifact.getFile().exists());
131 }
132
133 public void testGetRemoteJar()
134 throws TransferFailedException, ResourceDoesNotExistException, UnsupportedProtocolException, IOException,
135 AuthorizationException {
136 Artifact artifact = createTestArtifact("target/test-data/get-remote-jar", "jar");
137
138 ArtifactRepository repo = createStringRepo();
139
140 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
141 wagon.addExpectedContent(repo.getLayout().pathOf(artifact), "expected");
142
143 wagonManager.getArtifact(artifact, repo, null, false);
144
145 assertTrue(artifact.getFile().exists());
146 assertEquals("expected", FileUtils.fileRead(artifact.getFile(), "UTF-8"));
147 }
148
149 private Artifact createTestPomArtifact(String directory) throws IOException {
150 File testData = getTestFile(directory);
151 FileUtils.deleteDirectory(testData);
152 testData.mkdirs();
153
154 Artifact artifact = artifactFactory.createProjectArtifact("test", "test", "1.0");
155 artifact.setFile(new File(testData, "test-1.0.pom"));
156 assertFalse(artifact.getFile().exists());
157 return artifact;
158 }
159
160 private Artifact createTestArtifact(String directory, String type) throws IOException {
161 return createTestArtifact(directory, "1.0", type);
162 }
163
164 private Artifact createTestArtifact(String directory, String version, String type) throws IOException {
165 File testData = getTestFile(directory);
166 FileUtils.deleteDirectory(testData);
167 testData.mkdirs();
168
169 Artifact artifact = artifactFactory.createBuildArtifact("test", "test", version, type);
170 artifact.setFile(new File(
171 testData,
172 "test-" + version + "." + artifact.getArtifactHandler().getExtension()));
173 assertFalse(artifact.getFile().exists());
174 return artifact;
175 }
176
177 private ArtifactRepository createStringRepo() {
178 return artifactRepositoryFactory.createArtifactRepository(
179 "id", "string://url", new ArtifactRepositoryLayoutStub(), null, null);
180 }
181
182
183
184
185
186
187
188
189 private ArtifactRepository getRepo(String id, String url) {
190 return artifactRepositoryFactory.createArtifactRepository(id, url, new DefaultRepositoryLayout(), null, null);
191 }
192
193
194
195
196
197
198
199 private ArtifactRepository getRepo(String id) {
200 return getRepo(id, "http://something");
201 }
202
203 public void testDefaultWagonManager() throws Exception {
204 assertWagon("a");
205
206 assertWagon("b");
207
208 assertWagon("c");
209
210 assertWagon("string");
211
212 try {
213 assertWagon("d");
214
215 fail("Expected :" + UnsupportedProtocolException.class.getName());
216 } catch (UnsupportedProtocolException e) {
217
218 assertTrue(true);
219 }
220 }
221
222
223
224
225 public void testWagonTransferListenerRemovedAfterGetArtifactAndPutArtifact() throws Exception {
226 Artifact artifact = createTestArtifact("target/test-data/transfer-listener", "jar");
227 ArtifactRepository repo = createStringRepo();
228 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
229 wagon.addExpectedContent(repo.getLayout().pathOf(artifact), "expected");
230
231
232 assertFalse(
233 "Transfer listener is registered before test",
234 wagon.getTransferEventSupport().hasTransferListener(transferListener));
235 wagonManager.getArtifact(artifact, repo, transferListener, false);
236 assertFalse(
237 "Transfer listener still registered after getArtifact",
238 wagon.getTransferEventSupport().hasTransferListener(transferListener));
239
240
241 File sampleFile = getTestFile("target/test-file");
242 FileUtils.fileWrite(sampleFile.getAbsolutePath(), "sample file");
243
244 assertFalse(
245 "Transfer listener is registered before test",
246 wagon.getTransferEventSupport().hasTransferListener(transferListener));
247 wagonManager.putArtifact(sampleFile, artifact, repo, transferListener);
248 assertFalse(
249 "Transfer listener still registered after putArtifact",
250 wagon.getTransferEventSupport().hasTransferListener(transferListener));
251 }
252
253
254
255
256 public void xtestChecksumVerification() throws Exception {
257 ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy(
258 true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL);
259
260 ArtifactRepository repo = artifactRepositoryFactory.createArtifactRepository(
261 "id", "string://url", new ArtifactRepositoryLayoutStub(), policy, policy);
262
263 Artifact artifact = new DefaultArtifact(
264 "sample.group",
265 "sample-art",
266 VersionRange.createFromVersion("1.0"),
267 "scope",
268 "jar",
269 "classifier",
270 null);
271 artifact.setFile(getTestFile("target/sample-art"));
272
273 StringWagon wagon = (StringWagon) wagonManager.getWagon("string");
274
275 wagon.clearExpectedContent();
276 wagon.addExpectedContent("path", "lower-case-checksum");
277 wagon.addExpectedContent("path.sha1", "2a25dc564a3b34f68237fc849066cbc7bb7a36a1");
278
279 try {
280 wagonManager.getArtifact(artifact, repo, null, false);
281 } catch (ChecksumFailedException e) {
282 fail("Checksum verification did not pass: " + e.getMessage());
283 }
284
285 wagon.clearExpectedContent();
286 wagon.addExpectedContent("path", "upper-case-checksum");
287 wagon.addExpectedContent("path.sha1", "B7BB97D7D0B9244398D9B47296907F73313663E6");
288
289 try {
290 wagonManager.getArtifact(artifact, repo, null, false);
291 } catch (ChecksumFailedException e) {
292 fail("Checksum verification did not pass: " + e.getMessage());
293 }
294
295 wagon.clearExpectedContent();
296 wagon.addExpectedContent("path", "expected-failure");
297 wagon.addExpectedContent("path.sha1", "b7bb97d7d0b9244398d9b47296907f73313663e6");
298
299 try {
300 wagonManager.getArtifact(artifact, repo, null, false);
301 fail("Checksum verification did not fail");
302 } catch (ChecksumFailedException e) {
303
304 }
305
306 wagon.clearExpectedContent();
307 wagon.addExpectedContent("path", "lower-case-checksum");
308 wagon.addExpectedContent("path.md5", "50b2cf50a103a965efac62b983035cac");
309
310 try {
311 wagonManager.getArtifact(artifact, repo, null, false);
312 } catch (ChecksumFailedException e) {
313 fail("Checksum verification did not pass: " + e.getMessage());
314 }
315
316 wagon.clearExpectedContent();
317 wagon.addExpectedContent("path", "upper-case-checksum");
318 wagon.addExpectedContent("path.md5", "842F568FCCFEB7E534DC72133D42FFDC");
319
320 try {
321 wagonManager.getArtifact(artifact, repo, null, false);
322 } catch (ChecksumFailedException e) {
323 fail("Checksum verification did not pass: " + e.getMessage());
324 }
325
326 wagon.clearExpectedContent();
327 wagon.addExpectedContent("path", "expected-failure");
328 wagon.addExpectedContent("path.md5", "b7bb97d7d0b9244398d9b47296907f73313663e6");
329
330 try {
331 wagonManager.getArtifact(artifact, repo, null, false);
332 fail("Checksum verification did not fail");
333 } catch (ChecksumFailedException e) {
334
335 }
336 }
337
338 public void testPerLookupInstantiation() throws Exception {
339 String protocol = "perlookup";
340
341 Wagon one = wagonManager.getWagon(protocol);
342 Wagon two = wagonManager.getWagon(protocol);
343
344 assertNotSame(one, two);
345 }
346
347 private void assertWagon(String protocol) throws Exception {
348 Wagon wagon = wagonManager.getWagon(protocol);
349
350 assertNotNull("Check wagon, protocol=" + protocol, wagon);
351 }
352
353 private final class ArtifactRepositoryLayoutStub implements ArtifactRepositoryLayout {
354 public String getId() {
355 return "test";
356 }
357
358 public String pathOfRemoteRepositoryMetadata(ArtifactMetadata metadata) {
359 return "path";
360 }
361
362 public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
363 return "path";
364 }
365
366 public String pathOf(Artifact artifact) {
367 return "path";
368 }
369 }
370 }