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.policies.CachedFailuresPolicy;
23 import org.apache.archiva.policies.ChecksumPolicy;
24 import org.apache.archiva.policies.ReleasesPolicy;
25 import org.apache.archiva.policies.SnapshotsPolicy;
26 import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
27 import org.apache.archiva.repository.content.Artifact;
28 import org.apache.archiva.repository.storage.StorageAsset;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.maven.wagon.ResourceDoesNotExistException;
32 import org.easymock.EasyMock;
33 import org.junit.Test;
36 import java.nio.charset.Charset;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.nio.file.attribute.FileTime;
41 import java.util.concurrent.TimeUnit;
43 import static org.junit.Assert.*;
46 * ManagedDefaultTransferTest
48 public class ManagedDefaultTransferTest
49 extends AbstractProxyTestCase
52 public void testGetDefaultLayoutNotPresentConnectorOffline()
55 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
56 setupTestableManagedRepository( path );
58 Path expectedFile = managedDefaultDir.resolve(path);
60 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
62 Artifact artifact = layout.getArtifact( path );
64 // Ensure file isn't present first.
65 assertNotExistsInManagedDefaultRepo( expectedFile );
67 // Configure Connector (usually done within archiva.xml configuration)
68 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
69 CachedFailuresPolicy.NO, true );
71 // Attempt the proxy fetch.
72 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
73 assertNull( "File should not have been downloaded", downloadedFile );
77 public void testGetDefaultLayoutNotPresent()
80 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
81 setupTestableManagedRepository( path );
83 Path expectedFile = managedDefaultDir.resolve(path);
85 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
87 Artifact artifact = layout.getArtifact( path );
89 // Ensure file isn't present first.
90 assertNotExistsInManagedDefaultRepo( expectedFile );
92 // Configure Connector (usually done within archiva.xml configuration)
93 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
94 CachedFailuresPolicy.NO, false );
96 // Attempt the proxy fetch.
97 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
99 Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
100 assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
101 assertNoTempFiles( expectedFile );
105 public void testGetDefaultLayoutNotPresentPassthrough()
108 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
109 setupTestableManagedRepository( path );
111 Path expectedFile = managedDefaultDir.resolve(path);
113 // Ensure file isn't present first.
114 assertNotExistsInManagedDefaultRepo( expectedFile );
116 // Configure Connector (usually done within archiva.xml configuration)
117 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
118 CachedFailuresPolicy.NO, false );
120 // Attempt the proxy fetch.
121 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
123 Path sourceFile = Paths.get(REPOPATH_PROXIED1, path);
124 assertNotNull(downloadedFile);
125 assertFileEquals( expectedFile, downloadedFile.getFilePath(), sourceFile );
126 assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha1" )) );
127 assertFalse( Files.exists(downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".md5" ) ));
128 assertFalse( Files.exists( downloadedFile.getParent().getFilePath().resolve(downloadedFile.getName() + ".sha256" ) ));
129 assertNoTempFiles( expectedFile );
133 * The attempt here should result in no file being transferred.
135 * The file exists locally, and the policy is ONCE.
140 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
143 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
144 setupTestableManagedRepository( path );
146 Path expectedFile = managedDefaultDir.resolve(path);
148 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
150 Artifact artifact = layout.getArtifact( path );
152 assertTrue( Files.exists(expectedFile) );
154 // Configure Connector (usually done within archiva.xml configuration)
155 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
156 CachedFailuresPolicy.NO, false );
158 // Attempt the proxy fetch.
159 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
161 assertFileEquals( expectedFile, downloadedFile.getFilePath(), expectedFile );
162 assertNoTempFiles( expectedFile );
166 * The attempt here should result in no file being transferred.
168 * The file exists locally, and the policy is ONCE.
173 public void testGetDefaultLayoutAlreadyPresentPassthrough()
176 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
177 setupTestableManagedRepository( path );
179 Path expectedFile = managedDefaultDir.resolve(path);
180 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
182 assertTrue( Files.exists(expectedFile) );
184 // Set the managed File to be newer than local.
185 setManagedOlderThanRemote( expectedFile, remoteFile );
186 long originalModificationTime = Files.getLastModifiedTime(expectedFile).toMillis();
188 // Configure Connector (usually done within archiva.xml configuration)
189 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
190 CachedFailuresPolicy.NO, false );
192 // Attempt the proxy fetch.
193 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), path );
195 assertNotDownloaded( downloadedFile );
196 assertNotModified( expectedFile, originalModificationTime );
197 assertNoTempFiles( expectedFile );
202 * Request a file, that exists locally, and remotely.
205 * All policies are set to IGNORE.
208 * Managed file is newer than remote file.
211 * Transfer should not have occured, as managed file is newer.
217 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
220 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
221 setupTestableManagedRepository( path );
223 Path expectedFile = managedDefaultDir.resolve(path);
224 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
226 // Set the managed File to be newer than local.
227 setManagedNewerThanRemote( expectedFile, remoteFile );
229 long originalModificationTime = Files.getLastModifiedTime( expectedFile).toMillis();
231 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
233 Artifact artifact = layout.getArtifact( path );
235 assertTrue( Files.exists(expectedFile) );
237 // Configure Connector (usually done within archiva.xml configuration)
238 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
239 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
241 // Attempt the proxy fetch.
242 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
244 assertNotDownloaded( downloadedFile );
245 assertNotModified( expectedFile, originalModificationTime );
246 assertNoTempFiles( expectedFile );
251 * Request a file, that exists locally, and remotely.
254 * All policies are set to IGNORE.
257 * Managed file is older than Remote file.
260 * Transfer should have occured, as managed file is older than remote.
266 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
269 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
270 setupTestableManagedRepository( path );
272 Path expectedFile = managedDefaultDir.resolve(path);
273 Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
275 // Set the managed file to be newer than remote file.
276 setManagedOlderThanRemote( expectedFile, remoteFile );
278 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
280 Artifact artifact = layout.getArtifact( path );
282 assertTrue( Files.exists(expectedFile) );
284 // Configure Connector (usually done within archiva.xml configuration)
285 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
286 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
288 // Attempt the proxy fetch.
289 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
291 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
292 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
293 assertNoTempFiles( expectedFile );
297 * The attempt here should result in file being transferred.
299 * The file exists locally, is over 6 years old, and the policy is DAILY.
304 public void testGetDefaultLayoutRemoteUpdate()
307 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
308 setupTestableManagedRepository( path );
310 Path expectedFile = managedDefaultDir.resolve(path);
312 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
314 Artifact artifact = layout.getArtifact( path );
316 assertTrue( Files.exists(expectedFile) );
317 Files.setLastModifiedTime( expectedFile, FileTime.from(getPastDate().getTime(), TimeUnit.MILLISECONDS ));
319 // Configure Connector (usually done within archiva.xml configuration)
320 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
321 CachedFailuresPolicy.NO, false );
323 // Attempt the proxy fetch.
324 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
326 Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
327 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
328 assertNoTempFiles( expectedFile );
332 public void testGetWhenInBothProxiedRepos()
335 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
336 setupTestableManagedRepository( path );
338 Path expectedFile = managedDefaultDir.resolve(path);
340 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
342 Artifact artifact = layout.getArtifact( path );
344 assertNotExistsInManagedDefaultRepo( expectedFile );
346 // Configure Connector (usually done within archiva.xml configuration)
347 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
348 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
350 // Attempt the proxy fetch.
351 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
353 Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
354 Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
355 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied1File );
356 assertNoTempFiles( expectedFile );
358 // TODO: is this check even needed if it passes above?
359 String actualContents = FileUtils.readFileToString( downloadedFile.getFilePath().toFile(), Charset.defaultCharset() );
360 String badContents = FileUtils.readFileToString( proxied2File.toFile(), Charset.defaultCharset() );
361 assertFalse( "Downloaded file contents should not be that of proxy 2",
362 StringUtils.equals( actualContents, badContents ) );
366 public void testGetInSecondProxiedRepo()
369 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
370 setupTestableManagedRepository( path );
372 Path expectedFile = managedDefaultDir.resolve(path);
374 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
376 Artifact artifact = layout.getArtifact( path );
378 assertNotExistsInManagedDefaultRepo( expectedFile );
380 // Configure Connector (usually done within archiva.xml configuration)
381 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
382 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
384 // Attempt the proxy fetch.
385 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
387 Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
388 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
389 assertNoTempFiles( expectedFile );
393 public void testNotFoundInAnyProxies()
396 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
397 setupTestableManagedRepository( path );
399 Path expectedFile = managedDefaultDir.resolve(path);
401 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
403 Artifact artifact = layout.getArtifact( path );
405 assertNotExistsInManagedDefaultRepo( expectedFile );
407 // Configure Connector (usually done within archiva.xml configuration)
408 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
409 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
411 // Attempt the proxy fetch.
412 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
414 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
416 assertNoTempFiles( expectedFile );
420 public void testGetInSecondProxiedRepoFirstFails()
423 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
424 setupTestableManagedRepository( path );
426 Path expectedFile = managedDefaultDir.resolve(path);
428 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
430 Artifact artifact = layout.getArtifact( path );
432 assertNotExistsInManagedDefaultRepo( expectedFile );
434 // Configure Repository (usually done within archiva.xml configuration)
435 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "" +
436 "http://bad.machine.com/repo/", "default" );
438 wagonMock.get( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
439 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "transfer failed" ) );
440 wagonMockControl.replay();
442 // Configure Connector (usually done within archiva.xml configuration)
443 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
444 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
446 // Attempt the proxy fetch.
447 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
449 wagonMockControl.verify();
451 Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
452 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
453 assertNoTempFiles( expectedFile );
457 public void testGetAllRepositoriesFail()
460 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
461 setupTestableManagedRepository( path );
463 Path expectedFile = managedDefaultDir.resolve( path );
465 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
467 Artifact artifact = layout.getArtifact( path );
469 assertNotExistsInManagedDefaultRepo( expectedFile );
471 // Configure Repository (usually done within archiva.xml configuration)
472 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
473 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://dead.machine.com/repo/", "default" );
475 // Configure Connector (usually done within archiva.xml configuration)
476 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
477 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
479 Path tmpFile = expectedFile.getParent().resolve(expectedFile.getFileName() + ".tmp" );
481 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
482 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
484 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
485 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
487 wagonMockControl.replay();
489 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
491 assertNotDownloaded( downloadedFile );
493 wagonMockControl.verify();
494 assertNoTempFiles( expectedFile );
496 // TODO: do not want failures to present as a not found [MRM-492]
497 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?