1 package org.apache.maven.archiva.proxy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
26 import org.apache.maven.archiva.policies.ChecksumPolicy;
27 import org.apache.maven.archiva.policies.ReleasesPolicy;
28 import org.apache.maven.archiva.policies.SnapshotsPolicy;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
30
31 import java.io.File;
32
33
34
35
36
37
38 public class ManagedDefaultTransferTest
39 extends AbstractProxyTestCase
40 {
41 public void testGetDefaultLayoutNotPresentConnectorOffline()
42 throws Exception
43 {
44 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
45 setupTestableManagedRepository( path );
46
47 File expectedFile = new File( managedDefaultDir, path );
48 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
49
50
51 assertNotExistsInManagedDefaultRepo( expectedFile );
52
53
54 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
55 CachedFailuresPolicy.NO, true );
56
57
58 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
59 assertNull("File should not have been downloaded", downloadedFile);
60 }
61
62 public void testGetDefaultLayoutNotPresent()
63 throws Exception
64 {
65 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
66 setupTestableManagedRepository( path );
67
68 File expectedFile = new File( managedDefaultDir, path );
69 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
70
71
72 assertNotExistsInManagedDefaultRepo( expectedFile );
73
74
75 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
76 CachedFailuresPolicy.NO, false );
77
78
79 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
80
81 File sourceFile = new File( REPOPATH_PROXIED1, path );
82 assertFileEquals( expectedFile, downloadedFile, sourceFile );
83 assertNoTempFiles( expectedFile );
84 }
85
86 public void testGetDefaultLayoutNotPresentPassthrough()
87 throws Exception
88 {
89 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
90 setupTestableManagedRepository( path );
91
92 File expectedFile = new File( managedDefaultDir, path );
93
94
95 assertNotExistsInManagedDefaultRepo( expectedFile );
96
97
98 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
99 CachedFailuresPolicy.NO, false );
100
101
102 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
103
104 File sourceFile = new File( REPOPATH_PROXIED1, path );
105 assertFileEquals( expectedFile, downloadedFile, sourceFile );
106 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
107 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
108 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
109 assertNoTempFiles( expectedFile );
110 }
111
112
113
114
115
116
117
118
119 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
120 throws Exception
121 {
122 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
123 setupTestableManagedRepository( path );
124
125 File expectedFile = new File( managedDefaultDir, path );
126
127 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
128
129 assertTrue( expectedFile.exists() );
130
131
132 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
133 CachedFailuresPolicy.NO, false );
134
135
136 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
137
138 assertFileEquals( expectedFile, downloadedFile, expectedFile );
139 assertNoTempFiles( expectedFile );
140 }
141
142
143
144
145
146
147
148
149 public void testGetDefaultLayoutAlreadyPresentPassthrough()
150 throws Exception
151 {
152 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
153 setupTestableManagedRepository( path );
154
155 File expectedFile = new File( managedDefaultDir, path );
156 File remoteFile = new File( REPOPATH_PROXIED1, path );
157
158 assertTrue( expectedFile.exists() );
159
160
161 setManagedOlderThanRemote( expectedFile, remoteFile );
162 long originalModificationTime = expectedFile.lastModified();
163
164
165 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
166 CachedFailuresPolicy.NO, false );
167
168
169 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
170
171 assertNotDownloaded( downloadedFile );
172 assertNotModified( expectedFile, originalModificationTime );
173 assertNoTempFiles( expectedFile );
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
193 throws Exception
194 {
195 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
196 setupTestableManagedRepository( path );
197
198 File expectedFile = new File( managedDefaultDir, path );
199 File remoteFile = new File( REPOPATH_PROXIED1, path );
200
201
202 setManagedNewerThanRemote( expectedFile, remoteFile );
203
204 long originalModificationTime = expectedFile.lastModified();
205 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
206
207 assertTrue( expectedFile.exists() );
208
209
210 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
211 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
212
213
214 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
215
216 assertNotDownloaded( downloadedFile );
217 assertNotModified( expectedFile, originalModificationTime );
218 assertNoTempFiles( expectedFile );
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
238 throws Exception
239 {
240 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
241 setupTestableManagedRepository( path );
242
243 File expectedFile = new File( managedDefaultDir, path );
244 File remoteFile = new File( REPOPATH_PROXIED1, path );
245
246
247 setManagedOlderThanRemote( expectedFile, remoteFile );
248
249 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
250
251 assertTrue( expectedFile.exists() );
252
253
254 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
255 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
256
257
258 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
259
260 File proxiedFile = new File( REPOPATH_PROXIED1, path );
261 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
262 assertNoTempFiles( expectedFile );
263 }
264
265
266
267
268
269
270
271
272 public void testGetDefaultLayoutRemoteUpdate()
273 throws Exception
274 {
275 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
276 setupTestableManagedRepository( path );
277
278 File expectedFile = new File( managedDefaultDir, path );
279 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
280
281 assertTrue( expectedFile.exists() );
282 expectedFile.setLastModified( getPastDate().getTime() );
283
284
285 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
286 CachedFailuresPolicy.NO, false );
287
288
289 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
290
291 File proxiedFile = new File( REPOPATH_PROXIED1, path );
292 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
293 assertNoTempFiles( expectedFile );
294 }
295
296 public void testGetWhenInBothProxiedRepos()
297 throws Exception
298 {
299 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
300 setupTestableManagedRepository( path );
301
302 File expectedFile = new File( managedDefaultDir, path );
303 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
304
305 assertNotExistsInManagedDefaultRepo( expectedFile );
306
307
308 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 , false );
309 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 , false );
310
311
312 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
313
314 File proxied1File = new File( REPOPATH_PROXIED1, path );
315 File proxied2File = new File( REPOPATH_PROXIED2, path );
316 assertFileEquals( expectedFile, downloadedFile, proxied1File );
317 assertNoTempFiles( expectedFile );
318
319
320 String actualContents = FileUtils.readFileToString( downloadedFile, null );
321 String badContents = FileUtils.readFileToString( proxied2File, null );
322 assertFalse( "Downloaded file contents should not be that of proxy 2",
323 StringUtils.equals( actualContents, badContents ) );
324 }
325
326 public void testGetInSecondProxiedRepo()
327 throws Exception
328 {
329 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
330 setupTestableManagedRepository( path );
331
332 File expectedFile = new File( managedDefaultDir, path );
333 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
334
335 assertNotExistsInManagedDefaultRepo( expectedFile );
336
337
338 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
339 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
340
341
342 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
343
344 File proxied2File = new File( REPOPATH_PROXIED2, path );
345 assertFileEquals( expectedFile, downloadedFile, proxied2File );
346 assertNoTempFiles( expectedFile );
347 }
348
349 public void testNotFoundInAnyProxies()
350 throws Exception
351 {
352 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
353 setupTestableManagedRepository( path );
354
355 File expectedFile = new File( managedDefaultDir, path );
356 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
357
358 assertNotExistsInManagedDefaultRepo( expectedFile );
359
360
361 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
362 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
363 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
364
365
366 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
367
368 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
369 downloadedFile );
370 assertNoTempFiles( expectedFile );
371 }
372
373 public void testGetInSecondProxiedRepoFirstFails()
374 throws Exception
375 {
376 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
377 setupTestableManagedRepository( path );
378
379 File expectedFile = new File( managedDefaultDir, path );
380 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
381
382 assertNotExistsInManagedDefaultRepo( expectedFile );
383
384
385 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
386
387 wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
388 wagonMockControl.setMatcher(customWagonGetMatcher);
389 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
390 wagonMockControl.replay();
391
392
393 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
394 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
395
396
397 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
398
399 wagonMockControl.verify();
400
401 File proxied2File = new File( REPOPATH_PROXIED2, path );
402 assertFileEquals( expectedFile, downloadedFile, proxied2File );
403 assertNoTempFiles( expectedFile );
404 }
405
406 public void testGetAllRepositoriesFail()
407 throws Exception
408 {
409 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
410 setupTestableManagedRepository( path );
411
412 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
413 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
414
415 assertNotExistsInManagedDefaultRepo( expectedFile );
416
417
418 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
419 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
420
421
422 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
423 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
424
425 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
426 wagonMock.get( path, tmpFile );
427
428 wagonMockControl.setMatcher(customWagonGetMatcher);
429 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
430
431 wagonMock.get( path, tmpFile );
432
433 wagonMockControl.setMatcher(customWagonGetMatcher);
434 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
435
436 wagonMockControl.replay();
437
438 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
439
440 assertNotDownloaded( downloadedFile );
441
442 wagonMockControl.verify();
443 assertNoTempFiles( expectedFile );
444
445
446
447 }
448
449 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
450 throws Exception
451 {
452 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
453 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
454 setupTestableManagedRepository( path );
455
456 File expectedFile = new File( managedDefaultDir, path );
457 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
458
459
460 setManagedNewerThanRemote( expectedFile, remoteFile );
461 long expectedTimestamp = expectedFile.lastModified();
462
463 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
464
465 assertTrue( expectedFile.exists() );
466
467
468 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
469
470 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
471
472 assertNotDownloaded( downloadedFile );
473 assertNotModified( expectedFile, expectedTimestamp );
474 assertNoTempFiles( expectedFile );
475 }
476
477 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
478 throws Exception
479 {
480 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
481 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
482 setupTestableManagedRepository( path );
483
484 File expectedFile = new File( managedDefaultDir, path );
485 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
486
487
488 setManagedOlderThanRemote( expectedFile, remoteFile );
489
490 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
491
492 assertTrue( expectedFile.exists() );
493
494
495 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
496
497 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
498
499 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
500 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
501 assertNoTempFiles( expectedFile );
502 }
503
504 public void testGetFromLegacyProxyNotPresentInManaged()
505 throws Exception
506 {
507 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
508 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
509 setupTestableManagedRepository( path );
510
511 File expectedFile = new File( managedDefaultDir, path );
512 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
513
514 assertNotExistsInManagedDefaultRepo( expectedFile );
515
516
517 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false);
518
519 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
520
521 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
522 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
523 assertNoTempFiles( expectedFile );
524 }
525
526 public void testGetFromLegacyProxyPluginNotPresentInManaged()
527 throws Exception
528 {
529 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
530 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
531 setupTestableManagedRepository( path );
532
533 File expectedFile = new File( managedDefaultDir, path );
534 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
535
536 assertNotExistsInManagedDefaultRepo( expectedFile );
537
538
539 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
540
541 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
542
543 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
544 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
545 assertNoTempFiles( expectedFile );
546 }
547 }