]> source.dussan.org Git - archiva.git/blob
0061fa732c7347328d7ff78d2f970daae917a738
[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 javax.inject.Inject;
34 import java.io.File;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
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
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( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
123         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
124
125         wagonMockControl.replay();
126
127         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
128
129         wagonMockControl.verify();
130
131         // Second attempt to download same artifact DOES NOT use cache
132         wagonMockControl.reset();
133
134         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ));
135         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "resource does not exist." ) ).times( 2 );
136
137         wagonMockControl.replay();
138
139         downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
140
141         wagonMockControl.verify();
142
143         assertNotDownloaded( downloadedFile );
144         assertNoTempFiles( expectedFile );
145     }
146
147     @Test
148     public void testGetWhenInBothProxiedButFirstCacheFailure()
149         throws Exception
150     {
151         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
152         setupTestableManagedRepository( path );
153         Path expectedFile = managedDefaultDir.resolve(path );
154         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
155
156         Files.deleteIfExists(expectedFile);
157         assertFalse( Files.exists(expectedFile) );
158
159         String url = PathUtil.toUrl( REPOPATH_PROXIED1 + "/" + path );
160
161         // Intentionally set failure on url in proxied1 (for test)
162         UrlFailureCache failurlCache = lookupUrlFailureCache();
163         failurlCache.cacheFailure( url );
164
165         // Configure Connector (usually done within archiva.xml configuration)
166         saveConnector( ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
167                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
168         saveConnector( ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
169                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false );
170
171         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
172
173         // Validate that file actually came from proxied2 (as intended).
174         Path proxied2File = Paths.get( REPOPATH_PROXIED2, path );
175         assertFileEquals( expectedFile, downloadedFile, proxied2File );
176         assertNoTempFiles( expectedFile );
177     }
178
179     protected UrlFailureCache lookupUrlFailureCache()
180         throws Exception
181     {
182         assertNotNull( "URL Failure Cache cannot be null.", urlFailureCache );
183         return urlFailureCache;
184     }
185 }