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