1 package org.apache.archiva.maven.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import net.sf.ehcache.CacheManager;
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ArchivaRuntimeConfiguration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.archiva.policies.CachedFailuresPolicy;
28 import org.apache.archiva.policies.ChecksumPolicy;
29 import org.apache.archiva.policies.PolicyOption;
30 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
31 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
32 import org.apache.archiva.policies.ReleasesPolicy;
33 import org.apache.archiva.policies.SnapshotsPolicy;
34 import org.apache.archiva.proxy.ProxyRegistry;
35 import org.apache.archiva.proxy.model.RepositoryProxyHandler;
36 import org.apache.archiva.repository.ManagedRepository;
37 import org.apache.archiva.repository.ManagedRepositoryContent;
38 import org.apache.archiva.repository.RepositoryRegistry;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
41 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
42 import org.apache.archiva.repository.storage.StorageAsset;
43 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
44 import org.apache.maven.wagon.Wagon;
45 import org.easymock.EasyMock;
46 import org.easymock.IMocksControl;
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;
56 import java.io.FileReader;
57 import java.io.IOException;
58 import java.nio.charset.Charset;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
62 import java.nio.file.attribute.FileTime;
63 import java.text.ParseException;
64 import java.text.SimpleDateFormat;
65 import java.util.ArrayList;
66 import java.util.Calendar;
67 import java.util.Collection;
68 import java.util.Date;
69 import java.util.List;
70 import java.util.Locale;
71 import java.util.concurrent.TimeUnit;
72 import java.util.stream.Collectors;
74 import static org.junit.Assert.*;
77 * AbstractProxyTestCase
79 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
80 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
81 public abstract class AbstractProxyTestCase
84 protected ApplicationContext applicationContext;
87 private ProxyRegistry proxyRegistry;
90 RepositoryRegistry repositoryRegistry;
92 @SuppressWarnings( "unused" )
94 RepositoryHandlerDependencies repositoryHandlerDependencies;
96 protected static final String ID_PROXIED1 = "proxied1";
98 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
100 protected static final String ID_PROXIED2 = "proxied2";
102 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
104 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
106 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
108 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
110 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
112 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
114 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
116 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
118 protected IMocksControl wagonMockControl;
120 protected Wagon wagonMock;
123 protected RepositoryProxyHandler proxyHandler;
125 protected ManagedRepositoryContent managedDefaultRepository;
127 protected Path managedDefaultDir;
129 protected MockConfiguration config;
131 protected Logger log = LoggerFactory.getLogger( getClass() );
133 WagonDelegate delegate;
140 (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
142 config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
143 config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
144 config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
145 ArchivaRuntimeConfiguration runtimeConfiguration = new ArchivaRuntimeConfiguration();
146 List<String> checksumTypes = new ArrayList<>();
147 checksumTypes.add("md5");
148 checksumTypes.add("sha256");
149 checksumTypes.add("sha1");
150 checksumTypes.add("asc");
151 runtimeConfiguration.setChecksumTypes(checksumTypes);
152 config.getConfiguration().setArchivaRuntimeConfiguration(runtimeConfiguration);
153 repositoryRegistry.setArchivaConfiguration( config );
155 // Setup source repository (using default layout)
156 String name = getClass().getSimpleName();
157 Path repoPath = Paths.get("target/test-repository/managed/" + name);
159 managedDefaultRepository =
160 createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath.toString(), "default" );
162 managedDefaultDir = repoPath.resolve(ID_DEFAULT_MANAGED) ;
164 org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(ID_DEFAULT_MANAGED);
166 // Setup target (proxied to) repository.
167 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
168 Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
170 // Setup target (proxied to) repository.
171 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
172 Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
175 repositoryRegistry.reload();
176 repositoryRegistry.putRepository(repoConfig);
179 // Setup the proxy handler.
180 //proxyHandler = applicationContext.getBean (RepositoryProxyHandler) lookup( RepositoryProxyHandler.class.getName() );
182 proxyHandler = applicationContext.getBean( "repositoryProxyHandler#test", RepositoryProxyHandler.class );
183 assertNotNull( proxyRegistry );
184 assertTrue(proxyRegistry.getAllHandler( ).get( RepositoryType.MAVEN).contains( proxyHandler ));
187 // Setup the wagon mock.
188 wagonMockControl = EasyMock.createNiceControl();
189 wagonMock = wagonMockControl.createMock( Wagon.class );
191 delegate = (WagonDelegate) applicationContext.getBean( "wagon#http", Wagon.class );
193 delegate.setDelegate( wagonMock );
195 CacheManager.getInstance().clearAll();
197 log.info( "\n.\\ {}() \\._________________________________________\n", name );
200 protected void assertChecksums( Path expectedFile, String expectedSha1Contents, String expectedMd5Contents )
203 Path sha1File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString()+ ".sha1" );
204 Path md5File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString() + ".md5" );
206 if ( expectedSha1Contents == null )
208 assertFalse( "SHA1 File should NOT exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
212 assertTrue( "SHA1 File should exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
213 String actualSha1Contents = readChecksumFile( sha1File );
214 assertEquals( "SHA1 File contents: " + sha1File.toAbsolutePath(), expectedSha1Contents, actualSha1Contents );
217 if ( expectedMd5Contents == null )
219 assertFalse( "MD5 File should NOT exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
223 assertTrue( "MD5 File should exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
224 String actualMd5Contents = readChecksumFile( md5File );
225 assertEquals( "MD5 File contents: " + md5File.toAbsolutePath(), expectedMd5Contents, actualMd5Contents );
229 protected void assertFileEquals( Path expectedFile, Path actualFile, Path sourceFile )
232 assertNotNull( "Expected File should not be null.", expectedFile );
233 assertNotNull( "Actual File should not be null.", actualFile );
235 assertTrue( "Check actual file exists.", Files.exists(actualFile) );
236 assertTrue("Check expected file exists", Files.exists(expectedFile));
237 assertTrue( "Check file is the same.", Files.isSameFile( expectedFile,
239 String expectedContents =
240 org.apache.commons.io.FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
241 String actualContents =
242 org.apache.commons.io.FileUtils.readFileToString( actualFile.toFile(), Charset.defaultCharset() );
243 assertEquals( "Check file contents.", expectedContents, actualContents );
246 protected void assertNotDownloaded( StorageAsset downloadedFile )
248 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
251 @SuppressWarnings( "unchecked" )
252 protected void assertNoTempFiles( Path expectedFile )
254 Path workingDir = expectedFile.getParent();
255 if ( ( workingDir == null ) || !Files.isDirectory( workingDir) )
260 Collection<Path> tmpFiles = null;
262 tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
263 } catch (IOException e) {
264 log.error("Could not retrieve tmpFiles {}", workingDir);
266 if ( tmpFiles!=null && !tmpFiles.isEmpty() )
268 StringBuilder emsg = new StringBuilder();
269 emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
270 for ( Path tfile : tmpFiles )
272 emsg.append( "\n " ).append( tfile.getFileName().toString());
274 fail( emsg.toString() );
279 * A faster recursive copy that omits .svn directories.
281 * @param sourceDirectory the source directory to copy
282 * @param destDirectory the target location
283 * @throws java.io.IOException if there is a copying problem
284 * @todo get back into plexus-utils, share with converter module
286 protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory )
289 if ( !Files.exists(sourceDirectory) )
291 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
294 Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
296 String sourcePath = sourceDirectory.toAbsolutePath().toString();
298 for ( int i = 0; i < files.length; i++ )
300 Path file = files[i];
302 String dest = file.toAbsolutePath().toString();
304 dest = dest.substring( sourcePath.length() + 1 );
306 Path destination = destDirectory.resolve( dest );
308 if ( Files.isRegularFile(file) )
310 destination = destination.getParent();
312 org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
313 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
314 //FileUtils.copyFileToDirectory( file, destination );
316 else if ( Files.isDirectory(file) )
318 if ( !".svn".equals( file.getFileName().toString() ) )
320 if ( !Files.exists(destination))
322 Files.createDirectories(destination);
325 copyDirectoryStructure( file, destination );
330 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
336 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
339 ManagedRepository repo = BasicManagedRepository.newFilesystemInstance(id, name, Paths.get(path).resolve(id));
340 repositoryRegistry.putRepository(repo);
341 return repositoryRegistry.getManagedRepository(id).getContent();
345 * Read the first line from the checksum file, and return it (trimmed).
347 protected String readChecksumFile( Path checksumFile )
350 FileReader freader = null;
351 BufferedReader buf = null;
355 freader = new FileReader( checksumFile.toFile() );
356 buf = new BufferedReader( freader );
357 return buf.readLine();
366 if ( freader != null )
373 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
375 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
376 CachedFailuresPolicy.NO, disabled );
379 protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
380 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, boolean disabled )
382 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
383 PropagateErrorsDownloadPolicy.QUEUE, disabled );
386 protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
387 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
390 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
391 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
394 protected void saveConnector(String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
395 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
396 PolicyOption errorOnUpdatePolicy, boolean disabled )
398 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
399 connectorConfig.setSourceRepoId( sourceRepoId );
400 connectorConfig.setTargetRepoId( targetRepoId );
401 connectorConfig.setProxyId(sourceRepoId);
402 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy.getId() );
403 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy.getId() );
404 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy.getId() );
405 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy.getId());
406 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy.getId() );
407 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy.getId() );
408 connectorConfig.setDisabled( disabled );
410 int count = config.getConfiguration().getProxyConnectors().size();
411 config.getConfiguration().addProxyConnector( connectorConfig );
413 // Proper Triggering ...
414 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
415 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
416 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
417 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
418 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
419 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
420 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
421 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
422 config.triggerChange( prefix + ".policies.propagate-errors",
423 connectorConfig.getPolicy( "propagate-errors", "" ) );
424 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
425 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
428 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
430 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
432 repoConfig.setId( id );
433 repoConfig.setName( name );
434 repoConfig.setLayout( layout );
436 repoConfig.setLocation( path );
438 int count = config.getConfiguration().getManagedRepositories().size();
439 config.getConfiguration().addManagedRepository( repoConfig );
441 String prefix = "managedRepositories.managedRepository(" + count + ")";
442 config.triggerChange( prefix + ".id", repoConfig.getId() );
443 config.triggerChange( prefix + ".name", repoConfig.getName() );
444 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
445 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
448 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
450 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
452 repoConfig.setId( id );
453 repoConfig.setName( name );
454 repoConfig.setLayout( layout );
455 repoConfig.setUrl( url );
457 int count = config.getConfiguration().getRemoteRepositories().size();
458 config.getConfiguration().addRemoteRepository( repoConfig );
460 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
461 config.triggerChange( prefix + ".id", repoConfig.getId() );
462 config.triggerChange( prefix + ".name", repoConfig.getName() );
463 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
464 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
465 repositoryRegistry.reload();
468 protected Path saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
471 Path repoLocation = Paths.get( targetPath );
472 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoLocation );
473 copyDirectoryStructure( Paths.get(originalPath) , repoLocation );
475 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
482 * Copy the specified resource directory from the src/test/repository/managed/ to
483 * the testable directory under target/test-repository/managed/${testName}/
485 * @param resourcePath
486 * @throws IOException
488 protected void setupTestableManagedRepository( String resourcePath )
491 String resourceDir = resourcePath;
493 if ( !resourcePath.endsWith( "/" ) )
495 int idx = resourcePath.lastIndexOf( '/' );
496 resourceDir = resourcePath.substring( 0, idx );
499 Path sourceRepoDir = Paths.get( REPOPATH_DEFAULT_MANAGED );
500 Path sourceDir = sourceRepoDir.resolve(resourceDir );
502 Path destRepoDir = managedDefaultDir;
503 Path destDir = destRepoDir.resolve(resourceDir );
505 // Cleanout destination dirs.
506 if ( Files.exists(destDir))
508 org.apache.archiva.common.utils.FileUtils.deleteDirectory( destDir );
511 // Make the destination dir.
512 Files.createDirectories(destDir);
514 // Test the source dir.
515 if ( !Files.exists(sourceDir) )
517 // This is just a warning.
518 log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
524 // Test that the source is a dir.
525 if ( !Files.isDirectory( sourceDir) )
527 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
530 // Copy directory structure.
531 copyDirectoryStructure( sourceDir, destDir );
535 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile )
537 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
540 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile, long time )
542 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
543 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
547 Files.setLastModifiedTime( managedFile,
548 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() + time, TimeUnit.MILLISECONDS ));
550 catch ( IOException e )
552 e.printStackTrace( );
557 assertTrue( Files.getLastModifiedTime( managedFile).compareTo( Files.getLastModifiedTime( remoteFile )) > 0);
559 catch ( IOException e )
561 e.printStackTrace( );
565 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile )
567 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
570 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile, long time )
572 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
573 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
577 Files.setLastModifiedTime( managedFile,
578 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() - time, TimeUnit.MILLISECONDS ));
580 catch ( IOException e )
582 e.printStackTrace( );
587 assertTrue( Files.getLastModifiedTime( managedFile ).compareTo(Files.getLastModifiedTime( remoteFile )) < 0 );
589 catch ( IOException e )
591 e.printStackTrace( );
596 protected void assertNotModified( Path file, long expectedModificationTime )
600 assertEquals( "File <" + file.toAbsolutePath() + "> not have been modified.", expectedModificationTime,
601 Files.getLastModifiedTime( file ).toMillis());
603 catch ( IOException e )
605 e.printStackTrace( );
610 protected void assertNotExistsInManagedDefaultRepo( Path testFile )
613 Path managedDefaultPath = managedDefaultDir;
615 assertTrue( "Unit Test Failure: File <" + testFile
616 + "> should be have been defined within the managed default path of <" + managedDefaultPath
617 + ">", testFile.startsWith( managedDefaultPath ) );
619 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", Files.exists(testFile) );
622 protected static Date getFutureDate()
623 throws ParseException
625 Calendar cal = Calendar.getInstance();
626 cal.add( Calendar.YEAR, 1 );
627 return cal.getTime();
630 protected static Date getPastDate()
631 throws ParseException
633 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );