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