diff options
8 files changed, 385 insertions, 337 deletions
diff --git a/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java b/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java index 880c5f17a..f853db81d 100644 --- a/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java +++ b/archiva-cli/src/main/java/org/apache/archiva/cli/ArchivaCli.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -263,9 +264,9 @@ public class ArchivaCli p.load( fis ); } - File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) ); + Path oldRepositoryPath = Paths.get( p.getProperty( SOURCE_REPO_PATH ) ); - File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) ); + Path newRepositoryPath = Paths.get( p.getProperty( TARGET_REPO_PATH ) ); LOGGER.info( "Converting {} to {}", oldRepositoryPath, newRepositoryPath ); diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java index 71dcb8da1..46c459b10 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/utils/FileUtils.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; @@ -71,23 +72,34 @@ public class FileUtils public static void deleteDirectory( Path dir ) throws IOException { + if (!Files.exists(dir)) { + return; + } if (!Files.isDirectory( dir )) { - throw new IOException("Given path is not a directory "); + throw new IOException("Given path is not a directory "+dir); } - boolean result = Files.walk(dir) - .sorted( Comparator.reverseOrder()) - .map( file -> { - try - { - Files.delete( file ); - return Optional.of(Boolean.TRUE); - } - catch ( IOException e ) + boolean result = true; + try + { + result = Files.walk( dir ) + .sorted( Comparator.reverseOrder( ) ) + .map( file -> { - return Optional.empty(); - } + try + { + Files.delete( file ); + return Optional.of( Boolean.TRUE ); + } + catch ( UncheckedIOException | IOException e ) + { + log.warn( "File could not be deleted {}", file ); + return Optional.empty( ); + } - }).allMatch( Optional::isPresent ); + } ).allMatch( Optional::isPresent ); + } catch (UncheckedIOException e) { + throw new IOException("File deletion failed ", e); + } if (!result) { throw new IOException("Error during recursive delete of "+dir.toAbsolutePath()); } @@ -119,7 +131,7 @@ public class FileUtils } /** - * Return the base directory + * Return the base directory * @return */ public static String getBasedir() diff --git a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/FileUtilsTest.java b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/FileUtilsTest.java index d165cbdaa..c589e7c91 100644 --- a/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/FileUtilsTest.java +++ b/archiva-modules/archiva-base/archiva-common/src/test/java/org/apache/archiva/common/utils/FileUtilsTest.java @@ -19,12 +19,20 @@ package org.apache.archiva.common.utils; * under the License. */ +import org.junit.Assert; +import org.junit.Assume; import org.junit.Test; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.PosixFilePermission; +import java.util.HashSet; +import java.util.Set; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -102,12 +110,51 @@ public class FileUtilsTest } - @Test(expected = java.io.IOException.class) - public void testDeleteException() throws IOException + @Test + public void testDeleteNonExist() throws IOException { Path tf = Paths.get("aaserijdmcjdjhdejeidmdjdlasrjerjnbmckdkdk"); assertFalse(Files.exists(tf)); FileUtils.deleteDirectory( tf ); } + @Test(expected = IOException.class) + public void testDeleteWithException() throws IOException + { + Assume.assumeTrue( FileSystems.getDefault().supportedFileAttributeViews().contains("posix") ); + Path tmpDir = Files.createTempDirectory( "FileUtilsTest" ); + Path tmpDir2 = tmpDir.resolve("testdir1"); + Files.createDirectories( tmpDir2 ); + Path tmpFile = tmpDir2.resolve("testfile1.txt"); + OutputStream stream = null; + try + { + stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE ); + stream.write( 1 ); + stream.close( ); + assertTrue( Files.exists( tmpFile ) ); + stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE ); + stream.write( 1 ); + Set<PosixFilePermission> perms = new HashSet<>( ); + Files.setPosixFilePermissions( tmpFile, perms ); + Files.setPosixFilePermissions( tmpDir2, perms ); + FileUtils.deleteDirectory( tmpDir ); + assertFalse( Files.exists( tmpDir ) ); + assertFalse( Files.exists( tmpFile ) ); + } finally { + if (stream!=null) { + stream.close(); + } + Set<PosixFilePermission> perms = new HashSet<>( ); + perms.add(PosixFilePermission.OWNER_READ); + perms.add(PosixFilePermission.OWNER_WRITE); + perms.add(PosixFilePermission.OWNER_EXECUTE); + Files.setPosixFilePermissions( tmpDir2, perms ); + Files.setPosixFilePermissions( tmpFile, perms ); + Files.deleteIfExists( tmpFile ); + Files.deleteIfExists( tmpDir2 ); + Files.deleteIfExists( tmpDir ); + } + } + } diff --git a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java index ba8bf2703..9e0c49c59 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java +++ b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java @@ -50,13 +50,14 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.inject.Inject; -import java.io.File; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -178,12 +179,12 @@ public class LegacyToDefaultConverter artifact.getVersion() ); pom.setBaseVersion( artifact.getBaseVersion() ); ArtifactRepository repository = artifact.getRepository(); - File file = new File( repository.getBasedir(), repository.pathOf( pom ) ); + Path file = Paths.get( repository.getBasedir(), repository.pathOf( pom ) ); boolean result = true; - if ( file.exists() ) + if ( Files.exists(file) ) { - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pom ) ); String contents = null; boolean checksumsValid = false; @@ -195,7 +196,7 @@ public class LegacyToDefaultConverter } // Even if the checksums for the POM are invalid we should still convert the POM - contents = FileUtils.readFileToString( file, Charset.defaultCharset() ); + contents = org.apache.archiva.common.utils.FileUtils.readFileToString( file, Charset.defaultCharset() ); } catch ( IOException e ) { @@ -206,23 +207,15 @@ public class LegacyToDefaultConverter if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 ) //$NON-NLS-1$ { // v4 POM - try + boolean matching = false; + if ( !force && Files.exists( targetFile ) ) { - boolean matching = false; - if ( !force && targetFile.exists() ) - { - String targetContents = FileUtils.readFileToString( targetFile, Charset.defaultCharset() ); - matching = targetContents.equals( contents ); - } - if ( force || !matching ) - { - transaction.createFile( contents, targetFile, digesters ); - } + String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString( targetFile, Charset.defaultCharset( ) ); + matching = targetContents.equals( contents ); } - catch ( IOException e ) + if ( force || !matching ) { - throw new ArtifactConversionException( - Messages.getString( "unable.to.write.target.pom", e.getMessage() ), e ); //$NON-NLS-1$ + transaction.createFile( contents, targetFile.toFile( ), digesters ); } } else @@ -243,7 +236,7 @@ public class LegacyToDefaultConverter artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); targetFile = - new File( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) ); + Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) ); } Model v4Model = translator.translate( v3Model ); @@ -254,7 +247,7 @@ public class LegacyToDefaultConverter MavenXpp3Writer xpp3Writer = new MavenXpp3Writer(); xpp3Writer.write( writer, v4Model ); - transaction.createFile( writer.toString(), targetFile, digesters ); + transaction.createFile( writer.toString(), targetFile.toFile(), digesters ); List<String> warnings = translator.getWarnings(); @@ -290,13 +283,13 @@ public class LegacyToDefaultConverter return result; } - private boolean testChecksums( Artifact artifact, File file ) + private boolean testChecksums( Artifact artifact, Path file ) throws IOException { boolean result = true; for ( Digester digester : digesters ) { - result &= verifyChecksum( file, file.getName() + "." + getDigesterFileExtension( digester ), digester, + result &= verifyChecksum( file, file.getFileName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$ artifact, "failure.incorrect." + getDigesterFileExtension( digester ) ); //$NON-NLS-1$ @@ -304,18 +297,18 @@ public class LegacyToDefaultConverter return result; } - private boolean verifyChecksum( File file, String fileName, Digester digester, Artifact artifact, String key ) + private boolean verifyChecksum( Path file, String fileName, Digester digester, Artifact artifact, String key ) throws IOException { boolean result = true; - File checksumFile = new File( file.getParentFile(), fileName ); - if ( checksumFile.exists() ) + Path checksumFile = file.resolveSibling( fileName ); + if ( Files.exists(checksumFile) ) { - String checksum = FileUtils.readFileToString( checksumFile, Charset.defaultCharset() ); + String checksum = org.apache.archiva.common.utils.FileUtils.readFileToString( checksumFile, Charset.defaultCharset() ); try { - digester.verify( file, checksum ); + digester.verify( file.toFile(), checksum ); } catch ( DigesterException e ) { @@ -338,22 +331,22 @@ public class LegacyToDefaultConverter private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction ) throws ArtifactConversionException { - File sourceFile = artifact.getFile(); + Path sourceFile = artifact.getFile().toPath(); - if ( sourceFile.getAbsolutePath().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$ + if ( sourceFile.toAbsolutePath().toString().indexOf( "/plugins/" ) > -1 ) //$NON-NLS-1$ { artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) ); //$NON-NLS-1$ } - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); boolean result = true; try { boolean matching = false; - if ( !force && targetFile.exists() ) + if ( !force && Files.exists(targetFile) ) { - matching = FileUtils.contentEquals( sourceFile, targetFile ); + matching = FileUtils.contentEquals( sourceFile.toFile(), targetFile.toFile() ); if ( !matching ) { addWarning( artifact, Messages.getString( "failure.target.already.exists" ) ); //$NON-NLS-1$ @@ -366,7 +359,7 @@ public class LegacyToDefaultConverter { if ( testChecksums( artifact, sourceFile ) ) { - transaction.copyFile( sourceFile, targetFile, digesters ); + transaction.copyFile( sourceFile.toFile(), targetFile.toFile(), digesters ); } else { @@ -390,12 +383,12 @@ public class LegacyToDefaultConverter return metadata; } - private Metadata readMetadata( File file ) + private Metadata readMetadata( Path file ) throws ArtifactConversionException { MetadataXpp3Reader reader = new MetadataXpp3Reader(); - try (Reader fileReader = Files.newBufferedReader( file.toPath(), Charset.defaultCharset() )) + try (Reader fileReader = Files.newBufferedReader( file, Charset.defaultCharset() )) { return reader.read( fileReader ); } @@ -414,17 +407,16 @@ public class LegacyToDefaultConverter boolean result = true; RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact ); - File file = - new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); - if ( file.exists() ) + Path file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); + if ( Files.exists(file) ) { Metadata metadata = readMetadata( file ); result = validateMetadata( metadata, repositoryMetadata, artifact ); } repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); - if ( file.exists() ) + file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); + if ( Files.exists(file) ) { Metadata metadata = readMetadata( file ); result = result && validateMetadata( metadata, repositoryMetadata, artifact ); @@ -539,13 +531,13 @@ public class LegacyToDefaultConverter Metadata newMetadata, FileTransaction transaction ) throws ArtifactConversionException { - File file = new File( targetRepository.getBasedir(), + Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); Metadata metadata; boolean changed; - if ( file.exists() ) + if ( Files.exists(file) ) { metadata = readMetadata( file ); changed = metadata.merge( newMetadata ); @@ -565,7 +557,7 @@ public class LegacyToDefaultConverter mappingWriter.write( writer, metadata ); - transaction.createFile( writer.toString(), file, digesters ); + transaction.createFile( writer.toString(), file.toFile(), digesters ); } catch ( IOException e ) { @@ -647,13 +639,13 @@ public class LegacyToDefaultConverter pom.setDistributionManagement( dMngt ); Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); //$NON-NLS-1$ - File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) ); + Path pomFile = Paths.get( repository.getBasedir(), repository.pathOf( artifact ) ); StringWriter strWriter = new StringWriter(); MavenXpp3Writer pomWriter = new MavenXpp3Writer(); pomWriter.write( strWriter, pom ); - transaction.createFile( strWriter.toString(), pomFile, digesters ); + transaction.createFile( strWriter.toString(), pomFile.toFile(), digesters ); } private void addWarning( Artifact artifact, String message ) diff --git a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java index 61d393e8d..a0e8497c8 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java +++ b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/DefaultLegacyRepositoryConverter.java @@ -35,7 +35,7 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.inject.Named; -import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -82,18 +82,18 @@ public class DefaultLegacyRepositoryConverter } @Override - public void convertLegacyRepository( File legacyRepositoryDirectory, File repositoryDirectory, + public void convertLegacyRepository( Path legacyRepositoryDirectory, Path repositoryDirectory, List<String> fileExclusionPatterns ) throws RepositoryConversionException { try { - String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory ); + String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory.toFile() ); ManagedRepository legacyRepository = new ManagedRepository(); legacyRepository.setId( "legacy" ); legacyRepository.setName( "Legacy Repository" ); - legacyRepository.setLocation( legacyRepositoryDirectory.getAbsolutePath() ); + legacyRepository.setLocation( legacyRepositoryDirectory.toAbsolutePath().toString() ); legacyRepository.setLayout( "legacy" ); ArtifactRepository repository = diff --git a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/LegacyRepositoryConverter.java b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/LegacyRepositoryConverter.java index 8cbb8e655..bfa4b9ca2 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/LegacyRepositoryConverter.java +++ b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/legacy/LegacyRepositoryConverter.java @@ -21,7 +21,7 @@ package org.apache.archiva.converter.legacy; import org.apache.archiva.converter.RepositoryConversionException; -import java.io.File; +import java.nio.file.Path; import java.util.List; /** @@ -39,7 +39,7 @@ public interface LegacyRepositoryConverter * @param fileExclusionPatterns the list of patterns to exclude from the conversion. * @throws RepositoryConversionException */ - void convertLegacyRepository( File legacyRepositoryDirectory, File destinationRepositoryDirectory, + void convertLegacyRepository( Path legacyRepositoryDirectory, Path destinationRepositoryDirectory, List<String> fileExclusionPatterns ) throws RepositoryConversionException; } diff --git a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/RepositoryConverterTest.java b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/RepositoryConverterTest.java index 58ed74111..8abe89d4b 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/RepositoryConverterTest.java +++ b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/RepositoryConverterTest.java @@ -34,8 +34,10 @@ import org.springframework.test.context.ContextConfiguration; import javax.inject.Inject; import javax.inject.Named; -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.List; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; @@ -77,20 +79,20 @@ public class RepositoryConverterTest ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" ); //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" ); - File sourceBase = new File( "src/test/source-repository" ); - sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, + Path sourceBase = Paths.get( "src/test/source-repository" ); + sourceRepository = factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null ); layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" ); //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - File targetBase = new File( "target/test-target-repository" ); - copyDirectoryStructure( new File( "src/test/target-repository" ), targetBase ); + Path targetBase = Paths.get( "target/test-target-repository" ); + copyDirectoryStructure( Paths.get( "src/test/target-repository" ), targetBase ); targetRepository = new ManagedRepositoryConfiguration(); targetRepository.setId( "target" ); targetRepository.setName( "Target Repo" ); - targetRepository.setLocation( targetBase.getAbsolutePath() ); + targetRepository.setLocation( targetBase.toAbsolutePath().toString() ); targetRepository.setLayout( "default" ); //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" ); @@ -103,49 +105,48 @@ public class RepositoryConverterTest super.tearDown(); } - private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory ) + private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory ) throws IOException { - if ( !sourceDirectory.exists() ) + if ( !Files.exists(sourceDirectory) ) { - throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." ); + throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." ); } - File[] files = sourceDirectory.listFiles(); + Path[] files = Files.list(sourceDirectory).toArray(Path[]::new); - String sourcePath = sourceDirectory.getAbsolutePath(); + String sourcePath = sourceDirectory.toAbsolutePath().toString(); for ( int i = 0; i < files.length; i++ ) { - File file = files[i]; + Path file = files[i]; - String dest = file.getAbsolutePath(); + String dest = file.toAbsolutePath().toString(); dest = dest.substring( sourcePath.length() + 1 ); - File destination = new File( destinationDirectory, dest ); + Path destination = destinationDirectory.resolve( dest ); - if ( file.isFile() ) + if ( Files.isRegularFile(file) ) { - destination = destination.getParentFile(); + destination = destination.getParent(); - FileUtils.copyFileToDirectory( file, destination ); + FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() ); } - else if ( file.isDirectory() ) + else if ( Files.isDirectory(file) ) { - if ( !".svn".equals( file.getName() ) ) + if ( !".svn".equals( file.getFileName().toString() ) ) { - if ( !destination.exists() && !destination.mkdirs() ) + if ( !Files.exists(destination)) { - throw new IOException( "Could not create destination directory '" - + destination.getAbsolutePath() + "'." ); + Files.createDirectories( destination ); } copyDirectoryStructure( file, destination ); } } else { - throw new IOException( "Unknown file type: " + file.getAbsolutePath() ); + throw new IOException( "Unknown file type: " + file.toAbsolutePath() ); } } } @@ -154,8 +155,8 @@ public class RepositoryConverterTest public void testLegacyConversion() throws IOException, RepositoryConversionException { - File legacyRepoDir = new File( sourceRepository.getBasedir() ); - File destRepoDir = new File( targetRepository.getLocation() ); + Path legacyRepoDir = Paths.get( sourceRepository.getBasedir() ); + Path destRepoDir = Paths.get( targetRepository.getLocation() ); List<String> excludes = new ArrayList<>(); repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes ); } diff --git a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java index 8d79bd682..5084bffa9 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java +++ b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java @@ -38,14 +38,18 @@ import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import javax.inject.Inject; -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.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; /** @@ -86,17 +90,17 @@ public class LegacyToDefaultConverterTest ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" ); - File sourceBase = getTestFile( "src/test/source-repository" ); + Path sourceBase = getTestFile( "src/test/source-repository" ); sourceRepository = - factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null ); + factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null ); layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" ); - File targetBase = getTestFile( "target/test-target-repository" ); + Path targetBase = getTestFile( "target/test-target-repository" ); copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase ); targetRepository = - factory.createArtifactRepository( "target", targetBase.toURL().toString(), layout, null, null ); + factory.createArtifactRepository( "target", targetBase.toUri().toURL().toString(), layout, null, null ); artifactConverter = applicationContext.getBean( "artifactConverter#legacy-to-default", ArtifactConverter.class ); @@ -105,54 +109,53 @@ public class LegacyToDefaultConverterTest artifactFactory = (ArtifactFactory) plexusSisuBridge.lookup( ArtifactFactory.class ); } - public static File getTestFile( String path ) + public static Path getTestFile( String path ) { - return new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), path ); + return Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), path ); } - private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory ) + private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory ) throws IOException { - if ( !sourceDirectory.exists() ) + if ( !Files.exists(sourceDirectory) ) { - throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." ); + throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath()+ ")." ); } - File[] files = sourceDirectory.listFiles(); + Path[] files = Files.list( sourceDirectory ).toArray( Path[]::new ); - String sourcePath = sourceDirectory.getAbsolutePath(); + String sourcePath = sourceDirectory.toAbsolutePath().toString(); for ( int i = 0; i < files.length; i++ ) { - File file = files[i]; + Path file = files[i]; - String dest = file.getAbsolutePath(); + String dest = file.toAbsolutePath().toString(); dest = dest.substring( sourcePath.length() + 1 ); - File destination = new File( destinationDirectory, dest ); + Path destination = destinationDirectory.resolve( dest ); - if ( file.isFile() ) + if ( Files.isRegularFile( file ) ) { - destination = destination.getParentFile(); + destination = destination.getParent(); - FileUtils.copyFileToDirectory( file, destination ); + FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() ); } - else if ( file.isDirectory() ) + else if ( Files.isDirectory( file ) ) { - if ( !".svn".equals( file.getName() ) ) + if ( !".svn".equals( file.getFileName().toString() ) ) { - if ( !destination.exists() && !destination.mkdirs() ) + if ( !Files.exists(destination)) { - throw new IOException( - "Could not create destination directory '" + destination.getAbsolutePath() + "'." ); + Files.createDirectories( destination ); } copyDirectoryStructure( file, destination ); } } else { - throw new IOException( "Unknown file type: " + file.getAbsolutePath() ); + throw new IOException( "Unknown file type: " + file.toAbsolutePath() ); } } } @@ -165,38 +168,38 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v4artifact", "1.0.0" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists( artifactMetadataFile); ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File versionMetadataFile = new File( targetRepository.getBasedir(), + Path versionMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) ); - versionMetadataFile.delete(); + Files.deleteIfExists(versionMetadataFile); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - artifactFile.delete(); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Files.deleteIfExists(artifactFile); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( sourcePomFile, pomFile ); - assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); - assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() ); + assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) ); expectedMetadataFile = getTestFile( "src/test/expected-files/v4-version-metadata.xml" ); @@ -211,36 +214,36 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File versionMetadataFile = new File( targetRepository.getBasedir(), + Path versionMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) ); - versionMetadataFile.delete(); + Files.deleteIfExists(versionMetadataFile); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3.pom" ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); - assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); - assertTrue( "Check snapshot metadata created", versionMetadataFile.exists() ); + assertTrue( "Check snapshot metadata created", Files.exists(versionMetadataFile) ); expectedMetadataFile = getTestFile( "src/test/expected-files/v3-version-metadata.xml" ); @@ -253,30 +256,30 @@ public class LegacyToDefaultConverterTest { Artifact artifact = createArtifact( "test", "relocated-v3artifact", "1.0.0" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File versionMetadataFile = new File( targetRepository.getBasedir(), + Path versionMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) ); - versionMetadataFile.delete(); + Files.deleteIfExists(versionMetadataFile); artifactConverter.convert( artifact, targetRepository ); //checkSuccess(); --> commented until MNG-2100 is fixed - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check if relocated artifact created", artifactFile.exists() ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check if relocated artifact created", Files.exists(artifactFile) ); assertTrue( "Check if relocated artifact matches", - FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); Artifact pomArtifact = createArtifact( "relocated-test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" ); - File pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) ); - File testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) ); + Path pomFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( pomArtifact ) ); + Path testFile = getTestFile( "target/test-target-repository/" + targetRepository.pathOf( pomArtifact ) ); compareFiles( pomFile, testFile ); Artifact orig = createArtifact( "test", "relocated-v3artifact", "1.0.0", "1.0.0", "pom" ); - artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( orig ) ); - assertTrue( "Check if relocation artifact pom is created", artifactFile.exists() ); + artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( orig ) ); + assertTrue( "Check if relocation artifact pom is created", Files.exists(artifactFile) ); testFile = getTestFile( "src/test/expected-files/" + targetRepository.pathOf( orig ) ); compareFiles( artifactFile, testFile ); } @@ -289,26 +292,26 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v3-warnings-artifact", "1.0.0" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File versionMetadataFile = new File( targetRepository.getBasedir(), + Path versionMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) ); - versionMetadataFile.delete(); + Files.deleteIfExists(versionMetadataFile); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-warnings.pom" ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); @@ -322,36 +325,36 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v4artifact", version ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File snapshotMetadataFile = new File( targetRepository.getBasedir(), + Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) ); - snapshotMetadataFile.delete(); + Files.deleteIfExists(snapshotMetadataFile); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( sourcePomFile, pomFile ); - assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/v4-snapshot-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); - assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() ); + assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) ); expectedMetadataFile = getTestFile( expectedMetadataFileName ); @@ -366,36 +369,36 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-SNAPSHOT" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File snapshotMetadataFile = new File( targetRepository.getBasedir(), + Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) ); - snapshotMetadataFile.delete(); + Files.deleteIfExists(snapshotMetadataFile); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-snapshot.pom" ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); - assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); - assertTrue( "Check snapshot metadata created", snapshotMetadataFile.exists() ); + assertTrue( "Check snapshot metadata created", Files.exists(snapshotMetadataFile) ); expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-metadata.xml" ); @@ -428,23 +431,23 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "org.apache.maven.plugins", "maven-foo-plugin", "1.0", "1.0", "maven-plugin" ); artifact.setFile( - new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ) ); + Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "src/test/source-repository/test/plugins/maven-foo-plugin-1.0.jar" ).toFile() ); artifactConverter.convert( artifact, targetRepository ); // There is a warning but I can't figure out how to look at it. Eyeballing the results it appears // the plugin is being coverted correctly. //checkSuccess(); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); /* The POM isn't needed for Maven 1.x plugins but the raw conversion for artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); File expectedPomFile = getTestFile( "src/test/expected-files/maven-foo-plugin-1.0.pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); */ } @@ -457,36 +460,36 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "v3artifact", "1.0.0-20060105.130101-3" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - artifactMetadataFile.delete(); + Files.deleteIfExists(artifactMetadataFile); ArtifactMetadata snapshotMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File snapshotMetadataFile = new File( targetRepository.getBasedir(), + Path snapshotMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) ); - snapshotMetadataFile.delete(); + Files.deleteIfExists(snapshotMetadataFile); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path expectedPomFile = getTestFile( "src/test/expected-files/converted-v3-timestamped-snapshot.pom" ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); - assertTrue( "Check artifact snapshotMetadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact snapshotMetadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/v3-snapshot-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); - assertTrue( "Check snapshot snapshotMetadata created", snapshotMetadataFile.exists() ); + assertTrue( "Check snapshot snapshotMetadata created", Files.exists(snapshotMetadataFile) ); expectedMetadataFile = getTestFile( "src/test/expected-files/v3-timestamped-snapshot-metadata.xml" ); @@ -505,16 +508,16 @@ public class LegacyToDefaultConverterTest assertHasWarningReason( artifactConverter, Messages.getString( "warning.missing.pom" ) ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - assertFalse( "Check no POM created", pomFile.exists() ); - assertFalse( "No source POM", sourcePomFile.exists() ); + assertFalse( "Check no POM created", Files.exists(pomFile) ); + assertFalse( "No source POM", Files.exists(sourcePomFile) ); } @Test @@ -524,20 +527,19 @@ public class LegacyToDefaultConverterTest // test that it fails when the source md5 is wrong Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" ); - File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - file.delete(); + Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Files.deleteIfExists(file); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.md5" ) ); - assertFalse( "Check artifact not created", file.exists() ); + assertFalse( "Check artifact not created", Files.exists(file) ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -547,20 +549,19 @@ public class LegacyToDefaultConverterTest // test that it fails when the source sha1 is wrong Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" ); - File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - file.delete(); + Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Files.deleteIfExists(file); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.sha1" ) ); - assertFalse( "Check artifact not created", file.exists() ); + assertFalse( "Check artifact not created", Files.exists(file) ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -572,19 +573,19 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" ); Artifact pomArtifact = createPomArtifact( artifact ); - File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); + Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); - assertTrue( "Check target file exists", targetFile.exists() ); - assertTrue( "Check target POM exists", targetPomFile.exists() ); + assertTrue( "Check target file exists", Files.exists(targetFile) ); + assertTrue( "Check target POM exists", Files.exists(targetPomFile) ); - sourceFile.setLastModified( System.currentTimeMillis() ); - sourcePomFile.setLastModified( System.currentTimeMillis() ); + Files.setLastModifiedTime( sourceFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) ); + Files.setLastModifiedTime( sourcePomFile, FileTime.from(System.currentTimeMillis(), TimeUnit.MILLISECONDS) ); - long origTime = targetFile.lastModified(); - long origPomTime = targetPomFile.lastModified(); + long origTime = Files.getLastModifiedTime( targetFile ).toMillis(); + long origPomTime = Files.getLastModifiedTime( targetPomFile ).toMillis(); // Need to guarantee last modified is not equal Thread.sleep( SLEEP_MILLIS ); @@ -595,8 +596,8 @@ public class LegacyToDefaultConverterTest compareFiles( sourceFile, targetFile ); compareFiles( sourcePomFile, targetPomFile ); - assertEquals( "Check artifact unmodified", origTime, targetFile.lastModified() ); - assertEquals( "Check POM unmodified", origPomTime, targetPomFile.lastModified() ); + assertEquals( "Check artifact unmodified", origTime, Files.getLastModifiedTime( targetFile ).toMillis() ); + assertEquals( "Check POM unmodified", origPomTime, Files.getLastModifiedTime( targetPomFile ).toMillis() ); } @Test @@ -609,19 +610,19 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" ); Artifact pomArtifact = createPomArtifact( artifact ); - File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); + Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); - assertTrue( "Check target file exists", targetFile.exists() ); - assertTrue( "Check target POM exists", targetPomFile.exists() ); + assertTrue( "Check target file exists", Files.exists(targetFile) ); + assertTrue( "Check target POM exists", Files.exists(targetPomFile) ); - sourceFile.setLastModified( System.currentTimeMillis() ); - sourcePomFile.setLastModified( System.currentTimeMillis() ); + Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS)); + Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS)); - long origTime = targetFile.lastModified(); - long origPomTime = targetPomFile.lastModified(); + long origTime = Files.getLastModifiedTime(targetFile).toMillis(); + long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis(); // Need to guarantee last modified is not equal Thread.sleep( SLEEP_MILLIS ); @@ -631,13 +632,12 @@ public class LegacyToDefaultConverterTest assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) ); - assertEquals( "Check unmodified", origTime, targetFile.lastModified() ); - assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() ); + assertEquals( "Check unmodified", origTime, Files.getLastModifiedTime(targetFile).toMillis() ); + assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -652,18 +652,18 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "unmodified-artifact", "1.0.0" ); Artifact pomArtifact = createPomArtifact( artifact ); - File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); + Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd", Locale.getDefault() ); long origTime = dateFormat.parse( "2006-03-03" ).getTime(); - targetFile.setLastModified( origTime ); - targetPomFile.setLastModified( origTime ); + Files.setLastModifiedTime(targetFile, FileTime.from(origTime , TimeUnit.MILLISECONDS)); + Files.setLastModifiedTime(targetPomFile, FileTime.from(origTime , TimeUnit.MILLISECONDS)); - sourceFile.setLastModified( dateFormat.parse( "2006-01-01" ).getTime() ); - sourcePomFile.setLastModified( dateFormat.parse( "2006-02-02" ).getTime() ); + Files.setLastModifiedTime(sourceFile, FileTime.from(dateFormat.parse( "2006-01-01" ).getTime() , TimeUnit.MILLISECONDS)); + Files.setLastModifiedTime(sourcePomFile, FileTime.from(dateFormat.parse( "2006-02-02" ).getTime() , TimeUnit.MILLISECONDS)); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); @@ -671,13 +671,12 @@ public class LegacyToDefaultConverterTest compareFiles( sourceFile, targetFile ); compareFiles( sourcePomFile, targetPomFile ); - assertFalse( "Check modified", origTime == targetFile.lastModified() ); - assertFalse( "Check modified", origTime == targetPomFile.lastModified() ); + assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetFile).toMillis() ); + assertFalse( "Check modified", origTime == Files.getLastModifiedTime(targetPomFile).toMillis() ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertTrue( "Check metadata created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertTrue( "Check metadata created", Files.exists(metadataFile) ); } @Test @@ -692,26 +691,25 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "dryrun-artifact", "1.0.0" ); Artifact pomArtifact = createPomArtifact( artifact ); - File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); + Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); // clear warning before test related to MRM-1638 artifactConverter.clearWarnings(); artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - assertTrue( "Check source file exists", sourceFile.exists() ); - assertTrue( "Check source POM exists", sourcePomFile.exists() ); + assertTrue( "Check source file exists", Files.exists(sourceFile) ); + assertTrue( "Check source POM exists", Files.exists(sourcePomFile) ); - assertFalse( "Check target file doesn't exist", targetFile.exists() ); - assertFalse( "Check target POM doesn't exist", targetPomFile.exists() ); + assertFalse( "Check target file doesn't exist", Files.exists(targetFile) ); + assertFalse( "Check target POM doesn't exist", Files.exists(targetPomFile) ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -726,19 +724,19 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "modified-artifact", "1.0.0" ); Artifact pomArtifact = createPomArtifact( artifact ); - File sourceFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); - File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); + Path sourceFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( pomArtifact ) ); + Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path targetPomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) ); - assertTrue( "Check target file exists", targetFile.exists() ); - assertTrue( "Check target POM exists", targetPomFile.exists() ); + assertTrue( "Check target file exists", Files.exists(targetFile) ); + assertTrue( "Check target POM exists", Files.exists(targetPomFile) ); - sourceFile.setLastModified( System.currentTimeMillis() ); - sourcePomFile.setLastModified( System.currentTimeMillis() ); + Files.setLastModifiedTime(sourceFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS)); + Files.setLastModifiedTime(sourcePomFile, FileTime.from(System.currentTimeMillis() , TimeUnit.MILLISECONDS)); - long origTime = targetFile.lastModified(); - long origPomTime = targetPomFile.lastModified(); + long origTime = Files.getLastModifiedTime(targetFile).toMillis(); + long origPomTime = Files.getLastModifiedTime(targetPomFile).toMillis(); // Need to guarantee last modified is not equal Thread.sleep( SLEEP_MILLIS ); @@ -750,13 +748,12 @@ public class LegacyToDefaultConverterTest assertHasWarningReason( artifactConverter, Messages.getString( "failure.target.already.exists" ) ); - assertEquals( "Check unmodified", origTime, targetFile.lastModified() ); - assertEquals( "Check unmodified", origPomTime, targetPomFile.lastModified() ); + assertEquals( "Check unmodified", origTime, Files.getLastModifiedTime(targetFile).toMillis() ); + assertEquals( "Check unmodified", origPomTime, Files.getLastModifiedTime(targetPomFile).toMillis() ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -767,15 +764,15 @@ public class LegacyToDefaultConverterTest Artifact artifact = createArtifact( "test", "rollback-created-artifact", "1.0.0" ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - FileUtils.deleteDirectory( artifactMetadataFile.getParentFile() ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( artifactMetadataFile.getParent() ); ArtifactMetadata versionMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); - File versionMetadataFile = new File( targetRepository.getBasedir(), + Path versionMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); @@ -801,9 +798,9 @@ public class LegacyToDefaultConverterTest assertTrue( "Check failure message.", found ); - assertFalse( "check artifact rolled back", artifactFile.exists() ); - assertFalse( "check metadata rolled back", artifactMetadataFile.exists() ); - assertFalse( "check metadata rolled back", versionMetadataFile.exists() ); + assertFalse( "check artifact rolled back", Files.exists(artifactFile) ); + assertFalse( "check metadata rolled back", Files.exists(artifactMetadataFile) ); + assertFalse( "check metadata rolled back", Files.exists(versionMetadataFile) ); } @Test @@ -825,15 +822,15 @@ public class LegacyToDefaultConverterTest for ( Artifact artifact : artifacts ) { - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File expectedPomFile = + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path expectedPomFile = getTestFile( "src/test/expected-files/converted-" + artifact.getArtifactId() + ".pom" ); - assertTrue( "Check POM created", pomFile.exists() ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( expectedPomFile, pomFile ); } @@ -848,8 +845,8 @@ public class LegacyToDefaultConverterTest createModernSourceRepository(); Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" ); - File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - file.delete(); + Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Files.deleteIfExists(file); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); @@ -857,12 +854,11 @@ public class LegacyToDefaultConverterTest assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.artifactMetadata.versions" ) ); - assertFalse( "Check artifact not created", file.exists() ); + assertFalse( "Check artifact not created", Files.exists(file) ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -874,8 +870,8 @@ public class LegacyToDefaultConverterTest createModernSourceRepository(); Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" ); - File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - file.delete(); + Path file = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Files.deleteIfExists(file); artifactConverter.convert( artifact, targetRepository ); checkWarnings( artifactConverter, 2 ); @@ -883,12 +879,11 @@ public class LegacyToDefaultConverterTest assertHasWarningReason( artifactConverter, Messages.getString( "failure.incorrect.snapshotMetadata.snapshot" ) ); - assertFalse( "Check artifact not created", file.exists() ); + assertFalse( "Check artifact not created", Files.exists(file) ); ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); - File metadataFile = - new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); - assertFalse( "Check metadata not created", metadataFile.exists() ); + Path metadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", Files.exists(metadataFile) ); } @Test @@ -901,23 +896,23 @@ public class LegacyToDefaultConverterTest artifactConverter.convert( artifact, targetRepository ); checkSuccess( artifactConverter ); - File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - assertTrue( "Check artifact created", artifactFile.exists() ); - assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile, artifact.getFile() ) ); + Path artifactFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + assertTrue( "Check artifact created", Files.exists(artifactFile) ); + assertTrue( "Check artifact matches", FileUtils.contentEquals( artifactFile.toFile(), artifact.getFile() ) ); artifact = createPomArtifact( artifact ); - File pomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); - File sourcePomFile = new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); - assertTrue( "Check POM created", pomFile.exists() ); + Path pomFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + Path sourcePomFile = Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ); + assertTrue( "Check POM created", Files.exists(pomFile) ); compareFiles( sourcePomFile, pomFile ); ArtifactMetadata artifactMetadata = new ArtifactRepositoryMetadata( artifact ); - File artifactMetadataFile = new File( targetRepository.getBasedir(), + Path artifactMetadataFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) ); - assertTrue( "Check artifact metadata created", artifactMetadataFile.exists() ); + assertTrue( "Check artifact metadata created", Files.exists(artifactMetadataFile) ); - File expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" ); + Path expectedMetadataFile = getTestFile( "src/test/expected-files/newversion-artifact-metadata.xml" ); compareFiles( expectedMetadataFile, artifactMetadataFile ); } @@ -970,7 +965,7 @@ public class LegacyToDefaultConverterTest Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type ); artifact.setBaseVersion( baseVersion ); artifact.setRepository( sourceRepository ); - artifact.setFile( new File( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ) ); + artifact.setFile( Paths.get( sourceRepository.getBasedir(), sourceRepository.pathOf( artifact ) ).toFile() ); return artifact; } @@ -980,13 +975,13 @@ public class LegacyToDefaultConverterTest artifact.getVersion(), "pom" ); } - private static void compareFiles( File expectedPomFile, File pomFile ) + private static void compareFiles( Path expectedPomFile, Path pomFile ) throws IOException { String expectedContent = normalizeString( - org.apache.commons.io.FileUtils.readFileToString( expectedPomFile, Charset.defaultCharset() ) ); + org.apache.archiva.common.utils.FileUtils.readFileToString( expectedPomFile, Charset.defaultCharset() ) ); String targetContent = - normalizeString( org.apache.commons.io.FileUtils.readFileToString( pomFile, Charset.defaultCharset() ) ); + normalizeString( org.apache.archiva.common.utils.FileUtils.readFileToString( pomFile, Charset.defaultCharset() ) ); assertEquals( "Check file match between " + expectedPomFile + " and " + pomFile, expectedContent, targetContent ); } @@ -1056,8 +1051,8 @@ public class LegacyToDefaultConverterTest ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" ); - File sourceBase = getTestFile( "src/test/source-modern-repository" ); + Path sourceBase = getTestFile( "src/test/source-modern-repository" ); sourceRepository = - factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null ); + factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null, null ); } } |