1 package org.apache.maven.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.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 import org.junit.Test;
35 * ManagedDefaultTransferTest
39 public class ManagedDefaultTransferTest
40 extends AbstractProxyTestCase
43 public void testGetDefaultLayoutNotPresentConnectorOffline()
46 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
47 setupTestableManagedRepository( path );
49 File expectedFile = new File( managedDefaultDir, path );
50 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
52 // Ensure file isn't present first.
53 assertNotExistsInManagedDefaultRepo( expectedFile );
55 // Configure Connector (usually done within archiva.xml configuration)
56 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
57 CachedFailuresPolicy.NO, true );
59 // Attempt the proxy fetch.
60 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
61 assertNull("File should not have been downloaded", downloadedFile);
65 public void testGetDefaultLayoutNotPresent()
68 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
69 setupTestableManagedRepository( path );
71 File expectedFile = new File( managedDefaultDir, path );
72 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
74 // Ensure file isn't present first.
75 assertNotExistsInManagedDefaultRepo( expectedFile );
77 // Configure Connector (usually done within archiva.xml configuration)
78 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
79 CachedFailuresPolicy.NO, false );
81 // Attempt the proxy fetch.
82 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
84 File sourceFile = new File( REPOPATH_PROXIED1, path );
85 assertFileEquals( expectedFile, downloadedFile, sourceFile );
86 assertNoTempFiles( expectedFile );
90 public void testGetDefaultLayoutNotPresentPassthrough()
93 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
94 setupTestableManagedRepository( path );
96 File expectedFile = new File( managedDefaultDir, path );
98 // Ensure file isn't present first.
99 assertNotExistsInManagedDefaultRepo( expectedFile );
101 // Configure Connector (usually done within archiva.xml configuration)
102 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
103 CachedFailuresPolicy.NO, false );
105 // Attempt the proxy fetch.
106 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
108 File sourceFile = new File( REPOPATH_PROXIED1, path );
109 assertFileEquals( expectedFile, downloadedFile, sourceFile );
110 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
111 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
112 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
113 assertNoTempFiles( expectedFile );
117 * The attempt here should result in no file being transferred.
119 * The file exists locally, and the policy is ONCE.
124 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
127 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
128 setupTestableManagedRepository( path );
130 File expectedFile = new File( managedDefaultDir, path );
132 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
134 assertTrue( expectedFile.exists() );
136 // Configure Connector (usually done within archiva.xml configuration)
137 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
138 CachedFailuresPolicy.NO, false );
140 // Attempt the proxy fetch.
141 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
143 assertFileEquals( expectedFile, downloadedFile, expectedFile );
144 assertNoTempFiles( expectedFile );
148 * The attempt here should result in no file being transferred.
150 * The file exists locally, and the policy is ONCE.
155 public void testGetDefaultLayoutAlreadyPresentPassthrough()
158 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
159 setupTestableManagedRepository( path );
161 File expectedFile = new File( managedDefaultDir, path );
162 File remoteFile = new File( REPOPATH_PROXIED1, path );
164 assertTrue( expectedFile.exists() );
166 // Set the managed File to be newer than local.
167 setManagedOlderThanRemote( expectedFile, remoteFile );
168 long originalModificationTime = expectedFile.lastModified();
170 // Configure Connector (usually done within archiva.xml configuration)
171 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
172 CachedFailuresPolicy.NO, false );
174 // Attempt the proxy fetch.
175 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
177 assertNotDownloaded( downloadedFile );
178 assertNotModified( expectedFile, originalModificationTime );
179 assertNoTempFiles( expectedFile );
184 * Request a file, that exists locally, and remotely.
187 * All policies are set to IGNORE.
190 * Managed file is newer than remote file.
193 * Transfer should not have occured, as managed file is newer.
199 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
202 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
203 setupTestableManagedRepository( path );
205 File expectedFile = new File( managedDefaultDir, path );
206 File remoteFile = new File( REPOPATH_PROXIED1, path );
208 // Set the managed File to be newer than local.
209 setManagedNewerThanRemote( expectedFile, remoteFile );
211 long originalModificationTime = expectedFile.lastModified();
212 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
214 assertTrue( expectedFile.exists() );
216 // Configure Connector (usually done within archiva.xml configuration)
217 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
218 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
220 // Attempt the proxy fetch.
221 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
223 assertNotDownloaded( downloadedFile );
224 assertNotModified( expectedFile, originalModificationTime );
225 assertNoTempFiles( expectedFile );
230 * Request a file, that exists locally, and remotely.
233 * All policies are set to IGNORE.
236 * Managed file is older than Remote file.
239 * Transfer should have occured, as managed file is older than remote.
245 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
248 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
249 setupTestableManagedRepository( path );
251 File expectedFile = new File( managedDefaultDir, path );
252 File remoteFile = new File( REPOPATH_PROXIED1, path );
254 // Set the managed file to be newer than remote file.
255 setManagedOlderThanRemote( expectedFile, remoteFile );
257 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
259 assertTrue( expectedFile.exists() );
261 // Configure Connector (usually done within archiva.xml configuration)
262 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
263 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
265 // Attempt the proxy fetch.
266 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
268 File proxiedFile = new File( REPOPATH_PROXIED1, path );
269 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
270 assertNoTempFiles( expectedFile );
274 * The attempt here should result in file being transferred.
276 * The file exists locally, is over 6 years old, and the policy is DAILY.
281 public void testGetDefaultLayoutRemoteUpdate()
284 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
285 setupTestableManagedRepository( path );
287 File expectedFile = new File( managedDefaultDir, path );
288 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
290 assertTrue( expectedFile.exists() );
291 expectedFile.setLastModified( getPastDate().getTime() );
293 // Configure Connector (usually done within archiva.xml configuration)
294 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
295 CachedFailuresPolicy.NO, false );
297 // Attempt the proxy fetch.
298 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
300 File proxiedFile = new File( REPOPATH_PROXIED1, path );
301 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
302 assertNoTempFiles( expectedFile );
306 public void testGetWhenInBothProxiedRepos()
309 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
310 setupTestableManagedRepository( path );
312 File expectedFile = new File( managedDefaultDir, path );
313 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
315 assertNotExistsInManagedDefaultRepo( expectedFile );
317 // Configure Connector (usually done within archiva.xml configuration)
318 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 , false );
319 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 , false );
321 // Attempt the proxy fetch.
322 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
324 File proxied1File = new File( REPOPATH_PROXIED1, path );
325 File proxied2File = new File( REPOPATH_PROXIED2, path );
326 assertFileEquals( expectedFile, downloadedFile, proxied1File );
327 assertNoTempFiles( expectedFile );
329 // TODO: is this check even needed if it passes above?
330 String actualContents = FileUtils.readFileToString( downloadedFile, null );
331 String badContents = FileUtils.readFileToString( proxied2File, null );
332 assertFalse( "Downloaded file contents should not be that of proxy 2",
333 StringUtils.equals( actualContents, badContents ) );
337 public void testGetInSecondProxiedRepo()
340 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
341 setupTestableManagedRepository( path );
343 File expectedFile = new File( managedDefaultDir, path );
344 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
346 assertNotExistsInManagedDefaultRepo( expectedFile );
348 // Configure Connector (usually done within archiva.xml configuration)
349 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
350 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
352 // Attempt the proxy fetch.
353 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
355 File proxied2File = new File( REPOPATH_PROXIED2, path );
356 assertFileEquals( expectedFile, downloadedFile, proxied2File );
357 assertNoTempFiles( expectedFile );
361 public void testNotFoundInAnyProxies()
364 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
365 setupTestableManagedRepository( path );
367 File expectedFile = new File( managedDefaultDir, path );
368 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
370 assertNotExistsInManagedDefaultRepo( expectedFile );
372 // Configure Connector (usually done within archiva.xml configuration)
373 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
374 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
375 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
377 // Attempt the proxy fetch.
378 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
380 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
382 assertNoTempFiles( expectedFile );
386 public void testGetInSecondProxiedRepoFirstFails()
389 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
390 setupTestableManagedRepository( path );
392 File expectedFile = new File( managedDefaultDir, path );
393 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
395 assertNotExistsInManagedDefaultRepo( expectedFile );
397 // Configure Repository (usually done within archiva.xml configuration)
398 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
400 wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
401 wagonMockControl.setMatcher(customWagonGetMatcher);
402 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
403 wagonMockControl.replay();
405 // Configure Connector (usually done within archiva.xml configuration)
406 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
407 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
409 // Attempt the proxy fetch.
410 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
412 wagonMockControl.verify();
414 File proxied2File = new File( REPOPATH_PROXIED2, path );
415 assertFileEquals( expectedFile, downloadedFile, proxied2File );
416 assertNoTempFiles( expectedFile );
420 public void testGetAllRepositoriesFail()
423 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
424 setupTestableManagedRepository( path );
426 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
427 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
429 assertNotExistsInManagedDefaultRepo( expectedFile );
431 // Configure Repository (usually done within archiva.xml configuration)
432 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
433 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
435 // Configure Connector (usually done within archiva.xml configuration)
436 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
437 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
439 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
440 wagonMock.get( path, tmpFile );
442 wagonMockControl.setMatcher(customWagonGetMatcher);
443 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
445 wagonMock.get( path, tmpFile );
447 wagonMockControl.setMatcher(customWagonGetMatcher);
448 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
450 wagonMockControl.replay();
452 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
454 assertNotDownloaded( downloadedFile );
456 wagonMockControl.verify();
457 assertNoTempFiles( expectedFile );
459 // TODO: do not want failures to present as a not found [MRM-492]
460 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
464 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
467 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
468 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
469 setupTestableManagedRepository( path );
471 File expectedFile = new File( managedDefaultDir, path );
472 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
474 // Set the managed file to be newer than remote.
475 setManagedNewerThanRemote( expectedFile, remoteFile );
476 long expectedTimestamp = expectedFile.lastModified();
478 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
480 assertTrue( expectedFile.exists() );
482 // Configure Connector (usually done within archiva.xml configuration)
483 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
485 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
487 assertNotDownloaded( downloadedFile );
488 assertNotModified( expectedFile, expectedTimestamp );
489 assertNoTempFiles( expectedFile );
493 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
496 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
497 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
498 setupTestableManagedRepository( path );
500 File expectedFile = new File( managedDefaultDir, path );
501 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
503 // Set the managed file to be older than remote.
504 setManagedOlderThanRemote( expectedFile, remoteFile );
506 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
508 assertTrue( expectedFile.exists() );
510 // Configure Connector (usually done within archiva.xml configuration)
511 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
513 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
515 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
516 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
517 assertNoTempFiles( expectedFile );
521 public void testGetFromLegacyProxyNotPresentInManaged()
524 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
525 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
526 setupTestableManagedRepository( path );
528 File expectedFile = new File( managedDefaultDir, path );
529 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
531 assertNotExistsInManagedDefaultRepo( expectedFile );
533 // Configure Connector (usually done within archiva.xml configuration)
534 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false);
536 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
538 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
539 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
540 assertNoTempFiles( expectedFile );
544 public void testGetFromLegacyProxyPluginNotPresentInManaged()
547 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
548 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
549 setupTestableManagedRepository( path );
551 File expectedFile = new File( managedDefaultDir, path );
552 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
554 assertNotExistsInManagedDefaultRepo( expectedFile );
556 // Configure Connector (usually done within archiva.xml configuration)
557 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, false );
559 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
561 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
562 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
563 assertNoTempFiles( expectedFile );