From: Martin Stockhammer Date: Sun, 10 Sep 2017 18:40:50 +0000 (+0200) Subject: Migrating rest services to java.nio X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b12b8a3ec9a8aa247dea47ab4f9a7b7c6e28b4d3;p=archiva.git Migrating rest services to java.nio --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index d497e2a97..1bf577fa2 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -60,7 +60,6 @@ import org.apache.archiva.rest.services.utils.ArtifactContentEntryComparator; import org.apache.archiva.security.ArchivaSecurityException; import org.apache.archiva.xml.XMLException; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; @@ -68,7 +67,6 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.inject.Named; import javax.ws.rs.core.Response; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java index ed3ba676d..2f4058e03 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java @@ -35,7 +35,8 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.ws.rs.core.Response; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.List; @@ -151,7 +152,7 @@ public class DefaultManagedRepositoriesService throws ArchivaRestServiceException { String location = repositoryCommonValidator.removeExpressions( fileLocation ); - return new File( location ).exists(); + return Files.exists( Paths.get( location )); } @Override diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index a336849d0..158e48535 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -88,9 +88,11 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.inject.Named; import javax.ws.rs.core.Response; -import java.io.File; import java.io.IOException; +import java.nio.file.FileSystems; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -395,9 +397,9 @@ public class DefaultRepositoriesService null ); } - File artifactFile = new File( source.getLocation(), artifactSourcePath ); + Path artifactFile = Paths.get( source.getLocation(), artifactSourcePath ); - if ( !artifactFile.exists() ) + if ( !Files.exists(artifactFile) ) { log.error( "cannot find artifact {}", artifactTransferRequest ); throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(), @@ -412,18 +414,18 @@ public class DefaultRepositoriesService int lastIndex = artifactPath.lastIndexOf( '/' ); String path = artifactPath.substring( 0, lastIndex ); - File targetPath = new File( target.getLocation(), path ); + Path targetPath = Paths.get( target.getLocation(), path ); Date lastUpdatedTimestamp = Calendar.getInstance().getTime(); int newBuildNumber = 1; String timestamp = null; - File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA ); + Path versionMetadataFile = targetPath.resolve( MetadataTools.MAVEN_METADATA ); /* unused */ getMetadata( versionMetadataFile ); - if ( !targetPath.exists() ) + if ( !Files.exists(targetPath) ) { - targetPath.mkdirs(); + Files.createDirectories( targetPath ); } String filename = artifactPath.substring( lastIndex + 1 ); @@ -431,8 +433,8 @@ public class DefaultRepositoriesService boolean fixChecksums = !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) ); - File targetFile = new File( targetPath, filename ); - if ( targetFile.exists() && target.isBlockRedeployments() ) + Path targetFile = targetPath.resolve( filename ); + if ( Files.exists(targetFile) && target.isBlockRedeployments() ) { throw new ArchivaRestServiceException( "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() @@ -453,14 +455,14 @@ public class DefaultRepositoriesService } pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom"; - File pomFile = new File( - new File( source.getLocation(), artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) ), + Path pomFile = Paths.get(source.getLocation(), + artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) , pomFilename ); - if ( pomFile != null && pomFile.length() > 0 ) + if ( pomFile != null && Files.size( pomFile ) > 0 ) { copyFile( pomFile, targetPath, pomFilename, fixChecksums ); - queueRepositoryTask( target.getId(), new File( targetPath, pomFilename ) ); + queueRepositoryTask( target.getId(), targetPath.resolve( pomFilename ) ); } @@ -468,7 +470,7 @@ public class DefaultRepositoriesService // explicitly update only if metadata-updater consumer is not enabled! if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) ) { - updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber, + updateProjectMetadata( targetPath.toAbsolutePath().toString(), lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, artifactTransferRequest ); @@ -499,11 +501,11 @@ public class DefaultRepositoriesService return true; } - private void queueRepositoryTask( String repositoryId, File localFile ) + private void queueRepositoryTask( String repositoryId, Path localFile ) { RepositoryTask task = new RepositoryTask(); task.setRepositoryId( repositoryId ); - task.setResourceFile( localFile.toPath() ); + task.setResourceFile( localFile ); task.setUpdateRelatedArtifacts( true ); //task.setScanAll( true ); @@ -514,19 +516,19 @@ public class DefaultRepositoriesService catch ( TaskQueueException e ) { log.error( "Unable to queue repository task to execute consumers on resource file ['{}" - + "'].", localFile.getName() ); + + "'].", localFile.getFileName()); } } - private ArchivaRepositoryMetadata getMetadata( File metadataFile ) + private ArchivaRepositoryMetadata getMetadata( Path metadataFile ) throws RepositoryMetadataException { ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata(); - if ( metadataFile.exists() ) + if ( Files.exists(metadataFile) ) { try { - metadata = MavenMetadataReader.read( metadataFile.toPath() ); + metadata = MavenMetadataReader.read( metadataFile ); } catch ( XMLException e ) { @@ -536,28 +538,28 @@ public class DefaultRepositoriesService return metadata; } - private File getMetadata( String targetPath ) + private Path getMetadata( String targetPath ) { - String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) ); + String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( FileSystems.getDefault().getSeparator() )); - return new File( artifactPath, MetadataTools.MAVEN_METADATA ); + return Paths.get( artifactPath, MetadataTools.MAVEN_METADATA ); } - private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums ) + private void copyFile( Path sourceFile, Path targetPath, String targetFilename, boolean fixChecksums ) throws IOException { - Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath(), StandardCopyOption.REPLACE_EXISTING, + Files.copy( sourceFile, targetPath.resolve( targetFilename ), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES ); if ( fixChecksums ) { - fixChecksums( new File( targetPath, targetFilename ) ); + fixChecksums( targetPath.resolve( targetFilename ) ); } } - private void fixChecksums( File file ) + private void fixChecksums( Path file ) { - ChecksummedFile checksum = new ChecksummedFile( file.toPath() ); + ChecksummedFile checksum = new ChecksummedFile( file ); checksum.fixChecksums( algorithms ); } @@ -568,12 +570,12 @@ public class DefaultRepositoriesService List availableVersions = new ArrayList<>(); String latestVersion = artifactTransferRequest.getVersion(); - File projectDir = new File( targetPath ).getParentFile(); - File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA ); + Path projectDir = Paths.get( targetPath ).getParent(); + Path projectMetadataFile = projectDir.resolve( MetadataTools.MAVEN_METADATA ); ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile ); - if ( projectMetadataFile.exists() ) + if ( Files.exists(projectMetadataFile) ) { availableVersions = projectMetadata.getAvailableVersions(); @@ -613,7 +615,7 @@ public class DefaultRepositoriesService projectMetadata.setReleasedVersion( latestVersion ); } - RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() ); + RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile); if ( fixChecksums ) { @@ -813,9 +815,9 @@ public class DefaultRepositoriesService int index = path.lastIndexOf( '/' ); path = path.substring( 0, index ); - File targetPath = new File( repoConfig.getLocation(), path ); + Path targetPath = Paths.get( repoConfig.getLocation(), path ); - if ( !targetPath.exists() ) + if ( !Files.exists(targetPath) ) { //throw new ContentNotFoundException( // artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() ); @@ -837,7 +839,7 @@ public class DefaultRepositoriesService repository.deleteArtifact( artifactRef ); } } - File metadataFile = getMetadata( targetPath.getAbsolutePath() ); + Path metadataFile = getMetadata( targetPath.toAbsolutePath().toString() ); ArchivaRepositoryMetadata metadata = getMetadata( metadataFile ); updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact ); @@ -1136,14 +1138,14 @@ public class DefaultRepositoriesService * * @param metadata */ - private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp, + private void updateMetadata( ArchivaRepositoryMetadata metadata, Path metadataFile, Date lastUpdatedTimestamp, Artifact artifact ) throws RepositoryMetadataException { List availableVersions = new ArrayList<>(); String latestVersion = ""; - if ( metadataFile.exists() ) + if ( Files.exists(metadataFile) ) { if ( metadata.getAvailableVersions() != null ) { @@ -1187,8 +1189,8 @@ public class DefaultRepositoriesService metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp ); metadata.setAvailableVersions( availableVersions ); - RepositoryMetadataWriter.write( metadata, metadataFile.toPath() ); - ChecksummedFile checksum = new ChecksummedFile( metadataFile.toPath() ); + RepositoryMetadataWriter.write( metadata, metadataFile); + ChecksummedFile checksum = new ChecksummedFile( metadataFile ); checksum.fixChecksums( algorithms ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java index 8ad379b1b..7544dd355 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/ArtifactBuilder.java @@ -18,14 +18,13 @@ package org.apache.archiva.rest.services.utils; * under the License. */ +import org.apache.archiva.maven2.model.Artifact; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet; import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.ManagedRepositoryContent; -import org.apache.archiva.maven2.model.Artifact; import org.apache.commons.io.FilenameUtils; -import java.io.File; import java.nio.file.Path; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java index cce73ba2b..3e1f97793 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java @@ -25,15 +25,16 @@ import org.apache.archiva.redback.rest.api.services.RedbackServiceException; import org.apache.archiva.redback.rest.services.AbstractRestServicesTest; import org.apache.archiva.rest.api.services.ArchivaAdministrationService; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; -import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService; import org.apache.archiva.rest.api.services.BrowseService; import org.apache.archiva.rest.api.services.CommonServices; import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.rest.api.services.MergeRepositoriesService; import org.apache.archiva.rest.api.services.NetworkProxyService; import org.apache.archiva.rest.api.services.PingService; +import org.apache.archiva.rest.api.services.PluginsService; import org.apache.archiva.rest.api.services.ProxyConnectorRuleService; import org.apache.archiva.rest.api.services.ProxyConnectorService; +import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService; import org.apache.archiva.rest.api.services.RemoteRepositoriesService; import org.apache.archiva.rest.api.services.RepositoriesService; import org.apache.archiva.rest.api.services.RepositoryGroupService; @@ -51,11 +52,12 @@ import org.junit.runner.RunWith; import org.slf4j.LoggerFactory; import javax.ws.rs.core.MediaType; -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.Collections; import java.util.Date; -import org.apache.archiva.rest.api.services.PluginsService; /** * @author Olivier Lamy @@ -93,7 +95,7 @@ public abstract class AbstractArchivaRestTest public void startServer() throws Exception { - File appServerBase = new File( System.getProperty( "appserver.base" ) ); + Path appServerBase = Paths.get( System.getProperty( "appserver.base" ) ); removeAppsubFolder( appServerBase, "jcr" ); removeAppsubFolder( appServerBase, "conf" ); @@ -103,13 +105,13 @@ public abstract class AbstractArchivaRestTest } - private void removeAppsubFolder( File appServerBase, String folder ) + private void removeAppsubFolder( Path appServerBase, String folder ) throws Exception { - File directory = new File( appServerBase, folder ); - if ( directory.exists() ) + Path directory = appServerBase.resolve( folder ); + if ( Files.exists(directory) ) { - FileUtils.deleteDirectory( directory ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory ); } } @@ -332,7 +334,7 @@ public abstract class AbstractArchivaRestTest protected ManagedRepository getTestManagedRepository() { - String location = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).getAbsolutePath(); + String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).toAbsolutePath().toString(); return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null, false, 2, 3, true, false, "my nice repo", false ); @@ -355,13 +357,13 @@ public abstract class AbstractArchivaRestTest protected void initSourceTargetRepo() throws Exception { - File targetRepo = new File( "target/test-repo-copy" ); - if ( targetRepo.exists() ) + Path targetRepo = Paths.get( "target/test-repo-copy" ); + if ( Files.exists(targetRepo) ) { - FileUtils.deleteDirectory( targetRepo ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo ); } - assertFalse( targetRepo.exists() ); - targetRepo.mkdirs(); + assertFalse( Files.exists(targetRepo) ); + Files.createDirectories( targetRepo ); if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null ) { @@ -370,18 +372,18 @@ public abstract class AbstractArchivaRestTest } ManagedRepository managedRepository = getTestManagedRepository(); managedRepository.setId( TARGET_REPO_ID ); - managedRepository.setLocation( targetRepo.getCanonicalPath() ); + managedRepository.setLocation( targetRepo.toAbsolutePath().toString() ); managedRepository.setCronExpression( "* * * * * ?" ); getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository ); assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) ); - File originRepo = new File( "target/test-origin-repo" ); - if ( originRepo.exists() ) + Path originRepo = Paths.get( "target/test-origin-repo" ); + if ( Files.exists(originRepo) ) { - FileUtils.deleteDirectory( originRepo ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo ); } - assertFalse( originRepo.exists() ); - FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo ); + assertFalse( Files.exists(originRepo) ); + FileUtils.copyDirectory( Paths.get( "src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() ); if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null ) { @@ -391,7 +393,7 @@ public abstract class AbstractArchivaRestTest managedRepository = getTestManagedRepository(); managedRepository.setId( SOURCE_REPO_ID ); - managedRepository.setLocation( originRepo.getCanonicalPath() ); + managedRepository.setLocation( originRepo.toAbsolutePath().toString() ); getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository ); assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) ); @@ -446,19 +448,19 @@ public abstract class AbstractArchivaRestTest managedRepository.setId( testRepoId ); managedRepository.setName( "test repo" ); - File badContent = new File( repoPath, "target" ); - if ( badContent.exists() ) + Path badContent = Paths.get( repoPath, "target" ); + if ( Files.exists(badContent) ) { - FileUtils.deleteDirectory( badContent ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent ); } - File file = new File( repoPath ); + Path file = Paths.get( repoPath ); if ( !file.isAbsolute() ) { repoPath = getBasedir() + "/" + repoPath; } - managedRepository.setLocation( new File( repoPath ).getPath() ); + managedRepository.setLocation( Paths.get( repoPath ).toString() ); managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) ); @@ -518,9 +520,9 @@ public abstract class AbstractArchivaRestTest } } - public String getBasedir() + public Path getBasedir() { - return System.getProperty( "basedir" ); + return Paths.get(System.getProperty( "basedir" )); } protected void waitForScanToComplete( String repoId ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java index 8e692012b..1472de5d9 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java @@ -23,11 +23,9 @@ import org.apache.archiva.rest.api.model.ArtifactContentEntry; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java index f6e58855c..f7f8c5152 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java @@ -38,9 +38,12 @@ import org.junit.Before; import org.junit.Test; import javax.ws.rs.core.MediaType; -import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -491,7 +494,7 @@ public class BrowseServiceTest assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); } - createAndIndexRepo( TEST_REPO_ID, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), + createAndIndexRepo( TEST_REPO_ID, getBasedir().resolve( "src/test/repo-with-osgi" ).toAbsolutePath().toString(), false ); waitForScanToComplete( TEST_REPO_ID ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/CopyArtifactTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/CopyArtifactTest.java index 372ef7c28..51ee1cd86 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/CopyArtifactTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/CopyArtifactTest.java @@ -20,12 +20,13 @@ package org.apache.archiva.rest.services; import org.apache.archiva.rest.api.model.ArtifactTransferRequest; import org.apache.archiva.rest.api.services.RepositoriesService; -import org.assertj.core.api.Assertions; import org.junit.Ignore; import org.junit.Test; import javax.ws.rs.InternalServerErrorException; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; /** * @author Olivier Lamy @@ -62,13 +63,13 @@ public class CopyArtifactTest String targetRepoPath = getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ).getLocation(); - File artifact = new File( targetRepoPath, + Path artifact = Paths.get( targetRepoPath, "/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" ); - assertTrue( artifact.exists() ); - File pom = new File( targetRepoPath, + assertTrue( Files.exists(artifact) ); + Path pom = Paths.get( targetRepoPath, "/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.pom" ); - assertTrue( "not exists " + pom.getPath(), pom.exists() ); + assertTrue( "not exists " + pom, Files.exists(pom) ); // TODO find a way to force metadata generation and test it !! } finally diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java index a49995457..e5131e2d4 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java @@ -25,7 +25,9 @@ import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.rest.api.services.RepositoriesService; import org.junit.Test; -import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; + /** * @author Olivier Lamy @@ -118,9 +120,9 @@ public class ManagedRepositoriesServiceTest throws Exception { ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader ); - File target = new File( "target" ); + Path target = Paths.get( "target" ); - assertTrue( service.fileLocationExists( target.getCanonicalPath() ) ); + assertTrue( service.fileLocationExists( target.toAbsolutePath().toString() ) ); // normally should not exists :-) assertFalse( service.fileLocationExists( "/fooofofof/foddfdofd/dedede/kdeo" ) ); @@ -142,7 +144,7 @@ public class ManagedRepositoriesServiceTest RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader ); createAndIndexRepo( testRepoId, - new File( System.getProperty( "basedir" ), "src/test/repo-with-osgi" ).getAbsolutePath() ); + Paths.get( System.getProperty( "basedir" ), "src/test/repo-with-osgi" ).toAbsolutePath().toString() ); repositoriesService.scanRepositoryDirectoriesNow( testRepoId ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/MergeRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/MergeRepositoriesServiceTest.java index 7ec1ede96..0921c8557 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/MergeRepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/MergeRepositoriesServiceTest.java @@ -21,14 +21,17 @@ package org.apache.archiva.rest.services; import org.apache.archiva.maven2.model.Artifact; import org.apache.archiva.rest.api.services.MergeRepositoriesService; import org.apache.commons.io.FileUtils; -import static org.assertj.core.api.Assertions.assertThat; import org.junit.After; 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; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Olivier Lamy */ @@ -38,9 +41,9 @@ public class MergeRepositoriesServiceTest private static final String TEST_REPOSITORY = "test-repository"; - private File repo = new File( System.getProperty( "builddir" ), "test-repository" ); + private Path repo = Paths.get( System.getProperty( "builddir" ), "test-repository" ); - private File repoStage = new File( System.getProperty( "builddir" ), "test-repository-stage" ); + private Path repoStage = Paths.get( System.getProperty( "builddir" ), "test-repository-stage" ); @Test public void getMergeConflictedArtifacts() @@ -65,15 +68,15 @@ public class MergeRepositoriesServiceTest String mergedArtifactPomPath = "org/apache/felix/org.apache.felix.bundlerepository/1.6.4/org.apache.felix.bundlerepository-1.6.4.pom"; - assertTrue( new File( repoStage, mergedArtifactPath ).exists() ); - assertTrue( new File( repoStage, mergedArtifactPomPath ).exists() ); + assertTrue( Files.exists(repoStage.resolve(mergedArtifactPath)) ); + assertTrue( Files.exists(repoStage.resolve(mergedArtifactPomPath)) ); MergeRepositoriesService service = getMergeRepositoriesService( authorizationHeader ); service.mergeRepositories( TEST_REPOSITORY + "-stage", TEST_REPOSITORY, true ); - assertTrue( new File( repo, mergedArtifactPath ).exists() ); - assertTrue( new File( repo, mergedArtifactPomPath ).exists() ); + assertTrue( Files.exists(repo.resolve(mergedArtifactPath)) ); + assertTrue( Files.exists(repo.resolve(mergedArtifactPomPath)) ); } @After @@ -84,18 +87,18 @@ public class MergeRepositoriesServiceTest deleteTestRepo( TEST_REPOSITORY ); - FileUtils.deleteDirectory( repo ); - FileUtils.deleteDirectory( repoStage ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( repo ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoStage ); } @Before public void createStageRepo() throws Exception { - FileUtils.copyDirectory( new File( System.getProperty( "basedir" ), "src/test/repo-with-osgi" ), repo ); - FileUtils.copyDirectory( new File( System.getProperty( "basedir" ), "src/test/repo-with-osgi-stage" ), - repoStage ); + FileUtils.copyDirectory( Paths.get( System.getProperty( "basedir" ), "src/test/repo-with-osgi" ).toFile(), repo.toFile() ); + FileUtils.copyDirectory( Paths.get( System.getProperty( "basedir" ), "src/test/repo-with-osgi-stage" ).toFile(), + repoStage.toFile() ); - createStagedNeededRepo( TEST_REPOSITORY, repo.getAbsolutePath(), true ); + createStagedNeededRepo( TEST_REPOSITORY, repo.toAbsolutePath().toString(), true ); } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java index aab1ea962..934bfc693 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java @@ -33,7 +33,9 @@ import org.junit.Test; import javax.ws.rs.BadRequestException; import javax.ws.rs.ForbiddenException; import javax.ws.rs.core.Response; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -162,10 +164,10 @@ public class RepositoriesServiceTest try { - File artifactFile = new File( + Path artifactFile = Paths.get( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" ); - assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() ); + assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) ); Artifact artifact = new Artifact(); artifact.setGroupId( "org.apache.karaf.features" ); @@ -177,7 +179,7 @@ public class RepositoriesServiceTest repositoriesService.deleteArtifact( artifact ); - assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() ); + assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) ); artifacts = browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core", @@ -223,10 +225,10 @@ public class RepositoriesServiceTest try { - File artifactFile = new File( + Path artifactFile = Paths.get( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" ); - assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() ); + assertTrue( "artifact not exists:" + artifactFile.toString(), Files.exists(artifactFile) ); Artifact artifact = new Artifact(); artifact.setGroupId( "org.apache.karaf.features" ); @@ -239,7 +241,7 @@ public class RepositoriesServiceTest repositoriesService.deleteArtifact( artifact ); - assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() ); + assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) ); artifacts = browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core", @@ -280,19 +282,19 @@ public class RepositoriesServiceTest try { - File artifactFile = new File( + Path artifactFile = Paths.get( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" ); - File artifactFilemd5 = new File( + Path artifactFilemd5 = Paths.get( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" ); - File artifactFilesha1 = new File( + Path artifactFilesha1 = Paths.get( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" ); - assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() ); + assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) ); - assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() ); - assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() ); + assertTrue( "md5 not exists:" + artifactFilemd5, Files.exists(artifactFilemd5) ); + assertTrue( "sha1 not exists:" + artifactFilesha1, Files.exists(artifactFilesha1) ); Artifact artifact = new Artifact(); artifact.setGroupId( "commons-logging" ); @@ -306,9 +308,9 @@ public class RepositoriesServiceTest repositoriesService.deleteArtifact( artifact ); - assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() ); - assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() ); - assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() ); + assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) ); + assertFalse( "md5 still exists:" + artifactFilemd5, Files.exists(artifactFilemd5) ); + assertFalse( "sha1 still exists:" + artifactFilesha1, Files.exists(artifactFilesha1) ); artifacts = browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID ); @@ -350,15 +352,15 @@ public class RepositoriesServiceTest new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ), new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) ); - File directory = - new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" ); + Path directory = + Paths.get( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" ); - assertTrue( "directory not exists", directory.exists() ); + assertTrue( "directory not exists", Files.exists(directory) ); RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader ); repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID ); - assertFalse( "directory not exists", directory.exists() ); + assertFalse( "directory not exists", Files.exists(directory) ); browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID ); @@ -430,7 +432,7 @@ public class RepositoriesServiceTest public void deleteSnapshot() throws Exception { - File targetRepo = initSnapshotRepo(); + Path targetRepo = initSnapshotRepo(); try { @@ -446,18 +448,18 @@ public class RepositoriesServiceTest assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 ); - File artifactFile = new File( targetRepo, + Path artifactFile = targetRepo.resolve( "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" ); - File artifactFilemd5 = new File( targetRepo, + Path artifactFilemd5 = targetRepo.resolve( "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" ); - File artifactFilepom = new File( targetRepo, + Path artifactFilepom = targetRepo.resolve( "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" ); - assertThat( artifactFile ).exists(); - assertThat( artifactFilemd5 ).exists(); - assertThat( artifactFilepom ).exists(); + assertTrue( Files.exists(artifactFile) ); + assertTrue( Files.exists(artifactFilemd5) ); + assertTrue( Files.exists(artifactFilepom )); // we delete only one snapshot Artifact artifact = @@ -476,9 +478,9 @@ public class RepositoriesServiceTest assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 ); - assertThat( artifactFile ).doesNotExist(); - assertThat( artifactFilemd5 ).doesNotExist(); - assertThat( artifactFilepom ).doesNotExist(); + assertFalse( Files.exists(artifactFile) ); + assertFalse( Files.exists(artifactFilemd5 )); + assertFalse( Files.exists(artifactFilepom )); } catch ( Exception e ) { @@ -491,18 +493,18 @@ public class RepositoriesServiceTest } } - protected File initSnapshotRepo() + protected Path initSnapshotRepo() throws Exception { - File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" ); - if ( targetRepo.exists() ) + Path targetRepo = getBasedir().resolve( "target/repo-with-snapshots" ); + if ( Files.exists(targetRepo) ) { - FileUtils.deleteDirectory( targetRepo ); + org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo ); } - assertFalse( targetRepo.exists() ); + assertFalse( Files.exists(targetRepo) ); - FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ), - targetRepo.getParentFile() ); + FileUtils.copyDirectoryToDirectory( getBasedir().resolve( "src/test/repo-with-snapshots" ).toFile(), + targetRepo.getParent().toFile() ); if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null ) { @@ -541,7 +543,7 @@ public class RepositoriesServiceTest protected ManagedRepository getTestManagedRepository( String id, String path ) { - String location = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/" + path ).getAbsolutePath(); + String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/" + path ).toAbsolutePath().toString(); return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80, true, false ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java index 0fb6e917e..f0e825b79 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/utils/ArtifactBuilderTest.java @@ -18,13 +18,12 @@ package org.apache.archiva.rest.services.utils; * under the License. */ -import static org.assertj.core.api.Assertions.*; +import org.easymock.TestSubject; +import org.junit.Test; -import java.io.File; import java.nio.file.Paths; -import org.easymock.TestSubject; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; public class ArtifactBuilderTest {