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;
30 import org.junit.Test;
32 import javax.inject.Inject;
36 * CacheFailuresTransferTest
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)
47 public void testGetWithCacheFailuresOn()
50 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
51 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
52 setupTestableManagedRepository( path );
54 assertNotExistsInManagedDefaultRepo( expectedFile );
56 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
58 // Configure Repository (usually done within archiva.xml configuration)
59 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
60 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
62 // Configure Connector (usually done within archiva.xml configuration)
63 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
64 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
65 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
66 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
68 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
70 wagonMockControl.setMatcher( customWagonGetMatcher );
72 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
74 wagonMockControl.replay();
76 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
78 wagonMockControl.verify();
80 // Second attempt to download same artifact use cache
81 wagonMockControl.reset();
82 wagonMockControl.replay();
83 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
84 wagonMockControl.verify();
86 assertNotDownloaded( downloadedFile );
87 assertNoTempFiles( expectedFile );
91 public void testGetWithCacheFailuresOff()
94 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
95 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
96 setupTestableManagedRepository( path );
98 assertNotExistsInManagedDefaultRepo( expectedFile );
100 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
102 // Configure Repository (usually done within archiva.xml configuration)
103 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
104 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
106 // Configure Connector (usually done within archiva.xml configuration)
107 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
108 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
109 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
110 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
112 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
114 wagonMockControl.setMatcher( customWagonGetMatcher );
115 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
117 wagonMockControl.replay();
119 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
121 wagonMockControl.verify();
123 // Second attempt to download same artifact DOES NOT use cache
124 wagonMockControl.reset();
125 wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
127 wagonMockControl.setMatcher( customWagonGetMatcher );
128 wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
129 wagonMockControl.replay();
131 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
133 wagonMockControl.verify();
135 assertNotDownloaded( downloadedFile );
136 assertNoTempFiles( expectedFile );
140 public void testGetWhenInBothProxiedButFirstCacheFailure()
143 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
144 setupTestableManagedRepository( path );
145 File expectedFile = new File( managedDefaultDir, path );
146 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
148 expectedFile.delete();
149 assertFalse( expectedFile.exists() );
151 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
153 // Intentionally set failure on url in proxied1 (for test)
154 UrlFailureCache failurlCache = lookupUrlFailureCache();
155 failurlCache.cacheFailure( url );
157 // Configure Connector (usually done within archiva.xml configuration)
158 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
159 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
160 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
161 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
163 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
165 // Validate that file actually came from proxied2 (as intended).
166 File proxied2File = new File( REPOPATH_PROXIED2, path );
167 assertFileEquals( expectedFile, downloadedFile, proxied2File );
168 assertNoTempFiles( expectedFile );
172 UrlFailureCache urlFailureCache;
174 protected UrlFailureCache lookupUrlFailureCache()
177 //UrlFailureCache urlFailureCache = (UrlFailureCache) lookup( "urlFailureCache" );
178 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
179 return urlFailureCache;