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.easymock.EasyMock;
31 import org.junit.Test;
34 import java.nio.charset.Charset;
36 import static org.junit.Assert.*;
39 * 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, Charset.defaultCharset() );
333 String badContents = FileUtils.readFileToString( proxied2File, Charset.defaultCharset() );
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" ) );
406 wagonMock.get( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
407 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "transfer failed" ) );
408 wagonMockControl.replay();
410 // Configure Connector (usually done within archiva.xml configuration)
411 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
412 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
414 // Attempt the proxy fetch.
415 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
417 wagonMockControl.verify();
419 File proxied2File = new File( REPOPATH_PROXIED2, path );
420 assertFileEquals( expectedFile, downloadedFile, proxied2File );
421 assertNoTempFiles( expectedFile );
425 public void testGetAllRepositoriesFail()
428 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
429 setupTestableManagedRepository( path );
431 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
432 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
434 assertNotExistsInManagedDefaultRepo( expectedFile );
436 // Configure Repository (usually done within archiva.xml configuration)
437 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
438 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
440 // Configure Connector (usually done within archiva.xml configuration)
441 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
442 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
444 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
446 //wagonMock.get( path, tmpFile );
447 //wagonMockControl.setMatcher( customWagonGetMatcher );
448 //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
449 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
450 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
452 //wagonMock.get( path, tmpFile );
453 //wagonMockControl.setMatcher( customWagonGetMatcher );
454 //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
455 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
456 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
458 wagonMockControl.replay();
460 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
462 assertNotDownloaded( downloadedFile );
464 wagonMockControl.verify();
465 assertNoTempFiles( expectedFile );
467 // TODO: do not want failures to present as a not found [MRM-492]
468 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
472 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
475 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
476 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
477 setupTestableManagedRepository( path );
479 File expectedFile = new File( managedDefaultDir, path );
480 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
482 // Set the managed file to be newer than remote.
483 setManagedNewerThanRemote( expectedFile, remoteFile );
484 long expectedTimestamp = expectedFile.lastModified();
486 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
488 assertTrue( expectedFile.exists() );
490 // Configure Connector (usually done within archiva.xml configuration)
491 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
493 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
495 assertNotDownloaded( downloadedFile );
496 assertNotModified( expectedFile, expectedTimestamp );
497 assertNoTempFiles( expectedFile );
501 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
504 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
505 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
506 setupTestableManagedRepository( path );
508 File expectedFile = new File( managedDefaultDir, path );
509 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
511 // Set the managed file to be older than remote.
512 setManagedOlderThanRemote( expectedFile, remoteFile );
514 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
516 assertTrue( expectedFile.exists() );
518 // Configure Connector (usually done within archiva.xml configuration)
519 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
521 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
523 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
524 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
525 assertNoTempFiles( expectedFile );
529 public void testGetFromLegacyProxyNotPresentInManaged()
532 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
533 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
534 setupTestableManagedRepository( path );
536 File expectedFile = new File( managedDefaultDir, path );
537 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
539 assertNotExistsInManagedDefaultRepo( expectedFile );
541 // Configure Connector (usually done within archiva.xml configuration)
542 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
544 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
546 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
547 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
548 assertNoTempFiles( expectedFile );
552 public void testGetFromLegacyProxyPluginNotPresentInManaged()
555 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
556 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
557 setupTestableManagedRepository( path );
559 File expectedFile = new File( managedDefaultDir, path );
560 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
562 assertNotExistsInManagedDefaultRepo( expectedFile );
564 // Configure Connector (usually done within archiva.xml configuration)
565 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
567 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
569 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
570 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
571 assertNoTempFiles( expectedFile );