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