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.BaseRepositoryContentLayout;
30 import org.apache.archiva.repository.storage.StorageAsset;
31 import org.apache.maven.wagon.ResourceDoesNotExistException;
32 import org.easymock.EasyMock;
33 import org.junit.Test;
35 import javax.inject.Inject;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNotNull;
45 * 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 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
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 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
112 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
114 // Configure Repository (usually done within archiva.xml configuration)
115 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
116 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "http://bad.machine.com/anotherrepo/", "default" );
119 // Configure Connector (usually done within archiva.xml configuration)
120 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
121 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
122 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
123 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
125 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
126 EasyMock.expectLastCall( ).andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
128 wagonMockControl.replay( );
130 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
132 wagonMockControl.verify( );
134 // Second attempt to download same artifact DOES NOT use cache
135 wagonMockControl.reset( );
137 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
138 EasyMock.expectLastCall( ).andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
140 wagonMockControl.replay( );
142 downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
144 wagonMockControl.verify( );
146 assertNotDownloaded( downloadedFile );
147 assertNoTempFiles( expectedFile );
151 public void testGetWhenInBothProxiedButFirstCacheFailure( )
154 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
155 setupTestableManagedRepository( path );
156 Path expectedFile = managedDefaultDir.resolve( path );
157 BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
158 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
160 Files.deleteIfExists( expectedFile );
161 assertFalse( Files.exists( expectedFile ) );
163 String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
165 // Intentionally set failure on url in proxied1 (for test)
166 UrlFailureCache failurlCache = lookupUrlFailureCache( );
167 failurlCache.cacheFailure( url );
169 // Configure Connector (usually done within archiva.xml configuration)
170 saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
171 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
172 saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
173 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
175 StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
177 // Validate that file actually came from proxied2 (as intended).
178 Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
179 assertNotNull( downloadedFile );
180 assertFileEquals( expectedFile, downloadedFile.getFilePath( ), proxied2File );
181 assertNoTempFiles( expectedFile );
184 protected UrlFailureCache lookupUrlFailureCache( )
187 assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
188 return urlFailureCache;