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