]> source.dussan.org Git - archiva.git/blob
4b96766c7f05e5934af057c0805b19ce17c0e056
[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.maven.archiva.policies.CachedFailuresPolicy;
23 import org.apache.maven.archiva.policies.ChecksumPolicy;
24 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
25 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
26 import org.apache.maven.archiva.policies.ProxyDownloadException;
27 import org.apache.maven.archiva.policies.ReleasesPolicy;
28 import org.apache.maven.archiva.policies.SnapshotsPolicy;
29 import org.apache.maven.archiva.repository.layout.LayoutException;
30 import org.apache.maven.wagon.ResourceDoesNotExistException;
31 import org.apache.maven.wagon.TransferFailedException;
32 import org.apache.maven.wagon.authorization.AuthorizationException;
33
34 import java.io.File;
35
36 /**
37  * ErrorHandlingTest
38  *
39  * @author Brett Porter
40  * @version $Id$
41  */
42 public class ErrorHandlingTest
43     extends AbstractProxyTestCase
44 {
45     private static final String PATH_IN_BOTH_REMOTES_NOT_LOCAL =
46         "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
47
48     private static final String PATH_IN_BOTH_REMOTES_AND_LOCAL =
49         "org/apache/maven/test/get-on-multiple-repos/1.0/get-on-multiple-repos-1.0.pom";
50
51     private static final String ID_MOCKED_PROXIED1 = "badproxied1";
52
53     private static final String NAME_MOCKED_PROXIED1 = "Bad Proxied 1";
54
55     private static final String ID_MOCKED_PROXIED2 = "badproxied2";
56
57     private static final String NAME_MOCKED_PROXIED2 = "Bad Proxied 2";
58
59     public void testPropagateErrorImmediatelyWithErrorThenSuccess()
60         throws Exception
61     {
62         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
63         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
64
65         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP );
66         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
67
68         simulateGetError( path, expectedFile, createTransferException() );
69
70         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
71     }
72
73     public void testPropagateErrorImmediatelyWithNotFoundThenError()
74         throws Exception
75     {
76         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
77         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
78
79         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP );
80         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP );
81
82         simulateGetError( path, expectedFile, createResourceNotFoundException() );
83
84         simulateGetError( path, expectedFile, createTransferException() );
85
86         confirmSingleFailure( path, ID_MOCKED_PROXIED2 );
87     }
88
89     public void testPropagateErrorImmediatelyWithSuccessThenError()
90         throws Exception
91     {
92         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
93         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
94
95         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
96
97         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP );
98
99         confirmSuccess( path, expectedFile, REPOPATH_PROXIED1 );
100     }
101
102     public void testPropagateErrorImmediatelyWithNotFoundThenSuccess()
103         throws Exception
104     {
105         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
106         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
107
108         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP );
109
110         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
111
112         simulateGetError( path, expectedFile, createResourceNotFoundException() );
113
114         confirmSuccess( path, expectedFile, REPOPATH_PROXIED2 );
115     }
116
117     public void testPropagateErrorAtEndWithErrorThenSuccess()
118         throws Exception
119     {
120         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
121         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
122
123         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP );
124
125         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
126
127         simulateGetError( path, expectedFile, createTransferException() );
128
129         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
130     }
131
132     public void testPropagateErrorAtEndWithSuccessThenError()
133         throws Exception
134     {
135         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
136         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
137
138         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
139
140         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE );
141
142         confirmSuccess( path, expectedFile, REPOPATH_PROXIED1 );
143     }
144
145     public void testPropagateErrorAtEndWithNotFoundThenError()
146         throws Exception
147     {
148         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
149         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
150
151         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE );
152
153         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE );
154
155         simulateGetError( path, expectedFile, createResourceNotFoundException() );
156
157         simulateGetError( path, expectedFile, createTransferException() );
158
159         confirmSingleFailure( path, ID_MOCKED_PROXIED2 );
160     }
161
162     public void testPropagateErrorAtEndWithErrorThenNotFound()
163         throws Exception
164     {
165         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
166         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
167
168         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE );
169
170         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE );
171
172         simulateGetError( path, expectedFile, createTransferException() );
173
174         simulateGetError( path, expectedFile, createResourceNotFoundException() );
175
176         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
177     }
178
179     public void testPropagateErrorAtEndWithErrorThenError()
180         throws Exception
181     {
182         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
183         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
184
185         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE );
186
187         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE );
188
189         simulateGetError( path, expectedFile, createTransferException() );
190
191         simulateGetError( path, expectedFile, createTransferException() );
192
193         confirmFailures( path, new String[]{ID_MOCKED_PROXIED1, ID_MOCKED_PROXIED2} );
194     }
195
196     public void testPropagateErrorAtEndWithNotFoundThenSuccess()
197         throws Exception
198     {
199         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
200         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
201
202         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE );
203
204         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
205
206         simulateGetError( path, expectedFile, createResourceNotFoundException() );
207
208         confirmSuccess( path, expectedFile, REPOPATH_PROXIED2 );
209     }
210
211     public void testIgnoreErrorWithErrorThenSuccess()
212         throws Exception
213     {
214         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
215         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
216
217         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE );
218
219         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2 );
220
221         simulateGetError( path, expectedFile, createTransferException() );
222
223         confirmSuccess( path, expectedFile, REPOPATH_PROXIED2 );
224     }
225
226     public void testIgnoreErrorWithSuccessThenError()
227         throws Exception
228     {
229         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
230         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
231
232         saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1 );
233
234         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE );
235
236         confirmSuccess( path, expectedFile, REPOPATH_PROXIED1 );
237     }
238
239     public void testIgnoreErrorWithNotFoundThenError()
240         throws Exception
241     {
242         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
243         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
244
245         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE );
246
247         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE );
248
249         simulateGetError( path, expectedFile, createResourceNotFoundException() );
250
251         simulateGetError( path, expectedFile, createTransferException() );
252
253         confirmNotDownloadedNoError( path );
254     }
255
256     public void testIgnoreErrorWithErrorThenNotFound()
257         throws Exception
258     {
259         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
260         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
261
262         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE );
263
264         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE );
265
266         simulateGetError( path, expectedFile, createTransferException() );
267
268         simulateGetError( path, expectedFile, createResourceNotFoundException() );
269
270         confirmNotDownloadedNoError( path );
271     }
272
273     public void testIgnoreErrorWithErrorThenError()
274         throws Exception
275     {
276         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
277         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
278
279         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE );
280
281         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE );
282
283         simulateGetError( path, expectedFile, createTransferException() );
284
285         simulateGetError( path, expectedFile, createTransferException() );
286
287         confirmNotDownloadedNoError( path );
288     }
289
290     public void testPropagateOnUpdateAlwaysArtifactNotPresent()
291         throws Exception
292     {
293         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
294         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
295
296         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP,
297                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
298         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP,
299                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
300
301         simulateGetError( path, expectedFile, createTransferException() );
302
303         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
304     }
305
306     public void testPropagateOnUpdateAlwaysArtifactPresent()
307         throws Exception
308     {
309         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
310         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
311
312         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP,
313                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
314         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP,
315                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
316
317         simulateGetIfNewerError( path, expectedFile, createTransferException() );
318
319         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
320     }
321
322     public void testPropagateOnUpdateAlwaysQueueArtifactNotPresent()
323         throws Exception
324     {
325         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
326         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
327
328         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE,
329                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
330         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE,
331                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
332
333         simulateGetError( path, expectedFile, createTransferException() );
334         simulateGetError( path, expectedFile, createTransferException() );
335
336         confirmFailures( path, new String[] { ID_MOCKED_PROXIED1, ID_MOCKED_PROXIED2 } );
337     }
338
339     public void testPropagateOnUpdateAlwaysQueueArtifactPresent()
340         throws Exception
341     {
342         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
343         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
344
345         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE,
346                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
347         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE,
348                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
349
350         simulateGetIfNewerError( path, expectedFile, createTransferException() );
351         simulateGetIfNewerError( path, expectedFile, createTransferException() );
352
353         confirmFailures( path, new String[] { ID_MOCKED_PROXIED1, ID_MOCKED_PROXIED2 } );
354     }
355
356     public void testPropagateOnUpdateAlwaysIgnoreArtifactNotPresent()
357         throws Exception
358     {
359         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
360         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
361
362         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE,
363                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
364         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE,
365                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
366
367         simulateGetError( path, expectedFile, createTransferException() );
368         simulateGetError( path, expectedFile, createTransferException() );
369
370         confirmNotDownloadedNoError( path );
371     }
372
373     public void testPropagateOnUpdateAlwaysIgnoreArtifactPresent()
374         throws Exception
375     {
376         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
377         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
378
379         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE,
380                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
381         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE,
382                                     PropagateErrorsOnUpdateDownloadPolicy.ALWAYS );
383
384         simulateGetIfNewerError( path, expectedFile, createTransferException() );
385         simulateGetIfNewerError( path, expectedFile, createTransferException() );
386
387         confirmNotDownloadedNoError( path );
388         assertTrue( expectedFile.exists() );
389     }
390
391     public void testPropagateOnUpdateNotPresentArtifactNotPresent()
392         throws Exception
393     {
394         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
395         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
396
397         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP,
398                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
399         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP,
400                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
401
402         simulateGetError( path, expectedFile, createTransferException() );
403
404         confirmSingleFailure( path, ID_MOCKED_PROXIED1 );
405     }
406
407     public void testPropagateOnUpdateNotPresentArtifactPresent()
408         throws Exception
409     {
410         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
411         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
412
413         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.STOP,
414                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
415         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.STOP,
416                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
417
418         simulateGetIfNewerError( path, expectedFile, createTransferException() );
419
420         confirmNotDownloadedNoError( path );
421         assertTrue( expectedFile.exists() );
422     }
423
424     public void testPropagateOnUpdateNotPresentQueueArtifactNotPresent()
425         throws Exception
426     {
427         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
428         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
429
430         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE,
431                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
432         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE,
433                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
434
435         simulateGetError( path, expectedFile, createTransferException() );
436         simulateGetError( path, expectedFile, createTransferException() );
437
438         confirmFailures( path, new String[] { ID_MOCKED_PROXIED1, ID_MOCKED_PROXIED2 } );
439     }
440
441     public void testPropagateOnUpdateNotPresentQueueArtifactPresent()
442         throws Exception
443     {
444         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
445         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
446
447         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.QUEUE,
448                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
449         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.QUEUE,
450                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
451
452         simulateGetIfNewerError( path, expectedFile, createTransferException() );
453         simulateGetIfNewerError( path, expectedFile, createTransferException() );
454
455         confirmNotDownloadedNoError( path );
456         assertTrue( expectedFile.exists() );
457     }
458
459     public void testPropagateOnUpdateNotPresentIgnoreArtifactNotPresent()
460         throws Exception
461     {
462         String path = PATH_IN_BOTH_REMOTES_NOT_LOCAL;
463         File expectedFile = setupRepositoriesWithLocalFileNotPresent( path );
464
465         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE,
466                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
467         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE,
468                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
469
470         simulateGetError( path, expectedFile, createTransferException() );
471         simulateGetError( path, expectedFile, createTransferException() );
472
473         confirmNotDownloadedNoError( path );
474     }
475
476     public void testPropagateOnUpdateNotPresentIgnoreArtifactPresent()
477         throws Exception
478     {
479         String path = PATH_IN_BOTH_REMOTES_AND_LOCAL;
480         File expectedFile = setupRepositoriesWithLocalFilePresent( path );
481
482         createMockedProxyConnector( ID_MOCKED_PROXIED1, NAME_MOCKED_PROXIED1, PropagateErrorsDownloadPolicy.IGNORE,
483                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
484         createMockedProxyConnector( ID_MOCKED_PROXIED2, NAME_MOCKED_PROXIED2, PropagateErrorsDownloadPolicy.IGNORE,
485                                     PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
486
487         simulateGetIfNewerError( path, expectedFile, createTransferException() );
488         simulateGetIfNewerError( path, expectedFile, createTransferException() );
489
490         confirmNotDownloadedNoError( path );
491         assertTrue( expectedFile.exists() );
492     }
493
494     // ------------------------------------------
495     // HELPER METHODS
496     // ------------------------------------------
497
498     private void createMockedProxyConnector( String id, String name, String errorPolicy )
499     {
500         saveRemoteRepositoryConfig( id, name, "test://bad.machine.com/repo/", "default" );
501         saveConnector( ID_DEFAULT_MANAGED, id, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
502                        CachedFailuresPolicy.NO, errorPolicy );
503     }
504
505     private void createMockedProxyConnector( String id, String name, String errorPolicy, String errorOnUpdatePolicy )
506     {
507         saveRemoteRepositoryConfig( id, name, "test://bad.machine.com/repo/", "default" );
508         saveConnector( ID_DEFAULT_MANAGED, id, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
509                        CachedFailuresPolicy.NO, errorPolicy, errorOnUpdatePolicy );
510     }
511
512     private File setupRepositoriesWithLocalFileNotPresent( String path )
513         throws Exception
514     {
515         setupTestableManagedRepository( path );
516
517         File file = new File( managedDefaultDir, path );
518
519         assertNotExistsInManagedDefaultRepo( file );
520
521         return file;
522     }
523
524     private File setupRepositoriesWithLocalFilePresent( String path )
525         throws Exception
526     {
527         setupTestableManagedRepository( path );
528
529         File file = new File( managedDefaultDir, path );
530
531         assertTrue( file.exists() );
532
533         return file;
534     }
535
536     private void simulateGetError( String path, File expectedFile, Exception throwable )
537         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
538     {
539         wagonMock.get( path, createExpectedTempFile( expectedFile ) );
540         wagonMockControl.setMatcher(customWagonGetMatcher);
541         wagonMockControl.setThrowable( throwable, 1 );
542     }
543
544     private void simulateGetIfNewerError( String path, File expectedFile, TransferFailedException exception )
545         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
546     {
547         wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() );
548         wagonMockControl.setMatcher(customWagonGetIfNewerMatcher);
549         wagonMockControl.setThrowable( exception, 1 );
550     }
551
552     private File createExpectedTempFile( File expectedFile )
553     {
554         return new File( managedDefaultDir, expectedFile.getName() + ".tmp" ).getAbsoluteFile();
555     }
556
557     private void confirmSingleFailure( String path, String id )
558         throws LayoutException
559     {
560         confirmFailures( path, new String[]{id} );
561     }
562
563     private void confirmFailures( String path, String[] ids )
564         throws LayoutException
565     {
566         wagonMockControl.replay();
567
568         // Attempt the proxy fetch.
569         File downloadedFile = null;
570         try
571         {
572             downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository,
573                                                             managedDefaultRepository.toArtifactReference( path ) );
574             fail( "Proxy should not have succeeded" );
575         }
576         catch ( ProxyDownloadException e )
577         {
578             assertEquals( ids.length, e.getFailures().size() );
579             for ( String id : ids )
580             {
581                 assertTrue( e.getFailures().keySet().contains( id ) );
582             }
583         }
584
585         wagonMockControl.verify();
586
587         assertNotDownloaded( downloadedFile );
588     }
589
590     private void confirmSuccess( String path, File expectedFile, String basedir )
591         throws Exception
592     {
593         File downloadedFile = performDownload( path );
594
595         File proxied1File = new File( basedir, path );
596         assertFileEquals( expectedFile, downloadedFile, proxied1File );
597     }
598
599     private void confirmNotDownloadedNoError( String path )
600         throws Exception
601     {
602         File downloadedFile = performDownload( path );
603
604         assertNotDownloaded( downloadedFile );
605     }
606
607     private File performDownload( String path )
608         throws ProxyDownloadException, LayoutException
609     {
610         wagonMockControl.replay();
611
612         // Attempt the proxy fetch.
613         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository,
614                                                              managedDefaultRepository.toArtifactReference( path ) );
615
616         wagonMockControl.verify();
617         return downloadedFile;
618     }
619
620     private static TransferFailedException createTransferException()
621     {
622         return new TransferFailedException( "test download exception" );
623     }
624
625     private static ResourceDoesNotExistException createResourceNotFoundException()
626     {
627         return new ResourceDoesNotExistException( "test download not found" );
628     }
629 }