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.model.ArtifactReference;
23  import org.apache.archiva.policies.CachedFailuresPolicy;
24  import org.apache.archiva.policies.ChecksumPolicy;
25  import org.apache.archiva.policies.ReleasesPolicy;
26  import org.apache.archiva.policies.SnapshotsPolicy;
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.lang.StringUtils;
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.charset.Charset;
35  
36  import static org.junit.Assert.*;
37  
38  /**
39   * ManagedDefaultTransferTest
40   */
41  public class ManagedDefaultTransferTest
42      extends AbstractProxyTestCase
43  {
44      @Test
45      public void testGetDefaultLayoutNotPresentConnectorOffline()
46          throws Exception
47      {
48          String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
49          setupTestableManagedRepository( path );
50  
51          File expectedFile = new File( managedDefaultDir, path );
52          ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
53  
54          // Ensure file isn't present first.
55          assertNotExistsInManagedDefaultRepo( expectedFile );
56  
57          // Configure Connector (usually done within archiva.xml configuration)
58          saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
59                         CachedFailuresPolicy.NO, true );
60  
61          // Attempt the proxy fetch.
62          File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63          assertNull( "File should not have been downloaded", downloadedFile );
64      }
65  
66      @Test
67      public void testGetDefaultLayoutNotPresent()
68          throws Exception
69      {
70          String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
71          setupTestableManagedRepository( path );
72  
73          File expectedFile = new File( managedDefaultDir, path );
74          ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
75  
76          // Ensure file isn't present first.
77          assertNotExistsInManagedDefaultRepo( expectedFile );
78  
79          // Configure Connector (usually done within archiva.xml configuration)
80          saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
81                         CachedFailuresPolicy.NO, false );
82  
83          // Attempt the proxy fetch.
84          File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
85  
86          File sourceFile = new File( REPOPATH_PROXIED1, path );
87          assertFileEquals( expectedFile, downloadedFile, sourceFile );
88          assertNoTempFiles( expectedFile );
89      }
90  
91      @Test
92      public void testGetDefaultLayoutNotPresentPassthrough()
93          throws Exception
94      {
95          String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
96          setupTestableManagedRepository( path );
97  
98          File expectedFile = new File( managedDefaultDir, path );
99  
100         // Ensure file isn't present first.
101         assertNotExistsInManagedDefaultRepo( expectedFile );
102 
103         // Configure Connector (usually done within archiva.xml configuration)
104         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
105                        CachedFailuresPolicy.NO, false );
106 
107         // Attempt the proxy fetch.
108         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
109 
110         File sourceFile = new File( REPOPATH_PROXIED1, path );
111         assertFileEquals( expectedFile, downloadedFile, sourceFile );
112         assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
113         assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
114         assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
115         assertNoTempFiles( expectedFile );
116     }
117 
118     /**
119      * The attempt here should result in no file being transferred.
120      * <p/>
121      * The file exists locally, and the policy is ONCE.
122      *
123      * @throws Exception
124      */
125     @Test
126     public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
127         throws Exception
128     {
129         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
130         setupTestableManagedRepository( path );
131 
132         File expectedFile = new File( managedDefaultDir, path );
133 
134         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
135 
136         assertTrue( expectedFile.exists() );
137 
138         // Configure Connector (usually done within archiva.xml configuration)
139         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
140                        CachedFailuresPolicy.NO, false );
141 
142         // Attempt the proxy fetch.
143         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
144 
145         assertFileEquals( expectedFile, downloadedFile, expectedFile );
146         assertNoTempFiles( expectedFile );
147     }
148 
149     /**
150      * The attempt here should result in no file being transferred.
151      * <p/>
152      * The file exists locally, and the policy is ONCE.
153      *
154      * @throws Exception
155      */
156     @Test
157     public void testGetDefaultLayoutAlreadyPresentPassthrough()
158         throws Exception
159     {
160         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
161         setupTestableManagedRepository( path );
162 
163         File expectedFile = new File( managedDefaultDir, path );
164         File remoteFile = new File( REPOPATH_PROXIED1, path );
165 
166         assertTrue( expectedFile.exists() );
167 
168         // Set the managed File to be newer than local.
169         setManagedOlderThanRemote( expectedFile, remoteFile );
170         long originalModificationTime = expectedFile.lastModified();
171 
172         // Configure Connector (usually done within archiva.xml configuration)
173         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
174                        CachedFailuresPolicy.NO, false );
175 
176         // Attempt the proxy fetch.
177         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
178 
179         assertNotDownloaded( downloadedFile );
180         assertNotModified( expectedFile, originalModificationTime );
181         assertNoTempFiles( expectedFile );
182     }
183 
184     /**
185      * <p>
186      * Request a file, that exists locally, and remotely.
187      * </p>
188      * <p>
189      * All policies are set to IGNORE.
190      * </p>
191      * <p>
192      * Managed file is newer than remote file.
193      * </p>
194      * <p>
195      * Transfer should not have occured, as managed file is newer.
196      * </p>
197      *
198      * @throws Exception
199      */
200     @Test
201     public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
202         throws Exception
203     {
204         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
205         setupTestableManagedRepository( path );
206 
207         File expectedFile = new File( managedDefaultDir, path );
208         File remoteFile = new File( REPOPATH_PROXIED1, path );
209 
210         // Set the managed File to be newer than local.
211         setManagedNewerThanRemote( expectedFile, remoteFile );
212 
213         long originalModificationTime = expectedFile.lastModified();
214         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
215 
216         assertTrue( expectedFile.exists() );
217 
218         // Configure Connector (usually done within archiva.xml configuration)
219         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
220                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
221 
222         // Attempt the proxy fetch.
223         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
224 
225         assertNotDownloaded( downloadedFile );
226         assertNotModified( expectedFile, originalModificationTime );
227         assertNoTempFiles( expectedFile );
228     }
229 
230     /**
231      * <p>
232      * Request a file, that exists locally, and remotely.
233      * </p>
234      * <p>
235      * All policies are set to IGNORE.
236      * </p>
237      * <p>
238      * Managed file is older than Remote file.
239      * </p>
240      * <p>
241      * Transfer should have occured, as managed file is older than remote.
242      * </p>
243      *
244      * @throws Exception
245      */
246     @Test
247     public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
248         throws Exception
249     {
250         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
251         setupTestableManagedRepository( path );
252 
253         File expectedFile = new File( managedDefaultDir, path );
254         File remoteFile = new File( REPOPATH_PROXIED1, path );
255 
256         // Set the managed file to be newer than remote file.
257         setManagedOlderThanRemote( expectedFile, remoteFile );
258 
259         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
260 
261         assertTrue( expectedFile.exists() );
262 
263         // Configure Connector (usually done within archiva.xml configuration)
264         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
265                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
266 
267         // Attempt the proxy fetch.
268         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
269 
270         File proxiedFile = new File( REPOPATH_PROXIED1, path );
271         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
272         assertNoTempFiles( expectedFile );
273     }
274 
275     /**
276      * The attempt here should result in file being transferred.
277      * <p/>
278      * The file exists locally, is over 6 years old, and the policy is DAILY.
279      *
280      * @throws Exception
281      */
282     @Test
283     public void testGetDefaultLayoutRemoteUpdate()
284         throws Exception
285     {
286         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
287         setupTestableManagedRepository( path );
288 
289         File expectedFile = new File( managedDefaultDir, path );
290         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
291 
292         assertTrue( expectedFile.exists() );
293         expectedFile.setLastModified( getPastDate().getTime() );
294 
295         // Configure Connector (usually done within archiva.xml configuration)
296         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
297                        CachedFailuresPolicy.NO, false );
298 
299         // Attempt the proxy fetch.
300         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
301 
302         File proxiedFile = new File( REPOPATH_PROXIED1, path );
303         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
304         assertNoTempFiles( expectedFile );
305     }
306 
307     @Test
308     public void testGetWhenInBothProxiedRepos()
309         throws Exception
310     {
311         String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
312         setupTestableManagedRepository( path );
313 
314         File expectedFile = new File( managedDefaultDir, path );
315         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
316 
317         assertNotExistsInManagedDefaultRepo( expectedFile );
318 
319         // Configure Connector (usually done within archiva.xml configuration)
320         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
321         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
322 
323         // Attempt the proxy fetch.
324         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
325 
326         File proxied1File = new File( REPOPATH_PROXIED1, path );
327         File proxied2File = new File( REPOPATH_PROXIED2, path );
328         assertFileEquals( expectedFile, downloadedFile, proxied1File );
329         assertNoTempFiles( expectedFile );
330 
331         // TODO: is this check even needed if it passes above? 
332         String actualContents = FileUtils.readFileToString( downloadedFile, Charset.defaultCharset() );
333         String badContents = FileUtils.readFileToString( proxied2File, Charset.defaultCharset() );
334         assertFalse( "Downloaded file contents should not be that of proxy 2",
335                      StringUtils.equals( actualContents, badContents ) );
336     }
337 
338     @Test
339     public void testGetInSecondProxiedRepo()
340         throws Exception
341     {
342         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
343         setupTestableManagedRepository( path );
344 
345         File expectedFile = new File( managedDefaultDir, path );
346         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
347 
348         assertNotExistsInManagedDefaultRepo( expectedFile );
349 
350         // Configure Connector (usually done within archiva.xml configuration)
351         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
352         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
353 
354         // Attempt the proxy fetch.
355         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
356 
357         File proxied2File = new File( REPOPATH_PROXIED2, path );
358         assertFileEquals( expectedFile, downloadedFile, proxied2File );
359         assertNoTempFiles( expectedFile );
360     }
361 
362     @Test
363     public void testNotFoundInAnyProxies()
364         throws Exception
365     {
366         String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
367         setupTestableManagedRepository( path );
368 
369         File expectedFile = new File( managedDefaultDir, path );
370         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
371 
372         assertNotExistsInManagedDefaultRepo( expectedFile );
373 
374         // Configure Connector (usually done within archiva.xml configuration)
375         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
376         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
377 
378         // Attempt the proxy fetch.
379         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
380 
381         assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
382                     downloadedFile );
383         assertNoTempFiles( expectedFile );
384     }
385 
386     @Test
387     public void testGetInSecondProxiedRepoFirstFails()
388         throws Exception
389     {
390         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
391         setupTestableManagedRepository( path );
392 
393         File expectedFile = new File( managedDefaultDir, path );
394         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
395 
396         assertNotExistsInManagedDefaultRepo( expectedFile );
397 
398         // Configure Repository (usually done within archiva.xml configuration)
399         saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
400 
401         wagonMock.get( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
402         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "transfer failed" )  );
403         wagonMockControl.replay();
404 
405         // Configure Connector (usually done within archiva.xml configuration)
406         saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
407         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
408 
409         // Attempt the proxy fetch.
410         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
411 
412         wagonMockControl.verify();
413 
414         File proxied2File = new File( REPOPATH_PROXIED2, path );
415         assertFileEquals( expectedFile, downloadedFile, proxied2File );
416         assertNoTempFiles( expectedFile );
417     }
418 
419     @Test
420     public void testGetAllRepositoriesFail()
421         throws Exception
422     {
423         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
424         setupTestableManagedRepository( path );
425 
426         File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
427         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
428 
429         assertNotExistsInManagedDefaultRepo( expectedFile );
430 
431         // Configure Repository (usually done within archiva.xml configuration)
432         saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
433         saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
434 
435         // Configure Connector (usually done within archiva.xml configuration)
436         saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
437         saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
438 
439         File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
440 
441         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
442         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
443 
444         wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
445         EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
446 
447         wagonMockControl.replay();
448 
449         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
450 
451         assertNotDownloaded( downloadedFile );
452 
453         wagonMockControl.verify();
454         assertNoTempFiles( expectedFile );
455 
456         // TODO: do not want failures to present as a not found [MRM-492]
457         // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy? 
458     }
459 
460 
461 }