From 27cb4991b1f340519d11bdc403c08df6825c5e07 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Sun, 22 Nov 2009 10:51:07 +0000 Subject: [PATCH] [MRM-747] Archiva should prevent re-deployment of released or non-snapshot versioned artifacts o block re-deployment of released artifacts to repository of configured using web upload form o added unit test git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@883057 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva/web/action/UploadAction.java | 13 ++- .../archiva/web/action/UploadActionTest.java | 87 +++++++++++++++++-- 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java index 3bab1372b..c61ba16ff 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/UploadAction.java @@ -351,8 +351,17 @@ public class UploadAction try { - copyFile( artifactFile, targetPath, filename, fixChecksums ); - queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) ); + File targetFile = new File( targetPath, filename ); + if( targetFile.exists() && !VersionUtil.isSnapshot( version ) && repoConfig.isBlockRedeployments() ) + { + addActionError( "Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed." ); + return ERROR; + } + else + { + copyFile( artifactFile, targetPath, filename, fixChecksums ); + queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) ); + } } catch ( IOException ie ) { diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java index f18aa17ff..f86660c56 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/UploadActionTest.java @@ -97,6 +97,7 @@ public class UploadActionTest repoConfig.setLayout( "default" ); repoConfig.setLocation( testRepo.getPath() ); repoConfig.setName( REPOSITORY_ID ); + repoConfig.setBlockRedeployments( true ); config.addManagedRepository( repoConfig ); RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration(); @@ -336,7 +337,7 @@ public class UploadActionTest public void testArtifactUploadFailedRepositoryNotFound() throws Exception - { + { setUploadParameters( "1.0", null, new File( getBasedir(), "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, @@ -405,9 +406,9 @@ public class UploadActionTest true ); ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); - content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); - - + ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID ); + repoConfig.setBlockRedeployments( false ); + content.setRepository( repoConfig ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content ); @@ -451,5 +452,81 @@ public class UploadActionTest assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); verifyChecksums( repoLocation ); - } + } + + public void testUploadArtifactAlreadyExistingRedeploymentsBlocked() + throws Exception + { + setUploadParameters( "1.0", null, + new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, + true ); + + ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); + content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); + repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 ); + + archivaConfigControl.replay(); + repoFactoryControl.replay(); + + String returnString = uploadAction.doUpload(); + assertEquals( Action.SUCCESS, returnString ); + + setUploadParameters( "1.0", null, + new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, + true ); + + returnString = uploadAction.doUpload(); + assertEquals( Action.ERROR, returnString ); + + archivaConfigControl.verify(); + repoFactoryControl.verify(); + + String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); + assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); + + verifyChecksums( repoLocation ); + } + + public void testUploadArtifactAlreadyExistingRedeploymentsAllowed() + throws Exception + { + setUploadParameters( "1.0", null, + new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, + true ); + + ManagedRepositoryContent content = new ManagedDefaultRepositoryContent(); + ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( REPOSITORY_ID ); + repoConfig.setBlockRedeployments( false ); + content.setRepository( repoConfig ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 ); + repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content, 2 ); + + archivaConfigControl.replay(); + repoFactoryControl.replay(); + + String returnString = uploadAction.doUpload(); + assertEquals( Action.SUCCESS, returnString ); + + setUploadParameters( "1.0", null, + new File( getBasedir(), + "target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null, + true ); + + returnString = uploadAction.doUpload(); + assertEquals( Action.SUCCESS, returnString ); + + archivaConfigControl.verify(); + repoFactoryControl.verify(); + + String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation(); + assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation ); + + verifyChecksums( repoLocation ); + } } -- 2.39.5