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