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.archiva.repository.storage.StorageAsset;
30 import org.apache.maven.wagon.ResourceDoesNotExistException;
31 import org.easymock.EasyMock;
32 import org.junit.Test;
34 import javax.inject.Inject;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull;
44 * CacheFailuresTransferTest
48 public class CacheFailuresTransferTest
49 extends AbstractProxyTestCase
51 // TODO: test some hard failures (eg TransferFailedException)
52 // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
55 UrlFailureCache urlFailureCache;
58 public void testGetWithCacheFailuresOn()
61 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
62 Path expectedFile = managedDefaultDir.resolve( path );
63 setupTestableManagedRepository( path );
65 assertNotExistsInManagedDefaultRepo( expectedFile );
67 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
69 // Configure Repository (usually done within archiva.xml configuration)
70 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
71 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://bad.machine.com/anotherrepo/", "default" );
73 // Configure Connector (usually done within archiva.xml configuration)
74 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
75 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
76 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
77 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
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 //noinspection UnusedAssignment
87 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
89 wagonMockControl.verify();
91 // Second attempt to download same artifact use cache
92 wagonMockControl.reset();
93 wagonMockControl.replay();
94 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
95 wagonMockControl.verify();
97 assertNotDownloaded( downloadedFile);
98 assertNoTempFiles( expectedFile );
102 public void testGetWithCacheFailuresOff()
105 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
106 Path expectedFile = managedDefaultDir.resolve( path );
107 setupTestableManagedRepository( path );
109 assertNotExistsInManagedDefaultRepo( expectedFile );
111 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
113 // Configure Repository (usually done within archiva.xml configuration)
114 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
115 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://bad.machine.com/anotherrepo/", "default" );
118 // Configure Connector (usually done within archiva.xml configuration)
119 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
120 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
121 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
122 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
124 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
125 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
127 wagonMockControl.replay();
129 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
131 wagonMockControl.verify();
133 // Second attempt to download same artifact DOES NOT use cache
134 wagonMockControl.reset();
136 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
137 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
139 wagonMockControl.replay();
141 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
143 wagonMockControl.verify();
145 assertNotDownloaded( downloadedFile);
146 assertNoTempFiles( expectedFile );
150 public void testGetWhenInBothProxiedButFirstCacheFailure()
153 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
154 setupTestableManagedRepository( path );
155 Path expectedFile = managedDefaultDir.resolve(path );
156 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
158 Files.deleteIfExists(expectedFile);
159 assertFalse( Files.exists(expectedFile) );
161 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
163 // Intentionally set failure on url in proxied1 (for test)
164 UrlFailureCache failurlCache = lookupUrlFailureCache();
165 failurlCache.cacheFailure( url );
167 // Configure Connector (usually done within archiva.xml configuration)
168 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
169 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
170 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
171 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
173 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
175 // Validate that file actually came from proxied2 (as intended).
176 Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
177 assertNotNull(downloadedFile);
178 assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxied2File );
179 assertNoTempFiles( expectedFile );
182 protected UrlFailureCache lookupUrlFailureCache()
185 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
186 return urlFailureCache;