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.configuration.*;
24 import org.apache.archiva.policies.*;
25 import org.apache.archiva.proxy.model.RepositoryProxyHandler;
26 import org.apache.archiva.repository.*;
27 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
28 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
29 import org.apache.archiva.repository.storage.StorageAsset;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.maven.wagon.Wagon;
32 import org.easymock.EasyMock;
33 import org.easymock.IMocksControl;
34 import org.junit.Before;
35 import org.junit.runner.RunWith;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
42 import java.io.BufferedReader;
43 import java.io.FileReader;
44 import java.io.IOException;
45 import java.nio.charset.Charset;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.nio.file.attribute.FileTime;
50 import java.text.ParseException;
51 import java.text.SimpleDateFormat;
53 import java.util.concurrent.TimeUnit;
54 import java.util.stream.Collectors;
56 import static org.junit.Assert.*;
59 * AbstractProxyTestCase
61 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
63 public abstract class AbstractProxyTestCase
66 protected ApplicationContext applicationContext;
69 private ProxyRegistry proxyRegistry;
72 RepositoryRegistry repositoryRegistry;
74 @SuppressWarnings( "unused" )
76 ManagedRepositoryHandler managedRepositoryHandler;
79 protected static final String ID_PROXIED1 = "proxied1";
81 protected static final String ID_PROXIED1_TARGET = "proxied1-target";
83 protected static final String ID_PROXIED2 = "proxied2";
85 protected static final String ID_PROXIED2_TARGET = "proxied2-target";
87 protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
89 protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
91 protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
93 protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
95 protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
97 protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
99 // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
101 protected IMocksControl wagonMockControl;
103 protected Wagon wagonMock;
106 protected RepositoryProxyHandler proxyHandler;
108 protected ManagedRepositoryContent managedDefaultRepository;
110 protected Path managedDefaultDir;
112 protected MockConfiguration config;
114 protected Logger log = LoggerFactory.getLogger( getClass() );
116 WagonDelegate delegate;
123 (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
125 config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
126 config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
127 config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
128 ArchivaRuntimeConfiguration runtimeConfiguration = new ArchivaRuntimeConfiguration();
129 List<String> checksumTypes = new ArrayList<>();
130 checksumTypes.add("md5");
131 checksumTypes.add("sha256");
132 checksumTypes.add("sha1");
133 checksumTypes.add("asc");
134 runtimeConfiguration.setChecksumTypes(checksumTypes);
135 config.getConfiguration().setArchivaRuntimeConfiguration(runtimeConfiguration);
136 repositoryRegistry.setArchivaConfiguration( config );
138 // Setup source repository (using default layout)
139 String name = getClass().getSimpleName();
140 Path repoPath = Paths.get("target/test-repository/managed/" + name);
142 managedDefaultRepository =
143 createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath.toString(), "default" );
145 managedDefaultDir = repoPath.resolve(ID_DEFAULT_MANAGED) ;
147 org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(ID_DEFAULT_MANAGED);
149 // Setup target (proxied to) repository.
150 saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
151 Paths.get( REPOPATH_PROXIED1 ).toUri().toURL().toExternalForm(), "default" );
153 // Setup target (proxied to) repository.
154 saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
155 Paths.get( REPOPATH_PROXIED2 ).toUri().toURL().toExternalForm(), "default" );
158 repositoryRegistry.reload();
159 repositoryRegistry.putRepository(repoConfig);
162 // Setup the proxy handler.
163 //proxyHandler = applicationContext.getBean (RepositoryProxyHandler) lookup( RepositoryProxyHandler.class.getName() );
165 proxyHandler = applicationContext.getBean( "repositoryProxyHandler#test", RepositoryProxyHandler.class );
166 assertNotNull( proxyRegistry );
167 assertTrue(proxyRegistry.getAllHandler( ).get( RepositoryType.MAVEN).contains( proxyHandler ));
170 // Setup the wagon mock.
171 wagonMockControl = EasyMock.createNiceControl();
172 wagonMock = wagonMockControl.createMock( Wagon.class );
174 delegate = (WagonDelegate) applicationContext.getBean( "wagon#http", Wagon.class );
176 delegate.setDelegate( wagonMock );
178 CacheManager.getInstance().clearAll();
180 log.info( "\n.\\ {}() \\._________________________________________\n", name );
183 protected void assertChecksums( Path expectedFile, String expectedSha1Contents, String expectedMd5Contents )
186 Path sha1File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString()+ ".sha1" );
187 Path md5File = expectedFile.toAbsolutePath().resolveSibling( expectedFile.getFileName().toString() + ".md5" );
189 if ( expectedSha1Contents == null )
191 assertFalse( "SHA1 File should NOT exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
195 assertTrue( "SHA1 File should exist: " + sha1File.toAbsolutePath(), Files.exists(sha1File) );
196 String actualSha1Contents = readChecksumFile( sha1File );
197 assertEquals( "SHA1 File contents: " + sha1File.toAbsolutePath(), expectedSha1Contents, actualSha1Contents );
200 if ( expectedMd5Contents == null )
202 assertFalse( "MD5 File should NOT exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
206 assertTrue( "MD5 File should exist: " + md5File.toAbsolutePath(), Files.exists(md5File) );
207 String actualMd5Contents = readChecksumFile( md5File );
208 assertEquals( "MD5 File contents: " + md5File.toAbsolutePath(), expectedMd5Contents, actualMd5Contents );
212 protected void assertFileEquals( Path expectedFile, Path actualFile, Path sourceFile )
215 assertNotNull( "Expected File should not be null.", expectedFile );
216 assertNotNull( "Actual File should not be null.", actualFile );
218 assertTrue( "Check actual file exists.", Files.exists(actualFile) );
219 assertTrue("Check expected file exists", Files.exists(expectedFile));
220 assertTrue( "Check file is the same.", Files.isSameFile( expectedFile,
222 String expectedContents =
223 org.apache.commons.io.FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
224 String actualContents =
225 org.apache.commons.io.FileUtils.readFileToString( actualFile.toFile(), Charset.defaultCharset() );
226 assertEquals( "Check file contents.", expectedContents, actualContents );
229 protected void assertNotDownloaded( StorageAsset downloadedFile )
231 assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
234 @SuppressWarnings( "unchecked" )
235 protected void assertNoTempFiles( Path expectedFile )
237 Path workingDir = expectedFile.getParent();
238 if ( ( workingDir == null ) || !Files.isDirectory( workingDir) )
243 Collection<Path> tmpFiles = null;
245 tmpFiles = Files.list(workingDir).filter(path -> Files.isRegularFile(path) && path.getFileName().toString().endsWith(".tmp")).collect(Collectors.toList());
246 } catch (IOException e) {
247 log.error("Could not retrieve tmpFiles {}", workingDir);
249 if ( tmpFiles!=null && !tmpFiles.isEmpty() )
251 StringBuilder emsg = new StringBuilder();
252 emsg.append( "Found Temp Files in dir: " ).append( workingDir.toString() );
253 for ( Path tfile : tmpFiles )
255 emsg.append( "\n " ).append( tfile.getFileName().toString());
257 fail( emsg.toString() );
262 * A faster recursive copy that omits .svn directories.
264 * @param sourceDirectory the source directory to copy
265 * @param destDirectory the target location
266 * @throws java.io.IOException if there is a copying problem
267 * @todo get back into plexus-utils, share with converter module
269 protected void copyDirectoryStructure( Path sourceDirectory, Path destDirectory )
272 if ( !Files.exists(sourceDirectory) )
274 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
277 Path[] files = Files.list(sourceDirectory).filter(path -> Files.isRegularFile(path)).toArray(Path[]::new);
279 String sourcePath = sourceDirectory.toAbsolutePath().toString();
281 for ( int i = 0; i < files.length; i++ )
283 Path file = files[i];
285 String dest = file.toAbsolutePath().toString();
287 dest = dest.substring( sourcePath.length() + 1 );
289 Path destination = destDirectory.resolve( dest );
291 if ( Files.isRegularFile(file) )
293 destination = destination.getParent();
295 org.apache.commons.io.FileUtils.copyFile( file.toFile(), destination.resolve( file.getFileName() ).toFile(), false );
296 // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
297 //FileUtils.copyFileToDirectory( file, destination );
299 else if ( Files.isDirectory(file) )
301 if ( !".svn".equals( file.getFileName().toString() ) )
303 if ( !Files.exists(destination))
305 Files.createDirectories(destination);
308 copyDirectoryStructure( file, destination );
313 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
319 protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
322 ManagedRepository repo = BasicManagedRepository.newFilesystemInstance(id, name, Paths.get(path).resolve(id));
323 repositoryRegistry.putRepository(repo);
324 return repositoryRegistry.getManagedRepository(id).getContent();
328 * Read the first line from the checksum file, and return it (trimmed).
330 protected String readChecksumFile( Path checksumFile )
333 FileReader freader = null;
334 BufferedReader buf = null;
338 freader = new FileReader( checksumFile.toFile() );
339 buf = new BufferedReader( freader );
340 return buf.readLine();
349 if ( freader != null )
356 protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
358 saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
359 CachedFailuresPolicy.NO, disabled );
362 protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
363 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, boolean disabled )
365 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
366 PropagateErrorsDownloadPolicy.QUEUE, disabled );
369 protected void saveConnector( String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
370 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
373 saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
374 errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
377 protected void saveConnector(String sourceRepoId, String targetRepoId, PolicyOption checksumPolicy, PolicyOption releasePolicy,
378 PolicyOption snapshotPolicy, PolicyOption cacheFailuresPolicy, PolicyOption errorPolicy,
379 PolicyOption errorOnUpdatePolicy, boolean disabled )
381 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
382 connectorConfig.setSourceRepoId( sourceRepoId );
383 connectorConfig.setTargetRepoId( targetRepoId );
384 connectorConfig.setProxyId(sourceRepoId);
385 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy.getId() );
386 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy.getId() );
387 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy.getId() );
388 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy.getId());
389 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy.getId() );
390 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy.getId() );
391 connectorConfig.setDisabled( disabled );
393 int count = config.getConfiguration().getProxyConnectors().size();
394 config.getConfiguration().addProxyConnector( connectorConfig );
396 // Proper Triggering ...
397 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
398 config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
399 config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
400 config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
401 config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
402 config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
403 config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
404 config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
405 config.triggerChange( prefix + ".policies.propagate-errors",
406 connectorConfig.getPolicy( "propagate-errors", "" ) );
407 config.triggerChange( prefix + ".policies.propagate-errors-on-update",
408 connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
411 protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
413 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
415 repoConfig.setId( id );
416 repoConfig.setName( name );
417 repoConfig.setLayout( layout );
419 repoConfig.setLocation( path );
421 int count = config.getConfiguration().getManagedRepositories().size();
422 config.getConfiguration().addManagedRepository( repoConfig );
424 String prefix = "managedRepositories.managedRepository(" + count + ")";
425 config.triggerChange( prefix + ".id", repoConfig.getId() );
426 config.triggerChange( prefix + ".name", repoConfig.getName() );
427 config.triggerChange( prefix + ".location", repoConfig.getLocation() );
428 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
431 protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
433 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
435 repoConfig.setId( id );
436 repoConfig.setName( name );
437 repoConfig.setLayout( layout );
438 repoConfig.setUrl( url );
440 int count = config.getConfiguration().getRemoteRepositories().size();
441 config.getConfiguration().addRemoteRepository( repoConfig );
443 String prefix = "remoteRepositories.remoteRepository(" + count + ")";
444 config.triggerChange( prefix + ".id", repoConfig.getId() );
445 config.triggerChange( prefix + ".name", repoConfig.getName() );
446 config.triggerChange( prefix + ".url", repoConfig.getUrl() );
447 config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
448 repositoryRegistry.reload();
451 protected Path saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
454 Path repoLocation = Paths.get( targetPath );
455 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoLocation );
456 copyDirectoryStructure( Paths.get(originalPath) , repoLocation );
458 saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
465 * Copy the specified resource directory from the src/test/repository/managed/ to
466 * the testable directory under target/test-repository/managed/${testName}/
468 * @param resourcePath
469 * @throws IOException
471 protected void setupTestableManagedRepository( String resourcePath )
474 String resourceDir = resourcePath;
476 if ( !resourcePath.endsWith( "/" ) )
478 int idx = resourcePath.lastIndexOf( '/' );
479 resourceDir = resourcePath.substring( 0, idx );
482 Path sourceRepoDir = Paths.get( REPOPATH_DEFAULT_MANAGED );
483 Path sourceDir = sourceRepoDir.resolve(resourceDir );
485 Path destRepoDir = managedDefaultDir;
486 Path destDir = destRepoDir.resolve(resourceDir );
488 // Cleanout destination dirs.
489 if ( Files.exists(destDir))
491 org.apache.archiva.common.utils.FileUtils.deleteDirectory( destDir );
494 // Make the destination dir.
495 Files.createDirectories(destDir);
497 // Test the source dir.
498 if ( !Files.exists(sourceDir) )
500 // This is just a warning.
501 log.error( "[WARN] Skipping setup of testable managed repository, source dir does not exist: {}", //
507 // Test that the source is a dir.
508 if ( !Files.isDirectory( sourceDir) )
510 fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
513 // Copy directory structure.
514 copyDirectoryStructure( sourceDir, destDir );
518 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile )
520 setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
523 protected void setManagedNewerThanRemote( Path managedFile, Path remoteFile, long time )
525 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
526 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
530 Files.setLastModifiedTime( managedFile,
531 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() + time, TimeUnit.MILLISECONDS ));
533 catch ( IOException e )
535 e.printStackTrace( );
540 assertTrue( Files.getLastModifiedTime( managedFile).compareTo( Files.getLastModifiedTime( remoteFile )) > 0);
542 catch ( IOException e )
544 e.printStackTrace( );
548 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile )
550 setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
553 protected void setManagedOlderThanRemote( Path managedFile, Path remoteFile, long time )
555 assertTrue( "Managed File should exist: ", Files.exists(managedFile) );
556 assertTrue( "Remote File should exist: ", Files.exists(remoteFile) );
560 Files.setLastModifiedTime( managedFile,
561 FileTime.from(Files.getLastModifiedTime( remoteFile ).toMillis() - time, TimeUnit.MILLISECONDS ));
563 catch ( IOException e )
565 e.printStackTrace( );
570 assertTrue( Files.getLastModifiedTime( managedFile ).compareTo(Files.getLastModifiedTime( remoteFile )) < 0 );
572 catch ( IOException e )
574 e.printStackTrace( );
579 protected void assertNotModified( Path file, long expectedModificationTime )
583 assertEquals( "File <" + file.toAbsolutePath() + "> not have been modified.", expectedModificationTime,
584 Files.getLastModifiedTime( file ).toMillis());
586 catch ( IOException e )
588 e.printStackTrace( );
593 protected void assertNotExistsInManagedDefaultRepo( Path testFile )
596 Path managedDefaultPath = managedDefaultDir;
598 assertTrue( "Unit Test Failure: File <" + testFile
599 + "> should be have been defined within the managed default path of <" + managedDefaultPath
600 + ">", testFile.startsWith( managedDefaultPath ) );
602 assertFalse( "File < " + testFile + "> should not exist in managed default repository.", Files.exists(testFile) );
605 protected static Date getFutureDate()
606 throws ParseException
608 Calendar cal = Calendar.getInstance();
609 cal.add( Calendar.YEAR, 1 );
610 return cal.getTime();
613 protected static Date getPastDate()
614 throws ParseException
616 return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );