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.archiva.model.ArtifactReference;
23 import org.apache.archiva.policies.CachedFailuresPolicy;
24 import org.apache.archiva.policies.ChecksumPolicy;
25 import org.apache.archiva.policies.ReleasesPolicy;
26 import org.apache.archiva.policies.SnapshotsPolicy;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
30 import org.junit.Test;
33 import java.nio.charset.Charset;
35 import static org.junit.Assert.*;
38 * ManagedDefaultTransferTest
40 public class ManagedDefaultTransferTest
41 extends AbstractProxyTestCase
44 public void testGetDefaultLayoutNotPresentConnectorOffline()
47 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
48 setupTestableManagedRepository( path );
50 File expectedFile = new File( managedDefaultDir, path );
51 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
53 // Ensure file isn't present first.
54 assertNotExistsInManagedDefaultRepo( expectedFile );
56 // Configure Connector (usually done within archiva.xml configuration)
57 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
58 CachedFailuresPolicy.NO, true );
60 // Attempt the proxy fetch.
61 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
62 assertNull( "File should not have been downloaded", downloadedFile );
66 public void testGetDefaultLayoutNotPresent()
69 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
70 setupTestableManagedRepository( path );
72 File expectedFile = new File( managedDefaultDir, path );
73 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
75 // Ensure file isn't present first.
76 assertNotExistsInManagedDefaultRepo( expectedFile );
78 // Configure Connector (usually done within archiva.xml configuration)
79 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
80 CachedFailuresPolicy.NO, false );
82 // Attempt the proxy fetch.
83 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
85 File sourceFile = new File( REPOPATH_PROXIED1, path );
86 assertFileEquals( expectedFile, downloadedFile, sourceFile );
87 assertNoTempFiles( expectedFile );
91 public void testGetDefaultLayoutNotPresentPassthrough()
94 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
95 setupTestableManagedRepository( path );
97 File expectedFile = new File( managedDefaultDir, path );
99 // Ensure file isn't present first.
100 assertNotExistsInManagedDefaultRepo( expectedFile );
102 // Configure Connector (usually done within archiva.xml configuration)
103 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
104 CachedFailuresPolicy.NO, false );
106 // Attempt the proxy fetch.
107 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
109 File sourceFile = new File( REPOPATH_PROXIED1, path );
110 assertFileEquals( expectedFile, downloadedFile, sourceFile );
111 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
112 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
113 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
114 assertNoTempFiles( expectedFile );
118 * The attempt here should result in no file being transferred.
120 * The file exists locally, and the policy is ONCE.
125 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
128 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
129 setupTestableManagedRepository( path );
131 File expectedFile = new File( managedDefaultDir, path );
133 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
135 assertTrue( expectedFile.exists() );
137 // Configure Connector (usually done within archiva.xml configuration)
138 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
139 CachedFailuresPolicy.NO, false );
141 // Attempt the proxy fetch.
142 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
144 assertFileEquals( expectedFile, downloadedFile, expectedFile );
145 assertNoTempFiles( expectedFile );
149 * The attempt here should result in no file being transferred.
151 * The file exists locally, and the policy is ONCE.
156 public void testGetDefaultLayoutAlreadyPresentPassthrough()
159 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
160 setupTestableManagedRepository( path );
162 File expectedFile = new File( managedDefaultDir, path );
163 File remoteFile = new File( REPOPATH_PROXIED1, path );
165 assertTrue( expectedFile.exists() );
167 // Set the managed File to be newer than local.
168 setManagedOlderThanRemote( expectedFile, remoteFile );
169 long originalModificationTime = expectedFile.lastModified();
171 // Configure Connector (usually done within archiva.xml configuration)
172 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
173 CachedFailuresPolicy.NO, false );
175 // Attempt the proxy fetch.
176 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
178 assertNotDownloaded( downloadedFile );
179 assertNotModified( expectedFile, originalModificationTime );
180 assertNoTempFiles( expectedFile );
185 * Request a file, that exists locally, and remotely.
188 * All policies are set to IGNORE.
191 * Managed file is newer than remote file.
194 * Transfer should not have occured, as managed file is newer.
200 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
203 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
204 setupTestableManagedRepository( path );
206 File expectedFile = new File( managedDefaultDir, path );
207 File remoteFile = new File( REPOPATH_PROXIED1, path );
209 // Set the managed File to be newer than local.
210 setManagedNewerThanRemote( expectedFile, remoteFile );
212 long originalModificationTime = expectedFile.lastModified();
213 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
215 assertTrue( expectedFile.exists() );
217 // Configure Connector (usually done within archiva.xml configuration)
218 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
219 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
221 // Attempt the proxy fetch.
222 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
224 assertNotDownloaded( downloadedFile );
225 assertNotModified( expectedFile, originalModificationTime );
226 assertNoTempFiles( expectedFile );
231 * Request a file, that exists locally, and remotely.
234 * All policies are set to IGNORE.
237 * Managed file is older than Remote file.
240 * Transfer should have occured, as managed file is older than remote.
246 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
249 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
250 setupTestableManagedRepository( path );
252 File expectedFile = new File( managedDefaultDir, path );
253 File remoteFile = new File( REPOPATH_PROXIED1, path );
255 // Set the managed file to be newer than remote file.
256 setManagedOlderThanRemote( expectedFile, remoteFile );
258 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
260 assertTrue( expectedFile.exists() );
262 // Configure Connector (usually done within archiva.xml configuration)
263 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
264 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
266 // Attempt the proxy fetch.
267 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
269 File proxiedFile = new File( REPOPATH_PROXIED1, path );
270 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
271 assertNoTempFiles( expectedFile );
275 * The attempt here should result in file being transferred.
277 * The file exists locally, is over 6 years old, and the policy is DAILY.
282 public void testGetDefaultLayoutRemoteUpdate()
285 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
286 setupTestableManagedRepository( path );
288 File expectedFile = new File( managedDefaultDir, path );
289 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
291 assertTrue( expectedFile.exists() );
292 expectedFile.setLastModified( getPastDate().getTime() );
294 // Configure Connector (usually done within archiva.xml configuration)
295 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
296 CachedFailuresPolicy.NO, false );
298 // Attempt the proxy fetch.
299 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
301 File proxiedFile = new File( REPOPATH_PROXIED1, path );
302 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
303 assertNoTempFiles( expectedFile );
307 public void testGetWhenInBothProxiedRepos()
310 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
311 setupTestableManagedRepository( path );
313 File expectedFile = new File( managedDefaultDir, path );
314 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
316 assertNotExistsInManagedDefaultRepo( expectedFile );
318 // Configure Connector (usually done within archiva.xml configuration)
319 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
320 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
322 // Attempt the proxy fetch.
323 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
325 File proxied1File = new File( REPOPATH_PROXIED1, path );
326 File proxied2File = new File( REPOPATH_PROXIED2, path );
327 assertFileEquals( expectedFile, downloadedFile, proxied1File );
328 assertNoTempFiles( expectedFile );
330 // TODO: is this check even needed if it passes above?
331 String actualContents = FileUtils.readFileToString( downloadedFile, Charset.defaultCharset() );
332 String badContents = FileUtils.readFileToString( proxied2File, Charset.defaultCharset() );
333 assertFalse( "Downloaded file contents should not be that of proxy 2",
334 StringUtils.equals( actualContents, badContents ) );
338 public void testGetInSecondProxiedRepo()
341 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
342 setupTestableManagedRepository( path );
344 File expectedFile = new File( managedDefaultDir, path );
345 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
347 assertNotExistsInManagedDefaultRepo( expectedFile );
349 // Configure Connector (usually done within archiva.xml configuration)
350 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
351 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
353 // Attempt the proxy fetch.
354 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
356 File proxied2File = new File( REPOPATH_PROXIED2, path );
357 assertFileEquals( expectedFile, downloadedFile, proxied2File );
358 assertNoTempFiles( expectedFile );
362 public void testNotFoundInAnyProxies()
365 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
366 setupTestableManagedRepository( path );
368 File expectedFile = new File( managedDefaultDir, path );
369 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
371 assertNotExistsInManagedDefaultRepo( expectedFile );
373 // Configure Connector (usually done within archiva.xml configuration)
374 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
375 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
376 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
378 // Attempt the proxy fetch.
379 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
381 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
383 assertNoTempFiles( expectedFile );
387 public void testGetInSecondProxiedRepoFirstFails()
390 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
391 setupTestableManagedRepository( path );
393 File expectedFile = new File( managedDefaultDir, path );
394 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
396 assertNotExistsInManagedDefaultRepo( expectedFile );
398 // Configure Repository (usually done within archiva.xml configuration)
399 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
401 wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
402 wagonMockControl.setMatcher( customWagonGetMatcher );
403 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
404 wagonMockControl.replay();
406 // Configure Connector (usually done within archiva.xml configuration)
407 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
408 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
410 // Attempt the proxy fetch.
411 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
413 wagonMockControl.verify();
415 File proxied2File = new File( REPOPATH_PROXIED2, path );
416 assertFileEquals( expectedFile, downloadedFile, proxied2File );
417 assertNoTempFiles( expectedFile );
421 public void testGetAllRepositoriesFail()
424 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
425 setupTestableManagedRepository( path );
427 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
428 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
430 assertNotExistsInManagedDefaultRepo( expectedFile );
432 // Configure Repository (usually done within archiva.xml configuration)
433 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
434 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
436 // Configure Connector (usually done within archiva.xml configuration)
437 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
438 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
440 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
441 wagonMock.get( path, tmpFile );
443 wagonMockControl.setMatcher( customWagonGetMatcher );
444 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
446 wagonMock.get( path, tmpFile );
448 wagonMockControl.setMatcher( customWagonGetMatcher );
449 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
451 wagonMockControl.replay();
453 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
455 assertNotDownloaded( downloadedFile );
457 wagonMockControl.verify();
458 assertNoTempFiles( expectedFile );
460 // TODO: do not want failures to present as a not found [MRM-492]
461 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
465 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
468 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
469 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
470 setupTestableManagedRepository( path );
472 File expectedFile = new File( managedDefaultDir, path );
473 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
475 // Set the managed file to be newer than remote.
476 setManagedNewerThanRemote( expectedFile, remoteFile );
477 long expectedTimestamp = expectedFile.lastModified();
479 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
481 assertTrue( expectedFile.exists() );
483 // Configure Connector (usually done within archiva.xml configuration)
484 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
486 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
488 assertNotDownloaded( downloadedFile );
489 assertNotModified( expectedFile, expectedTimestamp );
490 assertNoTempFiles( expectedFile );
494 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
497 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
498 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
499 setupTestableManagedRepository( path );
501 File expectedFile = new File( managedDefaultDir, path );
502 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
504 // Set the managed file to be older than remote.
505 setManagedOlderThanRemote( expectedFile, remoteFile );
507 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
509 assertTrue( expectedFile.exists() );
511 // Configure Connector (usually done within archiva.xml configuration)
512 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
514 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
516 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
517 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
518 assertNoTempFiles( expectedFile );
522 public void testGetFromLegacyProxyNotPresentInManaged()
525 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
526 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
527 setupTestableManagedRepository( path );
529 File expectedFile = new File( managedDefaultDir, path );
530 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
532 assertNotExistsInManagedDefaultRepo( expectedFile );
534 // Configure Connector (usually done within archiva.xml configuration)
535 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
537 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
539 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
540 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
541 assertNoTempFiles( expectedFile );
545 public void testGetFromLegacyProxyPluginNotPresentInManaged()
548 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
549 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
550 setupTestableManagedRepository( path );
552 File expectedFile = new File( managedDefaultDir, path );
553 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
555 assertNotExistsInManagedDefaultRepo( expectedFile );
557 // Configure Connector (usually done within archiva.xml configuration)
558 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
560 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
562 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
563 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
564 assertNoTempFiles( expectedFile );