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.TransferFailedException;
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 expectedFile.delete();
54 assertFalse( expectedFile.exists() );
56 // Configure Connector (usually done within archiva.xml configuration)
57 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
58 CachedFailuresPolicy.IGNORED );
60 // Attempt the proxy fetch.
61 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63 File sourceFile = new File( REPOPATH_PROXIED1, path );
64 assertFileEquals( expectedFile, downloadedFile, sourceFile );
65 assertNoTempFiles( expectedFile );
69 * The attempt here should result in no file being transferred.
71 * The file exists locally, and the policy is ONCE.
75 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
78 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
79 setupTestableManagedRepository( path );
81 File expectedFile = new File( managedDefaultDir, path );
83 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
85 assertTrue( expectedFile.exists() );
87 // Configure Connector (usually done within archiva.xml configuration)
88 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
89 CachedFailuresPolicy.IGNORED );
91 // Attempt the proxy fetch.
92 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
94 assertFileEquals( expectedFile, downloadedFile, expectedFile );
95 assertNoTempFiles( expectedFile );
99 * The attempt here should result in file being transferred.
101 * The file exists locally, and the policy is IGNORE.
105 public void testGetDefaultLayoutAlreadyPresentPolicyIgnored()
108 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
109 setupTestableManagedRepository( path );
111 File expectedFile = new File( managedDefaultDir, path );
113 long originalModificationTime = expectedFile.lastModified();
114 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
116 assertTrue( expectedFile.exists() );
118 // Configure Connector (usually done within archiva.xml configuration)
119 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
120 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
122 // Attempt the proxy fetch.
123 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
125 File proxiedFile = new File( REPOPATH_PROXIED1, path );
126 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
128 long proxiedLastModified = proxiedFile.lastModified();
129 long downloadedLastModified = downloadedFile.lastModified();
130 assertFalse( "Check file timestamp is not that of proxy:", proxiedLastModified == downloadedLastModified );
132 if ( originalModificationTime != downloadedLastModified )
134 /* On some systems the timestamp functions are not accurate enough.
135 * This delta is the amount of milliseconds of 'fudge factor' we allow for
136 * the unit test to still be considered 'passed'.
140 long hirange = originalModificationTime + ( delta / 2 );
141 long lorange = originalModificationTime - ( delta / 2 );
143 if ( ( downloadedLastModified < lorange ) || ( downloadedLastModified > hirange ) )
145 fail( "Check file timestamp is that of original managed file: expected within range lo:<" + lorange +
146 "> hi:<" + hirange + "> but was:<" + downloadedLastModified + ">" );
149 assertNoTempFiles( expectedFile );
153 * The attempt here should result in file being transferred.
155 * The file exists locally, is over 6 years old, and the policy is DAILY.
159 public void testGetDefaultLayoutRemoteUpdate()
162 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
163 setupTestableManagedRepository( path );
165 File expectedFile = new File( managedDefaultDir, path );
166 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
168 assertTrue( expectedFile.exists() );
169 expectedFile.setLastModified( getPastDate().getTime() );
171 // Configure Connector (usually done within archiva.xml configuration)
172 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
173 CachedFailuresPolicy.IGNORED );
175 // Attempt the proxy fetch.
176 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
178 File proxiedFile = new File( REPOPATH_PROXIED1, path );
179 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
180 assertNoTempFiles( expectedFile );
183 public void testGetWhenInBothProxiedRepos()
186 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
187 setupTestableManagedRepository( path );
189 File expectedFile = new File( managedDefaultDir, path );
190 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
192 expectedFile.delete();
193 assertFalse( expectedFile.exists() );
195 // Configure Connector (usually done within archiva.xml configuration)
196 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
197 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
198 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
199 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
201 // Attempt the proxy fetch.
202 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
204 File proxied1File = new File( REPOPATH_PROXIED1, path );
205 File proxied2File = new File( REPOPATH_PROXIED2, path );
206 assertFileEquals( expectedFile, downloadedFile, proxied1File );
207 assertNoTempFiles( expectedFile );
209 // TODO: is this check even needed if it passes above?
210 String actualContents = FileUtils.readFileToString( downloadedFile, null );
211 String badContents = FileUtils.readFileToString( proxied2File, null );
212 assertFalse( "Downloaded file contents should not be that of proxy 2",
213 StringUtils.equals( actualContents, badContents ) );
216 public void testGetInSecondProxiedRepo()
219 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
220 setupTestableManagedRepository( path );
222 File expectedFile = new File( managedDefaultDir, path );
223 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
225 expectedFile.delete();
226 assertFalse( expectedFile.exists() );
228 // Configure Connector (usually done within archiva.xml configuration)
229 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
230 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
231 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
232 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
234 // Attempt the proxy fetch.
235 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
237 File proxied2File = new File( REPOPATH_PROXIED2, path );
238 assertFileEquals( expectedFile, downloadedFile, proxied2File );
239 assertNoTempFiles( expectedFile );
242 public void testNotFoundInAnyProxies()
245 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
246 setupTestableManagedRepository( path );
248 File expectedFile = new File( managedDefaultDir, path );
249 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
251 assertFalse( expectedFile.exists() );
253 // Configure Connector (usually done within archiva.xml configuration)
254 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
255 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
256 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
257 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
258 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
259 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
261 // Attempt the proxy fetch.
262 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
264 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
266 assertNoTempFiles( expectedFile );
269 public void testGetInSecondProxiedRepoFirstFails()
272 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
273 setupTestableManagedRepository( path );
275 File expectedFile = new File( managedDefaultDir, path );
276 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
278 expectedFile.delete();
279 assertFalse( expectedFile.exists() );
281 // Configure Repository (usually done within archiva.xml configuration)
282 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
284 wagonMock.getIfNewer( path, new File( expectedFile.getAbsolutePath() + ".tmp" ), 0 );
285 wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
286 wagonMockControl.replay();
288 // Configure Connector (usually done within archiva.xml configuration)
289 saveConnector( ID_DEFAULT_MANAGED, "badproxied" );
290 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
292 // Attempt the proxy fetch.
293 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
295 wagonMockControl.verify();
297 File proxied2File = new File( REPOPATH_PROXIED2, path );
298 assertFileEquals( expectedFile, downloadedFile, proxied2File );
299 assertNoTempFiles( expectedFile );
302 public void testGetAllRepositoriesFail()
305 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
306 setupTestableManagedRepository( path );
308 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
309 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
311 expectedFile.delete();
312 assertFalse( expectedFile.exists() );
314 // Configure Repository (usually done within archiva.xml configuration)
315 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
316 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
318 // Configure Connector (usually done within archiva.xml configuration)
319 saveConnector( ID_DEFAULT_MANAGED, "badproxied1" );
320 saveConnector( ID_DEFAULT_MANAGED, "badproxied2" );
322 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
323 wagonMock.getIfNewer( path, tmpFile, 0 );
324 wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
326 wagonMock.getIfNewer( path, tmpFile, 0 );
327 wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
329 wagonMockControl.replay();
331 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
333 assertNotDownloaded( downloadedFile );
335 wagonMockControl.verify();
336 assertNoTempFiles( expectedFile );
338 // TODO: do not want failures to present as a not found [MRM-492]
339 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
342 public void testLegacyProxyRepoGetAlreadyPresent()
345 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
346 setupTestableManagedRepository( path );
348 File expectedFile = new File( managedDefaultDir, path );
349 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
351 assertTrue( expectedFile.exists() );
353 // Configure Connector (usually done within archiva.xml configuration)
354 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
355 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
357 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
360 new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-present-1.0.jar" );
361 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
362 assertNoTempFiles( expectedFile );
365 public void testLegacyRequestConvertedToDefaultPathInManagedRepo()
368 // Check that a Maven1 legacy request is translated to a maven2 path in
369 // the managed repository.
371 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
372 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
373 setupTestableManagedRepository( path );
375 File expectedFile = new File( managedDefaultDir, path );
376 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
378 expectedFile.delete();
379 assertFalse( expectedFile.exists() );
381 // Configure Connector (usually done within archiva.xml configuration)
382 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
383 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
385 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
387 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
388 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
389 assertNoTempFiles( expectedFile );
393 public void testLegacyRequestPluginConvertedToDefaultPathInManagedRepo()
396 // Check that a Maven1 legacy request is translated to a maven2 path in
397 // the managed repository.
399 String legacyPath = "org.apache.maven.test/plugins/get-legacy-plugin-1.0.jar";
400 String path = "org/apache/maven/test/get-legacy-plugin/1.0/get-legacy-plugin-1.0.jar";
401 setupTestableManagedRepository( path );
403 File expectedFile = new File( managedDefaultDir, path );
404 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
406 expectedFile.delete();
407 assertFalse( expectedFile.exists() );
409 // Configure Connector (usually done within archiva.xml configuration)
410 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
411 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
413 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
415 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
416 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
417 assertNoTempFiles( expectedFile );
421 public void testLegacyProxyRepoGetNotPresent()
424 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
425 setupTestableManagedRepository( path );
427 File expectedFile = new File( managedDefaultDir, path );
428 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
430 expectedFile.delete();
431 assertFalse( expectedFile.exists() );
433 // Configure Connector (usually done within archiva.xml configuration)
434 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
435 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
437 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
439 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-1.0.jar" );
440 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
441 assertNoTempFiles( expectedFile );
443 // TODO: timestamp preservation requires support for that in wagon
444 // assertEquals( "Check file timestamp", proxiedFile.lastModified(), file.lastModified() );