1 package org.apache.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.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
30 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
31 import org.apache.archiva.policies.CachedFailuresPolicy;
32 import org.apache.archiva.policies.ChecksumPolicy;
33 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
34 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
35 import org.apache.archiva.policies.ReleasesPolicy;
36 import org.apache.archiva.policies.SnapshotsPolicy;
37 import org.apache.archiva.proxy.model.RepositoryProxyConnectors;
38 import org.apache.archiva.repository.ManagedRepositoryContent;
39 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
40 import org.apache.commons.io.FileUtils;
41 import org.apache.maven.index.NexusIndexer;
42 import org.apache.maven.index.context.IndexingContext;
43 import org.apache.maven.wagon.Wagon;
44 import org.easymock.EasyMock;
45 import org.easymock.IMocksControl;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.runner.RunWith;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.test.context.ContextConfiguration;
54 import javax.inject.Inject;
55 import java.io.BufferedReader;
57 import java.io.FileReader;
58 import java.io.IOException;
59 import java.nio.charset.Charset;
60 import java.text.ParseException;
61 import java.text.SimpleDateFormat;
62 import java.util.ArrayList;
63 import java.util.Calendar;
64 import java.util.Collection;
65 import java.util.Date;
66 import java.util.Locale;
68 import static org.junit.Assert.*;
71 * AbstractProxyTestCase
73 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
74 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
75 public abstract class AbstractProxyTestCase
78 protected ApplicationContext applicationContext;
80 protected static final String ID_PROXIED1 = "proxied1";
82 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
84 protected static final String ID_PROXIED2 = "proxied2";
86 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
88 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
90 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
92 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
94 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
96 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
98 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
100 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
102 protected IMocksControl wagonMockControl;
104 protected Wagon wagonMock;
107 protected RepositoryProxyConnectors proxyHandler;
109 protected ManagedRepositoryContent managedDefaultRepository;
111 protected File managedDefaultDir;
113 protected MockConfiguration config;
115 protected Logger log = LoggerFactory.getLogger( getClass() );
117 WagonDelegate delegate;
120 protected ManagedRepositoryAdmin managedRepositoryAdmin;
123 protected PlexusSisuBridge plexusSisuBridge;
130 (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
132 config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
133 config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
134 config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
136 // Setup source repository (using default layout)
137 String name = getClass().getSimpleName();
138 String repoPath = "target/test-repository/managed/" + name;
140 managedDefaultRepository =
141 createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath, "default" );
143 managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
145 ManagedRepository repoConfig = managedDefaultRepository.getRepository();
147 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
148 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
150 applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository( repoConfig, false, null );
152 // to prevent windauze file leaking
153 removeMavenIndexes();
155 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
157 if ( managedRepositoryAdmin.getManagedRepository( repoConfig.getId() ) != null )
159 managedRepositoryAdmin.deleteManagedRepository( repoConfig.getId(), null, true );
162 managedRepositoryAdmin.addManagedRepository( repoConfig, false, null );
164 // Setup target (proxied to) repository.
165 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
166 new File( REPOPATH_PROXIED1 ).toURL().toExternalForm(), "default" );
168 // Setup target (proxied to) repository.
169 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
170 new File( REPOPATH_PROXIED2 ).toURL().toExternalForm(), "default" );
172 // Setup the proxy handler.
173 //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
175 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
177 // Setup the wagon mock.
178 wagonMockControl = EasyMock.createNiceControl();
179 wagonMock = wagonMockControl.createMock( Wagon.class );
181 delegate = (WagonDelegate) applicationContext.getBean( "wagon#test", Wagon.class );
183 delegate.setDelegate( wagonMock );
185 CacheManager.getInstance().clearAll();
187 log.info( "\n.\\ {}() \\._________________________________________\n", name );
191 public void shutdown()
194 removeMavenIndexes();
198 protected void removeMavenIndexes()
201 NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
203 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
205 nexusIndexer.removeIndexingContext( indexingContext, false );
210 protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher()
213 public boolean matches( Object[] expected, Object[] actual )
215 if ( expected.length < 1 || actual.length < 1 )
219 return MockControl.ARRAY_MATCHER.matches( ArrayUtils.remove( expected, 1 ),
220 ArrayUtils.remove( actual, 1 ) );
223 public String toString( Object[] arguments )
225 return ArrayUtils.toString( arguments );
229 protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher()
232 public boolean matches( Object[] expected, Object[] actual )
234 if ( expected.length == 2 && actual.length == 2 )
236 if ( expected[0] == null && actual[0] == null )
241 if ( expected[0] == null )
243 return actual[0] == null;
246 if ( actual[0] == null )
248 return expected[0] == null;
251 return expected[0].equals( actual[0] );
256 public String toString( Object[] arguments )
258 return ArrayUtils.toString( arguments );
263 protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
266 File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
267 File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
269 if ( expectedSha1Contents == null )
271 assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
275 assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
276 String actualSha1Contents = readChecksumFile( sha1File );
277 assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
280 if ( expectedMd5Contents == null )
282 assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
286 assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
287 String actualMd5Contents = readChecksumFile( md5File );
288 assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
292 protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
295 assertNotNull( "Expected File should not be null.", expectedFile );
296 assertNotNull( "Actual File should not be null.", actualFile );
298 assertTrue( "Check actual file exists.", actualFile.exists() );
299 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
300 actualFile.getCanonicalPath() );
301 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
303 String expectedContents =
304 org.apache.commons.io.FileUtils.readFileToString( sourceFile, Charset.defaultCharset() );
305 String actualContents =
306 org.apache.commons.io.FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
307 assertEquals( "Check file contents.", expectedContents, actualContents );
310 protected void assertNotDownloaded( File downloadedFile )
312 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
315 @SuppressWarnings( "unchecked" )
316 protected void assertNoTempFiles( File expectedFile )
318 File workingDir = expectedFile.getParentFile();
319 if ( ( workingDir == null ) || !workingDir.isDirectory() )
324 Collection<File> tmpFiles =
325 org.apache.commons.io.FileUtils.listFiles( workingDir, new String[]{ "tmp" }, false );
326 if ( !tmpFiles.isEmpty() )
328 StringBuilder emsg = new StringBuilder();
329 emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
330 for ( File tfile : tmpFiles )
332 emsg.append( "\n " ).append( tfile.getName() );
334 fail( emsg.toString() );
339 * A faster recursive copy that omits .svn directories.
341 * @param sourceDirectory the source directory to copy
342 * @param destDirectory the target location
343 * @throws java.io.IOException if there is a copying problem
344 * @todo get back into plexus-utils, share with converter module
346 protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
349 if ( !sourceDirectory.exists() )
351 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
354 File[] files = sourceDirectory.listFiles();
356 String sourcePath = sourceDirectory.getAbsolutePath();
358 for ( int i = 0; i < files.length; i++ )
360 File file = files[i];
362 String dest = file.getAbsolutePath();
364 dest = dest.substring( sourcePath.length() + 1 );
366 File destination = new File( destDirectory, dest );
370 destination = destination.getParentFile();
372 org.apache.commons.io.FileUtils.copyFile( file, new File( destination, file.getName() ), false );
373 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
374 //FileUtils.copyFileToDirectory( file, destination );
376 else if ( file.isDirectory() )
378 if ( !".svn".equals( file.getName() ) )
380 if ( !destination.exists() && !destination.mkdirs() )
382 throw new IOException(
383 "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
386 copyDirectoryStructure( file, destination );
391 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
397 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
400 ManagedRepository repo = new ManagedRepository();
402 repo.setName( name );
403 repo.setLocation( path );
404 repo.setLayout( layout );
406 ManagedRepositoryContent repoContent =
407 applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
408 repoContent.setRepository( repo );
413 * Read the first line from the checksum file, and return it (trimmed).
415 protected String readChecksumFile( File checksumFile )
418 FileReader freader = null;
419 BufferedReader buf = null;
423 freader = new FileReader( checksumFile );
424 buf = new BufferedReader( freader );
425 return buf.readLine();
434 if ( freader != null )
441 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
443 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
444 CachedFailuresPolicy.NO, disabled );
447 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
448 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
450 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
451 PropagateErrorsDownloadPolicy.QUEUE, disabled );
454 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
455 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
458 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
459 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
462 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
463 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
464 String errorOnUpdatePolicy, boolean disabled )
466 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
467 connectorConfig.setSourceRepoId( sourceRepoId );
468 connectorConfig.setTargetRepoId( targetRepoId );
469 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
470 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
471 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
472 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
473 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
474 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
475 connectorConfig.setDisabled( disabled );
477 int count = config.getConfiguration().getProxyConnectors().size();
478 config.getConfiguration().addProxyConnector( connectorConfig );
480 // Proper Triggering ...
481 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
482 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
483 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
484 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
485 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
486 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
487 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
488 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
489 config.triggerChange( prefix + ".policies.propagate-errors",
490 connectorConfig.getPolicy( "propagate-errors", "" ) );
491 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
492 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
495 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
497 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
499 repoConfig.setId( id );
500 repoConfig.setName( name );
501 repoConfig.setLayout( layout );
503 repoConfig.setLocation( path );
505 int count = config.getConfiguration().getManagedRepositories().size();
506 config.getConfiguration().addManagedRepository( repoConfig );
508 String prefix = "managedRepositories.managedRepository(" + count + ")";
509 config.triggerChange( prefix + ".id", repoConfig.getId() );
510 config.triggerChange( prefix + ".name", repoConfig.getName() );
511 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
512 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
515 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
517 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
519 repoConfig.setId( id );
520 repoConfig.setName( name );
521 repoConfig.setLayout( layout );
522 repoConfig.setUrl( url );
524 int count = config.getConfiguration().getRemoteRepositories().size();
525 config.getConfiguration().addRemoteRepository( repoConfig );
527 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
528 config.triggerChange( prefix + ".id", repoConfig.getId() );
529 config.triggerChange( prefix + ".name", repoConfig.getName() );
530 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
531 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
534 protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
537 File repoLocation = new File( targetPath );
538 FileUtils.deleteDirectory( repoLocation );
539 copyDirectoryStructure( new File( originalPath ), repoLocation );
541 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
548 * Copy the specified resource directory from the src/test/repository/managed/ to
549 * the testable directory under target/test-repository/managed/${testName}/
551 * @param resourcePath
552 * @throws IOException
554 protected void setupTestableManagedRepository( String resourcePath )
557 String resourceDir = resourcePath;
559 if ( !resourcePath.endsWith( "/" ) )
561 int idx = resourcePath.lastIndexOf( '/' );
562 resourceDir = resourcePath.substring( 0, idx );
565 File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
566 File sourceDir = new File( sourceRepoDir, resourceDir );
568 File destRepoDir = managedDefaultDir;
569 File destDir = new File( destRepoDir, resourceDir );
571 // Cleanout destination dirs.
572 if ( destDir.exists() )
574 FileUtils.deleteDirectory( destDir );
577 // Make the destination dir.
580 // Test the source dir.
581 if ( !sourceDir.exists() )
583 // This is just a warning.
584 log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
590 // Test that the source is a dir.
591 if ( !sourceDir.isDirectory() )
593 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
596 // Copy directory structure.
597 copyDirectoryStructure( sourceDir, destDir );
601 protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
603 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
606 protected void setManagedNewerThanRemote( File managedFile, File remoteFile, long time )
608 assertTrue( "Managed File should exist: ", managedFile.exists() );
609 assertTrue( "Remote File should exist: ", remoteFile.exists() );
611 managedFile.setLastModified( remoteFile.lastModified() + time );
613 assertTrue( managedFile.lastModified() > remoteFile.lastModified() );
616 protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
618 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
621 protected void setManagedOlderThanRemote( File managedFile, File remoteFile, long time )
623 assertTrue( "Managed File should exist: ", managedFile.exists() );
624 assertTrue( "Remote File should exist: ", remoteFile.exists() );
626 managedFile.setLastModified( remoteFile.lastModified() - time );
628 assertTrue( managedFile.lastModified() < remoteFile.lastModified() );
632 protected void assertNotModified( File file, long expectedModificationTime )
634 assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.", expectedModificationTime,
635 file.lastModified() );
639 protected void assertNotExistsInManagedDefaultRepo( File file )
642 String managedDefaultPath = managedDefaultDir.getCanonicalPath();
643 String testFile = file.getCanonicalPath();
645 assertTrue( "Unit Test Failure: File <" + testFile
646 + "> should be have been defined within the managed default path of <" + managedDefaultPath
647 + ">", testFile.startsWith( managedDefaultPath ) );
649 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
652 protected static Date getFutureDate()
653 throws ParseException
655 Calendar cal = Calendar.getInstance();
656 cal.add( Calendar.YEAR, 1 );
657 return cal.getTime();
660 protected static Date getPastDate()
661 throws ParseException
663 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );