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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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 = createArtifactReference( "default", 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", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
290 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
291 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
292 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
294 // Attempt the proxy fetch.
295 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
297 wagonMockControl.verify();
299 File proxied2File = new File( REPOPATH_PROXIED2, path );
300 assertFileEquals( expectedFile, downloadedFile, proxied2File );
301 assertNoTempFiles( expectedFile );
304 public void testGetAllRepositoriesFail()
307 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
308 setupTestableManagedRepository( path );
310 File expectedFile = new File( managedDefaultDir, path );
311 ArtifactReference artifact = createArtifactReference( "default", path );
313 expectedFile.delete();
314 assertFalse( expectedFile.exists() );
316 // Configure Repository (usually done within archiva.xml configuration)
317 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
318 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
320 // Configure Connector (usually done within archiva.xml configuration)
321 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
322 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
323 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
324 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
326 wagonMock.getIfNewer( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ), 0 );
327 wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
329 wagonMock.getIfNewer( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ), 0 );
330 wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
332 wagonMockControl.replay();
334 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
336 assertNotDownloaded( downloadedFile );
338 wagonMockControl.verify();
339 assertNoTempFiles( expectedFile );
341 // TODO: do not want failures to present as a not found [MRM-492]
342 // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy?
345 public void testLegacyProxyRepoGetAlreadyPresent()
348 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
349 setupTestableManagedRepository( path );
351 File expectedFile = new File( managedDefaultDir, path );
352 ArtifactReference artifact = createArtifactReference( "default", path );
354 assertTrue( expectedFile.exists() );
356 // Configure Connector (usually done within archiva.xml configuration)
357 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
358 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
360 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
363 new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-present-1.0.jar" );
364 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
365 assertNoTempFiles( expectedFile );
368 public void testLegacyRequestConvertedToDefaultPathInManagedRepo()
371 // Check that a Maven1 legacy request is translated to a maven2 path in
372 // the managed repository.
374 String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
375 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
376 setupTestableManagedRepository( path );
378 File expectedFile = new File( managedDefaultDir, path );
379 ArtifactReference artifact = createArtifactReference( "default", path );
381 expectedFile.delete();
382 assertFalse( expectedFile.exists() );
384 // Configure Connector (usually done within archiva.xml configuration)
385 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
386 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
388 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
390 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
391 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
392 assertNoTempFiles( expectedFile );
395 public void testLegacyProxyRepoGetNotPresent()
398 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
399 setupTestableManagedRepository( path );
401 File expectedFile = new File( managedDefaultDir, path );
402 ArtifactReference artifact = createArtifactReference( "default", path );
404 expectedFile.delete();
405 assertFalse( expectedFile.exists() );
407 // Configure Connector (usually done within archiva.xml configuration)
408 saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
409 SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
411 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
413 File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-1.0.jar" );
414 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
415 assertNoTempFiles( expectedFile );
417 // TODO: timestamp preservation requires support for that in wagon
418 // assertEquals( "Check file timestamp", proxiedFile.lastModified(), file.lastModified() );