import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Date;
import static org.mockito.Mockito.*;
}
private void setUpMockRepository()
- throws RepositoryAdminException
+ throws RepositoryAdminException, IOException
{
- File repoDir = new File( "target/test-consumer-repo" );
- repoDir.mkdirs();
- repoDir.deleteOnExit();
+ Path repoDir = Paths.get( "target/test-consumer-repo" );
+ Files.createDirectories( repoDir );
+ repoDir.toFile().deleteOnExit();
testRepository = new ManagedRepository();
testRepository.setName( "Test-Consumer-Repository" );
testRepository.setId( "test-consumer-repository" );
- testRepository.setLocation( repoDir.getAbsolutePath() );
+ testRepository.setLocation( repoDir.toAbsolutePath().toString() );
when( managedRepositoryAdmin.getManagedRepository( testRepository.getId() ) ).thenReturn( testRepository );
}
import javax.annotation.PostConstruct;
import javax.inject.Inject;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
- private File repositoryDir;
+ private Path repositoryDir;
private List<String> includes = new ArrayList<>( 0 );
public void beginScan( ManagedRepository repo, Date whenGathered )
throws ConsumerException
{
- this.repositoryDir = new File( repo.getLocation( ) );
+ this.repositoryDir = Paths.get( repo.getLocation( ) );
}
@Override
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
{
- File artifactFile = new File( this.repositoryDir, path );
- File checksumFile = new File( this.repositoryDir, path + "." + checksumAlgorithm.getExt( ) );
+ Path artifactFile = repositoryDir.resolve(path);
+ Path checksumFile = repositoryDir.resolve(path + "." + checksumAlgorithm.getExt( ) );
- if ( checksumFile.exists( ) )
+ if ( Files.exists(checksumFile) )
{
- checksum = new ChecksummedFile( artifactFile.toPath() );
+ checksum = new ChecksummedFile( artifactFile);
try
{
if ( !checksum.isValidChecksum( checksumAlgorithm ) )
{
checksum.fixChecksums( new ChecksumAlgorithm[]{checksumAlgorithm} );
- log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath( ) );
- triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath( ) );
+ log.info( "Fixed checksum file {}", checksumFile.toAbsolutePath( ) );
+ triggerConsumerInfo( "Fixed checksum file " + checksumFile.toAbsolutePath( ) );
}
}
catch ( IOException e )
": " + e.getMessage( ) );
}
}
- else if ( !checksumFile.exists( ) )
+ else if ( !Files.exists(checksumFile) )
{
- checksum = new ChecksummedFile( artifactFile.toPath() );
+ checksum = new ChecksummedFile( artifactFile);
try
{
checksum.createChecksum( checksumAlgorithm );
- log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath( ) );
- triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath( ) );
+ log.info( "Created missing checksum file {}", checksumFile.toAbsolutePath( ) );
+ triggerConsumerInfo( "Created missing checksum file " + checksumFile.toAbsolutePath( ) );
}
catch ( IOException e )
{
}
else
{
- log.warn( "Checksum file {} is not a file. ", checksumFile.getAbsolutePath( ) );
+ log.warn( "Checksum file {} is not a file. ", checksumFile.toAbsolutePath( ) );
triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
- "Checksum file " + checksumFile.getAbsolutePath( ) + " is not a file." );
+ "Checksum file " + checksumFile.toAbsolutePath( ) + " is not a file." );
}
}
import javax.annotation.PostConstruct;
import javax.inject.Inject;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Inject
private FileTypes filetypes;
- private File repositoryDir;
+ private Path repositoryDir;
private List<String> includes = new ArrayList<>( 0 );
public void beginScan( ManagedRepository repository, Date whenGathered )
throws ConsumerException
{
- this.repositoryDir = new File( repository.getLocation( ) );
+ this.repositoryDir = Paths.get( repository.getLocation( ) );
}
@Override
public void processFile( String path )
throws ConsumerException
{
- File file = new File( this.repositoryDir, path );
- if ( file.exists( ) )
+ Path file = this.repositoryDir.resolve(path );
+ if ( Files.exists(file) )
{
- log.info( "(Auto) Removing File: {}", file.getAbsolutePath( ) );
- triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) );
- file.delete( );
+ log.info( "(Auto) Removing File: {}", file.toAbsolutePath( ) );
+ triggerConsumerInfo( "(Auto) Removing File: " + file.toAbsolutePath( ) );
+ try
+ {
+ Files.deleteIfExists( file );
+ }
+ catch ( IOException e )
+ {
+ log.error("Could not delete file {}: {}", file, e.getMessage(), e);
+ throw new ConsumerException( "Could not delete file "+file );
+ }
}
}
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.consumers.AbstractMonitoredConsumer;
+import org.apache.archiva.consumers.Consumer;
import org.apache.archiva.consumers.ConsumerException;
import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
private static final String RENAME_FAILURE = "rename_failure";
- private File repositoryDir;
+ private Path repositoryDir;
private List<String> includes = new ArrayList<>( 3 );
public void beginScan( ManagedRepository repository, Date whenGathered )
throws ConsumerException
{
- this.repositoryDir = new File( repository.getLocation( ) );
+ this.repositoryDir = Paths.get( repository.getLocation( ) );
}
@Override
public void processFile( String path )
throws ConsumerException
{
- File file = new File( this.repositoryDir, path );
- if ( file.exists( ) )
+ Path file = this.repositoryDir.resolve( path );
+ if ( Files.exists(file) )
{
Iterator<String> itExtensions = this.extensionRenameMap.keySet( ).iterator( );
while ( itExtensions.hasNext( ) )
{
String fixedExtension = this.extensionRenameMap.get( extension );
String correctedPath = path.substring( 0, path.length( ) - extension.length( ) ) + fixedExtension;
- File to = new File( this.repositoryDir, correctedPath );
+ Path to = repositoryDir.resolve(correctedPath);
try
{
// Rename the file.
- FileUtils.moveFile( file, to );
+ FileUtils.moveFile( file.toFile(), to.toFile() );
}
catch ( IOException e )
{
}
}
- log.info( "(Auto) Removing File: {} ", file.getAbsolutePath( ) );
- triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) );
- file.delete( );
+ log.info( "(Auto) Removing File: {} ", file.toAbsolutePath( ) );
+ triggerConsumerInfo( "(Auto) Removing File: " + file.toAbsolutePath( ) );
+ try
+ {
+ Files.delete( file );
+ }
+ catch ( IOException e )
+ {
+ log.error("Could not delete file {}: {}", file, e.getMessage(), e);
+ throw new ConsumerException( "File deletion failed "+file );
+ }
}
}
import javax.annotation.PostConstruct;
import javax.inject.Inject;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
private ManagedRepositoryContent repository;
- private File repositoryDir;
+ private Path repositoryDir;
private List<String> includes = new ArrayList<>( 0 );
try
{
this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId( ) );
- this.repositoryDir = new File( repository.getRepoRoot( ) );
+ this.repositoryDir = Paths.get( repository.getRepoRoot( ) );
this.scanStartTimestamp = System.currentTimeMillis( );
}
catch ( RepositoryNotFoundException e )
{
String metadataPath = this.metadataTools.toPath( projectRef );
- File projectMetadata = new File( this.repositoryDir, metadataPath );
+ Path projectMetadata = this.repositoryDir.resolve( metadataPath );
- if ( projectMetadata.exists( ) && ( projectMetadata.lastModified( ) >= this.scanStartTimestamp ) )
+ if ( Files.exists(projectMetadata) && ( Files.getLastModifiedTime( projectMetadata).toMillis() >= this.scanStartTimestamp ) )
{
// This metadata is up to date. skip it.
log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
{
String metadataPath = this.metadataTools.toPath( versionRef );
- File projectMetadata = new File( this.repositoryDir, metadataPath );
+ Path projectMetadata = this.repositoryDir.resolve( metadataPath );
- if ( projectMetadata.exists( ) && ( projectMetadata.lastModified( ) >= this.scanStartTimestamp ) )
+ if ( Files.exists(projectMetadata) && ( Files.getLastModifiedTime( projectMetadata ).toMillis() >= this.scanStartTimestamp ) )
{
// This metadata is up to date. skip it.
log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
import javax.annotation.PostConstruct;
import javax.inject.Inject;
-import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Inject
private DigesterUtils digesterUtils;
- private File repositoryDir;
+ private Path repositoryDir;
private List<String> includes;
public void beginScan( ManagedRepository repository, Date whenGathered )
throws ConsumerException
{
- this.repositoryDir = new File( repository.getLocation( ) );
+ this.repositoryDir = Paths.get( repository.getLocation( ) );
}
@Override
public void processFile( String path )
throws ConsumerException
{
- File checksumFile = new File( this.repositoryDir, path );
+ Path checksumFile = this.repositoryDir.resolve( path );
try
{
- if ( !checksum.isValidChecksum( checksumFile ) )
+ if ( !checksum.isValidChecksum( checksumFile.toFile() ) )
{
log.warn( "The checksum for {} is invalid.", checksumFile );
triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
import org.apache.archiva.repository.metadata.MetadataTools;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
{
try
{
- File artifactFile = new File( repository.getRepoRoot( ), path );
+ Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
- if ( !artifactFile.exists( ) )
+ if ( !Files.exists(artifactFile) )
{
// Nothing to do here, file doesn't exist, skip it.
return;
{
listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
artifactRef.getArtifactId( ), artifactRef.getVersion( ),
- artifactFile.getName( ) );
+ artifactFile.getFileName().toString() );
}
metadataRepository.removeProjectVersion( repository.getId( ), artifactRef.getGroupId( ),
artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
import org.apache.archiva.repository.layout.LayoutException;
import org.apache.commons.lang.time.DateUtils;
-import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
{
try
{
- File artifactFile = new File( repository.getRepoRoot( ), path );
+ Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
- if ( !artifactFile.exists( ) )
+ if ( !Files.exists(artifactFile) )
{
return;
}
}
ArtifactReference newArtifactReference = repository.toArtifactReference(
- artifactFile.getAbsolutePath( ) );
+ artifactFile.toAbsolutePath( ).toString() );
newArtifactReference.setVersion( version );
Path newArtifactFile = repository.toFile( newArtifactReference );
import org.apache.archiva.repository.events.RepositoryListener;
import org.apache.archiva.repository.layout.LayoutException;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
{
try
{
- File artifactFile = new File( repository.getRepoRoot( ), path );
+ Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
- if ( !artifactFile.exists( ) )
+ if ( !Files.exists(artifactFile) )
{
return;
}
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import java.io.File;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
public abstract class AbstractArtifactConsumerTest
{
- private File repoLocation;
+ private Path repoLocation;
protected KnownRepositoryContentConsumer consumer;
assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
fileType.addPattern( "**/*.xml" );
- repoLocation = new File( "target/test-" + getName() + "/test-repo" );
+ repoLocation = Paths.get( "target/test-" + getName() + "/test-repo" );
}
@After
@Test
public void testConsumption()
{
- File localFile =
- new File( repoLocation, "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
+ Path localFile =
+ repoLocation.resolve( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
- BaseFile baseFile = new BaseFile( repoLocation, localFile );
+ BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
predicate.setBasefile( baseFile );
assertFalse( predicate.evaluate( consumer ) );
@Test
public void testConsumptionOfOtherMetadata()
{
- File localFile =
- new File( repoLocation, "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
+ Path localFile =
+ repoLocation.resolve( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
- BaseFile baseFile = new BaseFile( repoLocation, localFile );
+ BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
predicate.setBasefile( baseFile );
assertFalse( predicate.evaluate( consumer ) );
import org.junit.Before;
import org.junit.Test;
-import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
repoConfig.setId( "test-repo" );
repoConfig.setName( "Test Repository" );
repoConfig.setLayout( "default" );
- repoConfig.setLocation( new File( "target/test-classes/test-repo/" ).getPath() );
+ repoConfig.setLocation( Paths.get( "target/test-classes/test-repo/" ).toString() );
consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
KnownRepositoryContentConsumer.class );
public void testNoExistingChecksums()
throws Exception
{
- String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
+ String path = "no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
Path md5FilePath = Paths.get( repoConfig.getLocation(), path + ".md5" );
public void testExistingIncorrectChecksums()
throws Exception
{
- File newLocation = new File( "target/test-repo" );
- FileUtils.deleteDirectory( newLocation );
- FileUtils.copyDirectory( new File( repoConfig.getLocation() ), newLocation );
- repoConfig.setLocation( newLocation.getAbsolutePath() );
+ Path newLocation = Paths.get( "target/test-repo" );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( newLocation );
+ FileUtils.copyDirectory( Paths.get(repoConfig.getLocation() ).toFile(), newLocation.toFile() );
+ repoConfig.setLocation( newLocation.toAbsolutePath().toString() );
- String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
+ String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
config.setName( repoName );
config.setDaysOlder( TEST_DAYS_OLDER );
String path = AbstractRepositoryPurgeTest.fixPath(
- new File( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
+ Paths.get( "target/test-" + getName() + "/" + repoId ).toAbsolutePath().toString() );
config.setLocation( path );
config.setReleases( true );
config.setSnapshots( true );
protected void assertDeleted( String path )
{
- assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
+ assertFalse( "File should have been deleted: " + path, Files.exists(Paths.get( path )) );
}
protected void assertExists( String path )
{
- assertTrue( "File should exist: " + path, new File( path ).exists() );
+ assertTrue( "File should exist: " + path, Files.exists(Paths.get(path)) );
}
- protected File getTestRepoRoot()
+ protected Path getTestRepoRoot()
{
- return new File( "target/test-" + getName() + "/" + TEST_REPO_ID );
+ return Paths.get( "target/test-" + getName() + "/" + TEST_REPO_ID );
}
protected Path getTestRepoRootPath() {
throws Exception
{
removeMavenIndexes();
- File testDir = new File( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().getAbsolutePath() ) );
- FileUtils.deleteDirectory( testDir );
- File sourceDir = new File( new File( "target/test-classes/" + TEST_REPO_ID ).getAbsolutePath() );
- FileUtils.copyDirectory( sourceDir, testDir );
+ Path testDir = Paths.get( AbstractRepositoryPurgeTest.fixPath( getTestRepoRoot().toAbsolutePath().toString() ) );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( testDir );
+ Path sourceDir = Paths.get( Paths.get( "target/test-classes/" + TEST_REPO_ID ).toAbsolutePath().toString() );
+ FileUtils.copyDirectory( sourceDir.toFile(), testDir.toFile() );
- File releasesTestDir = new File( AbstractRepositoryPurgeTest.fixPath(
- new File( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() ) );
+ Path releasesTestDir = Paths.get( AbstractRepositoryPurgeTest.fixPath(
+ Paths.get( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() ) );
- FileUtils.deleteDirectory( releasesTestDir );
- File sourceReleasesDir =
- new File( new File( "target/test-classes/" + RELEASES_TEST_REPO_ID ).getAbsolutePath() );
- FileUtils.copyDirectory( sourceReleasesDir, releasesTestDir );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( releasesTestDir );
+ Path sourceReleasesDir =
+ Paths.get( Paths.get( "target/test-classes/" + RELEASES_TEST_REPO_ID ).toAbsolutePath().toString() );
+ FileUtils.copyDirectory( sourceReleasesDir.toFile(), releasesTestDir.toFile() );
- return AbstractRepositoryPurgeTest.fixPath( testDir.getAbsolutePath() );
+ return AbstractRepositoryPurgeTest.fixPath( testDir.toAbsolutePath().toString() );
}
public String getName()
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import java.io.File;
import java.nio.charset.Charset;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
assertExists( projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1" );
// check if metadata file was updated
- File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
+ Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
- String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
+ String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
String expectedVersions =
"<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
// test listeners for the correct artifacts
listenerControl.replay();
- File file = new File( repoRoot, INDEX_PATH );
- if ( !file.exists() )
+ Path file = Paths.get(repoRoot, INDEX_PATH );
+ if ( !Files.exists(file) )
{
// help windauze to create directory with .
- file.getParentFile().mkdirs();
- file.createNewFile();
+ Files.createDirectories( file.getParent() );
+ Files.createFile( file );
}
- assertTrue( file.exists() );
+ assertTrue( Files.exists(file) );
repoPurge.process( INDEX_PATH );
listenerControl.verify();
- assertTrue( file.exists() );
+ assertTrue( Files.exists(file) );
}
@Test
assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" );
String releasesProjectRoot =
- AbstractRepositoryPurgeTest.fixPath( new File( "target/test-" + getName() + "/releases-test-repo-one" ).getAbsolutePath()
+ AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString()
+ "/org/apache/archiva/released-artifact-in-diff-repo" );
// check if the released version was not removed
assertExists( projectRoot + "/2.0.4-SNAPSHOT/maven-source-plugin-2.0.4-SNAPSHOT.pom.sha1" );
// check if metadata file was not updated (because nothing was removed)
- File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
+ Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
- String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
+ String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
String expectedVersions = "<expected><versions><version>2.0.3-SNAPSHOT</version>"
+ "<version>2.0.4-SNAPSHOT</version></versions></expected>";
import org.junit.Test;
import org.mockito.ArgumentCaptor;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
{
private static final int OLD_TIMESTAMP = 1179382029;
- private void setLastModified( String dirPath, long lastModified )
+ private void setLastModified( String dirPath, long lastModified ) throws IOException
{
- File dir = new File( dirPath );
- File[] contents = dir.listFiles();
- for ( File content : contents )
+ Path dir = Paths.get( dirPath );
+ Path[] contents = Files.list( dir ).toArray(Path[]::new );
+ for ( Path content : contents )
{
- content.setLastModified( lastModified );
+ Files.setLastModifiedTime( content, FileTime.fromMillis( lastModified ));
}
}
for ( int i = 5; i <= 7; i++ )
{
- File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
- jarFile.createNewFile();
- File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
- pomFile.createNewFile();
+ Path jarFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
+ Files.createFile( jarFile );
+ Path pomFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
+ Files.createFile(pomFile);
// set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
if ( i == 5 )
{
- jarFile.setLastModified( OLD_TIMESTAMP );
- pomFile.setLastModified( OLD_TIMESTAMP );
+ Files.setLastModifiedTime( jarFile, FileTime.fromMillis( OLD_TIMESTAMP ));
+ Files.setLastModifiedTime( pomFile, FileTime.fromMillis( OLD_TIMESTAMP ));
}
}
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.model.MetadataFacet;
import org.apache.archiva.mock.MockRepositorySessionFactory;
-import org.apache.commons.io.FileUtils;
import org.custommonkey.xmlunit.XMLAssert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
-import java.io.File;
+import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.FileTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RepositoryPurgeConsumerTest
extends AbstractRepositoryPurgeTest
{
+ private static final Logger log = LoggerFactory.getLogger( RepositoryPurgeConsumerTest.class );
+
@Before
@Override
public void setUp()
applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
KnownRepositoryContentConsumer.class );
- File repoLocation = new File( "target/test-" + getName() + "/test-repo" );
+ Path repoLocation = Paths.get( "target/test-" + getName() + "/test-repo" );
- File localFile = new File( repoLocation, path );
+ Path localFile = repoLocation.resolve( path );
ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
- BaseFile baseFile = new BaseFile( repoLocation, localFile );
+ BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
predicate.setBasefile( baseFile );
assertFalse( predicate.evaluate( repoPurgeConsumer ) );
}
- private void setLastModified( String path )
+ private void setLastModified( String path ) throws IOException
{
- File dir = new File( path );
- File[] contents = dir.listFiles();
+ Path dir = Paths.get( path );
+ Path[] contents = new Path[0];
+ try
+ {
+ contents = Files.list( dir ).toArray(Path[]::new);
+ }
+ catch ( IOException e )
+ {
+ log.error("Could not list files {}: {}", dir, e.getMessage(), e);
+ contents = new Path[0];
+ }
for ( int i = 0; i < contents.length; i++ )
{
- contents[i].setLastModified( 1179382029 );
+ Files.setLastModifiedTime( contents[i], FileTime.fromMillis( 1179382029 ) );
}
}
assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
// check if metadata file wasn't updated
- File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
+ Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
- String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
+ String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
// check if metadata file was updated
- File artifactMetadataFile = new File( projectRoot + "/maven-metadata.xml" );
+ Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
- String metadataXml = FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
+ String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
String expectedVersions =
"<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
private FileTypes filetypes;
- private File managedRepository;
+ private Path managedRepository;
private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
throws ConsumerException
{
this.repository = repository;
- managedRepository = new File( repository.getLocation() );
+ managedRepository = Paths.get( repository.getLocation() );
try
{
else
{
this.repository = repository;
- managedRepository = new File( repository.getLocation() );
+ managedRepository = Paths.get( repository.getLocation() );
}
}
public void processFile( String path )
throws ConsumerException
{
- File artifactFile = new File( managedRepository, path );
+ Path artifactFile = managedRepository.resolve(path);
ArtifactIndexingTask task =
- new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
+ new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD, getIndexingContext() );
try
{
log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
}
else
{
- File artifactFile = new File( managedRepository, path );
+ Path artifactFile = managedRepository.resolve(path);
// specify in indexing task that this is not a repo scan request!
ArtifactIndexingTask task =
- new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
+ new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD,
getIndexingContext(), false );
// only update index we don't need to scan the full repo here
task.setOnlyUpdate( true );
import org.apache.archiva.scheduler.ArchivaTaskScheduler;
import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
-import org.apache.commons.io.FileUtils;
import org.apache.maven.index.NexusIndexer;
import org.apache.maven.index.context.IndexCreator;
import org.junit.After;
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
private final class ArchivaTaskSchedulerStub
implements ArchivaTaskScheduler<ArtifactIndexingTask>
{
- Set<File> indexed = new HashSet<>();
+ Set<Path> indexed = new HashSet<>();
@Override
public void queueTask( ArtifactIndexingTask task )
switch ( task.getAction() )
{
case ADD:
- indexed.add( task.getResourceFile() );
+ indexed.add( task.getResourceFile().toPath() );
break;
case DELETE:
indexed.remove( task.getResourceFile() );
throws Exception
{
// delete created index in the repository
- File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
- FileUtils.deleteDirectory( indexDir );
- assertFalse( indexDir.exists() );
+ Path indexDir = Paths.get( repositoryConfig.getLocation(), ".indexer" );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
+ assertFalse( Files.exists(indexDir) );
- indexDir = new File( repositoryConfig.getLocation(), ".index" );
- FileUtils.deleteDirectory( indexDir );
- assertFalse( indexDir.exists() );
+ indexDir = Paths.get( repositoryConfig.getLocation(), ".index" );
+ org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
+ assertFalse( Files.exists(indexDir) );
super.tearDown();
}
public void testIndexerIndexArtifact()
throws Exception
{
- File artifactFile = new File( repositoryConfig.getLocation(),
+ Path artifactFile = Paths.get( repositoryConfig.getLocation(),
"org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
// begin scan
public void testIndexerArtifactAlreadyIndexed()
throws Exception
{
- File artifactFile = new File( repositoryConfig.getLocation(),
+ Path artifactFile = Paths.get( repositoryConfig.getLocation(),
"org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
// begin scan
public void testIndexerIndexArtifactThenPom()
throws Exception
{
- File artifactFile = new File( repositoryConfig.getLocation(),
+ Path artifactFile = Paths.get( repositoryConfig.getLocation(),
"org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
// begin scan
assertTrue( scheduler.indexed.contains( artifactFile ) );
artifactFile =
- new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
+ Paths.get( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
// scan and index again
now = Calendar.getInstance().getTime();