1 package org.apache.archiva.proxy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.model.ArtifactReference;
23 import org.apache.archiva.policies.CachedFailuresPolicy;
24 import org.apache.archiva.policies.ChecksumPolicy;
25 import org.apache.archiva.policies.ReleasesPolicy;
26 import org.apache.archiva.policies.SnapshotsPolicy;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.wagon.ResourceDoesNotExistException;
30 import org.easymock.EasyMock;
31 import org.junit.Test;
32
33 import java.io.File;
34 import java.nio.charset.Charset;
35
36 import static org.junit.Assert.*;
37
38
39
40
41 public class ManagedDefaultTransferTest
42 extends AbstractProxyTestCase
43 {
44 @Test
45 public void testGetDefaultLayoutNotPresentConnectorOffline()
46 throws Exception
47 {
48 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
49 setupTestableManagedRepository( path );
50
51 File expectedFile = new File( managedDefaultDir, path );
52 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
53
54
55 assertNotExistsInManagedDefaultRepo( expectedFile );
56
57
58 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
59 CachedFailuresPolicy.NO, true );
60
61
62 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
63 assertNull( "File should not have been downloaded", downloadedFile );
64 }
65
66 @Test
67 public void testGetDefaultLayoutNotPresent()
68 throws Exception
69 {
70 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
71 setupTestableManagedRepository( path );
72
73 File expectedFile = new File( managedDefaultDir, path );
74 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
75
76
77 assertNotExistsInManagedDefaultRepo( expectedFile );
78
79
80 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
81 CachedFailuresPolicy.NO, false );
82
83
84 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
85
86 File sourceFile = new File( REPOPATH_PROXIED1, path );
87 assertFileEquals( expectedFile, downloadedFile, sourceFile );
88 assertNoTempFiles( expectedFile );
89 }
90
91 @Test
92 public void testGetDefaultLayoutNotPresentPassthrough()
93 throws Exception
94 {
95 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar.asc";
96 setupTestableManagedRepository( path );
97
98 File expectedFile = new File( managedDefaultDir, path );
99
100
101 assertNotExistsInManagedDefaultRepo( expectedFile );
102
103
104 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
105 CachedFailuresPolicy.NO, false );
106
107
108 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
109
110 File sourceFile = new File( REPOPATH_PROXIED1, path );
111 assertFileEquals( expectedFile, downloadedFile, sourceFile );
112 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".sha1" ).exists() );
113 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".md5" ).exists() );
114 assertFalse( new File( downloadedFile.getParentFile(), downloadedFile.getName() + ".asc" ).exists() );
115 assertNoTempFiles( expectedFile );
116 }
117
118
119
120
121
122
123
124
125 @Test
126 public void testGetDefaultLayoutAlreadyPresentPolicyOnce()
127 throws Exception
128 {
129 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
130 setupTestableManagedRepository( path );
131
132 File expectedFile = new File( managedDefaultDir, path );
133
134 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
135
136 assertTrue( expectedFile.exists() );
137
138
139 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
140 CachedFailuresPolicy.NO, false );
141
142
143 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
144
145 assertFileEquals( expectedFile, downloadedFile, expectedFile );
146 assertNoTempFiles( expectedFile );
147 }
148
149
150
151
152
153
154
155
156 @Test
157 public void testGetDefaultLayoutAlreadyPresentPassthrough()
158 throws Exception
159 {
160 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.asc";
161 setupTestableManagedRepository( path );
162
163 File expectedFile = new File( managedDefaultDir, path );
164 File remoteFile = new File( REPOPATH_PROXIED1, path );
165
166 assertTrue( expectedFile.exists() );
167
168
169 setManagedOlderThanRemote( expectedFile, remoteFile );
170 long originalModificationTime = expectedFile.lastModified();
171
172
173 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ONCE, SnapshotsPolicy.ONCE,
174 CachedFailuresPolicy.NO, false );
175
176
177 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, path );
178
179 assertNotDownloaded( downloadedFile );
180 assertNotModified( expectedFile, originalModificationTime );
181 assertNoTempFiles( expectedFile );
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 @Test
201 public void testGetDefaultLayoutAlreadyPresentNewerThanRemotePolicyIgnored()
202 throws Exception
203 {
204 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
205 setupTestableManagedRepository( path );
206
207 File expectedFile = new File( managedDefaultDir, path );
208 File remoteFile = new File( REPOPATH_PROXIED1, path );
209
210
211 setManagedNewerThanRemote( expectedFile, remoteFile );
212
213 long originalModificationTime = expectedFile.lastModified();
214 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
215
216 assertTrue( expectedFile.exists() );
217
218
219 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
220 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
221
222
223 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
224
225 assertNotDownloaded( downloadedFile );
226 assertNotModified( expectedFile, originalModificationTime );
227 assertNoTempFiles( expectedFile );
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 @Test
247 public void testGetDefaultLayoutAlreadyPresentOlderThanRemotePolicyIgnored()
248 throws Exception
249 {
250 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
251 setupTestableManagedRepository( path );
252
253 File expectedFile = new File( managedDefaultDir, path );
254 File remoteFile = new File( REPOPATH_PROXIED1, path );
255
256
257 setManagedOlderThanRemote( expectedFile, remoteFile );
258
259 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
260
261 assertTrue( expectedFile.exists() );
262
263
264 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
265 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false );
266
267
268 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
269
270 File proxiedFile = new File( REPOPATH_PROXIED1, path );
271 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
272 assertNoTempFiles( expectedFile );
273 }
274
275
276
277
278
279
280
281
282 @Test
283 public void testGetDefaultLayoutRemoteUpdate()
284 throws Exception
285 {
286 String path = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
287 setupTestableManagedRepository( path );
288
289 File expectedFile = new File( managedDefaultDir, path );
290 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
291
292 assertTrue( expectedFile.exists() );
293 expectedFile.setLastModified( getPastDate().getTime() );
294
295
296 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.DAILY, SnapshotsPolicy.DAILY,
297 CachedFailuresPolicy.NO, false );
298
299
300 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
301
302 File proxiedFile = new File( REPOPATH_PROXIED1, path );
303 assertFileEquals( expectedFile, downloadedFile, proxiedFile );
304 assertNoTempFiles( expectedFile );
305 }
306
307 @Test
308 public void testGetWhenInBothProxiedRepos()
309 throws Exception
310 {
311 String path = "org/apache/maven/test/get-in-both-proxies/1.0/get-in-both-proxies-1.0.jar";
312 setupTestableManagedRepository( path );
313
314 File expectedFile = new File( managedDefaultDir, path );
315 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
316
317 assertNotExistsInManagedDefaultRepo( expectedFile );
318
319
320 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
321 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
322
323
324 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
325
326 File proxied1File = new File( REPOPATH_PROXIED1, path );
327 File proxied2File = new File( REPOPATH_PROXIED2, path );
328 assertFileEquals( expectedFile, downloadedFile, proxied1File );
329 assertNoTempFiles( expectedFile );
330
331
332 String actualContents = FileUtils.readFileToString( downloadedFile, Charset.defaultCharset() );
333 String badContents = FileUtils.readFileToString( proxied2File, Charset.defaultCharset() );
334 assertFalse( "Downloaded file contents should not be that of proxy 2",
335 StringUtils.equals( actualContents, badContents ) );
336 }
337
338 @Test
339 public void testGetInSecondProxiedRepo()
340 throws Exception
341 {
342 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
343 setupTestableManagedRepository( path );
344
345 File expectedFile = new File( managedDefaultDir, path );
346 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
347
348 assertNotExistsInManagedDefaultRepo( expectedFile );
349
350
351 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
352 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
353
354
355 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
356
357 File proxied2File = new File( REPOPATH_PROXIED2, path );
358 assertFileEquals( expectedFile, downloadedFile, proxied2File );
359 assertNoTempFiles( expectedFile );
360 }
361
362 @Test
363 public void testNotFoundInAnyProxies()
364 throws Exception
365 {
366 String path = "org/apache/maven/test/does-not-exist/1.0/does-not-exist-1.0.jar";
367 setupTestableManagedRepository( path );
368
369 File expectedFile = new File( managedDefaultDir, path );
370 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
371
372 assertNotExistsInManagedDefaultRepo( expectedFile );
373
374
375 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, false );
376 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
377
378
379 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
380
381 assertNull( "File returned was: " + downloadedFile + "; should have got a not found exception",
382 downloadedFile );
383 assertNoTempFiles( expectedFile );
384 }
385
386 @Test
387 public void testGetInSecondProxiedRepoFirstFails()
388 throws Exception
389 {
390 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
391 setupTestableManagedRepository( path );
392
393 File expectedFile = new File( managedDefaultDir, path );
394 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
395
396 assertNotExistsInManagedDefaultRepo( expectedFile );
397
398
399 saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
400
401 wagonMock.get( EasyMock.eq( path), EasyMock.anyObject( File.class ) );
402 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "transfer failed" ) );
403 wagonMockControl.replay();
404
405
406 saveConnector( ID_DEFAULT_MANAGED, "badproxied", false );
407 saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, false );
408
409
410 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
411
412 wagonMockControl.verify();
413
414 File proxied2File = new File( REPOPATH_PROXIED2, path );
415 assertFileEquals( expectedFile, downloadedFile, proxied2File );
416 assertNoTempFiles( expectedFile );
417 }
418
419 @Test
420 public void testGetAllRepositoriesFail()
421 throws Exception
422 {
423 String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
424 setupTestableManagedRepository( path );
425
426 File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), path );
427 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
428
429 assertNotExistsInManagedDefaultRepo( expectedFile );
430
431
432 saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default" );
433 saveRemoteRepositoryConfig( "badproxied2", "Bad Proxied 2", "test://dead.machine.com/repo/", "default" );
434
435
436 saveConnector( ID_DEFAULT_MANAGED, "badproxied1", false );
437 saveConnector( ID_DEFAULT_MANAGED, "badproxied2", false );
438
439 File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
440
441 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
442 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
443
444 wagonMock.get( EasyMock.eq( path ), EasyMock.anyObject( File.class ) );
445 EasyMock.expectLastCall().andThrow( new ResourceDoesNotExistException( "Can't find resource." ) );
446
447 wagonMockControl.replay();
448
449 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
450
451 assertNotDownloaded( downloadedFile );
452
453 wagonMockControl.verify();
454 assertNoTempFiles( expectedFile );
455
456
457
458 }
459
460
461 }