1 package org.apache.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.archiva.common.utils.PathUtil;
23 import org.apache.archiva.model.ArtifactReference;
24 import org.apache.archiva.policies.CachedFailuresPolicy;
25 import org.apache.archiva.policies.ChecksumPolicy;
26 import org.apache.archiva.policies.ReleasesPolicy;
27 import org.apache.archiva.policies.SnapshotsPolicy;
28 import org.apache.archiva.policies.urlcache.UrlFailureCache;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
30 import org.easymock.EasyMock;
31 import org.junit.Test;
34 import javax.inject.Inject;
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertNotNull;
40 * CacheFailuresTransferTest
44 public class CacheFailuresTransferTest
45 extends AbstractProxyTestCase
47 // TODO: test some hard failures (eg TransferFailedException)
48 // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
51 UrlFailureCache urlFailureCache;
54 public void testGetWithCacheFailuresOn()
57 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
58 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
59 setupTestableManagedRepository( path );
61 assertNotExistsInManagedDefaultRepo( expectedFile );
63 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
65 // Configure Repository (usually done within archiva.xml configuration)
66 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
67 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
69 // Configure Connector (usually done within archiva.xml configuration)
70 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
71 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
72 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
73 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
75 //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
76 //wagonMockControl.setMatcher( customWagonGetMatcher );
77 //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
79 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
81 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
84 wagonMockControl.replay();
86 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
88 wagonMockControl.verify();
90 // Second attempt to download same artifact use cache
91 wagonMockControl.reset();
92 wagonMockControl.replay();
93 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
94 wagonMockControl.verify();
96 assertNotDownloaded( downloadedFile );
97 assertNoTempFiles( expectedFile );
101 public void testGetWithCacheFailuresOff()
104 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
105 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
106 setupTestableManagedRepository( path );
108 assertNotExistsInManagedDefaultRepo( expectedFile );
110 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
112 // Configure Repository (usually done within archiva.xml configuration)
113 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
114 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
116 // Configure Connector (usually done within archiva.xml configuration)
117 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
118 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
119 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
120 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
122 //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
123 //wagonMockControl.setMatcher( customWagonGetMatcher );
124 //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
126 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
127 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
129 wagonMockControl.replay();
131 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
133 wagonMockControl.verify();
135 // Second attempt to download same artifact DOES NOT use cache
136 wagonMockControl.reset();
139 //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
140 //wagonMockControl.setMatcher( customWagonGetMatcher );
141 //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
143 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
144 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
146 wagonMockControl.replay();
148 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
150 wagonMockControl.verify();
152 assertNotDownloaded( downloadedFile );
153 assertNoTempFiles( expectedFile );
157 public void testGetWhenInBothProxiedButFirstCacheFailure()
160 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
161 setupTestableManagedRepository( path );
162 File expectedFile = new File( managedDefaultDir, path );
163 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
165 expectedFile.delete();
166 assertFalse( expectedFile.exists() );
168 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
170 // Intentionally set failure on url in proxied1 (for test)
171 UrlFailureCache failurlCache = lookupUrlFailureCache();
172 failurlCache.cacheFailure( url );
174 // Configure Connector (usually done within archiva.xml configuration)
175 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
176 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
177 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
178 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
180 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
182 // Validate that file actually came from proxied2 (as intended).
183 File proxied2File = new File( REPOPATH_PROXIED2, path );
184 assertFileEquals( expectedFile, downloadedFile, proxied2File );
185 assertNoTempFiles( expectedFile );
188 protected UrlFailureCache lookupUrlFailureCache()
191 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
192 return urlFailureCache;