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.repository.RepositoryRegistry;
40 import org.apache.archiva.repository.maven2.MavenManagedRepository;
41 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
42 import org.apache.commons.io.FileUtils;
43 import org.apache.maven.index.NexusIndexer;
44 import org.apache.maven.index.context.IndexingContext;
45 import org.apache.maven.wagon.Wagon;
46 import org.easymock.EasyMock;
47 import org.easymock.IMocksControl;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.runner.RunWith;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.test.context.ContextConfiguration;
56 import javax.inject.Inject;
57 import java.io.BufferedReader;
58 import java.io.FileReader;
59 import java.io.IOException;
61 import java.nio.charset.Charset;
62 import java.nio.file.Files;
63 import java.nio.file.Path;
64 import java.nio.file.Paths;
65 import java.nio.file.attribute.FileTime;
66 import java.text.ParseException;
67 import java.text.SimpleDateFormat;
69 import java.util.concurrent.TimeUnit;
70 import java.util.stream.Collectors;
72 import static org.junit.Assert.*;
75 * AbstractProxyTestCase
77 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
78 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
79 public abstract class AbstractProxyTestCase
82 protected ApplicationContext applicationContext;
84 protected static final String ID_PROXIED1 = "proxied1";
86 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
88 protected static final String ID_PROXIED2 = "proxied2";
90 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
92 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
94 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
96 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
98 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
100 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
102 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
104 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
106 protected IMocksControl wagonMockControl;
108 protected Wagon wagonMock;
111 protected RepositoryProxyConnectors proxyHandler;
113 protected ManagedRepositoryContent managedDefaultRepository;
115 protected Path managedDefaultDir;
117 protected MockConfiguration config;
119 protected Logger log = LoggerFactory.getLogger( getClass() );
121 WagonDelegate delegate;
123 protected RepositoryRegistry repositoryRegistry;
126 protected NexusIndexer nexusIndexer;
133 (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
135 config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
136 config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
137 config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
139 // Setup source repository (using default layout)
140 String name = getClass().getSimpleName();
141 String repoPath = "target/test-repository/managed/" + name;
143 managedDefaultRepository =
144 createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath, "default" );
146 managedDefaultDir = Paths.get( managedDefaultRepository.getRepoRoot() );
148 org.apache.archiva.repository.ManagedRepository repoConfig = managedDefaultRepository.getRepository();
150 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
151 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
153 applicationContext.getBean( RepositoryRegistry.class ).putRepository( repoConfig );
155 // to prevent windauze file leaking
156 removeMavenIndexes();
158 repositoryRegistry = applicationContext.getBean( RepositoryRegistry.class );
159 repositoryRegistry.setArchivaConfiguration( config );
161 // Setup target (proxied to) repository.
162 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
163 Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
165 // Setup target (proxied to) repository.
166 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
167 Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
169 repositoryRegistry.reload();
171 if ( repositoryRegistry.getManagedRepository( repoConfig.getId() ) != null )
173 org.apache.archiva.repository.ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( repoConfig.getId() );
174 repositoryRegistry.removeRepository( managedRepository );
177 repositoryRegistry.putRepository( repoConfig );
180 // Setup the proxy handler.
181 //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
183 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
186 // Setup the wagon mock.
187 wagonMockControl = EasyMock.createNiceControl();
188 wagonMock = wagonMockControl.createMock( Wagon.class );
190 delegate = (WagonDelegate) applicationContext.getBean( "wagon#test", Wagon.class );
192 delegate.setDelegate( wagonMock );
194 CacheManager.getInstance().clearAll();
196 log.info( "\n.\\ {}() \\._________________________________________\n", name );
200 public void shutdown()
203 removeMavenIndexes();
207 protected void removeMavenIndexes()
211 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
213 nexusIndexer.removeIndexingContext( indexingContext, false );
218 protected void assertChecksums( Path expectedFile, String expectedSha1Contents, String expectedMd5Contents )
221 Path sha1File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString()+ ".sha1" );
222 Path md5File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString() + ".md5" );
224 if ( expectedSha1Contents == null )
226 assertFalse( "SHA1 File should NOT exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
230 assertTrue( "SHA1 File should exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
231 String actualSha1Contents = readChecksumFile( sha1File );
232 assertEquals( "SHA1 File contents: " + sha1File.toAbsolutePath(), expectedSha1Contents, actualSha1Contents );
235 if ( expectedMd5Contents == null )
237 assertFalse( "MD5 File should NOT exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
241 assertTrue( "MD5 File should exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
242 String actualMd5Contents = readChecksumFile( md5File );
243 assertEquals( "MD5 File contents: " + md5File.toAbsolutePath(), expectedMd5Contents, actualMd5Contents );
247 protected void assertFileEquals( Path expectedFile, Path actualFile, Path sourceFile )
250 assertNotNull( "Expected File should not be null.", expectedFile );
251 assertNotNull( "Actual File should not be null.", actualFile );
253 assertTrue( "Check actual file exists.", Files.exists(actualFile) );
254 assertTrue( "Check file is the same.", Files.isSameFile( expectedFile,
256 String expectedContents =
257 org.apache.commons.io.FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
258 String actualContents =
259 org.apache.commons.io.FileUtils.readFileToString( actualFile.toFile(), Charset.defaultCharset() );
260 assertEquals( "Check file contents.", expectedContents, actualContents );
263 protected void assertNotDownloaded( Path downloadedFile )
265 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
268 @SuppressWarnings( "unchecked" )
269 protected void assertNoTempFiles( Path expectedFile )
271 Path workingDir = expectedFile.getParent();
272 if ( ( workingDir == null ) || !Files.isDirectory( workingDir) )
277 Collection<Path> tmpFiles = null;
279 tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
280 } catch (IOException e) {
281 log.error("Could not retrieve tmpFiles {}", workingDir);
283 if ( tmpFiles!=null && !tmpFiles.isEmpty() )
285 StringBuilder emsg = new StringBuilder();
286 emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
287 for ( Path tfile : tmpFiles )
289 emsg.append( "\n " ).append( tfile.getFileName().toString());
291 fail( emsg.toString() );
296 * A faster recursive copy that omits .svn directories.
298 * @param sourceDirectory the source directory to copy
299 * @param destDirectory the target location
300 * @throws java.io.IOException if there is a copying problem
301 * @todo get back into plexus-utils, share with converter module
303 protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory )
306 if ( !Files.exists(sourceDirectory) )
308 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
311 Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
313 String sourcePath = sourceDirectory.toAbsolutePath().toString();
315 for ( int i = 0; i < files.length; i++ )
317 Path file = files[i];
319 String dest = file.toAbsolutePath().toString();
321 dest = dest.substring( sourcePath.length() + 1 );
323 Path destination = destDirectory.resolve( dest );
325 if ( Files.isRegularFile(file) )
327 destination = destination.getParent();
329 org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
330 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
331 //FileUtils.copyFileToDirectory( file, destination );
333 else if ( Files.isDirectory(file) )
335 if ( !".svn".equals( file.getFileName().toString() ) )
337 if ( !Files.exists(destination))
339 Files.createDirectories(destination);
342 copyDirectoryStructure( file, destination );
347 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
353 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
356 MavenManagedRepository repo = new MavenManagedRepository(id, name);
357 repo.setLocation( new URI(path) );
358 repo.setLayout( layout );
360 ManagedRepositoryContent repoContent =
361 applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
362 repoContent.setRepository( repo );
367 * Read the first line from the checksum file, and return it (trimmed).
369 protected String readChecksumFile( Path checksumFile )
372 FileReader freader = null;
373 BufferedReader buf = null;
377 freader = new FileReader( checksumFile.toFile() );
378 buf = new BufferedReader( freader );
379 return buf.readLine();
388 if ( freader != null )
395 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
397 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
398 CachedFailuresPolicy.NO, disabled );
401 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
402 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
404 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
405 PropagateErrorsDownloadPolicy.QUEUE, disabled );
408 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
409 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
412 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
413 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
416 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
417 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
418 String errorOnUpdatePolicy, boolean disabled )
420 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
421 connectorConfig.setSourceRepoId( sourceRepoId );
422 connectorConfig.setTargetRepoId( targetRepoId );
423 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
424 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
425 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
426 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
427 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
428 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
429 connectorConfig.setDisabled( disabled );
431 int count = config.getConfiguration().getProxyConnectors().size();
432 config.getConfiguration().addProxyConnector( connectorConfig );
434 // Proper Triggering ...
435 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
436 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
437 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
438 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
439 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
440 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
441 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
442 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
443 config.triggerChange( prefix + ".policies.propagate-errors",
444 connectorConfig.getPolicy( "propagate-errors", "" ) );
445 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
446 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
449 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
451 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
453 repoConfig.setId( id );
454 repoConfig.setName( name );
455 repoConfig.setLayout( layout );
457 repoConfig.setLocation( path );
459 int count = config.getConfiguration().getManagedRepositories().size();
460 config.getConfiguration().addManagedRepository( repoConfig );
462 String prefix = "managedRepositories.managedRepository(" + count + ")";
463 config.triggerChange( prefix + ".id", repoConfig.getId() );
464 config.triggerChange( prefix + ".name", repoConfig.getName() );
465 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
466 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
469 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
471 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
473 repoConfig.setId( id );
474 repoConfig.setName( name );
475 repoConfig.setLayout( layout );
476 repoConfig.setUrl( url );
478 int count = config.getConfiguration().getRemoteRepositories().size();
479 config.getConfiguration().addRemoteRepository( repoConfig );
481 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
482 config.triggerChange( prefix + ".id", repoConfig.getId() );
483 config.triggerChange( prefix + ".name", repoConfig.getName() );
484 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
485 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
486 repositoryRegistry.reload();
489 protected Path saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
492 Path repoLocation = Paths.get( targetPath );
493 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoLocation );
494 copyDirectoryStructure( Paths.get(originalPath) , repoLocation );
496 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
503 * Copy the specified resource directory from the src/test/repository/managed/ to
504 * the testable directory under target/test-repository/managed/${testName}/
506 * @param resourcePath
507 * @throws IOException
509 protected void setupTestableManagedRepository( String resourcePath )
512 String resourceDir = resourcePath;
514 if ( !resourcePath.endsWith( "/" ) )
516 int idx = resourcePath.lastIndexOf( '/' );
517 resourceDir = resourcePath.substring( 0, idx );
520 Path sourceRepoDir = Paths.get( REPOPATH_DEFAULT_MANAGED );
521 Path sourceDir = sourceRepoDir.resolve(resourceDir );
523 Path destRepoDir = managedDefaultDir;
524 Path destDir = destRepoDir.resolve(resourceDir );
526 // Cleanout destination dirs.
527 if ( Files.exists(destDir))
529 org.apache.archiva.common.utils.FileUtils.deleteDirectory( destDir );
532 // Make the destination dir.
533 Files.createDirectories(destDir);
535 // Test the source dir.
536 if ( !Files.exists(sourceDir) )
538 // This is just a warning.
539 log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
545 // Test that the source is a dir.
546 if ( !Files.isDirectory( sourceDir) )
548 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
551 // Copy directory structure.
552 copyDirectoryStructure( sourceDir, destDir );
556 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile )
558 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
561 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile, long time )
563 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
564 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
568 Files.setLastModifiedTime( managedFile,
569 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() + time, TimeUnit.MILLISECONDS ));
571 catch ( IOException e )
573 e.printStackTrace( );
578 assertTrue( Files.getLastModifiedTime( managedFile).compareTo( Files.getLastModifiedTime( remoteFile )) > 0);
580 catch ( IOException e )
582 e.printStackTrace( );
586 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile )
588 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
591 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile, long time )
593 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
594 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
598 Files.setLastModifiedTime( managedFile,
599 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() - time, TimeUnit.MILLISECONDS ));
601 catch ( IOException e )
603 e.printStackTrace( );
608 assertTrue( Files.getLastModifiedTime( managedFile ).compareTo(Files.getLastModifiedTime( remoteFile )) < 0 );
610 catch ( IOException e )
612 e.printStackTrace( );
617 protected void assertNotModified( Path file, long expectedModificationTime )
621 assertEquals( "File <" + file.toAbsolutePath() + "> not have been modified.", expectedModificationTime,
622 Files.getLastModifiedTime( file ).toMillis());
624 catch ( IOException e )
626 e.printStackTrace( );
631 protected void assertNotExistsInManagedDefaultRepo( Path testFile )
634 Path managedDefaultPath = managedDefaultDir;
636 assertTrue( "Unit Test Failure: File <" + testFile
637 + "> should be have been defined within the managed default path of <" + managedDefaultPath
638 + ">", testFile.startsWith( managedDefaultPath ) );
640 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", Files.exists(testFile) );
643 protected static Date getFutureDate()
644 throws ParseException
646 Calendar cal = Calendar.getInstance();
647 cal.add( Calendar.YEAR, 1 );
648 return cal.getTime();
651 protected static Date getPastDate()
652 throws ParseException
654 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );