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