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.NO );
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.NO );
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.ALWAYS,
132 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
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.ALWAYS,
176 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
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.NO );
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.setMatcher(customWagonGetMatcher);
310 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
311 wagonMockControl.replay();
313 // Configure Connector (usually done within archiva.xml configuration)
314 saveConnector( ID_DEFAULT_MANAGED, "badproxied" );
315 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
317 // Attempt the proxy fetch.
318 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
320 wagonMockControl.verify();
322 File proxied2File = new File( REPOPATH_PROXIED2, path );
323 assertFileEquals( expectedFile, downloadedFile, proxied2File );
324 assertNoTempFiles( expectedFile );
327 public void testGetAllRepositoriesFail()
330 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
331 setupTestableManagedRepository( path );
333 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
334 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
336 assertNotExistsInManagedDefaultRepo( expectedFile );
338 // Configure Repository (usually done within archiva.xml configuration)
339 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
340 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
342 // Configure Connector (usually done within archiva.xml configuration)
343 saveConnector( ID_DEFAULT_MANAGED, "badproxied1" );
344 saveConnector( ID_DEFAULT_MANAGED, "badproxied2" );
346 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
347 wagonMock.get( path, tmpFile );
349 wagonMockControl.setMatcher(customWagonGetMatcher);
350 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
352 wagonMock.get( path, tmpFile );
354 wagonMockControl.setMatcher(customWagonGetMatcher);
355 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
357 wagonMockControl.replay();
359 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
361 assertNotDownloaded( downloadedFile );
363 wagonMockControl.verify();
364 assertNoTempFiles( expectedFile );
366 // TODO: do not want failures to present as a not found [MRM-492]
367 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
370 public void testGetFromLegacyProxyAlreadyPresentInManaged_NewerThanRemote()
373 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
374 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
375 setupTestableManagedRepository( path );
377 File expectedFile = new File( managedDefaultDir, path );
378 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
380 // Set the managed file to be newer than remote.
381 setManagedNewerThanRemote( expectedFile, remoteFile );
382 long expectedTimestamp = expectedFile.lastModified();
384 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
386 assertTrue( expectedFile.exists() );
388 // Configure Connector (usually done within archiva.xml configuration)
389 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
391 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
393 assertNotDownloaded( downloadedFile );
394 assertNotModified( expectedFile, expectedTimestamp );
395 assertNoTempFiles( expectedFile );
398 public void testGetFromLegacyProxyAlreadyPresentInManaged_OlderThanRemote()
401 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
402 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
403 setupTestableManagedRepository( path );
405 File expectedFile = new File( managedDefaultDir, path );
406 File remoteFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
408 // Set the managed file to be older than remote.
409 setManagedOlderThanRemote( expectedFile, remoteFile );
411 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
413 assertTrue( expectedFile.exists() );
415 // Configure Connector (usually done within archiva.xml configuration)
416 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
418 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
420 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
421 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
422 assertNoTempFiles( expectedFile );
425 public void testGetFromLegacyProxyNotPresentInManaged()
428 String legacyPath = "org.apache.maven.test/jars/example-lib-2.2.jar";
429 String path = "org/apache/maven/test/example-lib/2.2/example-lib-2.2.jar";
430 setupTestableManagedRepository( path );
432 File expectedFile = new File( managedDefaultDir, path );
433 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
435 assertNotExistsInManagedDefaultRepo( expectedFile );
437 // Configure Connector (usually done within archiva.xml configuration)
438 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
440 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
442 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
443 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
444 assertNoTempFiles( expectedFile );
447 public void testGetFromLegacyProxyPluginNotPresentInManaged()
450 String legacyPath = "org.apache.maven.test/maven-plugins/example-maven-plugin-0.42.jar";
451 String path = "org/apache/maven/test/example-maven-plugin/0.42/example-maven-plugin-0.42.jar";
452 setupTestableManagedRepository( path );
454 File expectedFile = new File( managedDefaultDir, path );
455 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
457 assertNotExistsInManagedDefaultRepo( expectedFile );
459 // Configure Connector (usually done within archiva.xml configuration)
460 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED );
462 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
464 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
465 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
466 assertNoTempFiles( expectedFile );