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;
34 * ManagedDefaultTransferTest
36 * @author Brett Porter
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
40 public class ManagedDefaultTransferTest
41 extends AbstractProxyTestCase
43 public void testGetDefaultLayoutNotPresent()
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.IGNORED );
59 // Attempt the proxy fetch.
60 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
62 File sourceFile = new File( REPOPATH_PROXIED1, path );
63 assertFileEquals( expectedFile, downloadedFile, sourceFile );
64 assertNoTempFiles( expectedFile );
68 * The attempt here should result in no file being transferred.
70 * The file exists locally, and the policy is ONCE.
74 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
77 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
78 setupTestableManagedRepository( path );
80 File expectedFile = new File( managedDefaultDir, path );
82 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
84 assertTrue( expectedFile.exists() );
86 // Configure Connector (usually done within archiva.xml configuration)
87 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
88 CachedFailuresPolicy.IGNORED );
90 // Attempt the proxy fetch.
91 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
93 assertFileEquals( expectedFile, downloadedFile, expectedFile );
94 assertNoTempFiles( expectedFile );
99 * Request a file, that exists locally, and remotely.
102 * All policies are set to IGNORE.
105 * Managed file is newer than remote file.
108 * Transfer should not have occured, as managed file is newer.
113 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
116 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
117 setupTestableManagedRepository( path );
119 File expectedFile = new File( managedDefaultDir, path );
120 File remoteFile = new File( REPOPATH_PROXIED1, path );
122 // Set the managed File to be newer than local.
123 setManagedNewerThanRemote( expectedFile, remoteFile );
125 long originalModificationTime = expectedFile.lastModified();
126 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
128 assertTrue( expectedFile.exists() );
130 // Configure Connector (usually done within archiva.xml configuration)
131 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
132 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
134 // Attempt the proxy fetch.
135 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
137 assertNotDownloaded( downloadedFile );
138 assertNotModified( expectedFile, originalModificationTime );
139 assertNoTempFiles( expectedFile );
144 * Request a file, that exists locally, and remotely.
147 * All policies are set to IGNORE.
150 * Managed file is older than Remote file.
153 * Transfer should have occured, as managed file is older than remote.
158 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
161 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
162 setupTestableManagedRepository( path );
164 File expectedFile = new File( managedDefaultDir, path );
165 File remoteFile = new File( REPOPATH_PROXIED1, path );
167 // Set the managed file to be newer than remote file.
168 setManagedOlderThanRemote( expectedFile, remoteFile );
170 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
172 assertTrue( expectedFile.exists() );
174 // Configure Connector (usually done within archiva.xml configuration)
175 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
176 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
178 // Attempt the proxy fetch.
179 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
181 File proxiedFile = new File( REPOPATH_PROXIED1, path );
182 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
183 assertNoTempFiles( expectedFile );
187 * The attempt here should result in file being transferred.
189 * The file exists locally, is over 6 years old, and the policy is DAILY.
193 public void testGetDefaultLayoutRemoteUpdate()
196 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
197 setupTestableManagedRepository( path );
199 File expectedFile = new File( managedDefaultDir, path );
200 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
202 assertTrue( expectedFile.exists() );
203 expectedFile.setLastModified( getPastDate().getTime() );
205 // Configure Connector (usually done within archiva.xml configuration)
206 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
207 CachedFailuresPolicy.IGNORED );
209 // Attempt the proxy fetch.
210 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
212 File proxiedFile = new File( REPOPATH_PROXIED1, path );
213 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
214 assertNoTempFiles( expectedFile );
217 public void testGetWhenInBothProxiedRepos()
220 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
221 setupTestableManagedRepository( path );
223 File expectedFile = new File( managedDefaultDir, path );
224 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
226 assertNotExistsInManagedDefaultRepo( expectedFile );
228 // Configure Connector (usually done within archiva.xml configuration)
229 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
230 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
232 // Attempt the proxy fetch.
233 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
235 File proxied1File = new File( REPOPATH_PROXIED1, path );
236 File proxied2File = new File( REPOPATH_PROXIED2, path );
237 assertFileEquals( expectedFile, downloadedFile, proxied1File );
238 assertNoTempFiles( expectedFile );
240 // TODO: is this check even needed if it passes above?
241 String actualContents = FileUtils.readFileToString( downloadedFile, null );
242 String badContents = FileUtils.readFileToString( proxied2File, null );
243 assertFalse( "Downloaded file contents should not be that of proxy 2",
244 StringUtils.equals( actualContents, badContents ) );
247 public void testGetInSecondProxiedRepo()
250 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
251 setupTestableManagedRepository( path );
253 File expectedFile = new File( managedDefaultDir, path );
254 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
256 assertNotExistsInManagedDefaultRepo( expectedFile );
258 // Configure Connector (usually done within archiva.xml configuration)
259 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
260 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
262 // Attempt the proxy fetch.
263 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
265 File proxied2File = new File( REPOPATH_PROXIED2, path );
266 assertFileEquals( expectedFile, downloadedFile, proxied2File );
267 assertNoTempFiles( expectedFile );
270 public void testNotFoundInAnyProxies()
273 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
274 setupTestableManagedRepository( path );
276 File expectedFile = new File( managedDefaultDir, path );
277 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
279 assertNotExistsInManagedDefaultRepo( expectedFile );
281 // Configure Connector (usually done within archiva.xml configuration)
282 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
283 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
284 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
286 // Attempt the proxy fetch.
287 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
289 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
291 assertNoTempFiles( expectedFile );
294 public void testGetInSecondProxiedRepoFirstFails()
297 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
298 setupTestableManagedRepository( path );
300 File expectedFile = new File( managedDefaultDir, path );
301 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
303 assertNotExistsInManagedDefaultRepo( expectedFile );
305 // Configure Repository (usually done within archiva.xml configuration)
306 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
308 wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
309 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
310 wagonMockControl.replay();
312 // Configure Connector (usually done within archiva.xml configuration)
313 saveConnector( ID_DEFAULT_MANAGED, "badproxied" );
314 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
316 // Attempt the proxy fetch.
317 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
319 wagonMockControl.verify();
321 File proxied2File = new File( REPOPATH_PROXIED2, path );
322 assertFileEquals( expectedFile, downloadedFile, proxied2File );
323 assertNoTempFiles( expectedFile );
326 public void testGetAllRepositoriesFail()
329 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
330 setupTestableManagedRepository( path );
332 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
333 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
335 assertNotExistsInManagedDefaultRepo( expectedFile );
337 // Configure Repository (usually done within archiva.xml configuration)
338 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
339 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
341 // Configure Connector (usually done within archiva.xml configuration)
342 saveConnector( ID_DEFAULT_MANAGED, "badproxied1" );
343 saveConnector( ID_DEFAULT_MANAGED, "badproxied2" );
345 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
346 wagonMock.get( path, tmpFile );
347 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
349 wagonMock.get( path, tmpFile );
350 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
352 wagonMockControl.replay();
354 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
356 assertNotDownloaded( downloadedFile );
358 wagonMockControl.verify();
359 assertNoTempFiles( expectedFile );
361 // TODO: do not want failures to present as a not found [MRM-492]
362 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
365 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
368 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
369 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
370 setupTestableManagedRepository( path );
372 File expectedFile = new File( managedDefaultDir, path );
373 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
375 // Set the managed file to be newer than remote.
376 setManagedNewerThanRemote( expectedFile, remoteFile );
377 long expectedTimestamp = expectedFile.lastModified();
379 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
381 assertTrue( expectedFile.exists() );
383 // Configure Connector (usually done within archiva.xml configuration)
384 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
386 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
388 assertNotDownloaded( downloadedFile );
389 assertNotModified( expectedFile, expectedTimestamp );
390 assertNoTempFiles( expectedFile );
393 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
396 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
397 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
398 setupTestableManagedRepository( path );
400 File expectedFile = new File( managedDefaultDir, path );
401 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
403 // Set the managed file to be older than remote.
404 setManagedOlderThanRemote( expectedFile, remoteFile );
406 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
408 assertTrue( expectedFile.exists() );
410 // Configure Connector (usually done within archiva.xml configuration)
411 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
413 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
415 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
416 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
417 assertNoTempFiles( expectedFile );
420 public void testGetFromLegacyProxyNotPresentInManaged()
423 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
424 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
425 setupTestableManagedRepository( path );
427 File expectedFile = new File( managedDefaultDir, path );
428 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
430 assertNotExistsInManagedDefaultRepo( expectedFile );
432 // Configure Connector (usually done within archiva.xml configuration)
433 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
435 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
437 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
438 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
439 assertNoTempFiles( expectedFile );
442 public void testGetFromLegacyProxyPluginNotPresentInManaged()
445 String legacyPath = "org.apache.maven.test/plugins/example-maven-plugin-0.42.jar";
446 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
447 setupTestableManagedRepository( path );
449 File expectedFile = new File( managedDefaultDir, path );
450 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
452 assertNotExistsInManagedDefaultRepo( expectedFile );
454 // Configure Connector (usually done within archiva.xml configuration)
455 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
457 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
459 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
460 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
461 assertNoTempFiles( expectedFile );