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.RepositoryContentProvider;
40 import org.apache.archiva.repository.RepositoryRegistry;
41 import org.apache.archiva.repository.maven2.MavenManagedRepository;
42 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
43 import org.apache.commons.io.FileUtils;
44 import org.apache.maven.index.NexusIndexer;
45 import org.apache.maven.index.context.IndexingContext;
46 import org.apache.maven.wagon.Wagon;
47 import org.easymock.EasyMock;
48 import org.easymock.IMocksControl;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.runner.RunWith;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.context.ApplicationContext;
55 import org.springframework.test.context.ContextConfiguration;
57 import javax.inject.Inject;
58 import java.io.BufferedReader;
59 import java.io.FileReader;
60 import java.io.IOException;
62 import java.nio.charset.Charset;
63 import java.nio.file.Files;
64 import java.nio.file.Path;
65 import java.nio.file.Paths;
66 import java.nio.file.attribute.FileTime;
67 import java.text.ParseException;
68 import java.text.SimpleDateFormat;
70 import java.util.concurrent.TimeUnit;
71 import java.util.stream.Collectors;
73 import static org.junit.Assert.*;
76 * AbstractProxyTestCase
78 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
79 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
80 public abstract class AbstractProxyTestCase
83 protected ApplicationContext applicationContext;
86 RepositoryRegistry repositoryRegistry;
88 protected static final String ID_PROXIED1 = "proxied1";
90 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
92 protected static final String ID_PROXIED2 = "proxied2";
94 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
96 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
98 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
100 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
102 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
104 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
106 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
108 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
110 protected IMocksControl wagonMockControl;
112 protected Wagon wagonMock;
115 protected RepositoryProxyConnectors proxyHandler;
117 protected ManagedRepositoryContent managedDefaultRepository;
119 protected Path managedDefaultDir;
121 protected MockConfiguration config;
123 protected Logger log = LoggerFactory.getLogger( getClass() );
125 WagonDelegate delegate;
129 protected NexusIndexer nexusIndexer;
136 (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", 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;
146 managedDefaultRepository =
147 createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath, "default" );
149 managedDefaultDir = Paths.get( managedDefaultRepository.getRepoRoot() );
151 org.apache.archiva.repository.ManagedRepository repoConfig = managedDefaultRepository.getRepository();
153 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
154 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
156 applicationContext.getBean( RepositoryRegistry.class ).putRepository( repoConfig );
158 // to prevent windauze file leaking
159 removeMavenIndexes();
161 repositoryRegistry.setArchivaConfiguration( config );
163 // Setup target (proxied to) repository.
164 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
165 Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
167 // Setup target (proxied to) repository.
168 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
169 Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
172 repositoryRegistry.reload();
174 if ( repositoryRegistry.getManagedRepository( repoConfig.getId() ) != null )
176 org.apache.archiva.repository.ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( repoConfig.getId() );
177 repositoryRegistry.removeRepository( managedRepository );
180 repositoryRegistry.putRepository( repoConfig );
183 // Setup the proxy handler.
184 //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
186 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
189 // Setup the wagon mock.
190 wagonMockControl = EasyMock.createNiceControl();
191 wagonMock = wagonMockControl.createMock( Wagon.class );
193 delegate = (WagonDelegate) applicationContext.getBean( "wagon#test", Wagon.class );
195 delegate.setDelegate( wagonMock );
197 CacheManager.getInstance().clearAll();
199 log.info( "\n.\\ {}() \\._________________________________________\n", name );
203 public void shutdown()
206 removeMavenIndexes();
210 protected void removeMavenIndexes()
214 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
216 nexusIndexer.removeIndexingContext( indexingContext, false );
221 protected void assertChecksums( Path expectedFile, String expectedSha1Contents, String expectedMd5Contents )
224 Path sha1File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString()+ ".sha1" );
225 Path md5File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString() + ".md5" );
227 if ( expectedSha1Contents == null )
229 assertFalse( "SHA1 File should NOT exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
233 assertTrue( "SHA1 File should exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
234 String actualSha1Contents = readChecksumFile( sha1File );
235 assertEquals( "SHA1 File contents: " + sha1File.toAbsolutePath(), expectedSha1Contents, actualSha1Contents );
238 if ( expectedMd5Contents == null )
240 assertFalse( "MD5 File should NOT exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
244 assertTrue( "MD5 File should exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
245 String actualMd5Contents = readChecksumFile( md5File );
246 assertEquals( "MD5 File contents: " + md5File.toAbsolutePath(), expectedMd5Contents, actualMd5Contents );
250 protected void assertFileEquals( Path expectedFile, Path actualFile, Path sourceFile )
253 assertNotNull( "Expected File should not be null.", expectedFile );
254 assertNotNull( "Actual File should not be null.", actualFile );
256 assertTrue( "Check actual file exists.", Files.exists(actualFile) );
257 assertTrue( "Check file is the same.", Files.isSameFile( expectedFile,
259 String expectedContents =
260 org.apache.commons.io.FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
261 String actualContents =
262 org.apache.commons.io.FileUtils.readFileToString( actualFile.toFile(), Charset.defaultCharset() );
263 assertEquals( "Check file contents.", expectedContents, actualContents );
266 protected void assertNotDownloaded( Path downloadedFile )
268 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
271 @SuppressWarnings( "unchecked" )
272 protected void assertNoTempFiles( Path expectedFile )
274 Path workingDir = expectedFile.getParent();
275 if ( ( workingDir == null ) || !Files.isDirectory( workingDir) )
280 Collection<Path> tmpFiles = null;
282 tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
283 } catch (IOException e) {
284 log.error("Could not retrieve tmpFiles {}", workingDir);
286 if ( tmpFiles!=null && !tmpFiles.isEmpty() )
288 StringBuilder emsg = new StringBuilder();
289 emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
290 for ( Path tfile : tmpFiles )
292 emsg.append( "\n " ).append( tfile.getFileName().toString());
294 fail( emsg.toString() );
299 * A faster recursive copy that omits .svn directories.
301 * @param sourceDirectory the source directory to copy
302 * @param destDirectory the target location
303 * @throws java.io.IOException if there is a copying problem
304 * @todo get back into plexus-utils, share with converter module
306 protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory )
309 if ( !Files.exists(sourceDirectory) )
311 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
314 Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
316 String sourcePath = sourceDirectory.toAbsolutePath().toString();
318 for ( int i = 0; i < files.length; i++ )
320 Path file = files[i];
322 String dest = file.toAbsolutePath().toString();
324 dest = dest.substring( sourcePath.length() + 1 );
326 Path destination = destDirectory.resolve( dest );
328 if ( Files.isRegularFile(file) )
330 destination = destination.getParent();
332 org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
333 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
334 //FileUtils.copyFileToDirectory( file, destination );
336 else if ( Files.isDirectory(file) )
338 if ( !".svn".equals( file.getFileName().toString() ) )
340 if ( !Files.exists(destination))
342 Files.createDirectories(destination);
345 copyDirectoryStructure( file, destination );
350 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
356 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
359 MavenManagedRepository repo = new MavenManagedRepository(id, name, Paths.get(path).getParent());
360 repo.setLocation( new URI(path) );
361 repo.setLayout( layout );
363 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
364 ManagedRepositoryContent repoContent =
365 provider.createManagedContent( repo );
370 * Read the first line from the checksum file, and return it (trimmed).
372 protected String readChecksumFile( Path checksumFile )
375 FileReader freader = null;
376 BufferedReader buf = null;
380 freader = new FileReader( checksumFile.toFile() );
381 buf = new BufferedReader( freader );
382 return buf.readLine();
391 if ( freader != null )
398 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
400 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
401 CachedFailuresPolicy.NO, disabled );
404 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
405 String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
407 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
408 PropagateErrorsDownloadPolicy.QUEUE, disabled );
411 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
412 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
415 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
416 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
419 protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
420 String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
421 String errorOnUpdatePolicy, boolean disabled )
423 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
424 connectorConfig.setSourceRepoId( sourceRepoId );
425 connectorConfig.setTargetRepoId( targetRepoId );
426 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
427 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
428 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
429 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
430 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
431 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
432 connectorConfig.setDisabled( disabled );
434 int count = config.getConfiguration().getProxyConnectors().size();
435 config.getConfiguration().addProxyConnector( connectorConfig );
437 // Proper Triggering ...
438 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
439 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
440 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
441 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
442 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
443 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
444 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
445 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
446 config.triggerChange( prefix + ".policies.propagate-errors",
447 connectorConfig.getPolicy( "propagate-errors", "" ) );
448 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
449 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
452 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
454 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
456 repoConfig.setId( id );
457 repoConfig.setName( name );
458 repoConfig.setLayout( layout );
460 repoConfig.setLocation( path );
462 int count = config.getConfiguration().getManagedRepositories().size();
463 config.getConfiguration().addManagedRepository( repoConfig );
465 String prefix = "managedRepositories.managedRepository(" + count + ")";
466 config.triggerChange( prefix + ".id", repoConfig.getId() );
467 config.triggerChange( prefix + ".name", repoConfig.getName() );
468 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
469 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
472 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
474 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
476 repoConfig.setId( id );
477 repoConfig.setName( name );
478 repoConfig.setLayout( layout );
479 repoConfig.setUrl( url );
481 int count = config.getConfiguration().getRemoteRepositories().size();
482 config.getConfiguration().addRemoteRepository( repoConfig );
484 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
485 config.triggerChange( prefix + ".id", repoConfig.getId() );
486 config.triggerChange( prefix + ".name", repoConfig.getName() );
487 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
488 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
489 repositoryRegistry.reload();
492 protected Path saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
495 Path repoLocation = Paths.get( targetPath );
496 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoLocation );
497 copyDirectoryStructure( Paths.get(originalPath) , repoLocation );
499 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
506 * Copy the specified resource directory from the src/test/repository/managed/ to
507 * the testable directory under target/test-repository/managed/${testName}/
509 * @param resourcePath
510 * @throws IOException
512 protected void setupTestableManagedRepository( String resourcePath )
515 String resourceDir = resourcePath;
517 if ( !resourcePath.endsWith( "/" ) )
519 int idx = resourcePath.lastIndexOf( '/' );
520 resourceDir = resourcePath.substring( 0, idx );
523 Path sourceRepoDir = Paths.get( REPOPATH_DEFAULT_MANAGED );
524 Path sourceDir = sourceRepoDir.resolve(resourceDir );
526 Path destRepoDir = managedDefaultDir;
527 Path destDir = destRepoDir.resolve(resourceDir );
529 // Cleanout destination dirs.
530 if ( Files.exists(destDir))
532 org.apache.archiva.common.utils.FileUtils.deleteDirectory( destDir );
535 // Make the destination dir.
536 Files.createDirectories(destDir);
538 // Test the source dir.
539 if ( !Files.exists(sourceDir) )
541 // This is just a warning.
542 log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
548 // Test that the source is a dir.
549 if ( !Files.isDirectory( sourceDir) )
551 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
554 // Copy directory structure.
555 copyDirectoryStructure( sourceDir, destDir );
559 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile )
561 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
564 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile, long time )
566 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
567 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
571 Files.setLastModifiedTime( managedFile,
572 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() + time, TimeUnit.MILLISECONDS ));
574 catch ( IOException e )
576 e.printStackTrace( );
581 assertTrue( Files.getLastModifiedTime( managedFile).compareTo( Files.getLastModifiedTime( remoteFile )) > 0);
583 catch ( IOException e )
585 e.printStackTrace( );
589 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile )
591 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
594 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile, long time )
596 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
597 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
601 Files.setLastModifiedTime( managedFile,
602 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() - time, TimeUnit.MILLISECONDS ));
604 catch ( IOException e )
606 e.printStackTrace( );
611 assertTrue( Files.getLastModifiedTime( managedFile ).compareTo(Files.getLastModifiedTime( remoteFile )) < 0 );
613 catch ( IOException e )
615 e.printStackTrace( );
620 protected void assertNotModified( Path file, long expectedModificationTime )
624 assertEquals( "File <" + file.toAbsolutePath() + "> not have been modified.", expectedModificationTime,
625 Files.getLastModifiedTime( file ).toMillis());
627 catch ( IOException e )
629 e.printStackTrace( );
634 protected void assertNotExistsInManagedDefaultRepo( Path testFile )
637 Path managedDefaultPath = managedDefaultDir;
639 assertTrue( "Unit Test Failure: File <" + testFile
640 + "> should be have been defined within the managed default path of <" + managedDefaultPath
641 + ">", testFile.startsWith( managedDefaultPath ) );
643 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", Files.exists(testFile) );
646 protected static Date getFutureDate()
647 throws ParseException
649 Calendar cal = Calendar.getInstance();
650 cal.add( Calendar.YEAR, 1 );
651 return cal.getTime();
654 protected static Date getPastDate()
655 throws ParseException
657 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );