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.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
37 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
38 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
39 import org.apache.maven.archiva.policies.ChecksumPolicy;
40 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
41 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
42 import org.apache.maven.archiva.policies.ReleasesPolicy;
43 import org.apache.maven.archiva.policies.SnapshotsPolicy;
44 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
45 import org.apache.maven.wagon.Wagon;
46 import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext;
47 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
48 import org.easymock.MockControl;
49 import org.springframework.beans.factory.BeanFactory;
52 * AbstractProxyTestCase
54 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
57 public abstract class AbstractProxyTestCase
58 extends PlexusInSpringTestCase
60 protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
62 protected static final String ID_PROXIED1 = "proxied1";
64 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
66 protected static final String ID_PROXIED2 = "proxied2";
68 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
70 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
72 protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
74 protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
76 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
78 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
80 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
82 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
84 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
86 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
88 protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
90 protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
92 protected MockControl wagonMockControl;
94 protected Wagon wagonMock;
96 protected RepositoryProxyConnectors proxyHandler;
98 protected ManagedRepositoryContent managedDefaultRepository;
100 protected File managedDefaultDir;
102 protected ManagedRepositoryContent managedLegacyRepository;
104 protected File managedLegacyDir;
106 protected MockConfiguration config;
108 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
111 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
112 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
114 if ( expectedSha1Contents == null )
116 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
120 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
121 String actualSha1Contents = readChecksumFile( sha1File );
122 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
125 if ( expectedMd5Contents == null )
127 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
131 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
132 String actualMd5Contents = readChecksumFile( md5File );
133 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
137 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
140 assertNotNull( "Expected File should not be null.", expectedFile );
141 assertNotNull( "Actual File should not be null.", actualFile );
143 assertTrue( "Check actual file exists.", actualFile.exists() );
144 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(), actualFile.getCanonicalPath() );
145 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
147 String expectedContents = FileUtils.readFileToString( sourceFile, null );
148 String actualContents = FileUtils.readFileToString( actualFile, null );
149 assertEquals( "Check file contents.", expectedContents, actualContents );
152 protected void assertNotDownloaded( File downloadedFile )
154 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
157 protected void assertNoTempFiles( File expectedFile )
159 File workingDir = expectedFile.getParentFile();
160 if ( ( workingDir == null ) || !workingDir.isDirectory() )
165 Collection<File> tmpFiles = FileUtils.listFiles( workingDir, new String[] { "tmp" }, false );
166 if ( !tmpFiles.isEmpty() )
168 StringBuffer emsg = new StringBuffer();
169 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
170 for ( File tfile : tmpFiles )
172 emsg.append( "\n " ).append( tfile.getName() );
174 fail( emsg.toString() );
179 * A faster recursive copy that omits .svn directories.
181 * @param sourceDirectory the source directory to copy
182 * @param destDirectory the target location
183 * @throws java.io.IOException if there is a copying problem
184 * @todo get back into plexus-utils, share with converter module
186 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
189 if ( !sourceDirectory.exists() )
191 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
194 File[] files = sourceDirectory.listFiles();
196 String sourcePath = sourceDirectory.getAbsolutePath();
198 for ( int i = 0; i < files.length; i++ )
200 File file = files[i];
202 String dest = file.getAbsolutePath();
204 dest = dest.substring( sourcePath.length() + 1 );
206 File destination = new File( destDirectory, dest );
210 destination = destination.getParentFile();
212 FileUtils.copyFile( file, new File( destination, file.getName() ), false );
213 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
214 //FileUtils.copyFileToDirectory( file, destination );
216 else if ( file.isDirectory() )
218 if ( !".svn".equals( file.getName() ) )
220 if ( !destination.exists() && !destination.mkdirs() )
222 throw new IOException( "Could not create destination directory '"
223 + destination.getAbsolutePath() + "'." );
226 copyDirectoryStructure( file, destination );
231 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
236 protected ManagedRepositoryContent createManagedLegacyRepository()
239 return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
240 "src/test/repositories/legacy-managed", "legacy" );
243 protected ManagedRepositoryContent createProxiedLegacyRepository()
246 return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
247 "src/test/repositories/legacy-proxied", "legacy" );
250 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
253 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
255 repo.setName( name );
256 repo.setLocation( path );
257 repo.setLayout( layout );
259 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
261 repoContent.setRepository( repo );
266 * Read the first line from the checksum file, and return it (trimmed).
268 protected String readChecksumFile( File checksumFile )
271 FileReader freader = null;
272 BufferedReader buf = null;
276 freader = new FileReader( checksumFile );
277 buf = new BufferedReader( freader );
278 return buf.readLine();
287 if ( freader != null )
294 protected void saveConnector( String sourceRepoId, String targetRepoId )
296 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
297 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
300 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
301 String snapshotPolicy, String cacheFailuresPolicy )
303 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
304 PropagateErrorsDownloadPolicy.QUEUE );
307 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
308 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy )
310 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
311 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
314 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
315 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
316 String errorOnUpdatePolicy )
318 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
319 connectorConfig.setSourceRepoId( sourceRepoId );
320 connectorConfig.setTargetRepoId( targetRepoId );
321 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
322 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
323 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
324 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
325 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
326 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
328 int count = config.getConfiguration().getProxyConnectors().size();
329 config.getConfiguration().addProxyConnector( connectorConfig );
331 // Proper Triggering ...
332 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
333 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
334 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
335 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
336 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
337 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
338 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
339 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
340 config.triggerChange( prefix + ".policies.propagate-errors",
341 connectorConfig.getPolicy( "propagate-errors", "" ) );
342 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
343 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
346 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
348 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
350 repoConfig.setId( id );
351 repoConfig.setName( name );
352 repoConfig.setLayout( layout );
354 repoConfig.setLocation( path );
356 int count = config.getConfiguration().getManagedRepositories().size();
357 config.getConfiguration().addManagedRepository( repoConfig );
359 String prefix = "managedRepositories.managedRepository(" + count + ")";
360 config.triggerChange( prefix + ".id", repoConfig.getId() );
361 config.triggerChange( prefix + ".name", repoConfig.getName() );
362 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
363 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
366 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
368 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
370 repoConfig.setId( id );
371 repoConfig.setName( name );
372 repoConfig.setLayout( layout );
373 repoConfig.setUrl( url );
375 int count = config.getConfiguration().getRemoteRepositories().size();
376 config.getConfiguration().addRemoteRepository( repoConfig );
378 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
379 config.triggerChange( prefix + ".id", repoConfig.getId() );
380 config.triggerChange( prefix + ".name", repoConfig.getName() );
381 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
382 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
385 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
388 File repoLocation = getTestFile( targetPath );
389 FileUtils.deleteDirectory( repoLocation );
390 copyDirectoryStructure( getTestFile( originalPath ), repoLocation );
392 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
399 * @see org.codehaus.plexus.spring.PlexusInSpringTestCase#getConfigLocation()
402 protected String getSpringConfigLocation()
404 return "org/apache/maven/archiva/proxy/spring-context.xml";
408 protected void setUp()
413 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
415 // Setup source repository (using default layout)
416 String repoPath = "target/test-repository/managed/" + getName();
417 File repoLocation = getTestFile( repoPath );
419 managedDefaultRepository = createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath,
422 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
424 ManagedRepositoryConfiguration repoConfig = managedDefaultRepository.getRepository();
426 config.getConfiguration().addManagedRepository( repoConfig );
428 // Setup source repository (using legacy layout)
429 repoLocation = getTestFile( REPOPATH_LEGACY_MANAGED_TARGET );
430 FileUtils.deleteDirectory( repoLocation );
431 copyDirectoryStructure( getTestFile( REPOPATH_LEGACY_MANAGED ), repoLocation );
433 managedLegacyRepository = createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository",
434 REPOPATH_LEGACY_MANAGED_TARGET, "legacy" );
436 managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
438 repoConfig = managedLegacyRepository.getRepository();
440 config.getConfiguration().addManagedRepository( repoConfig );
442 // Setup target (proxied to) repository.
443 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1", new File( REPOPATH_PROXIED1 ).toURL()
444 .toExternalForm(), "default" );
446 // Setup target (proxied to) repository.
447 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2", new File( REPOPATH_PROXIED2 ).toURL()
448 .toExternalForm(), "default" );
450 // Setup target (proxied to) repository using legacy layout.
451 saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository", new File( REPOPATH_PROXIED_LEGACY )
452 .toURL().toExternalForm(), "legacy" );
454 // Setup the proxy handler.
455 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
457 // Setup the wagon mock.
458 wagonMockControl = MockControl.createNiceControl( Wagon.class );
459 wagonMock = (Wagon) wagonMockControl.getMock();
460 WagonDelegate delegate = (WagonDelegate) lookup( Wagon.ROLE, "test" );
461 delegate.setDelegate( wagonMock );
463 System.out.println( "\n.\\ " + getName() + "() \\._________________________________________\n" );
467 * Copy the specified resource directory from the src/test/repository/managed/ to
468 * the testable directory under target/test-repository/managed/${testName}/
471 * @throws IOException
473 protected void setupTestableManagedRepository( String resourcePath )
476 String resourceDir = resourcePath;
478 if ( !resourcePath.endsWith( "/" ) )
480 int idx = resourcePath.lastIndexOf( '/' );
481 resourceDir = resourcePath.substring( 0, idx );
484 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
485 File sourceDir = new File( sourceRepoDir, resourceDir );
487 File destRepoDir = managedDefaultDir;
488 File destDir = new File( destRepoDir, resourceDir );
490 // Cleanout destination dirs.
491 if ( destDir.exists() )
493 FileUtils.deleteDirectory( destDir );
496 // Test the source dir.
497 if ( !sourceDir.exists() )
499 // This is just a warning.
500 System.err.println( "[WARN] Skipping setup of testable managed repository, source dir does not exist: "
505 // Test that the source is a dir.
506 if ( !sourceDir.isDirectory() )
508 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
511 // Make the destination dir.
514 // Copy directory structure.
515 copyDirectoryStructure( sourceDir, destDir );
518 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
520 assertTrue( "Managed File should exist: ", managedFile.exists() );
521 assertTrue( "Remote File should exist: ", remoteFile.exists() );
523 managedFile.setLastModified( remoteFile.lastModified() + 55000 );
526 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
528 assertTrue( "Managed File should exist: ", managedFile.exists() );
529 assertTrue( "Remote File should exist: ", remoteFile.exists() );
531 managedFile.setLastModified( remoteFile.lastModified() - 55000 );
534 protected void assertNotModified( File file, long expectedModificationTime )
536 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.",
537 expectedModificationTime, file.lastModified() );
540 protected void assertNotExistsInManagedLegacyRepo( File file )
543 String managedLegacyPath = managedLegacyDir.getCanonicalPath();
544 String testFile = file.getCanonicalPath();
546 assertTrue( "Unit Test Failure: File <" + testFile
547 + "> should be have been defined within the legacy managed path of <" + managedLegacyPath + ">", testFile
548 .startsWith( managedLegacyPath ) );
550 assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
553 protected void assertNotExistsInManagedDefaultRepo( File file )
556 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
557 String testFile = file.getCanonicalPath();
559 assertTrue( "Unit Test Failure: File <" + testFile
560 + "> should be have been defined within the managed default path of <" + managedDefaultPath + ">", testFile
561 .startsWith( managedDefaultPath ) );
563 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
566 protected static Date getFutureDate()
567 throws ParseException
569 Calendar cal = Calendar.getInstance();
570 cal.add( Calendar.YEAR, 1 );
571 return cal.getTime();
574 protected static Date getPastDate()
575 throws ParseException
577 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );