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