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.junit.Test;
33 import javax.inject.Inject;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertNotNull;
39 * CacheFailuresTransferTest
43 public class CacheFailuresTransferTest
44 extends AbstractProxyTestCase
46 // TODO: test some hard failures (eg TransferFailedException)
47 // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
50 public void testGetWithCacheFailuresOn()
53 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
54 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
55 setupTestableManagedRepository( path );
57 assertNotExistsInManagedDefaultRepo( expectedFile );
59 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
61 // Configure Repository (usually done within archiva.xml configuration)
62 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
63 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
65 // Configure Connector (usually done within archiva.xml configuration)
66 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
67 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
68 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
69 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
71 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
73 wagonMockControl.setMatcher( customWagonGetMatcher );
75 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
77 wagonMockControl.replay();
79 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
81 wagonMockControl.verify();
83 // Second attempt to download same artifact use cache
84 wagonMockControl.reset();
85 wagonMockControl.replay();
86 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
87 wagonMockControl.verify();
89 assertNotDownloaded( downloadedFile );
90 assertNoTempFiles( expectedFile );
94 public void testGetWithCacheFailuresOff()
97 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
98 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
99 setupTestableManagedRepository( path );
101 assertNotExistsInManagedDefaultRepo( expectedFile );
103 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
105 // Configure Repository (usually done within archiva.xml configuration)
106 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
107 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
109 // Configure Connector (usually done within archiva.xml configuration)
110 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
111 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
112 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
113 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
115 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
117 wagonMockControl.setMatcher( customWagonGetMatcher );
118 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
120 wagonMockControl.replay();
122 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
124 wagonMockControl.verify();
126 // Second attempt to download same artifact DOES NOT use cache
127 wagonMockControl.reset();
128 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
130 wagonMockControl.setMatcher( customWagonGetMatcher );
131 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
132 wagonMockControl.replay();
134 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
136 wagonMockControl.verify();
138 assertNotDownloaded( downloadedFile );
139 assertNoTempFiles( expectedFile );
143 public void testGetWhenInBothProxiedButFirstCacheFailure()
146 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
147 setupTestableManagedRepository( path );
148 File expectedFile = new File( managedDefaultDir, path );
149 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
151 expectedFile.delete();
152 assertFalse( expectedFile.exists() );
154 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
156 // Intentionally set failure on url in proxied1 (for test)
157 UrlFailureCache failurlCache = lookupUrlFailureCache();
158 failurlCache.cacheFailure( url );
160 // Configure Connector (usually done within archiva.xml configuration)
161 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
162 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
163 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
164 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
166 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
168 // Validate that file actually came from proxied2 (as intended).
169 File proxied2File = new File( REPOPATH_PROXIED2, path );
170 assertFileEquals( expectedFile, downloadedFile, proxied2File );
171 assertNoTempFiles( expectedFile );
175 UrlFailureCache urlFailureCache;
177 protected UrlFailureCache lookupUrlFailureCache()
180 //UrlFailureCache urlFailureCache = (UrlFailureCache) lookup( "urlFailureCache" );
181 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
182 return urlFailureCache;