fixed a bug with isRelease and isSnapshot; added a few unit tests.

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@607510 13f79535-47bb-0310-9956-ffa450edef68
master
Brian E Fox 2007-12-30 04:10:52 +00:00
parent ccd58347ac
commit 4909a36c81
3 changed files with 67 additions and 47 deletions

View File

@ -40,17 +40,13 @@ import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.StringUtils;
/**
* This class creates artifacts to be used for testing purposes. It can optionally create actual
* files on the local disk for things like copying. It can create these files as archives with named
* files inside to be used for testing things like unpack.
* This class creates artifacts to be used for testing purposes. It can optionally create actual files on the local disk
* for things like copying. It can create these files as archives with named files inside to be used for testing things
* like unpack. Also provided are some utility methods to quickly get a set of artifacts distinguished by various things
* like group,artifact,type,scope, etc It was originally developed for the dependency plugin, but can be useful in other
* plugins that need to simulate artifacts for unit tests.
*
* Also provided are some utility methods to quickly get a set of artifacts distinguished by various things like
* group,artifact,type,scope, etc
*
* It was originally developed for the dependency plugin, but can be useful in other plugins that
* need to simulate artifacts for unit tests.
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*
*/
public class ArtifactStubFactory
{
@ -66,7 +62,6 @@ public class ArtifactStubFactory
/**
* Default constructor. This should be used only if real files aren't needed...just the artifact objects
*
*/
public ArtifactStubFactory()
{
@ -76,6 +71,7 @@ public class ArtifactStubFactory
/**
* This constructor is to be used if files are needed and to set a working dir
*
* @param workingDir
* @param createFiles
*/
@ -86,8 +82,9 @@ public class ArtifactStubFactory
}
/**
* If set, the file will be created as a zip/jar/war with a file inside that can be
* checked to exist after unpacking.
* If set, the file will be created as a zip/jar/war with a file inside that can be checked to exist after
* unpacking.
*
* @param archiverManager
*/
public void setUnpackableFile( ArchiverManager archiverManager )
@ -122,8 +119,12 @@ public class ArtifactStubFactory
{
ArtifactHandler ah = new DefaultArtifactHandlerStub( type, classifier );
Artifact artifact = new DefaultArtifact( groupId, artifactId, versionRange, scope, type, classifier, ah,
optional );
Artifact artifact =
new DefaultArtifact( groupId, artifactId, versionRange, scope, type, classifier, ah, optional );
//i have no idea why this needs to be done manually when isSnapshot is able to figure it out.
artifact.setRelease( !artifact.isSnapshot() );
if ( createFiles )
{
setArtifactFile( artifact );
@ -132,15 +133,14 @@ public class ArtifactStubFactory
}
/*
* Creates a file that can be copied or unpacked based on the passed in
* artifact
* Creates a file that can be copied or unpacked based on the passed in artifact
*/
public void setArtifactFile( Artifact artifact )
throws IOException
{
if (this.workingDir == null )
if ( this.workingDir == null )
{
throw new IllegalArgumentException("The workingDir must be set if createFiles is true.");
throw new IllegalArgumentException( "The workingDir must be set if createFiles is true." );
}
String fileName = getFormattedFileName( artifact, false );
@ -184,8 +184,8 @@ public class ArtifactStubFactory
static public String getUnpackableFileName( Artifact artifact )
{
return "" + artifact.getGroupId() + "-" + artifact.getArtifactId() + "-" + artifact.getVersion() + "-"
+ artifact.getClassifier() + "-" + artifact.getType() + ".txt";
return "" + artifact.getGroupId() + "-" + artifact.getArtifactId() + "-" + artifact.getVersion() + "-" +
artifact.getClassifier() + "-" + artifact.getType() + ".txt";
}
public void createUnpackableFile( Artifact artifact, File destFile )
@ -323,8 +323,7 @@ public class ArtifactStubFactory
}
/**
* @param createFiles
* The createFiles to set.
* @param createFiles The createFiles to set.
*/
public void setCreateFiles( boolean createFiles )
{
@ -340,8 +339,7 @@ public class ArtifactStubFactory
}
/**
* @param workingDir
* The workingDir to set.
* @param workingDir The workingDir to set.
*/
public void setWorkingDir( File workingDir )
{
@ -357,8 +355,7 @@ public class ArtifactStubFactory
}
/**
* @param srcFile
* The srcFile to set.
* @param srcFile The srcFile to set.
*/
public void setSrcFile( File srcFile )
{
@ -366,8 +363,7 @@ public class ArtifactStubFactory
}
/**
* convience method to set values to variables in objects that don't have
* setters
* convience method to set values to variables in objects that don't have setters
*
* @param object
* @param variable
@ -385,16 +381,12 @@ public class ArtifactStubFactory
}
/**
* Builds the file name. If removeVersion is set, then the file name must be
* reconstructed from the artifactId, Classifier (if used) and Type.
* Otherwise, this method returns the artifact file name.
* Builds the file name. If removeVersion is set, then the file name must be reconstructed from the artifactId,
* Classifier (if used) and Type. Otherwise, this method returns the artifact file name.
*
* @param artifact
* File to be formatted.
* @param removeVersion
* Specifies if the version should be removed from the file name.
* @return Formatted file name in the format
* artifactId-[version]-[classifier].[type]
* @param artifact File to be formatted.
* @param removeVersion Specifies if the version should be removed from the file name.
* @return Formatted file name in the format artifactId-[version]-[classifier].[type]
*/
public static String getFormattedFileName( Artifact artifact, boolean removeVersion )
{
@ -426,8 +418,9 @@ public class ArtifactStubFactory
classifierString = "-" + artifact.getClassifier();
}
destFileName = artifact.getArtifactId() + versionString + classifierString + "."
+ artifact.getArtifactHandler().getExtension();
destFileName =
artifact.getArtifactId() + versionString + classifierString + "." +
artifact.getArtifactHandler().getExtension();
}
return destFileName;
}

View File

@ -385,7 +385,8 @@ public class ArtifactStub
*/
public boolean isSnapshot()
{
return false;
return Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() ||
version.endsWith( Artifact.SNAPSHOT_VERSION );
}
/**
@ -435,7 +436,7 @@ public class ArtifactStub
*/
public boolean isRelease()
{
return false;
return !isSnapshot();
}
/**

View File

@ -0,0 +1,26 @@
package org.apache.maven.plugin.testing;
import java.io.IOException;
import junit.framework.TestCase;
public class ArtifactStubFactoryTest
extends TestCase
{
public void testVersionChecks() throws IOException
{
ArtifactStubFactory factory = new ArtifactStubFactory();
assertTrue(factory.getReleaseArtifact().isRelease());
assertFalse(factory.getReleaseArtifact().isSnapshot());
assertTrue(factory.getSnapshotArtifact().isSnapshot());
assertFalse(factory.getSnapshotArtifact().isRelease());
}
public void testCreateFiles() throws IOException
{
ArtifactStubFactory factory = new ArtifactStubFactory();
assertFalse(factory.isCreateFiles());
factory.getReleaseArtifact();
}
}