1 package org.apache.maven.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.io.BufferedReader;
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;
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;
52 * AbstractProxyTestCase
56 public abstract class AbstractProxyTestCase
57 extends PlexusInSpringTestCase
59 protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
61 protected static final String ID_PROXIED1 = "proxied1";
63 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
65 protected static final String ID_PROXIED2 = "proxied2";
67 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
69 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
71 protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
73 protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
75 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
77 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
79 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
81 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
83 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
85 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
87 protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
89 protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
91 protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher() {
93 public boolean matches(Object[] expected, Object[] actual) {
94 if (expected.length < 1 || actual.length < 1)
98 return MockControl.ARRAY_MATCHER.matches(ArrayUtils.remove(expected, 1), ArrayUtils.remove(actual, 1));
101 public String toString(Object[] arguments) {
102 return ArrayUtils.toString(arguments);
106 protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher() {
108 public boolean matches(Object[] expected, Object[] actual)
110 if (expected.length == 2 && actual.length == 2)
112 if (expected[0] == null && actual[0] == null)
117 if (expected[0] == null)
119 return actual[0] == null;
122 if (actual[0] == null)
124 return expected[0] == null;
127 return expected[0].equals(actual[0]);
132 public String toString(Object[] arguments)
134 return ArrayUtils.toString(arguments);
138 protected MockControl wagonMockControl;
140 protected Wagon wagonMock;
142 protected RepositoryProxyConnectors proxyHandler;
144 protected ManagedRepositoryContent managedDefaultRepository;
146 protected File managedDefaultDir;
148 protected ManagedRepositoryContent managedLegacyRepository;
150 protected File managedLegacyDir;
152 protected MockConfiguration config;
154 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
157 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
158 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
160 if ( expectedSha1Contents == null )
162 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
166 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
167 String actualSha1Contents = readChecksumFile( sha1File );
168 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
171 if ( expectedMd5Contents == null )
173 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
177 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
178 String actualMd5Contents = readChecksumFile( md5File );
179 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
183 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
186 assertNotNull( "Expected File should not be null.", expectedFile );
187 assertNotNull( "Actual File should not be null.", actualFile );
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() );
193 String expectedContents = FileUtils.readFileToString( sourceFile, null );
194 String actualContents = FileUtils.readFileToString( actualFile, null );
195 assertEquals( "Check file contents.", expectedContents, actualContents );
198 protected void assertNotDownloaded( File downloadedFile )
200 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
203 @SuppressWarnings("unchecked")
204 protected void assertNoTempFiles( File expectedFile )
206 File workingDir = expectedFile.getParentFile();
207 if ( ( workingDir == null ) || !workingDir.isDirectory() )
212 Collection<File> tmpFiles = FileUtils.listFiles( workingDir, new String[] { "tmp" }, false );
213 if ( !tmpFiles.isEmpty() )
215 StringBuffer emsg = new StringBuffer();
216 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
217 for ( File tfile : tmpFiles )
219 emsg.append( "\n " ).append( tfile.getName() );
221 fail( emsg.toString() );
226 * A faster recursive copy that omits .svn directories.
228 * @param sourceDirectory the source directory to copy
229 * @param destDirectory the target location
230 * @throws java.io.IOException if there is a copying problem
231 * @todo get back into plexus-utils, share with converter module
233 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
236 if ( !sourceDirectory.exists() )
238 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
241 File[] files = sourceDirectory.listFiles();
243 String sourcePath = sourceDirectory.getAbsolutePath();
245 for ( int i = 0; i < files.length; i++ )
247 File file = files[i];
249 String dest = file.getAbsolutePath();
251 dest = dest.substring( sourcePath.length() + 1 );
253 File destination = new File( destDirectory, dest );
257 destination = destination.getParentFile();
259 FileUtils.copyFile( file, new File( destination, file.getName() ), false );
260 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
261 //FileUtils.copyFileToDirectory( file, destination );
263 else if ( file.isDirectory() )
265 if ( !".svn".equals( file.getName() ) )
267 if ( !destination.exists() && !destination.mkdirs() )
269 throw new IOException( "Could not create destination directory '"
270 + destination.getAbsolutePath() + "'." );
273 copyDirectoryStructure( file, destination );
278 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
283 protected ManagedRepositoryContent createManagedLegacyRepository()
286 return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
287 "src/test/repositories/legacy-managed", "legacy" );
290 protected ManagedRepositoryContent createProxiedLegacyRepository()
293 return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
294 "src/test/repositories/legacy-proxied", "legacy" );
297 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
300 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
302 repo.setName( name );
303 repo.setLocation( path );
304 repo.setLayout( layout );
306 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
308 repoContent.setRepository( repo );
313 * Read the first line from the checksum file, and return it (trimmed).
315 protected String readChecksumFile( File checksumFile )
318 FileReader freader = null;
319 BufferedReader buf = null;
323 freader = new FileReader( checksumFile );
324 buf = new BufferedReader( freader );
325 return buf.readLine();
334 if ( freader != null )
341 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
343 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
344 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, disabled );
347 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
348 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
350 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
351 PropagateErrorsDownloadPolicy.QUEUE, disabled );
354 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
355 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy, boolean disabled )
357 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
358 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
361 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
362 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
363 String errorOnUpdatePolicy, boolean disabled )
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);
376 int count = config.getConfiguration().getProxyConnectors().size();
377 config.getConfiguration().addProxyConnector( connectorConfig );
379 // Proper Triggering ...
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", "" ) );
394 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
396 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
398 repoConfig.setId( id );
399 repoConfig.setName( name );
400 repoConfig.setLayout( layout );
402 repoConfig.setLocation( path );
404 int count = config.getConfiguration().getManagedRepositories().size();
405 config.getConfiguration().addManagedRepository( repoConfig );
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() );
414 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
416 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
418 repoConfig.setId( id );
419 repoConfig.setName( name );
420 repoConfig.setLayout( layout );
421 repoConfig.setUrl( url );
423 int count = config.getConfiguration().getRemoteRepositories().size();
424 config.getConfiguration().addRemoteRepository( repoConfig );
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() );
433 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
436 File repoLocation = getTestFile( targetPath );
437 FileUtils.deleteDirectory( repoLocation );
438 copyDirectoryStructure( getTestFile( originalPath ), repoLocation );
440 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
447 * @see org.codehaus.plexus.spring.PlexusInSpringTestCase#getConfigLocation()
450 protected String getSpringConfigLocation()
452 return "org/apache/maven/archiva/proxy/spring-context.xml";
456 protected void setUp()
461 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
463 // Setup source repository (using default layout)
464 String repoPath = "target/test-repository/managed/" + getName();
465 File repoLocation = getTestFile( repoPath );
467 managedDefaultRepository = createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath,
470 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
472 ManagedRepositoryConfiguration repoConfig = managedDefaultRepository.getRepository();
474 config.getConfiguration().addManagedRepository( repoConfig );
476 // Setup source repository (using legacy layout)
477 repoLocation = getTestFile( REPOPATH_LEGACY_MANAGED_TARGET );
478 FileUtils.deleteDirectory( repoLocation );
479 copyDirectoryStructure( getTestFile( REPOPATH_LEGACY_MANAGED ), repoLocation );
481 managedLegacyRepository = createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository",
482 REPOPATH_LEGACY_MANAGED_TARGET, "legacy" );
484 managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
486 repoConfig = managedLegacyRepository.getRepository();
488 config.getConfiguration().addManagedRepository( repoConfig );
490 // Setup target (proxied to) repository.
491 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1", new File( REPOPATH_PROXIED1 ).toURL()
492 .toExternalForm(), "default" );
494 // Setup target (proxied to) repository.
495 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2", new File( REPOPATH_PROXIED2 ).toURL()
496 .toExternalForm(), "default" );
498 // Setup target (proxied to) repository using legacy layout.
499 saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository", new File( REPOPATH_PROXIED_LEGACY )
500 .toURL().toExternalForm(), "legacy" );
502 // Setup the proxy handler.
503 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
505 // Setup the wagon mock.
506 wagonMockControl = MockControl.createNiceControl( Wagon.class );
507 wagonMock = (Wagon) wagonMockControl.getMock();
508 WagonDelegate delegate = (WagonDelegate) lookup( Wagon.ROLE, "test" );
509 delegate.setDelegate( wagonMock );
511 System.out.println( "\n.\\ " + getName() + "() \\._________________________________________\n" );
515 * Copy the specified resource directory from the src/test/repository/managed/ to
516 * the testable directory under target/test-repository/managed/${testName}/
519 * @throws IOException
521 protected void setupTestableManagedRepository( String resourcePath )
524 String resourceDir = resourcePath;
526 if ( !resourcePath.endsWith( "/" ) )
528 int idx = resourcePath.lastIndexOf( '/' );
529 resourceDir = resourcePath.substring( 0, idx );
532 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
533 File sourceDir = new File( sourceRepoDir, resourceDir );
535 File destRepoDir = managedDefaultDir;
536 File destDir = new File( destRepoDir, resourceDir );
538 // Cleanout destination dirs.
539 if ( destDir.exists() )
541 FileUtils.deleteDirectory( destDir );
544 // Make the destination dir.
547 // Test the source dir.
548 if ( !sourceDir.exists() )
550 // This is just a warning.
551 System.err.println( "[WARN] Skipping setup of testable managed repository, source dir does not exist: "
557 // Test that the source is a dir.
558 if ( !sourceDir.isDirectory() )
560 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
563 // Copy directory structure.
564 copyDirectoryStructure( sourceDir, destDir );
568 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
570 assertTrue( "Managed File should exist: ", managedFile.exists() );
571 assertTrue( "Remote File should exist: ", remoteFile.exists() );
573 managedFile.setLastModified( remoteFile.lastModified() + 55000 );
576 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
578 assertTrue( "Managed File should exist: ", managedFile.exists() );
579 assertTrue( "Remote File should exist: ", remoteFile.exists() );
581 managedFile.setLastModified( remoteFile.lastModified() - 55000 );
584 protected void assertNotModified( File file, long expectedModificationTime )
586 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.",
587 expectedModificationTime, file.lastModified() );
590 protected void assertNotExistsInManagedLegacyRepo( File file )
593 String managedLegacyPath = managedLegacyDir.getCanonicalPath();
594 String testFile = file.getCanonicalPath();
596 assertTrue( "Unit Test Failure: File <" + testFile
597 + "> should be have been defined within the legacy managed path of <" + managedLegacyPath + ">", testFile
598 .startsWith( managedLegacyPath ) );
600 assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
603 protected void assertNotExistsInManagedDefaultRepo( File file )
606 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
607 String testFile = file.getCanonicalPath();
609 assertTrue( "Unit Test Failure: File <" + testFile
610 + "> should be have been defined within the managed default path of <" + managedDefaultPath + ">", testFile
611 .startsWith( managedDefaultPath ) );
613 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
616 protected static Date getFutureDate()
617 throws ParseException
619 Calendar cal = Calendar.getInstance();
620 cal.add( Calendar.YEAR, 1 );
621 return cal.getTime();
624 protected static Date getPastDate()
625 throws ParseException
627 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );