[MRM-1303] fixed npe in delete artifact + unit test

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1031518 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2010-11-05 11:22:06 +00:00
parent b719ecb818
commit 031f607b73
11 changed files with 164 additions and 3 deletions

View File

@ -339,7 +339,7 @@ public class DeleteArtifactAction
if ( !VersionUtil.isSnapshot( version ) )
{
if ( metadata.getReleasedVersion().equals( version ) )
if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals( version ) )
{
metadata.setReleasedVersion( latestVersion );
}
@ -404,4 +404,19 @@ public class DeleteArtifactAction
{
return listeners;
}
public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
{
this.repositoryFactory = repositoryFactory;
}
public void setConfiguration( ArchivaConfiguration configuration )
{
this.configuration = configuration;
}
public void setMetadataRepository( MetadataRepository metadataRepository )
{
this.metadataRepository = metadataRepository;
}
}

View File

@ -20,17 +20,137 @@ package org.apache.maven.archiva.web.action;
*/
import com.opensymphony.xwork2.Action;
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.repository.ManagedRepositoryContent;
import org.apache.maven.archiva.repository.RepositoryContentFactory;
import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
public class DeleteArtifactActionTest
extends PlexusInSpringTestCase
{
private DeleteArtifactAction action;
private ArchivaConfiguration configuration;
private MockControl configurationControl;
private RepositoryContentFactory repositoryFactory;
private MockControl repositoryFactoryControl;
private MetadataRepository metadataRepository;
private MockControl metadataRepositoryControl;
private static final String REPOSITORY_ID = "test-repo";
private static final String GROUP_ID = "org.apache.archiva";
private static final String ARTIFACT_ID = "npe-metadata";
private static final String VERSION = "1.0";
private static final String REPO_LOCATION = getBasedir() + "/target/test-classes/test-repo";
@Override
protected void setUp()
throws Exception
{
super.setUp();
action = (DeleteArtifactAction) lookup( Action.class.getName(), "deleteArtifactAction" );
assertNotNull( action );
configurationControl = MockControl.createControl( ArchivaConfiguration.class );
configuration = ( ArchivaConfiguration ) configurationControl.getMock();
repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
repositoryFactory = ( RepositoryContentFactory ) repositoryFactoryControl.getMock();
metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
metadataRepository = ( MetadataRepository ) metadataRepositoryControl.getMock();
action.setConfiguration( configuration );
action.setRepositoryFactory( repositoryFactory );
action.setMetadataRepository( metadataRepository );
}
@Override
protected void tearDown()
throws Exception
{
action = null;
super.tearDown();
}
public void testGetListeners()
throws Exception
{
DeleteArtifactAction action = (DeleteArtifactAction) lookup( Action.class.getName(), "deleteArtifactAction" );
assertNotNull( action );
assertNotNull( action.getListeners() );
assertFalse( action.getListeners().isEmpty() );
}
public void testNPEInDeleteArtifact()
throws Exception
{
action.setGroupId( GROUP_ID );
action.setArtifactId( ARTIFACT_ID );
action.setVersion( VERSION );
action.setRepositoryId( REPOSITORY_ID );
Configuration config = createConfiguration();
ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
repoContent.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
configurationControl.expectAndReturn( configuration.getConfiguration(), config );
repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ), repoContent );
metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID, ARTIFACT_ID, VERSION ),
new ArrayList<ArtifactMetadata>() );
configurationControl.replay();
repositoryFactoryControl.replay();
metadataRepositoryControl.replay();
action.doDelete();
String artifactPath = REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/" +
StringUtils.replace( ARTIFACT_ID, ".", "/" ) + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION;
assertFalse( new File( artifactPath + ".jar" ).exists() );
assertFalse( new File( artifactPath + ".jar.sha1" ).exists() );
assertFalse( new File( artifactPath + ".jar.md5" ).exists() );
assertFalse( new File( artifactPath + ".pom" ).exists() );
assertFalse( new File( artifactPath + ".pom.sha1" ).exists() );
assertFalse( new File( artifactPath + ".pom.md5" ).exists() );
}
private Configuration createConfiguration()
{
ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
managedRepo.setId( REPOSITORY_ID );
managedRepo.setName( "Test Repository" );
managedRepo.setLocation( REPO_LOCATION );
managedRepo.setReleases( true );
Configuration config = new Configuration();
config.addManagedRepository( managedRepo );
return config;
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.archiva</groupId>
<artifactId>npe-metadata</artifactId>
<version>1.0</version>
</project>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.apache.archiva</groupId>
<artifactId>npe-metadata</artifactId>
<version>1.0</version>
<versioning>
<versions>
<version>1.0</version>
</versions>
<lastUpdated>20101105071933</lastUpdated>
</versioning>
</metadata>