[MRM-1296] improve audit logging and testing of upload action

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@900683 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2010-01-19 07:17:02 +00:00
parent 606c82cbca
commit 640d3436f6
3 changed files with 322 additions and 137 deletions

View File

@ -22,9 +22,10 @@ package org.apache.maven.archiva.web.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.maven.archiva.repository.audit.AuditEvent;
import org.apache.maven.archiva.repository.audit.AuditListener;
import org.apache.maven.archiva.repository.audit.Auditable;
@ -34,9 +35,6 @@ import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* LogEnabled and SessionAware ActionSupport
*/
@ -129,4 +127,9 @@ public abstract class PlexusActionSupport
{
this.principal = principal;
}
public void setAuditListeners( List<AuditListener> auditListeners )
{
this.auditListeners = auditListeners;
}
}

View File

@ -305,7 +305,8 @@ public class UploadAction
int lastIndex = artifactPath.lastIndexOf( '/' );
File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
String path = artifactPath.substring( 0, lastIndex );
File targetPath = new File( repoConfig.getLocation(), path );
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
int newBuildNumber = -1;
@ -357,6 +358,7 @@ public class UploadAction
else
{
copyFile( artifactFile, targetPath, filename, fixChecksums );
triggerAuditEvent( repository.getId(), path + "/" + filename, AuditEvent.UPLOAD_FILE );
queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) );
}
}
@ -378,6 +380,7 @@ public class UploadAction
try
{
File generatedPomFile = createPom( targetPath, pomFilename );
triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE );
if ( fixChecksums )
{
fixChecksums( generatedPomFile );
@ -396,6 +399,7 @@ public class UploadAction
try
{
copyFile( pomFile, targetPath, pomFilename, fixChecksums );
triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE );
queueRepositoryTask( repoConfig.getId(), new File( targetPath, pomFilename ) );
}
catch ( IOException ie )
@ -415,8 +419,6 @@ public class UploadAction
String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version +
"\' was successfully deployed to repository \'" + repositoryId + "\'";
triggerAuditEvent( repositoryId, artifactPath, AuditEvent.UPLOAD_FILE );
addActionMessage( msg );
reset();

View File

@ -21,7 +21,13 @@ package org.apache.maven.archiva.web.action;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import com.opensymphony.xwork2.Action;
import org.apache.archiva.checksum.ChecksumAlgorithm;
@ -36,9 +42,12 @@ import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
import org.apache.maven.archiva.repository.ManagedRepositoryContent;
import org.apache.maven.archiva.repository.RepositoryContentFactory;
import org.apache.maven.archiva.repository.RepositoryNotFoundException;
import org.apache.maven.archiva.repository.audit.AuditEvent;
import org.apache.maven.archiva.repository.audit.AuditListener;
import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
import org.apache.maven.archiva.repository.metadata.MetadataTools;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.ArgumentsMatcher;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
@ -128,15 +137,22 @@ public class UploadActionTest
private void assertAllArtifactsIncludingSupportArtifactsArePresent( String repoLocation )
{
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
@ -147,62 +163,52 @@ public class UploadActionTest
throws IOException
{
// verify checksums of jar file
ChecksummedFile checksum =
new ChecksummedFile( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
ChecksummedFile checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
String contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
String contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
// verify checksums of pom file
checksum =
new ChecksummedFile( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
// verify checksums of metadata file
checksum =
new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA ) );
checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
contents =
FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA + ".sha1" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents =
FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA + ".md5" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
}
public void testArtifactUploadWithPomSuccessful()
throws Exception
{
setUploadParameters( "1.0", null,
new File( getBasedir(),
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
@ -215,11 +221,16 @@ public class UploadActionTest
archivaConfigControl.replay();
repoFactoryControl.replay();
MockControl control = mockAuditLogs(
Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
"org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
String returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
@ -227,13 +238,37 @@ public class UploadActionTest
verifyChecksums( repoLocation );
}
private MockControl mockAuditLogs( List<String> resources )
{
return mockAuditLogs( AuditEvent.UPLOAD_FILE, resources );
}
private MockControl mockAuditLogs( String action, List<String> resources )
{
MockControl control = MockControl.createControl( AuditListener.class );
AuditListener listener = (AuditListener) control.getMock();
boolean matcherSet = false;
for ( String resource : resources )
{
listener.auditEvent( new AuditEvent( REPOSITORY_ID, "guest", resource, action ) );
if ( !matcherSet )
{
control.setMatcher( new AuditEventArgumentsMatcher() );
matcherSet = true;
}
}
control.replay();
uploadAction.setAuditListeners( Collections.singletonList( listener ) );
return control;
}
public void testArtifactUploadWithClassifier()
throws Exception
{
setUploadParameters( "1.0", "tests",
new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
false );
setUploadParameters( "1.0", "tests", new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
new File( getBasedir(), "target/test-classes/upload-artifact-test/pom.xml" ), false );
ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
@ -244,70 +279,88 @@ public class UploadActionTest
archivaConfigControl.replay();
repoFactoryControl.replay();
MockControl control = mockAuditLogs(
Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar",
"org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
String returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
assertTrue( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ).exists() );
assertTrue( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
assertTrue( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ).exists() );
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".md5" ).exists() );
// verify checksums of jar file
ChecksummedFile checksum =
new ChecksummedFile( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) );
ChecksummedFile checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar" ) );
String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
String contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) );
String contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents =
FileUtils.readFileToString( new File( repoLocation,
"/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0-tests.jar.md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
// verify checksums of metadata file
checksum =
new ChecksummedFile( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA ) );
// verify checksums of jar file
checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
contents =
FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA + ".sha1" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents =
FileUtils.readFileToString( new File( repoLocation, "/org/apache/archiva/artifact-upload/" +
MetadataTools.MAVEN_METADATA + ".md5" ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
// verify checksums of metadata file
checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
}
public void testArtifactUploadGeneratePomSuccessful()
throws Exception
{
setUploadParameters( "1.0", null,
new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
true );
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 ) );
@ -318,11 +371,16 @@ public class UploadActionTest
archivaConfigControl.replay();
repoFactoryControl.replay();
MockControl control = mockAuditLogs(
Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
"org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
String returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
@ -330,13 +388,89 @@ public class UploadActionTest
verifyChecksums( repoLocation );
}
public void testArtifactUploadNoPomSuccessful()
throws Exception
{
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
null, false );
ManagedRepositoryContent content = new ManagedDefaultRepositoryContent();
content.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ), content );
archivaConfigControl.replay();
repoFactoryControl.replay();
MockControl control =
mockAuditLogs( Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
String returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.sha1" ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom.md5" ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".md5" ).exists() );
// verify checksums of jar file
ChecksummedFile checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ) );
String sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
String md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
String contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar.md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
// verify checksums of metadata file
checksum = new ChecksummedFile(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ) );
sha1 = checksum.calculateChecksum( ChecksumAlgorithm.SHA1 );
md5 = checksum.calculateChecksum( ChecksumAlgorithm.MD5 );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".sha1" ) );
assertTrue( StringUtils.contains( contents, sha1 ) );
contents = FileUtils.readFileToString(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA + ".md5" ) );
assertTrue( StringUtils.contains( contents, md5 ) );
}
public void testArtifactUploadFailedRepositoryNotFound()
throws Exception
{
setUploadParameters( "1.0", null,
new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
false );
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
null, false );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
repoFactoryControl.expectAndThrow( repoFactory.getManagedRepositoryContent( REPOSITORY_ID ),
@ -352,20 +486,22 @@ public class UploadActionTest
repoFactoryControl.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar" ).exists() );
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ).exists() );
assertFalse( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertFalse(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
}
public void testArtifactUploadSnapshots()
throws Exception
{
setUploadParameters( "1.0-SNAPSHOT", null,
new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
true );
setUploadParameters( "1.0-SNAPSHOT", 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 ) );
@ -376,16 +512,25 @@ public class UploadActionTest
archivaConfigControl.replay();
repoFactoryControl.replay();
SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
String timestamp = fmt.format( new Date() );
MockControl control = mockAuditLogs( Arrays.asList(
"org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.jar",
"org/apache/archiva/artifact-upload/1.0-SNAPSHOT/artifact-upload-1.0-" + timestamp + "-1.pom" ) );
String returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertEquals( 6, new File( repoLocation, "/org/apache/archiva/artifact-upload/1.0-SNAPSHOT/" ).list().length );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue(
new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
".sha1" ).exists() );
assertTrue( new File( repoLocation, "/org/apache/archiva/artifact-upload/" + MetadataTools.MAVEN_METADATA +
@ -395,10 +540,9 @@ public class UploadActionTest
public void testChecksumIsCorrectWhenArtifactIsReUploaded()
throws Exception
{
setUploadParameters( "1.0", null,
new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ), null,
true );
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 );
@ -426,8 +570,7 @@ public class UploadActionTest
verifyChecksums( repoLocation );
// RE-upload artifact
setUploadParameters( "1.0", null,
new File( getBasedir(),
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-reuploaded.jar" ),
null, true );
@ -437,11 +580,18 @@ public class UploadActionTest
archivaConfigControl.replay();
repoFactoryControl.replay();
// TODO: track modifications?
// MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
MockControl control = mockAuditLogs(
Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
"org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
@ -452,10 +602,9 @@ public class UploadActionTest
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 );
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 ) );
@ -469,16 +618,18 @@ public class UploadActionTest
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 );
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
null, true );
MockControl control = mockAuditLogs( Collections.<String>emptyList() );
returnString = uploadAction.doUpload();
assertEquals( Action.ERROR, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
@ -489,10 +640,9 @@ public class UploadActionTest
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 );
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 );
@ -508,20 +658,50 @@ public class UploadActionTest
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 );
setUploadParameters( "1.0", null, new File( getBasedir(),
"target/test-classes/upload-artifact-test/artifact-to-be-uploaded.jar" ),
null, true );
// TODO: track modifications?
// MockControl control = mockAuditLogs( AuditEvent.MODIFY_FILE, Arrays.asList(
MockControl control = mockAuditLogs(
Arrays.asList( "org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.jar",
"org/apache/archiva/artifact-upload/1.0/artifact-upload-1.0.pom" ) );
returnString = uploadAction.doUpload();
assertEquals( Action.SUCCESS, returnString );
archivaConfigControl.verify();
repoFactoryControl.verify();
control.verify();
String repoLocation = config.findManagedRepositoryById( REPOSITORY_ID ).getLocation();
assertAllArtifactsIncludingSupportArtifactsArePresent( repoLocation );
verifyChecksums( repoLocation );
}
private static class AuditEventArgumentsMatcher
implements ArgumentsMatcher
{
public boolean matches( Object[] objects, Object[] objects1 )
{
if ( objects.length != 1 || objects1.length != 1 )
{
return false;
}
else
{
AuditEvent o1 = (AuditEvent) objects[0];
AuditEvent o2 = (AuditEvent) objects1[0];
o2.setTimestamp( o1.getTimestamp() ); // effectively ignore the timestamp
return o1.equals( o2 );
}
}
public String toString( Object[] objects )
{
return Arrays.asList( objects ).toString();
}
}
}