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;
33 import javax.inject.Inject;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertNotNull;
43 * CacheFailuresTransferTest
47 public class CacheFailuresTransferTest
48 extends AbstractProxyTestCase
50 // TODO: test some hard failures (eg TransferFailedException)
51 // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
54 UrlFailureCache urlFailureCache;
57 public void testGetWithCacheFailuresOn()
60 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
61 Path expectedFile = managedDefaultDir.resolve( path );
62 setupTestableManagedRepository( path );
64 assertNotExistsInManagedDefaultRepo( expectedFile );
66 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
68 // Configure Repository (usually done within archiva.xml configuration)
69 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
70 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
72 // Configure Connector (usually done within archiva.xml configuration)
73 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
74 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
75 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
76 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
78 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
80 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
83 wagonMockControl.replay();
85 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
87 wagonMockControl.verify();
89 // Second attempt to download same artifact use cache
90 wagonMockControl.reset();
91 wagonMockControl.replay();
92 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
93 wagonMockControl.verify();
95 assertNotDownloaded( downloadedFile);
96 assertNoTempFiles( expectedFile );
100 public void testGetWithCacheFailuresOff()
103 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
104 Path expectedFile = managedDefaultDir.resolve( path );
105 setupTestableManagedRepository( path );
107 assertNotExistsInManagedDefaultRepo( expectedFile );
109 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
111 // Configure Repository (usually done within archiva.xml configuration)
112 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
113 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( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
123 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
125 wagonMockControl.replay();
127 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
129 wagonMockControl.verify();
131 // Second attempt to download same artifact DOES NOT use cache
132 wagonMockControl.reset();
134 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
135 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
137 wagonMockControl.replay();
139 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
141 wagonMockControl.verify();
143 assertNotDownloaded( downloadedFile );
144 assertNoTempFiles( expectedFile );
148 public void testGetWhenInBothProxiedButFirstCacheFailure()
151 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
152 setupTestableManagedRepository( path );
153 Path expectedFile = managedDefaultDir.resolve(path );
154 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
156 Files.deleteIfExists(expectedFile);
157 assertFalse( Files.exists(expectedFile) );
159 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
161 // Intentionally set failure on url in proxied1 (for test)
162 UrlFailureCache failurlCache = lookupUrlFailureCache();
163 failurlCache.cacheFailure( url );
165 // Configure Connector (usually done within archiva.xml configuration)
166 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
167 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
168 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
169 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
171 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
173 // Validate that file actually came from proxied2 (as intended).
174 Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
175 assertFileEquals( expectedFile, downloadedFile, proxied2File );
176 assertNoTempFiles( expectedFile );
179 protected UrlFailureCache lookupUrlFailureCache()
182 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
183 return urlFailureCache;