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 net.sf.ehcache.CacheManager;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.commons.lang.ArrayUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
30 import org.apache.maven.archiva.policies.ChecksumPolicy;
31 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
32 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
33 import org.apache.maven.archiva.policies.ReleasesPolicy;
34 import org.apache.maven.archiva.policies.SnapshotsPolicy;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.wagon.Wagon;
37 import org.codehaus.plexus.util.FileUtils;
38 import org.easymock.ArgumentsMatcher;
39 import org.easymock.MockControl;
40 import org.junit.Before;
41 import org.junit.runner.RunWith;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 import java.io.BufferedReader;
50 import java.io.FileReader;
51 import java.io.IOException;
52 import java.text.ParseException;
53 import java.text.SimpleDateFormat;
54 import java.util.ArrayList;
55 import java.util.Calendar;
56 import java.util.Collection;
57 import java.util.Date;
58 import java.util.Locale;
59 import javax.inject.Inject;
61 import static org.junit.Assert.*;
64 * AbstractProxyTestCase
68 @RunWith( SpringJUnit4ClassRunner.class )
69 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml"} )
70 public abstract class AbstractProxyTestCase
73 protected ApplicationContext applicationContext;
75 protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
77 protected static final String ID_PROXIED1 = "proxied1";
79 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
81 protected static final String ID_PROXIED2 = "proxied2";
83 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
85 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
87 protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
89 protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
91 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
93 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
95 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
97 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
99 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
101 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
103 protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
105 protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
107 protected MockControl wagonMockControl;
109 protected Wagon wagonMock;
112 protected RepositoryProxyConnectors proxyHandler;
114 protected ManagedRepositoryContent managedDefaultRepository;
116 protected File managedDefaultDir;
118 protected ManagedRepositoryContent managedLegacyRepository;
120 protected File managedLegacyDir;
122 protected MockConfiguration config;
124 protected Logger log = LoggerFactory.getLogger( getClass() );
126 WagonDelegate delegate;
129 PlexusSisuBridge plexusSisuBridge;
135 config = (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock",
136 ArchivaConfiguration.class );
138 config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
139 config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
140 config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
142 // Setup source repository (using default layout)
143 String name = getClass().getSimpleName();
144 String repoPath = "target/test-repository/managed/" + name;
145 File repoLocation = new File( repoPath );
147 managedDefaultRepository = createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath,
150 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
152 ManagedRepositoryConfiguration repoConfig = managedDefaultRepository.getRepository();
154 config.getConfiguration().addManagedRepository( repoConfig );
156 // Setup source repository (using legacy layout)
157 repoLocation = new File( REPOPATH_LEGACY_MANAGED_TARGET );
158 if ( repoLocation.exists() )
160 FileUtils.deleteDirectory( repoLocation );
162 copyDirectoryStructure( new File( REPOPATH_LEGACY_MANAGED ), repoLocation );
164 managedLegacyRepository = createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository",
165 REPOPATH_LEGACY_MANAGED_TARGET, "legacy" );
167 managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
169 repoConfig = managedLegacyRepository.getRepository();
171 config.getConfiguration().addManagedRepository( repoConfig );
173 // Setup target (proxied to) repository.
174 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1", new File(
175 REPOPATH_PROXIED1 ).toURL().toExternalForm(), "default" );
177 // Setup target (proxied to) repository.
178 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2", new File(
179 REPOPATH_PROXIED2 ).toURL().toExternalForm(), "default" );
181 // Setup target (proxied to) repository using legacy layout.
182 saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository", new File(
183 REPOPATH_PROXIED_LEGACY ).toURL().toExternalForm(), "legacy" );
185 // Setup the proxy handler.
186 //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
188 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
190 // Setup the wagon mock.
191 wagonMockControl = MockControl.createNiceControl( Wagon.class );
192 wagonMock = (Wagon) wagonMockControl.getMock();
194 delegate = (WagonDelegate) plexusSisuBridge.lookup( Wagon.class, "test" );
196 delegate.setDelegate( wagonMock );
198 CacheManager.getInstance().clearAll();
200 log.info( "\n.\\ " + name + "() \\._________________________________________\n" );
203 protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher()
206 public boolean matches( Object[] expected, Object[] actual )
208 if ( expected.length < 1 || actual.length < 1 )
212 return MockControl.ARRAY_MATCHER.matches( ArrayUtils.remove( expected, 1 ), ArrayUtils.remove( actual,
216 public String toString( Object[] arguments )
218 return ArrayUtils.toString( arguments );
222 protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher()
225 public boolean matches( Object[] expected, Object[] actual )
227 if ( expected.length == 2 && actual.length == 2 )
229 if ( expected[0] == null && actual[0] == null )
234 if ( expected[0] == null )
236 return actual[0] == null;
239 if ( actual[0] == null )
241 return expected[0] == null;
244 return expected[0].equals( actual[0] );
249 public String toString( Object[] arguments )
251 return ArrayUtils.toString( arguments );
256 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
259 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
260 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
262 if ( expectedSha1Contents == null )
264 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
268 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
269 String actualSha1Contents = readChecksumFile( sha1File );
270 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
273 if ( expectedMd5Contents == null )
275 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
279 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
280 String actualMd5Contents = readChecksumFile( md5File );
281 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
285 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
288 assertNotNull( "Expected File should not be null.", expectedFile );
289 assertNotNull( "Actual File should not be null.", actualFile );
291 assertTrue( "Check actual file exists.", actualFile.exists() );
292 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
293 actualFile.getCanonicalPath() );
294 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
296 String expectedContents = org.apache.commons.io.FileUtils.readFileToString( sourceFile, null );
297 String actualContents = org.apache.commons.io.FileUtils.readFileToString( actualFile, null );
298 assertEquals( "Check file contents.", expectedContents, actualContents );
301 protected void assertNotDownloaded( File downloadedFile )
303 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
306 @SuppressWarnings( "unchecked" )
307 protected void assertNoTempFiles( File expectedFile )
309 File workingDir = expectedFile.getParentFile();
310 if ( ( workingDir == null ) || !workingDir.isDirectory() )
315 Collection<File> tmpFiles = org.apache.commons.io.FileUtils.listFiles( workingDir, new String[]{"tmp"}, false );
316 if ( !tmpFiles.isEmpty() )
318 StringBuffer emsg = new StringBuffer();
319 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
320 for ( File tfile : tmpFiles )
322 emsg.append( "\n " ).append( tfile.getName() );
324 fail( emsg.toString() );
329 * A faster recursive copy that omits .svn directories.
331 * @param sourceDirectory the source directory to copy
332 * @param destDirectory the target location
333 * @throws java.io.IOException if there is a copying problem
334 * @todo get back into plexus-utils, share with converter module
336 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
339 if ( !sourceDirectory.exists() )
341 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
344 File[] files = sourceDirectory.listFiles();
346 String sourcePath = sourceDirectory.getAbsolutePath();
348 for ( int i = 0; i < files.length; i++ )
350 File file = files[i];
352 String dest = file.getAbsolutePath();
354 dest = dest.substring( sourcePath.length() + 1 );
356 File destination = new File( destDirectory, dest );
360 destination = destination.getParentFile();
362 org.apache.commons.io.FileUtils.copyFile( file, new File( destination, file.getName() ), false );
363 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
364 //FileUtils.copyFileToDirectory( file, destination );
366 else if ( file.isDirectory() )
368 if ( !".svn".equals( file.getName() ) )
370 if ( !destination.exists() && !destination.mkdirs() )
372 throw new IOException(
373 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
376 copyDirectoryStructure( file, destination );
381 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
386 protected ManagedRepositoryContent createManagedLegacyRepository()
389 return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
390 "src/test/repositories/legacy-managed", "legacy" );
393 protected ManagedRepositoryContent createProxiedLegacyRepository()
396 return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
397 "src/test/repositories/legacy-proxied", "legacy" );
400 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
403 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
405 repo.setName( name );
406 repo.setLocation( path );
407 repo.setLayout( layout );
409 ManagedRepositoryContent repoContent = applicationContext.getBean( "managedRepositoryContent#" + layout,
410 ManagedRepositoryContent.class );
411 repoContent.setRepository( repo );
416 * Read the first line from the checksum file, and return it (trimmed).
418 protected String readChecksumFile( File checksumFile )
421 FileReader freader = null;
422 BufferedReader buf = null;
426 freader = new FileReader( checksumFile );
427 buf = new BufferedReader( freader );
428 return buf.readLine();
437 if ( freader != null )
444 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
446 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
447 CachedFailuresPolicy.NO, disabled );
450 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
451 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
453 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
454 PropagateErrorsDownloadPolicy.QUEUE, disabled );
457 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
458 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
461 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
462 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
465 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
466 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
467 String errorOnUpdatePolicy, boolean disabled )
469 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
470 connectorConfig.setSourceRepoId( sourceRepoId );
471 connectorConfig.setTargetRepoId( targetRepoId );
472 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
473 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
474 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
475 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
476 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
477 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
478 connectorConfig.setDisabled( disabled );
480 int count = config.getConfiguration().getProxyConnectors().size();
481 config.getConfiguration().addProxyConnector( connectorConfig );
483 // Proper Triggering ...
484 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
485 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
486 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
487 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
488 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
489 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
490 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
491 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
492 config.triggerChange( prefix + ".policies.propagate-errors", connectorConfig.getPolicy( "propagate-errors",
494 config.triggerChange( prefix + ".policies.propagate-errors-on-update", connectorConfig.getPolicy(
495 "propagate-errors-on-update", "" ) );
498 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
500 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
502 repoConfig.setId( id );
503 repoConfig.setName( name );
504 repoConfig.setLayout( layout );
506 repoConfig.setLocation( path );
508 int count = config.getConfiguration().getManagedRepositories().size();
509 config.getConfiguration().addManagedRepository( repoConfig );
511 String prefix = "managedRepositories.managedRepository(" + count + ")";
512 config.triggerChange( prefix + ".id", repoConfig.getId() );
513 config.triggerChange( prefix + ".name", repoConfig.getName() );
514 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
515 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
518 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
520 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
522 repoConfig.setId( id );
523 repoConfig.setName( name );
524 repoConfig.setLayout( layout );
525 repoConfig.setUrl( url );
527 int count = config.getConfiguration().getRemoteRepositories().size();
528 config.getConfiguration().addRemoteRepository( repoConfig );
530 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
531 config.triggerChange( prefix + ".id", repoConfig.getId() );
532 config.triggerChange( prefix + ".name", repoConfig.getName() );
533 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
534 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
537 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
540 File repoLocation = new File( targetPath );
541 FileUtils.deleteDirectory( repoLocation );
542 copyDirectoryStructure( new File( originalPath ), repoLocation );
544 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
551 * Copy the specified resource directory from the src/test/repository/managed/ to
552 * the testable directory under target/test-repository/managed/${testName}/
554 * @param resourcePath
555 * @throws IOException
557 protected void setupTestableManagedRepository( String resourcePath )
560 String resourceDir = resourcePath;
562 if ( !resourcePath.endsWith( "/" ) )
564 int idx = resourcePath.lastIndexOf( '/' );
565 resourceDir = resourcePath.substring( 0, idx );
568 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
569 File sourceDir = new File( sourceRepoDir, resourceDir );
571 File destRepoDir = managedDefaultDir;
572 File destDir = new File( destRepoDir, resourceDir );
574 // Cleanout destination dirs.
575 if ( destDir.exists() )
577 FileUtils.deleteDirectory( destDir );
580 // Make the destination dir.
583 // Test the source dir.
584 if ( !sourceDir.exists() )
586 // This is just a warning.
588 "[WARN] Skipping setup of testable managed repository, source dir does not exist: " + sourceDir );
593 // Test that the source is a dir.
594 if ( !sourceDir.isDirectory() )
596 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
599 // Copy directory structure.
600 copyDirectoryStructure( sourceDir, destDir );
604 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
606 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
609 protected void setManagedNewerThanRemote( File managedFile, File remoteFile, long time )
611 assertTrue( "Managed File should exist: ", managedFile.exists() );
612 assertTrue( "Remote File should exist: ", remoteFile.exists() );
614 managedFile.setLastModified( remoteFile.lastModified() + time );
616 assertTrue( managedFile.lastModified() > remoteFile.lastModified() );
619 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
621 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
624 protected void setManagedOlderThanRemote( File managedFile, File remoteFile, long time )
626 assertTrue( "Managed File should exist: ", managedFile.exists() );
627 assertTrue( "Remote File should exist: ", remoteFile.exists() );
629 managedFile.setLastModified( remoteFile.lastModified() - time );
631 assertTrue( managedFile.lastModified() < remoteFile.lastModified() );
635 protected void assertNotModified( File file, long expectedModificationTime )
637 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.", expectedModificationTime,
638 file.lastModified() );
641 protected void assertNotExistsInManagedLegacyRepo( File file )
644 String managedLegacyPath = managedLegacyDir.getCanonicalPath();
645 String testFile = file.getCanonicalPath();
647 assertTrue( "Unit Test Failure: File <" + testFile +
648 "> should be have been defined within the legacy managed path of <" + managedLegacyPath + ">",
649 testFile.startsWith( managedLegacyPath ) );
651 assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
654 protected void assertNotExistsInManagedDefaultRepo( File file )
657 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
658 String testFile = file.getCanonicalPath();
660 assertTrue( "Unit Test Failure: File <" + testFile +
661 "> should be have been defined within the managed default path of <" + managedDefaultPath + ">",
662 testFile.startsWith( managedDefaultPath ) );
664 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
667 protected static Date getFutureDate()
668 throws ParseException
670 Calendar cal = Calendar.getInstance();
671 cal.add( Calendar.YEAR, 1 );
672 return cal.getTime();
675 protected static Date getPastDate()
676 throws ParseException
678 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );