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.maven.archiva.common.utils.PathUtil;
23 import org.apache.maven.archiva.model.ArtifactReference;
24 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
25 import org.apache.maven.archiva.policies.ChecksumPolicy;
26 import org.apache.maven.archiva.policies.ReleasesPolicy;
27 import org.apache.maven.archiva.policies.SnapshotsPolicy;
28 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
34 * CacheFailuresTransferTest
36 * @author Brett Porter
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
40 public class CacheFailuresTransferTest
41 extends AbstractProxyTestCase
43 // TODO: test some hard failures (eg TransferFailedException)
44 // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
46 public void testGetWithCacheFailuresOn()
49 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
50 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
51 setupTestableManagedRepository( path );
53 assertNotExistsInManagedDefaultRepo( expectedFile );
55 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
57 // Configure Repository (usually done within archiva.xml configuration)
58 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
59 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
61 // Configure Connector (usually done within archiva.xml configuration)
62 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
63 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
64 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
65 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
67 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
68 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
70 wagonMockControl.replay();
72 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
74 wagonMockControl.verify();
76 // Second attempt to download same artifact use cache
77 wagonMockControl.reset();
78 wagonMockControl.replay();
79 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
80 wagonMockControl.verify();
82 assertNotDownloaded( downloadedFile );
83 assertNoTempFiles( expectedFile );
86 public void testGetWithCacheFailuresOff()
89 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
90 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
91 setupTestableManagedRepository( path );
93 assertNotExistsInManagedDefaultRepo( expectedFile );
95 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
97 // Configure Repository (usually done within archiva.xml configuration)
98 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
99 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
101 // Configure Connector (usually done within archiva.xml configuration)
102 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
103 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
104 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
105 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
107 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
108 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
110 wagonMockControl.replay();
112 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
114 wagonMockControl.verify();
116 // Second attempt to download same artifact DOES NOT use cache
117 wagonMockControl.reset();
118 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
119 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
120 wagonMockControl.replay();
122 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
124 wagonMockControl.verify();
126 assertNotDownloaded( downloadedFile );
127 assertNoTempFiles( expectedFile );
130 public void testGetWhenInBothProxiedButFirstCacheFailure()
133 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
134 File expectedFile = new File( managedDefaultDir, path );
135 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
137 expectedFile.delete();
138 assertFalse( expectedFile.exists() );
140 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
142 // Intentionally set failure on url in proxied1 (for test)
143 UrlFailureCache failurlCache = lookupUrlFailureCache();
144 failurlCache.cacheFailure( url );
146 // Configure Connector (usually done within archiva.xml configuration)
147 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
148 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
149 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
150 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
152 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
154 // Validate that file actually came from proxied2 (as intended).
155 File proxied2File = new File( REPOPATH_PROXIED2, path );
156 assertFileEquals( expectedFile, downloadedFile, proxied2File );
157 assertNoTempFiles( expectedFile );
160 protected UrlFailureCache lookupUrlFailureCache()
163 UrlFailureCache urlFailureCache = (UrlFailureCache) lookup( "urlFailureCache" );
164 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
165 return urlFailureCache;