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