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