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( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
403 EasyMock.expectLastCall().andThrow( 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" );
442 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
443 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
445 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
446 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
448 wagonMockControl.replay();
450 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
452 assertNotDownloaded( downloadedFile );
454 wagonMockControl.verify();
455 assertNoTempFiles( expectedFile );
457 // TODO: do not want failures to present as a not found [MRM-492]
458 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
462 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
465 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
466 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
467 setupTestableManagedRepository( path );
469 File expectedFile = new File( managedDefaultDir, path );
470 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
472 // Set the managed file to be newer than remote.
473 setManagedNewerThanRemote( expectedFile, remoteFile );
474 long expectedTimestamp = expectedFile.lastModified();
476 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
478 assertTrue( expectedFile.exists() );
480 // Configure Connector (usually done within archiva.xml configuration)
481 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
483 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
485 assertNotDownloaded( downloadedFile );
486 assertNotModified( expectedFile, expectedTimestamp );
487 assertNoTempFiles( expectedFile );
491 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
494 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
495 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
496 setupTestableManagedRepository( path );
498 File expectedFile = new File( managedDefaultDir, path );
499 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
501 // Set the managed file to be older than remote.
502 setManagedOlderThanRemote( expectedFile, remoteFile );
504 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
506 assertTrue( expectedFile.exists() );
508 // Configure Connector (usually done within archiva.xml configuration)
509 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
511 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
513 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
514 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
515 assertNoTempFiles( expectedFile );
519 public void testGetFromLegacyProxyNotPresentInManaged()
522 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
523 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
524 setupTestableManagedRepository( path );
526 File expectedFile = new File( managedDefaultDir, path );
527 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
529 assertNotExistsInManagedDefaultRepo( expectedFile );
531 // Configure Connector (usually done within archiva.xml configuration)
532 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
534 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
536 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
537 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
538 assertNoTempFiles( expectedFile );
542 public void testGetFromLegacyProxyPluginNotPresentInManaged()
545 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
546 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
547 setupTestableManagedRepository( path );
549 File expectedFile = new File( managedDefaultDir, path );
550 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
552 assertNotExistsInManagedDefaultRepo( expectedFile );
554 // Configure Connector (usually done within archiva.xml configuration)
555 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
557 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
559 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
560 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
561 assertNoTempFiles( expectedFile );