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.ReleasesPolicy;
41 import org.apache.maven.archiva.policies.SnapshotsPolicy;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.apache.maven.wagon.Wagon;
44 import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext;
45 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
46 import org.easymock.MockControl;
47 import org.springframework.beans.factory.BeanFactory;
50 * AbstractProxyTestCase
52 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
55 public abstract class AbstractProxyTestCase
56 extends PlexusInSpringTestCase
58 protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
60 protected static final String ID_PROXIED1 = "proxied1";
62 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
64 protected static final String ID_PROXIED2 = "proxied2";
66 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
68 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
70 protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
72 protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
74 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
76 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
78 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
80 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
82 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
84 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
86 protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
88 protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
90 protected MockControl wagonMockControl;
92 protected Wagon wagonMock;
94 protected RepositoryProxyConnectors proxyHandler;
96 protected ManagedRepositoryContent managedDefaultRepository;
98 protected File managedDefaultDir;
100 protected ManagedRepositoryContent managedLegacyRepository;
102 protected File managedLegacyDir;
104 protected MockConfiguration config;
106 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
109 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
110 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
112 if ( expectedSha1Contents == null )
114 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
118 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
119 String actualSha1Contents = readChecksumFile( sha1File );
120 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
123 if ( expectedMd5Contents == null )
125 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
129 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
130 String actualMd5Contents = readChecksumFile( md5File );
131 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
135 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
138 assertNotNull( "Expected File should not be null.", expectedFile );
139 assertNotNull( "Actual File should not be null.", actualFile );
141 assertTrue( "Check actual file exists.", actualFile.exists() );
142 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(), actualFile.getCanonicalPath() );
143 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
145 String expectedContents = FileUtils.readFileToString( sourceFile, null );
146 String actualContents = FileUtils.readFileToString( actualFile, null );
147 assertEquals( "Check file contents.", expectedContents, actualContents );
150 protected void assertNotDownloaded( File downloadedFile )
152 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
155 protected void assertNoTempFiles( File expectedFile )
157 File workingDir = expectedFile.getParentFile();
158 if ( ( workingDir == null ) || !workingDir.isDirectory() )
163 Collection<File> tmpFiles = FileUtils.listFiles( workingDir, new String[] { "tmp" }, false );
164 if ( !tmpFiles.isEmpty() )
166 StringBuffer emsg = new StringBuffer();
167 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
168 for ( File tfile : tmpFiles )
170 emsg.append( "\n " ).append( tfile.getName() );
172 fail( emsg.toString() );
177 * A faster recursive copy that omits .svn directories.
179 * @param sourceDirectory the source directory to copy
180 * @param destDirectory the target location
181 * @throws java.io.IOException if there is a copying problem
182 * @todo get back into plexus-utils, share with converter module
184 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
187 if ( !sourceDirectory.exists() )
189 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
192 File[] files = sourceDirectory.listFiles();
194 String sourcePath = sourceDirectory.getAbsolutePath();
196 for ( int i = 0; i < files.length; i++ )
198 File file = files[i];
200 String dest = file.getAbsolutePath();
202 dest = dest.substring( sourcePath.length() + 1 );
204 File destination = new File( destDirectory, dest );
208 destination = destination.getParentFile();
210 FileUtils.copyFile( file, new File( destination, file.getName() ), false );
211 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
212 //FileUtils.copyFileToDirectory( file, destination );
214 else if ( file.isDirectory() )
216 if ( !".svn".equals( file.getName() ) )
218 if ( !destination.exists() && !destination.mkdirs() )
220 throw new IOException( "Could not create destination directory '"
221 + destination.getAbsolutePath() + "'." );
224 copyDirectoryStructure( file, destination );
229 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
234 protected ManagedRepositoryContent createManagedLegacyRepository()
237 return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
238 "src/test/repositories/legacy-managed", "legacy" );
241 protected ManagedRepositoryContent createProxiedLegacyRepository()
244 return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
245 "src/test/repositories/legacy-proxied", "legacy" );
248 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
251 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
253 repo.setName( name );
254 repo.setLocation( path );
255 repo.setLayout( layout );
257 ManagedRepositoryContent repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class,
259 repoContent.setRepository( repo );
264 * Read the first line from the checksum file, and return it (trimmed).
266 protected String readChecksumFile( File checksumFile )
269 FileReader freader = null;
270 BufferedReader buf = null;
274 freader = new FileReader( checksumFile );
275 buf = new BufferedReader( freader );
276 return buf.readLine();
285 if ( freader != null )
292 protected void saveConnector( String sourceRepoId, String targetRepoId )
294 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS,
295 SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
298 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy,
299 String releasePolicy, String snapshotPolicy, String cacheFailuresPolicy )
301 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
302 connectorConfig.setSourceRepoId( sourceRepoId );
303 connectorConfig.setTargetRepoId( targetRepoId );
304 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
305 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
306 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
307 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
309 int count = config.getConfiguration().getProxyConnectors().size();
310 config.getConfiguration().addProxyConnector( connectorConfig );
312 // Proper Triggering ...
313 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
314 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
315 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
316 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
317 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
318 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
319 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
320 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
323 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
325 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
327 repoConfig.setId( id );
328 repoConfig.setName( name );
329 repoConfig.setLayout( layout );
331 repoConfig.setLocation( path );
333 int count = config.getConfiguration().getManagedRepositories().size();
334 config.getConfiguration().addManagedRepository( repoConfig );
336 String prefix = "managedRepositories.managedRepository(" + count + ")";
337 config.triggerChange( prefix + ".id", repoConfig.getId() );
338 config.triggerChange( prefix + ".name", repoConfig.getName() );
339 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
340 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
343 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
345 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
347 repoConfig.setId( id );
348 repoConfig.setName( name );
349 repoConfig.setLayout( layout );
350 repoConfig.setUrl( url );
352 int count = config.getConfiguration().getRemoteRepositories().size();
353 config.getConfiguration().addRemoteRepository( repoConfig );
355 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
356 config.triggerChange( prefix + ".id", repoConfig.getId() );
357 config.triggerChange( prefix + ".name", repoConfig.getName() );
358 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
359 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
362 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
365 File repoLocation = getTestFile( targetPath );
366 FileUtils.deleteDirectory( repoLocation );
367 copyDirectoryStructure( getTestFile( originalPath ), repoLocation );
369 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
376 * @see org.codehaus.plexus.spring.PlexusInSpringTestCase#getConfigLocation()
379 protected String getSpringConfigLocation()
382 return "org/apache/maven/archiva/proxy/spring-context.xml";
386 protected void setUp()
391 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
393 // Setup source repository (using default layout)
394 String repoPath = "target/test-repository/managed/" + getName();
395 File repoLocation = getTestFile( repoPath );
397 managedDefaultRepository = createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath,
400 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
402 ManagedRepositoryConfiguration repoConfig = managedDefaultRepository.getRepository();
404 config.getConfiguration().addManagedRepository( repoConfig );
406 // Setup source repository (using legacy layout)
407 repoLocation = getTestFile( REPOPATH_LEGACY_MANAGED_TARGET );
408 FileUtils.deleteDirectory( repoLocation );
409 copyDirectoryStructure( getTestFile( REPOPATH_LEGACY_MANAGED ), repoLocation );
411 managedLegacyRepository = createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository",
412 REPOPATH_LEGACY_MANAGED_TARGET, "legacy" );
414 managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
416 repoConfig = managedLegacyRepository.getRepository();
418 config.getConfiguration().addManagedRepository( repoConfig );
420 // Setup target (proxied to) repository.
421 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1", new File( REPOPATH_PROXIED1 ).toURL()
422 .toExternalForm(), "default" );
424 // Setup target (proxied to) repository.
425 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2", new File( REPOPATH_PROXIED2 ).toURL()
426 .toExternalForm(), "default" );
428 // Setup target (proxied to) repository using legacy layout.
429 saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository", new File( REPOPATH_PROXIED_LEGACY )
430 .toURL().toExternalForm(), "legacy" );
432 // Setup the proxy handler.
433 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
435 // Setup the wagon mock.
436 wagonMockControl = MockControl.createNiceControl( Wagon.class );
437 wagonMock = (Wagon) wagonMockControl.getMock();
438 WagonDelegate delegate = (WagonDelegate) lookup( Wagon.ROLE, "test" );
439 delegate.setDelegate( wagonMock );
441 System.out.println( "\n.\\ " + getName() + "() \\._________________________________________\n" );
445 * Copy the specified resource directory from the src/test/repository/managed/ to
446 * the testable directory under target/test-repository/managed/${testName}/
449 * @throws IOException
451 protected void setupTestableManagedRepository( String resourcePath )
454 String resourceDir = resourcePath;
456 if ( !resourcePath.endsWith( "/" ) )
458 int idx = resourcePath.lastIndexOf( '/' );
459 resourceDir = resourcePath.substring( 0, idx );
462 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
463 File sourceDir = new File( sourceRepoDir, resourceDir );
465 File destRepoDir = managedDefaultDir;
466 File destDir = new File( destRepoDir, resourceDir );
468 // Cleanout destination dirs.
469 if ( destDir.exists() )
471 FileUtils.deleteDirectory( destDir );
474 // Test the source dir.
475 if ( !sourceDir.exists() )
477 // This is just a warning.
478 System.err.println( "[WARN] Skipping setup of testable managed repository, source dir does not exist: "
483 // Test that the source is a dir.
484 if ( !sourceDir.isDirectory() )
486 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
489 // Make the destination dir.
492 // Copy directory structure.
493 copyDirectoryStructure( sourceDir, destDir );
496 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
498 assertTrue( "Managed File should exist: ", managedFile.exists() );
499 assertTrue( "Remote File should exist: ", remoteFile.exists() );
501 managedFile.setLastModified( remoteFile.lastModified() + 55000 );
504 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
506 assertTrue( "Managed File should exist: ", managedFile.exists() );
507 assertTrue( "Remote File should exist: ", remoteFile.exists() );
509 managedFile.setLastModified( remoteFile.lastModified() - 55000 );
512 protected void assertNotModified( File file, long expectedModificationTime )
514 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.",
515 expectedModificationTime, file.lastModified() );
518 protected void assertNotExistsInManagedLegacyRepo( File file )
521 String managedLegacyPath = managedLegacyDir.getCanonicalPath();
522 String testFile = file.getCanonicalPath();
524 assertTrue( "Unit Test Failure: File <" + testFile
525 + "> should be have been defined within the legacy managed path of <" + managedLegacyPath + ">", testFile
526 .startsWith( managedLegacyPath ) );
528 assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
531 protected void assertNotExistsInManagedDefaultRepo( File file )
534 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
535 String testFile = file.getCanonicalPath();
537 assertTrue( "Unit Test Failure: File <" + testFile
538 + "> should be have been defined within the managed default path of <" + managedDefaultPath + ">", testFile
539 .startsWith( managedDefaultPath ) );
541 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
544 protected static Date getFutureDate()
545 throws ParseException
547 Calendar cal = Calendar.getInstance();
548 cal.add( Calendar.YEAR, 1 );
549 return cal.getTime();
552 protected static Date getPastDate()
553 throws ParseException
555 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );