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;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.nio.file.attribute.FileTime;
39 import java.util.concurrent.TimeUnit;
41 import static org.junit.Assert.*;
44 * ManagedDefaultTransferTest
46 public class ManagedDefaultTransferTest
47 extends AbstractProxyTestCase
50 public void testGetDefaultLayoutNotPresentConnectorOffline()
53 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
54 setupTestableManagedRepository( path );
56 Path expectedFile = managedDefaultDir.resolve(path);
57 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
59 // Ensure file isn't present first.
60 assertNotExistsInManagedDefaultRepo( expectedFile );
62 // Configure Connector (usually done within archiva.xml configuration)
63 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
64 CachedFailuresPolicy.NO, true );
66 // Attempt the proxy fetch.
67 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
68 assertNull( "File should not have been downloaded", downloadedFile );
72 public void testGetDefaultLayoutNotPresent()
75 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
76 setupTestableManagedRepository( path );
78 Path expectedFile = managedDefaultDir.resolve(path);
79 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
81 // Ensure file isn't present first.
82 assertNotExistsInManagedDefaultRepo( expectedFile );
84 // Configure Connector (usually done within archiva.xml configuration)
85 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
86 CachedFailuresPolicy.NO, false );
88 // Attempt the proxy fetch.
89 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
91 Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
92 assertFileEquals( expectedFile, downloadedFile, sourceFile );
93 assertNoTempFiles( expectedFile );
97 public void testGetDefaultLayoutNotPresentPassthrough()
100 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
101 setupTestableManagedRepository( path );
103 Path expectedFile = managedDefaultDir.resolve(path);
105 // Ensure file isn't present first.
106 assertNotExistsInManagedDefaultRepo( expectedFile );
108 // Configure Connector (usually done within archiva.xml configuration)
109 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
110 CachedFailuresPolicy.NO, false );
112 // Attempt the proxy fetch.
113 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
115 Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
116 assertFileEquals( expectedFile, downloadedFile, sourceFile );
117 assertFalse( Files.exists( downloadedFile.getParent().resolve(downloadedFile.getFileName() + ".sha1" )) );
118 assertFalse( Files.exists(downloadedFile.getParent().resolve(downloadedFile.getFileName() + ".md5" ) ));
119 assertFalse( Files.exists( downloadedFile.getParent().resolve(downloadedFile.getFileName() + ".asc" ) ));
120 assertNoTempFiles( expectedFile );
124 * The attempt here should result in no file being transferred.
126 * The file exists locally, and the policy is ONCE.
131 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
134 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
135 setupTestableManagedRepository( path );
137 Path expectedFile = managedDefaultDir.resolve(path);
139 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
141 assertTrue( Files.exists(expectedFile) );
143 // Configure Connector (usually done within archiva.xml configuration)
144 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
145 CachedFailuresPolicy.NO, false );
147 // Attempt the proxy fetch.
148 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
150 assertFileEquals( expectedFile, downloadedFile, expectedFile );
151 assertNoTempFiles( expectedFile );
155 * The attempt here should result in no file being transferred.
157 * The file exists locally, and the policy is ONCE.
162 public void testGetDefaultLayoutAlreadyPresentPassthrough()
165 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
166 setupTestableManagedRepository( path );
168 Path expectedFile = managedDefaultDir.resolve(path);
169 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
171 assertTrue( Files.exists(expectedFile) );
173 // Set the managed File to be newer than local.
174 setManagedOlderThanRemote( expectedFile, remoteFile );
175 long originalModificationTime = Files.getLastModifiedTime(expectedFile).toMillis();
177 // Configure Connector (usually done within archiva.xml configuration)
178 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
179 CachedFailuresPolicy.NO, false );
181 // Attempt the proxy fetch.
182 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
184 assertNotDownloaded( downloadedFile );
185 assertNotModified( expectedFile, originalModificationTime );
186 assertNoTempFiles( expectedFile );
191 * Request a file, that exists locally, and remotely.
194 * All policies are set to IGNORE.
197 * Managed file is newer than remote file.
200 * Transfer should not have occured, as managed file is newer.
206 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
209 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
210 setupTestableManagedRepository( path );
212 Path expectedFile = managedDefaultDir.resolve(path);
213 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
215 // Set the managed File to be newer than local.
216 setManagedNewerThanRemote( expectedFile, remoteFile );
218 long originalModificationTime = Files.getLastModifiedTime( expectedFile).toMillis();
219 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
221 assertTrue( Files.exists(expectedFile) );
223 // Configure Connector (usually done within archiva.xml configuration)
224 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
225 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
227 // Attempt the proxy fetch.
228 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
230 assertNotDownloaded( downloadedFile );
231 assertNotModified( expectedFile, originalModificationTime );
232 assertNoTempFiles( expectedFile );
237 * Request a file, that exists locally, and remotely.
240 * All policies are set to IGNORE.
243 * Managed file is older than Remote file.
246 * Transfer should have occured, as managed file is older than remote.
252 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
255 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
256 setupTestableManagedRepository( path );
258 Path expectedFile = managedDefaultDir.resolve(path);
259 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
261 // Set the managed file to be newer than remote file.
262 setManagedOlderThanRemote( expectedFile, remoteFile );
264 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
266 assertTrue( Files.exists(expectedFile) );
268 // Configure Connector (usually done within archiva.xml configuration)
269 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
270 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
272 // Attempt the proxy fetch.
273 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
275 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
276 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
277 assertNoTempFiles( expectedFile );
281 * The attempt here should result in file being transferred.
283 * The file exists locally, is over 6 years old, and the policy is DAILY.
288 public void testGetDefaultLayoutRemoteUpdate()
291 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
292 setupTestableManagedRepository( path );
294 Path expectedFile = managedDefaultDir.resolve(path);
295 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
297 assertTrue( Files.exists(expectedFile) );
298 Files.setLastModifiedTime( expectedFile, FileTime.from(getPastDate().getTime(), TimeUnit.MILLISECONDS ));
300 // Configure Connector (usually done within archiva.xml configuration)
301 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
302 CachedFailuresPolicy.NO, false );
304 // Attempt the proxy fetch.
305 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
307 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
308 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
309 assertNoTempFiles( expectedFile );
313 public void testGetWhenInBothProxiedRepos()
316 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
317 setupTestableManagedRepository( path );
319 Path expectedFile = managedDefaultDir.resolve(path);
320 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
322 assertNotExistsInManagedDefaultRepo( expectedFile );
324 // Configure Connector (usually done within archiva.xml configuration)
325 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
326 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
328 // Attempt the proxy fetch.
329 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
331 Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
332 Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
333 assertFileEquals( expectedFile, downloadedFile, proxied1File );
334 assertNoTempFiles( expectedFile );
336 // TODO: is this check even needed if it passes above?
337 String actualContents = FileUtils.readFileToString( downloadedFile.toFile(), Charset.defaultCharset() );
338 String badContents = FileUtils.readFileToString( proxied2File.toFile(), Charset.defaultCharset() );
339 assertFalse( "Downloaded file contents should not be that of proxy 2",
340 StringUtils.equals( actualContents, badContents ) );
344 public void testGetInSecondProxiedRepo()
347 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
348 setupTestableManagedRepository( path );
350 Path expectedFile = managedDefaultDir.resolve(path);
351 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
353 assertNotExistsInManagedDefaultRepo( expectedFile );
355 // Configure Connector (usually done within archiva.xml configuration)
356 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
357 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
359 // Attempt the proxy fetch.
360 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
362 Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
363 assertFileEquals( expectedFile, downloadedFile, proxied2File );
364 assertNoTempFiles( expectedFile );
368 public void testNotFoundInAnyProxies()
371 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
372 setupTestableManagedRepository( path );
374 Path expectedFile = managedDefaultDir.resolve(path);
375 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
377 assertNotExistsInManagedDefaultRepo( expectedFile );
379 // Configure Connector (usually done within archiva.xml configuration)
380 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
381 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
383 // Attempt the proxy fetch.
384 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
386 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
388 assertNoTempFiles( expectedFile );
392 public void testGetInSecondProxiedRepoFirstFails()
395 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
396 setupTestableManagedRepository( path );
398 Path expectedFile = managedDefaultDir.resolve(path);
399 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
401 assertNotExistsInManagedDefaultRepo( expectedFile );
403 // Configure Repository (usually done within archiva.xml configuration)
404 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
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 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
417 wagonMockControl.verify();
419 Path proxied2File = Paths.get(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 Path expectedFile = managedDefaultDir.resolve( 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 Path tmpFile = expectedFile.getParent().resolve(expectedFile.getFileName() + ".tmp" );
446 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
447 EasyMock.expectLastCall().andThrow( 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 wagonMockControl.replay();
454 Path 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?