]> source.dussan.org Git - archiva.git/blob
5afb561e37160aa1aaa57838b11ff4f6e67e9bf4
[archiva.git] /
1 package org.apache.archiva.proxy;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
34
35 import javax.inject.Inject;
36 import java.io.File;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNotNull;
43
44 /**
45  * CacheFailuresTransferTest
46  */
47 public class CacheFailuresTransferTest
48     extends AbstractProxyTestCase
49 {
50     // TODO: test some hard failures (eg TransferFailedException)
51     // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
52
53     @Inject
54     UrlFailureCache urlFailureCache;
55
56     @Test
57     public void testGetWithCacheFailuresOn( )
58         throws Exception
59     {
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 );
63
64         assertNotExistsInManagedDefaultRepo( expectedFile );
65
66         BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
67         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
68
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" );
72
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 );
78
79         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
80
81         EasyMock.expectLastCall( ).andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
82
83
84         wagonMockControl.replay( );
85
86         //noinspection UnusedAssignment
87         StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
88
89         wagonMockControl.verify( );
90
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( );
96
97         assertNotDownloaded( downloadedFile );
98         assertNoTempFiles( expectedFile );
99     }
100
101     @Test
102     public void testGetWithCacheFailuresOff( )
103         throws Exception
104     {
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 );
108
109         assertNotExistsInManagedDefaultRepo( expectedFile );
110
111         BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
112         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
113
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" );
117
118
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 );
124
125         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
126         EasyMock.expectLastCall( ).andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
127
128         wagonMockControl.replay( );
129
130         StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
131
132         wagonMockControl.verify( );
133
134         // Second attempt to download same artifact DOES NOT use cache
135         wagonMockControl.reset( );
136
137         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
138         EasyMock.expectLastCall( ).andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
139
140         wagonMockControl.replay( );
141
142         downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
143
144         wagonMockControl.verify( );
145
146         assertNotDownloaded( downloadedFile );
147         assertNoTempFiles( expectedFile );
148     }
149
150     @Test
151     public void testGetWhenInBothProxiedButFirstCacheFailure( )
152         throws Exception
153     {
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 );
159
160         Files.deleteIfExists( expectedFile );
161         assertFalse( Files.exists( expectedFile ) );
162
163         String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
164
165         // Intentionally set failure on url in proxied1 (for test)
166         UrlFailureCache failurlCache = lookupUrlFailureCache( );
167         failurlCache.cacheFailure( url );
168
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 );
174
175         StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository( ), artifact );
176
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 );
182     }
183
184     protected UrlFailureCache lookupUrlFailureCache( )
185         throws Exception
186     {
187         assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
188         return urlFailureCache;
189     }
190 }