1 package org.apache.archiva.proxy;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.policies.CachedFailuresPolicy;
26 import org.apache.archiva.policies.ChecksumPolicy;
27 import org.apache.archiva.policies.ReleasesPolicy;
28 import org.apache.archiva.policies.SnapshotsPolicy;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
30 import org.junit.Test;
34 import static org.junit.Assert.*;
37 * ManagedDefaultTransferTest
41 public class ManagedDefaultTransferTest
42 extends AbstractProxyTestCase
45 public void testGetDefaultLayoutNotPresentConnectorOffline()
48 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
49 setupTestableManagedRepository( path );
51 File expectedFile = new File( managedDefaultDir, path );
52 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
54 // Ensure file isn't present first.
55 assertNotExistsInManagedDefaultRepo( expectedFile );
57 // Configure Connector (usually done within archiva.xml configuration)
58 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
59 CachedFailuresPolicy.NO, true );
61 // Attempt the proxy fetch.
62 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63 assertNull("File should not have been downloaded", downloadedFile);
67 public void testGetDefaultLayoutNotPresent()
70 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
71 setupTestableManagedRepository( path );
73 File expectedFile = new File( managedDefaultDir, path );
74 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
76 // Ensure file isn't present first.
77 assertNotExistsInManagedDefaultRepo( expectedFile );
79 // Configure Connector (usually done within archiva.xml configuration)
80 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
81 CachedFailuresPolicy.NO, false );
83 // Attempt the proxy fetch.
84 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
86 File sourceFile = new File( REPOPATH_PROXIED1, path );
87 assertFileEquals( expectedFile, downloadedFile, sourceFile );
88 assertNoTempFiles( expectedFile );
92 public void testGetDefaultLayoutNotPresentPassthrough()
95 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
96 setupTestableManagedRepository( path );
98 File expectedFile = new File( managedDefaultDir, path );
100 // Ensure file isn't present first.
101 assertNotExistsInManagedDefaultRepo( expectedFile );
103 // Configure Connector (usually done within archiva.xml configuration)
104 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
105 CachedFailuresPolicy.NO, false );
107 // Attempt the proxy fetch.
108 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
110 File sourceFile = new File( REPOPATH_PROXIED1, path );
111 assertFileEquals( expectedFile, downloadedFile, sourceFile );
112 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
113 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
114 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
115 assertNoTempFiles( expectedFile );
119 * The attempt here should result in no file being transferred.
121 * The file exists locally, and the policy is ONCE.
126 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
129 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
130 setupTestableManagedRepository( path );
132 File expectedFile = new File( managedDefaultDir, path );
134 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
136 assertTrue( expectedFile.exists() );
138 // Configure Connector (usually done within archiva.xml configuration)
139 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
140 CachedFailuresPolicy.NO, false );
142 // Attempt the proxy fetch.
143 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
145 assertFileEquals( expectedFile, downloadedFile, expectedFile );
146 assertNoTempFiles( expectedFile );
150 * The attempt here should result in no file being transferred.
152 * The file exists locally, and the policy is ONCE.
157 public void testGetDefaultLayoutAlreadyPresentPassthrough()
160 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
161 setupTestableManagedRepository( path );
163 File expectedFile = new File( managedDefaultDir, path );
164 File remoteFile = new File( REPOPATH_PROXIED1, path );
166 assertTrue( expectedFile.exists() );
168 // Set the managed File to be newer than local.
169 setManagedOlderThanRemote( expectedFile, remoteFile );
170 long originalModificationTime = expectedFile.lastModified();
172 // Configure Connector (usually done within archiva.xml configuration)
173 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
174 CachedFailuresPolicy.NO, false );
176 // Attempt the proxy fetch.
177 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
179 assertNotDownloaded( downloadedFile );
180 assertNotModified( expectedFile, originalModificationTime );
181 assertNoTempFiles( expectedFile );
186 * Request a file, that exists locally, and remotely.
189 * All policies are set to IGNORE.
192 * Managed file is newer than remote file.
195 * Transfer should not have occured, as managed file is newer.
201 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
204 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
205 setupTestableManagedRepository( path );
207 File expectedFile = new File( managedDefaultDir, path );
208 File remoteFile = new File( REPOPATH_PROXIED1, path );
210 // Set the managed File to be newer than local.
211 setManagedNewerThanRemote( expectedFile, remoteFile );
213 long originalModificationTime = expectedFile.lastModified();
214 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
216 assertTrue( expectedFile.exists() );
218 // Configure Connector (usually done within archiva.xml configuration)
219 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
220 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
222 // Attempt the proxy fetch.
223 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
225 assertNotDownloaded( downloadedFile );
226 assertNotModified( expectedFile, originalModificationTime );
227 assertNoTempFiles( expectedFile );
232 * Request a file, that exists locally, and remotely.
235 * All policies are set to IGNORE.
238 * Managed file is older than Remote file.
241 * Transfer should have occured, as managed file is older than remote.
247 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
250 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
251 setupTestableManagedRepository( path );
253 File expectedFile = new File( managedDefaultDir, path );
254 File remoteFile = new File( REPOPATH_PROXIED1, path );
256 // Set the managed file to be newer than remote file.
257 setManagedOlderThanRemote( expectedFile, remoteFile );
259 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
261 assertTrue( expectedFile.exists() );
263 // Configure Connector (usually done within archiva.xml configuration)
264 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
265 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
267 // Attempt the proxy fetch.
268 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
270 File proxiedFile = new File( REPOPATH_PROXIED1, path );
271 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
272 assertNoTempFiles( expectedFile );
276 * The attempt here should result in file being transferred.
278 * The file exists locally, is over 6 years old, and the policy is DAILY.
283 public void testGetDefaultLayoutRemoteUpdate()
286 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
287 setupTestableManagedRepository( path );
289 File expectedFile = new File( managedDefaultDir, path );
290 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
292 assertTrue( expectedFile.exists() );
293 expectedFile.setLastModified( getPastDate().getTime() );
295 // Configure Connector (usually done within archiva.xml configuration)
296 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
297 CachedFailuresPolicy.NO, false );
299 // Attempt the proxy fetch.
300 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
302 File proxiedFile = new File( REPOPATH_PROXIED1, path );
303 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
304 assertNoTempFiles( expectedFile );
308 public void testGetWhenInBothProxiedRepos()
311 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
312 setupTestableManagedRepository( path );
314 File expectedFile = new File( managedDefaultDir, path );
315 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
317 assertNotExistsInManagedDefaultRepo( expectedFile );
319 // Configure Connector (usually done within archiva.xml configuration)
320 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 , false );
321 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 , false );
323 // Attempt the proxy fetch.
324 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
326 File proxied1File = new File( REPOPATH_PROXIED1, path );
327 File proxied2File = new File( REPOPATH_PROXIED2, path );
328 assertFileEquals( expectedFile, downloadedFile, proxied1File );
329 assertNoTempFiles( expectedFile );
331 // TODO: is this check even needed if it passes above?
332 String actualContents = FileUtils.readFileToString( downloadedFile, null );
333 String badContents = FileUtils.readFileToString( proxied2File, null );
334 assertFalse( "Downloaded file contents should not be that of proxy 2",
335 StringUtils.equals( actualContents, badContents ) );
339 public void testGetInSecondProxiedRepo()
342 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
343 setupTestableManagedRepository( path );
345 File expectedFile = new File( managedDefaultDir, path );
346 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
348 assertNotExistsInManagedDefaultRepo( expectedFile );
350 // Configure Connector (usually done within archiva.xml configuration)
351 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
352 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
354 // Attempt the proxy fetch.
355 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
357 File proxied2File = new File( REPOPATH_PROXIED2, path );
358 assertFileEquals( expectedFile, downloadedFile, proxied2File );
359 assertNoTempFiles( expectedFile );
363 public void testNotFoundInAnyProxies()
366 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
367 setupTestableManagedRepository( path );
369 File expectedFile = new File( managedDefaultDir, path );
370 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
372 assertNotExistsInManagedDefaultRepo( expectedFile );
374 // Configure Connector (usually done within archiva.xml configuration)
375 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
376 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
377 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
379 // Attempt the proxy fetch.
380 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
382 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
384 assertNoTempFiles( expectedFile );
388 public void testGetInSecondProxiedRepoFirstFails()
391 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
392 setupTestableManagedRepository( path );
394 File expectedFile = new File( managedDefaultDir, path );
395 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
397 assertNotExistsInManagedDefaultRepo( expectedFile );
399 // Configure Repository (usually done within archiva.xml configuration)
400 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
402 wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
403 wagonMockControl.setMatcher(customWagonGetMatcher);
404 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
405 wagonMockControl.replay();
407 // Configure Connector (usually done within archiva.xml configuration)
408 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
409 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
411 // Attempt the proxy fetch.
412 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
414 wagonMockControl.verify();
416 File proxied2File = new File( REPOPATH_PROXIED2, path );
417 assertFileEquals( expectedFile, downloadedFile, proxied2File );
418 assertNoTempFiles( expectedFile );
422 public void testGetAllRepositoriesFail()
425 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
426 setupTestableManagedRepository( path );
428 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
429 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
431 assertNotExistsInManagedDefaultRepo( expectedFile );
433 // Configure Repository (usually done within archiva.xml configuration)
434 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
435 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
437 // Configure Connector (usually done within archiva.xml configuration)
438 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
439 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
441 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
442 wagonMock.get( path, tmpFile );
444 wagonMockControl.setMatcher(customWagonGetMatcher);
445 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
447 wagonMock.get( path, tmpFile );
449 wagonMockControl.setMatcher(customWagonGetMatcher);
450 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
452 wagonMockControl.replay();
454 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
456 assertNotDownloaded( downloadedFile );
458 wagonMockControl.verify();
459 assertNoTempFiles( expectedFile );
461 // TODO: do not want failures to present as a not found [MRM-492]
462 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
466 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
469 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
470 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
471 setupTestableManagedRepository( path );
473 File expectedFile = new File( managedDefaultDir, path );
474 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
476 // Set the managed file to be newer than remote.
477 setManagedNewerThanRemote( expectedFile, remoteFile );
478 long expectedTimestamp = expectedFile.lastModified();
480 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
482 assertTrue( expectedFile.exists() );
484 // Configure Connector (usually done within archiva.xml configuration)
485 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
487 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
489 assertNotDownloaded( downloadedFile );
490 assertNotModified( expectedFile, expectedTimestamp );
491 assertNoTempFiles( expectedFile );
495 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
498 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
499 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
500 setupTestableManagedRepository( path );
502 File expectedFile = new File( managedDefaultDir, path );
503 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
505 // Set the managed file to be older than remote.
506 setManagedOlderThanRemote( expectedFile, remoteFile );
508 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
510 assertTrue( expectedFile.exists() );
512 // Configure Connector (usually done within archiva.xml configuration)
513 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
515 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
517 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
518 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
519 assertNoTempFiles( expectedFile );
523 public void testGetFromLegacyProxyNotPresentInManaged()
526 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
527 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
528 setupTestableManagedRepository( path );
530 File expectedFile = new File( managedDefaultDir, path );
531 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
533 assertNotExistsInManagedDefaultRepo( expectedFile );
535 // Configure Connector (usually done within archiva.xml configuration)
536 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false);
538 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
540 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
541 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
542 assertNoTempFiles( expectedFile );
546 public void testGetFromLegacyProxyPluginNotPresentInManaged()
549 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
550 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
551 setupTestableManagedRepository( path );
553 File expectedFile = new File( managedDefaultDir, path );
554 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
556 assertNotExistsInManagedDefaultRepo( expectedFile );
558 // Configure Connector (usually done within archiva.xml configuration)
559 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
561 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
563 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
564 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
565 assertNoTempFiles( expectedFile );