]> source.dussan.org Git - archiva.git/blob
1b7933489a0f6bea8718ab86020832fefff149c8
[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.maven.wagon.ResourceDoesNotExistException;
30 import org.easymock.EasyMock;
31 import org.junit.Test;
32
33 import java.io.File;
34 import javax.inject.Inject;
35
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertNotNull;
38
39 /**
40  * CacheFailuresTransferTest
41  *
42  *
43  */
44 public class CacheFailuresTransferTest
45     extends AbstractProxyTestCase
46 {
47     // TODO: test some hard failures (eg TransferFailedException)
48     // TODO: test the various combinations of fetchFrom* (note: need only test when caching is enabled)
49
50     @Inject
51     UrlFailureCache urlFailureCache;
52
53     @Test
54     public void testGetWithCacheFailuresOn()
55         throws Exception
56     {
57         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
58         File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
59         setupTestableManagedRepository( path );
60
61         assertNotExistsInManagedDefaultRepo( expectedFile );
62
63         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
64
65         // Configure Repository (usually done within archiva.xml configuration)
66         saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
67         saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
68
69         // Configure Connector (usually done within archiva.xml configuration)
70         saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
71                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
72         saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
73                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
74
75         //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
76         //wagonMockControl.setMatcher( customWagonGetMatcher );
77         //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
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         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
87
88         wagonMockControl.verify();
89
90         // Second attempt to download same artifact use cache
91         wagonMockControl.reset();
92         wagonMockControl.replay();
93         downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
94         wagonMockControl.verify();
95
96         assertNotDownloaded( downloadedFile );
97         assertNoTempFiles( expectedFile );
98     }
99
100     @Test
101     public void testGetWithCacheFailuresOff()
102         throws Exception
103     {
104         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
105         File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
106         setupTestableManagedRepository( path );
107
108         assertNotExistsInManagedDefaultRepo( expectedFile );
109
110         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
111
112         // Configure Repository (usually done within archiva.xml configuration)
113         saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
114         saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default" );
115
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 );
121
122         //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
123         //wagonMockControl.setMatcher( customWagonGetMatcher );
124         //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
125
126         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
127         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
128
129         wagonMockControl.replay();
130
131         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
132
133         wagonMockControl.verify();
134
135         // Second attempt to download same artifact DOES NOT use cache
136         wagonMockControl.reset();
137
138
139         //wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
140         //wagonMockControl.setMatcher( customWagonGetMatcher );
141         //wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
142
143         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
144         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
145
146         wagonMockControl.replay();
147
148         downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
149
150         wagonMockControl.verify();
151
152         assertNotDownloaded( downloadedFile );
153         assertNoTempFiles( expectedFile );
154     }
155
156     @Test
157     public void testGetWhenInBothProxiedButFirstCacheFailure()
158         throws Exception
159     {
160         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
161         setupTestableManagedRepository( path );
162         File expectedFile = new File( managedDefaultDir, path );
163         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
164
165         expectedFile.delete();
166         assertFalse( expectedFile.exists() );
167
168         String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
169
170         // Intentionally set failure on url in proxied1 (for test)
171         UrlFailureCache failurlCache = lookupUrlFailureCache();
172         failurlCache.cacheFailure( url );
173
174         // Configure Connector (usually done within archiva.xml configuration)
175         saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
176                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
177         saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
178                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
179
180         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
181
182         // Validate that file actually came from proxied2 (as intended).
183         File proxied2File = new File( REPOPATH_PROXIED2, path );
184         assertFileEquals( expectedFile, downloadedFile, proxied2File );
185         assertNoTempFiles( expectedFile );
186     }
187
188     protected UrlFailureCache lookupUrlFailureCache()
189         throws Exception
190     {
191         assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
192         return urlFailureCache;
193     }
194 }