]> source.dussan.org Git - archiva.git/blob
1c436287cb757f3fb0ba6741e6c8682b7dbf0688
[archiva.git] /
1 package org.apache.maven.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.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
26 import org.apache.maven.archiva.policies.ChecksumPolicy;
27 import org.apache.maven.archiva.policies.ReleasesPolicy;
28 import org.apache.maven.archiva.policies.SnapshotsPolicy;
29 import org.apache.maven.wagon.TransferFailedException;
30
31 import java.io.File;
32
33 /**
34  * ManagedDefaultTransferTest
35  *
36  * @author Brett Porter
37  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38  * @version $Id$
39  */
40 public class ManagedDefaultTransferTest
41     extends AbstractProxyTestCase
42 {
43     public void testGetDefaultLayoutNotPresent()
44         throws Exception
45     {
46         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
47         setupTestableManagedRepository( path );
48
49         File expectedFile = new File( managedDefaultDir, path );
50         ArtifactReference artifact = createArtifactReference( "default", path );
51
52         // Ensure file isn't present first.
53         expectedFile.delete();
54         assertFalse( expectedFile.exists() );
55
56         // Configure Connector (usually done within archiva.xml configuration)
57         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
58                        CachedFailuresPolicy.IGNORED );
59
60         // Attempt the proxy fetch.
61         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
62
63         File sourceFile = new File( REPOPATH_PROXIED1, path );
64         assertFileEquals( expectedFile, downloadedFile, sourceFile );
65         assertNoTempFiles( expectedFile );
66     }
67
68     /**
69      * The attempt here should result in no file being transferred.
70      * <p/>
71      * The file exists locally, and the policy is ONCE.
72      *
73      * @throws Exception
74      */
75     public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
76         throws Exception
77     {
78         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
79         setupTestableManagedRepository( path );
80
81         File expectedFile = new File( managedDefaultDir, path );
82
83         ArtifactReference artifact = createArtifactReference( "default", path );
84
85         assertTrue( expectedFile.exists() );
86
87         // Configure Connector (usually done within archiva.xml configuration)
88         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
89                        CachedFailuresPolicy.IGNORED );
90
91         // Attempt the proxy fetch.
92         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
93
94         assertFileEquals( expectedFile, downloadedFile, expectedFile );
95         assertNoTempFiles( expectedFile );
96     }
97
98     /**
99      * The attempt here should result in file being transferred.
100      * <p/>
101      * The file exists locally, and the policy is IGNORE.
102      *
103      * @throws Exception
104      */
105     public void testGetDefaultLayoutAlreadyPresentPolicyIgnored()
106         throws Exception
107     {
108         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
109         setupTestableManagedRepository( path );
110
111         File expectedFile = new File( managedDefaultDir, path );
112
113         long originalModificationTime = expectedFile.lastModified();
114         ArtifactReference artifact = createArtifactReference( "default", path );
115
116         assertTrue( expectedFile.exists() );
117
118         // Configure Connector (usually done within archiva.xml configuration)
119         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
120                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
121
122         // Attempt the proxy fetch.
123         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
124
125         File proxiedFile = new File( REPOPATH_PROXIED1, path );
126         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
127
128         long proxiedLastModified = proxiedFile.lastModified();
129         long downloadedLastModified = downloadedFile.lastModified();
130         assertFalse( "Check file timestamp is not that of proxy:", proxiedLastModified == downloadedLastModified );
131
132         if ( originalModificationTime != downloadedLastModified )
133         {
134             /* On some systems the timestamp functions are not accurate enough.
135              * This delta is the amount of milliseconds of 'fudge factor' we allow for
136              * the unit test to still be considered 'passed'.
137              */
138             int delta = 20000;
139
140             long hirange = originalModificationTime + ( delta / 2 );
141             long lorange = originalModificationTime - ( delta / 2 );
142
143             if ( ( downloadedLastModified < lorange ) || ( downloadedLastModified > hirange ) )
144             {
145                 fail( "Check file timestamp is that of original managed file: expected within range lo:<" + lorange +
146                     "> hi:<" + hirange + "> but was:<" + downloadedLastModified + ">" );
147             }
148         }
149         assertNoTempFiles( expectedFile );
150     }
151
152     /**
153      * The attempt here should result in file being transferred.
154      * <p/>
155      * The file exists locally, is over 6 years old, and the policy is DAILY.
156      *
157      * @throws Exception
158      */
159     public void testGetDefaultLayoutRemoteUpdate()
160         throws Exception
161     {
162         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
163         setupTestableManagedRepository( path );
164
165         File expectedFile = new File( managedDefaultDir, path );
166         ArtifactReference artifact = createArtifactReference( "default", path );
167
168         assertTrue( expectedFile.exists() );
169         expectedFile.setLastModified( getPastDate().getTime() );
170
171         // Configure Connector (usually done within archiva.xml configuration)
172         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
173                        CachedFailuresPolicy.IGNORED );
174
175         // Attempt the proxy fetch.
176         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
177
178         File proxiedFile = new File( REPOPATH_PROXIED1, path );
179         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
180         assertNoTempFiles( expectedFile );
181     }
182
183     public void testGetWhenInBothProxiedRepos()
184         throws Exception
185     {
186         String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
187         setupTestableManagedRepository( path );
188
189         File expectedFile = new File( managedDefaultDir, path );
190         ArtifactReference artifact = createArtifactReference( "default", path );
191
192         expectedFile.delete();
193         assertFalse( expectedFile.exists() );
194
195         // Configure Connector (usually done within archiva.xml configuration)
196         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
197                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
198         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
199                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
200
201         // Attempt the proxy fetch.
202         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
203
204         File proxied1File = new File( REPOPATH_PROXIED1, path );
205         File proxied2File = new File( REPOPATH_PROXIED2, path );
206         assertFileEquals( expectedFile, downloadedFile, proxied1File );
207         assertNoTempFiles( expectedFile );
208
209         // TODO: is this check even needed if it passes above? 
210         String actualContents = FileUtils.readFileToString( downloadedFile, null );
211         String badContents = FileUtils.readFileToString( proxied2File, null );
212         assertFalse( "Downloaded file contents should not be that of proxy 2",
213                      StringUtils.equals( actualContents, badContents ) );
214     }
215
216     public void testGetInSecondProxiedRepo()
217         throws Exception
218     {
219         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
220         setupTestableManagedRepository( path );
221
222         File expectedFile = new File( managedDefaultDir, path );
223         ArtifactReference artifact = createArtifactReference( "default", path );
224
225         expectedFile.delete();
226         assertFalse( expectedFile.exists() );
227
228         // Configure Connector (usually done within archiva.xml configuration)
229         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
230                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
231         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
232                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
233
234         // Attempt the proxy fetch.
235         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
236
237         File proxied2File = new File( REPOPATH_PROXIED2, path );
238         assertFileEquals( expectedFile, downloadedFile, proxied2File );
239         assertNoTempFiles( expectedFile );
240     }
241
242     public void testNotFoundInAnyProxies()
243         throws Exception
244     {
245         String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
246         setupTestableManagedRepository( path );
247
248         File expectedFile = new File( managedDefaultDir, path );
249         ArtifactReference artifact = createArtifactReference( "default", path );
250
251         assertFalse( expectedFile.exists() );
252
253         // Configure Connector (usually done within archiva.xml configuration)
254         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
255                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
256         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
257                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
258         saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
259                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
260
261         // Attempt the proxy fetch.
262         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
263
264         assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
265                     downloadedFile );
266         assertNoTempFiles( expectedFile );
267     }
268
269     public void testGetInSecondProxiedRepoFirstFails()
270         throws Exception
271     {
272         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
273         setupTestableManagedRepository( path );
274
275         File expectedFile = new File( managedDefaultDir, path );
276         ArtifactReference artifact = createArtifactReference( "default", path );
277
278         expectedFile.delete();
279         assertFalse( expectedFile.exists() );
280
281         // Configure Repository (usually done within archiva.xml configuration)
282         saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
283
284         wagonMock.getIfNewer( path, new File( expectedFile.getAbsolutePath() + ".tmp" ), 0 );
285         wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
286         wagonMockControl.replay();
287
288         // Configure Connector (usually done within archiva.xml configuration)
289         saveConnector( ID_DEFAULT_MANAGED, "badproxied", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
290                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
291         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
292                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
293
294         // Attempt the proxy fetch.
295         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
296
297         wagonMockControl.verify();
298
299         File proxied2File = new File( REPOPATH_PROXIED2, path );
300         assertFileEquals( expectedFile, downloadedFile, proxied2File );
301         assertNoTempFiles( expectedFile );
302     }
303
304     public void testGetAllRepositoriesFail()
305         throws Exception
306     {
307         String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
308         setupTestableManagedRepository( path );
309
310         File expectedFile = new File( managedDefaultDir, path );
311         ArtifactReference artifact = createArtifactReference( "default", path );
312
313         expectedFile.delete();
314         assertFalse( expectedFile.exists() );
315
316         // Configure Repository (usually done within archiva.xml configuration)
317         saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
318         saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
319
320         // Configure Connector (usually done within archiva.xml configuration)
321         saveConnector( ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
322                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
323         saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.IGNORED,
324                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
325
326         wagonMock.getIfNewer( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ), 0 );
327         wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
328
329         wagonMock.getIfNewer( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ), 0 );
330         wagonMockControl.setThrowable( new TransferFailedException( "transfer failed" ) );
331
332         wagonMockControl.replay();
333
334         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
335
336         assertNotDownloaded( downloadedFile );
337
338         wagonMockControl.verify();
339         assertNoTempFiles( expectedFile );
340
341         // TODO: do not want failures to present as a not found [MRM-492]
342         // TODO: How much information on each failure should we pass back to the user vs. logging in the proxy? 
343     }
344
345     public void testLegacyProxyRepoGetAlreadyPresent()
346         throws Exception
347     {
348         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
349         setupTestableManagedRepository( path );
350
351         File expectedFile = new File( managedDefaultDir, path );
352         ArtifactReference artifact = createArtifactReference( "default", path );
353
354         assertTrue( expectedFile.exists() );
355
356         // Configure Connector (usually done within archiva.xml configuration)
357         saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
358                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
359
360         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
361
362         File proxiedFile =
363             new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-present-1.0.jar" );
364         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
365         assertNoTempFiles( expectedFile );
366     }
367
368     public void testLegacyRequestConvertedToDefaultPathInManagedRepo()
369         throws Exception
370     {
371         // Check that a Maven1 legacy request is translated to a maven2 path in
372         // the managed repository.
373
374         String legacyPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
375         String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
376         setupTestableManagedRepository( path );
377
378         File expectedFile = new File( managedDefaultDir, path );
379         ArtifactReference artifact = createArtifactReference( "default", path );
380
381         expectedFile.delete();
382         assertFalse( expectedFile.exists() );
383
384         // Configure Connector (usually done within archiva.xml configuration)
385         saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
386                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
387
388         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
389
390         File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, legacyPath );
391         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
392         assertNoTempFiles( expectedFile );
393     }
394
395     public void testLegacyProxyRepoGetNotPresent()
396         throws Exception
397     {
398         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
399         setupTestableManagedRepository( path );
400
401         File expectedFile = new File( managedDefaultDir, path );
402         ArtifactReference artifact = createArtifactReference( "default", path );
403
404         expectedFile.delete();
405         assertFalse( expectedFile.exists() );
406
407         // Configure Connector (usually done within archiva.xml configuration)
408         saveConnector( ID_DEFAULT_MANAGED, ID_LEGACY_PROXIED, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
409                        SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
410
411         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
412
413         File proxiedFile = new File( REPOPATH_PROXIED_LEGACY, "org.apache.maven.test/jars/get-default-layout-1.0.jar" );
414         assertFileEquals( expectedFile, downloadedFile, proxiedFile );
415         assertNoTempFiles( expectedFile );
416
417         // TODO: timestamp preservation requires support for that in wagon
418         //    assertEquals( "Check file timestamp", proxiedFile.lastModified(), file.lastModified() );
419     }
420 }