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 java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import javax.inject.Inject;
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" );
115 // Configure Connector (usually done within archiva.xml configuration)
116 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
117 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
118 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
119 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
121 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
122 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
124 wagonMockControl.replay();
126 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
128 wagonMockControl.verify();
130 // Second attempt to download same artifact DOES NOT use cache
131 wagonMockControl.reset();
133 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
134 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
136 wagonMockControl.replay();
138 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
140 wagonMockControl.verify();
142 assertNotDownloaded( downloadedFile );
143 assertNoTempFiles( expectedFile );
147 public void testGetWhenInBothProxiedButFirstCacheFailure()
150 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
151 setupTestableManagedRepository( path );
152 Path expectedFile = managedDefaultDir.resolve(path );
153 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
155 Files.deleteIfExists(expectedFile);
156 assertFalse( Files.exists(expectedFile) );
158 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
160 // Intentionally set failure on url in proxied1 (for test)
161 UrlFailureCache failurlCache = lookupUrlFailureCache();
162 failurlCache.cacheFailure( url );
164 // Configure Connector (usually done within archiva.xml configuration)
165 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
166 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
167 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
168 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
170 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
172 // Validate that file actually came from proxied2 (as intended).
173 Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
174 assertFileEquals( expectedFile, downloadedFile, proxied2File );
175 assertNoTempFiles( expectedFile );
178 protected UrlFailureCache lookupUrlFailureCache()
181 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
182 return urlFailureCache;