1 package org.apache.maven.archiva.proxy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.Calendar;
29 import java.util.Collection;
30 import java.util.Date;
31 import java.util.Locale;
32
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.lang.ArrayUtils;
35 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
38 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
39 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
40 import org.apache.maven.archiva.policies.ChecksumPolicy;
41 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
42 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
43 import org.apache.maven.archiva.policies.ReleasesPolicy;
44 import org.apache.maven.archiva.policies.SnapshotsPolicy;
45 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
46 import org.apache.maven.wagon.Wagon;
47 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
48 import org.easymock.ArgumentsMatcher;
49 import org.easymock.MockControl;
50
51
52
53
54
55
56 public abstract class AbstractProxyTestCase
57 extends PlexusInSpringTestCase
58 {
59 protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
60
61 protected static final String ID_PROXIED1 = "proxied1";
62
63 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
64
65 protected static final String ID_PROXIED2 = "proxied2";
66
67 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
68
69 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
70
71 protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
72
73 protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
74
75 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
76
77 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
78
79 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
80
81 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
82
83 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
84
85
86
87 protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
88
89 protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
90
91 protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher() {
92
93 public boolean matches(Object[] expected, Object[] actual) {
94 if (expected.length < 1 || actual.length < 1)
95 {
96 return false;
97 }
98 return MockControl.ARRAY_MATCHER.matches(ArrayUtils.remove(expected, 1), ArrayUtils.remove(actual, 1));
99 }
100
101 public String toString(Object[] arguments) {
102 return ArrayUtils.toString(arguments);
103 }
104 };
105
106 protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher() {
107
108 public boolean matches(Object[] expected, Object[] actual)
109 {
110 if (expected.length == 2 && actual.length == 2)
111 {
112 if (expected[0] == null && actual[0] == null)
113 {
114 return true;
115 }
116
117 if (expected[0] == null)
118 {
119 return actual[0] == null;
120 }
121
122 if (actual[0] == null)
123 {
124 return expected[0] == null;
125 }
126
127 return expected[0].equals(actual[0]);
128 }
129 return false;
130 }
131
132 public String toString(Object[] arguments)
133 {
134 return ArrayUtils.toString(arguments);
135 }
136 };
137
138 protected MockControl wagonMockControl;
139
140 protected Wagon wagonMock;
141
142 protected RepositoryProxyConnectors proxyHandler;
143
144 protected ManagedRepositoryContent managedDefaultRepository;
145
146 protected File managedDefaultDir;
147
148 protected ManagedRepositoryContent managedLegacyRepository;
149
150 protected File managedLegacyDir;
151
152 protected MockConfiguration config;
153
154 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
155 throws Exception
156 {
157 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
158 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
159
160 if ( expectedSha1Contents == null )
161 {
162 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
163 }
164 else
165 {
166 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
167 String actualSha1Contents = readChecksumFile( sha1File );
168 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
169 }
170
171 if ( expectedMd5Contents == null )
172 {
173 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
174 }
175 else
176 {
177 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
178 String actualMd5Contents = readChecksumFile( md5File );
179 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
180 }
181 }
182
183 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
184 throws Exception
185 {
186 assertNotNull( "Expected File should not be null.", expectedFile );
187 assertNotNull( "Actual File should not be null.", actualFile );
188
189 assertTrue( "Check actual file exists.", actualFile.exists() );
190 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(), actualFile.getCanonicalPath() );
191 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
192
193 String expectedContents = FileUtils.readFileToString( sourceFile, null );
194 String actualContents = FileUtils.readFileToString( actualFile, null );
195 assertEquals( "Check file contents.", expectedContents, actualContents );
196 }
197
198 protected void assertNotDownloaded( File downloadedFile )
199 {
200 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
201 }
202
203 @SuppressWarnings("unchecked")
204 protected void assertNoTempFiles( File expectedFile )
205 {
206 File workingDir = expectedFile.getParentFile();
207 if ( ( workingDir == null ) || !workingDir.isDirectory() )
208 {
209 return;
210 }
211
212 Collection<File> tmpFiles = FileUtils.listFiles( workingDir, new String[] { "tmp" }, false );
213 if ( !tmpFiles.isEmpty() )
214 {
215 StringBuffer emsg = new StringBuffer();
216 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
217 for ( File tfile : tmpFiles )
218 {
219 emsg.append( "\n " ).append( tfile.getName() );
220 }
221 fail( emsg.toString() );
222 }
223 }
224
225
226
227
228
229
230
231
232
233 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
234 throws IOException
235 {
236 if ( !sourceDirectory.exists() )
237 {
238 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
239 }
240
241 File[] files = sourceDirectory.listFiles();
242
243 String sourcePath = sourceDirectory.getAbsolutePath();
244
245 for ( int i = 0; i < files.length; i++ )
246 {
247 File file = files[i];
248
249 String dest = file.getAbsolutePath();
250
251 dest = dest.substring( sourcePath.length() + 1 );
252
253 File destination = new File( destDirectory, dest );
254
255 if ( file.isFile() )
256 {
257 destination = destination.getParentFile();
258
259 FileUtils.copyFile( file, new File( destination, file.getName() ), false );
260
261
262 }
263 else if ( file.isDirectory() )
264 {
265 if ( !".svn".equals( file.getName() ) )
266 {
267 if ( !destination.exists() && !destination.mkdirs() )
268 {
269 throw new IOException( "Could not create destination directory '"
270 + destination.getAbsolutePath() + "'." );
271 }
272
273 copyDirectoryStructure( file, destination );
274 }
275 }
276 else
277 {
278 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
279 }
280 }
281 }
282
283 protected ManagedRepositoryContent createManagedLegacyRepository()
284 throws Exception
285 {
286 return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
287 "src/test/repositories/legacy-managed", "legacy" );
288 }
289
290 protected ManagedRepositoryContent createProxiedLegacyRepository()
291 throws Exception
292 {
293 return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
294 "src/test/repositories/legacy-proxied", "legacy" );
295 }
296
297 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
298 throws Exception
299 {
300 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
301 repo.setId( id );
302 repo.setName( name );
303 repo.setLocation( path );
304 repo.setLayout( layout );
305
306 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
307 layout );
308 repoContent.setRepository( repo );
309 return repoContent;
310 }
311
312
313
314
315 protected String readChecksumFile( File checksumFile )
316 throws Exception
317 {
318 FileReader freader = null;
319 BufferedReader buf = null;
320
321 try
322 {
323 freader = new FileReader( checksumFile );
324 buf = new BufferedReader( freader );
325 return buf.readLine();
326 }
327 finally
328 {
329 if ( buf != null )
330 {
331 buf.close();
332 }
333
334 if ( freader != null )
335 {
336 freader.close();
337 }
338 }
339 }
340
341 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
342 {
343 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
344 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, disabled );
345 }
346
347 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
348 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
349 {
350 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
351 PropagateErrorsDownloadPolicy.QUEUE, disabled );
352 }
353
354 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
355 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy, boolean disabled )
356 {
357 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
358 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
359 }
360
361 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
362 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
363 String errorOnUpdatePolicy, boolean disabled )
364 {
365 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
366 connectorConfig.setSourceRepoId( sourceRepoId );
367 connectorConfig.setTargetRepoId( targetRepoId );
368 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
369 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
370 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
371 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
372 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
373 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
374 connectorConfig.setDisabled(disabled);
375
376 int count = config.getConfiguration().getProxyConnectors().size();
377 config.getConfiguration().addProxyConnector( connectorConfig );
378
379
380 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
381 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
382 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
383 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
384 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
385 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
386 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
387 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
388 config.triggerChange( prefix + ".policies.propagate-errors",
389 connectorConfig.getPolicy( "propagate-errors", "" ) );
390 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
391 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
392 }
393
394 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
395 {
396 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
397
398 repoConfig.setId( id );
399 repoConfig.setName( name );
400 repoConfig.setLayout( layout );
401
402 repoConfig.setLocation( path );
403
404 int count = config.getConfiguration().getManagedRepositories().size();
405 config.getConfiguration().addManagedRepository( repoConfig );
406
407 String prefix = "managedRepositories.managedRepository(" + count + ")";
408 config.triggerChange( prefix + ".id", repoConfig.getId() );
409 config.triggerChange( prefix + ".name", repoConfig.getName() );
410 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
411 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
412 }
413
414 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
415 {
416 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
417
418 repoConfig.setId( id );
419 repoConfig.setName( name );
420 repoConfig.setLayout( layout );
421 repoConfig.setUrl( url );
422
423 int count = config.getConfiguration().getRemoteRepositories().size();
424 config.getConfiguration().addRemoteRepository( repoConfig );
425
426 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
427 config.triggerChange( prefix + ".id", repoConfig.getId() );
428 config.triggerChange( prefix + ".name", repoConfig.getName() );
429 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
430 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
431 }
432
433 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
434 throws IOException
435 {
436 File repoLocation = getTestFile( targetPath );
437 FileUtils.deleteDirectory( repoLocation );
438 copyDirectoryStructure( getTestFile( originalPath ), repoLocation );
439
440 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
441
442 return repoLocation;
443 }
444
445
446
447
448
449 @Override
450 protected String getSpringConfigLocation()
451 {
452 return "org/apache/maven/archiva/proxy/spring-context.xml";
453 }
454
455 @Override
456 protected void setUp()
457 throws Exception
458 {
459 super.setUp();
460
461 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
462
463
464 String repoPath = "target/test-repository/managed/" + getName();
465 File repoLocation = getTestFile( repoPath );
466
467 managedDefaultRepository = createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath,
468 "default" );
469
470 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
471
472 ManagedRepositoryConfiguration repoConfig = managedDefaultRepository.getRepository();
473
474 config.getConfiguration().addManagedRepository( repoConfig );
475
476
477 repoLocation = getTestFile( REPOPATH_LEGACY_MANAGED_TARGET );
478 FileUtils.deleteDirectory( repoLocation );
479 copyDirectoryStructure( getTestFile( REPOPATH_LEGACY_MANAGED ), repoLocation );
480
481 managedLegacyRepository = createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository",
482 REPOPATH_LEGACY_MANAGED_TARGET, "legacy" );
483
484 managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
485
486 repoConfig = managedLegacyRepository.getRepository();
487
488 config.getConfiguration().addManagedRepository( repoConfig );
489
490
491 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1", new File( REPOPATH_PROXIED1 ).toURL()
492 .toExternalForm(), "default" );
493
494
495 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2", new File( REPOPATH_PROXIED2 ).toURL()
496 .toExternalForm(), "default" );
497
498
499 saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository", new File( REPOPATH_PROXIED_LEGACY )
500 .toURL().toExternalForm(), "legacy" );
501
502
503 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
504
505
506 wagonMockControl = MockControl.createNiceControl( Wagon.class );
507 wagonMock = (Wagon) wagonMockControl.getMock();
508 WagonDelegate delegate = (WagonDelegate) lookup( Wagon.ROLE, "test" );
509 delegate.setDelegate( wagonMock );
510
511 System.out.println( "\n.\\ " + getName() + "() \\._________________________________________\n" );
512 }
513
514
515
516
517
518
519
520
521 protected void setupTestableManagedRepository( String resourcePath )
522 throws IOException
523 {
524 String resourceDir = resourcePath;
525
526 if ( !resourcePath.endsWith( "/" ) )
527 {
528 int idx = resourcePath.lastIndexOf( '/' );
529 resourceDir = resourcePath.substring( 0, idx );
530 }
531
532 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
533 File sourceDir = new File( sourceRepoDir, resourceDir );
534
535 File destRepoDir = managedDefaultDir;
536 File destDir = new File( destRepoDir, resourceDir );
537
538
539 if ( destDir.exists() )
540 {
541 FileUtils.deleteDirectory( destDir );
542 }
543
544
545 destDir.mkdirs();
546
547
548 if ( !sourceDir.exists() )
549 {
550
551 System.err.println( "[WARN] Skipping setup of testable managed repository, source dir does not exist: "
552 + sourceDir );
553 }
554 else
555 {
556
557
558 if ( !sourceDir.isDirectory() )
559 {
560 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
561 }
562
563
564 copyDirectoryStructure( sourceDir, destDir );
565 }
566 }
567
568 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
569 {
570 assertTrue( "Managed File should exist: ", managedFile.exists() );
571 assertTrue( "Remote File should exist: ", remoteFile.exists() );
572
573 managedFile.setLastModified( remoteFile.lastModified() + 55000 );
574 }
575
576 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
577 {
578 assertTrue( "Managed File should exist: ", managedFile.exists() );
579 assertTrue( "Remote File should exist: ", remoteFile.exists() );
580
581 managedFile.setLastModified( remoteFile.lastModified() - 55000 );
582 }
583
584 protected void assertNotModified( File file, long expectedModificationTime )
585 {
586 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.",
587 expectedModificationTime, file.lastModified() );
588 }
589
590 protected void assertNotExistsInManagedLegacyRepo( File file )
591 throws Exception
592 {
593 String managedLegacyPath = managedLegacyDir.getCanonicalPath();
594 String testFile = file.getCanonicalPath();
595
596 assertTrue( "Unit Test Failure: File <" + testFile
597 + "> should be have been defined within the legacy managed path of <" + managedLegacyPath + ">", testFile
598 .startsWith( managedLegacyPath ) );
599
600 assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
601 }
602
603 protected void assertNotExistsInManagedDefaultRepo( File file )
604 throws Exception
605 {
606 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
607 String testFile = file.getCanonicalPath();
608
609 assertTrue( "Unit Test Failure: File <" + testFile
610 + "> should be have been defined within the managed default path of <" + managedDefaultPath + ">", testFile
611 .startsWith( managedDefaultPath ) );
612
613 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
614 }
615
616 protected static Date getFutureDate()
617 throws ParseException
618 {
619 Calendar cal = Calendar.getInstance();
620 cal.add( Calendar.YEAR, 1 );
621 return cal.getTime();
622 }
623
624 protected static Date getPastDate()
625 throws ParseException
626 {
627 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );
628 }
629 }