Class ArtifactStubFactory

java.lang.Object
org.apache.maven.plugin.testing.ArtifactStubFactory

public class ArtifactStubFactory extends Object
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.
Author:
Brian Fox
  • Constructor Details

    • ArtifactStubFactory

      public ArtifactStubFactory()
      Default constructor. This should be used only if real files aren't needed...just the artifact objects
    • ArtifactStubFactory

      public ArtifactStubFactory(File workingDir, boolean createFiles)
      This constructor is to be used if files are needed and to set a working dir
      Parameters:
      workingDir -
      createFiles -
  • Method Details

    • setUnpackableFile

      public void setUnpackableFile(org.codehaus.plexus.archiver.manager.ArchiverManager archiverManager)
      If set, the file will be created as a zip/jar/war with a file inside that can be checked to exist after unpacking.
      Parameters:
      archiverManager -
    • createArtifact

      public org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, String version) throws IOException
      Parameters:
      groupId -
      artifactId -
      version -
      Returns:
      a DefaultArtifact instance for the given parameters
      Throws:
      IOException - if any
      See Also:
    • createArtifact

      public org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, String version, String scope) throws IOException
      Parameters:
      groupId -
      artifactId -
      version -
      scope -
      Returns:
      a DefaultArtifact instance for the given parameters
      Throws:
      IOException - if any
      See Also:
    • createArtifact

      public org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type, String classifier) throws IOException
      Parameters:
      groupId -
      artifactId -
      version -
      scope -
      type -
      classifier -
      Returns:
      a DefaultArtifact instance for the given parameters
      Throws:
      IOException - if any
      See Also:
    • createArtifact

      public org.apache.maven.artifact.Artifact createArtifact(String groupId, String artifactId, org.apache.maven.artifact.versioning.VersionRange versionRange, String scope, String type, String classifier, boolean optional) throws IOException
      Parameters:
      groupId - not null
      artifactId - not null
      versionRange - not null
      scope - not null
      type - not null
      classifier -
      optional - not null
      Returns:
      a DefaultArtifact instance
      Throws:
      IOException - if any
    • setArtifactFile

      public void setArtifactFile(org.apache.maven.artifact.Artifact artifact, File workingDir) throws IOException
      Creates a new empty file and attaches it to the artifact.
      Parameters:
      artifact - to attach the file to.
      workingDir - where to locate the new file
      Throws:
      IOException
    • setArtifactFile

      public void setArtifactFile(org.apache.maven.artifact.Artifact artifact, File workingDir, File srcFile) throws IOException
      Copyies the srcFile to the workingDir and then attaches it to the artifact. If srcFile is null, a new empty file will be created.
      Parameters:
      artifact - to attach
      workingDir - where to copy the srcFile.
      srcFile - file to be attached.
      Throws:
      IOException
    • setUnpackableArtifactFile

      public void setUnpackableArtifactFile(org.apache.maven.artifact.Artifact artifact, File workingDir) throws IOException
      Creates an unpackable file (zip,jar etc) containing an empty file.
      Parameters:
      artifact - to attach
      workingDir - where to create the file.
      Throws:
      IOException
    • setUnpackableArtifactFile

      public void setUnpackableArtifactFile(org.apache.maven.artifact.Artifact artifact, File workingDir, File srcFile) throws IOException
      Creates an unpackable file (zip,jar etc) containing the srcFile. If srcFile is null, a new empty file will be created.
      Parameters:
      artifact - to attach
      workingDir - where to create the file.
      srcFile -
      Throws:
      IOException - if any
    • getUnpackableFileName

      public static String getUnpackableFileName(org.apache.maven.artifact.Artifact artifact)
      Parameters:
      artifact -
      Returns:
    • createUnpackableFile

      public void createUnpackableFile(org.apache.maven.artifact.Artifact artifact, File destFile) throws org.codehaus.plexus.archiver.manager.NoSuchArchiverException, org.codehaus.plexus.archiver.ArchiverException, IOException
      Parameters:
      artifact -
      destFile -
      Throws:
      org.codehaus.plexus.archiver.manager.NoSuchArchiverException
      org.codehaus.plexus.archiver.ArchiverException - if any
      IOException - if any
    • getReleaseArtifact

      public org.apache.maven.artifact.Artifact getReleaseArtifact() throws IOException
      Returns:
      a DefaultArtifact instance for testGroupId:release:jar:1.0
      Throws:
      IOException - if any
    • getSnapshotArtifact

      public org.apache.maven.artifact.Artifact getSnapshotArtifact() throws IOException
      Returns:
      a default DefaultArtifact instance for testGroupId:snapshot:jar:2.0-SNAPSHOT
      Throws:
      IOException - if any
    • getReleaseAndSnapshotArtifacts

      public Set<org.apache.maven.artifact.Artifact> getReleaseAndSnapshotArtifacts() throws IOException
      Returns:
      a default set of release and snapshot DefaultArtifact, i.e.: testGroupId:snapshot:jar:2.0-SNAPSHOT, testGroupId:release:jar:1.0
      Throws:
      IOException - if any
      See Also:
    • getScopedArtifacts

      public Set<org.apache.maven.artifact.Artifact> getScopedArtifacts() throws IOException
      Returns:
      a default set of DefaultArtifact, i.e.: g:provided:jar:1.0, g:compile:jar:1.0, g:system:jar:1.0, g:test:jar:1.0, g:runtime:jar:1.0
      Throws:
      IOException - if any
    • getTypedArtifacts

      public Set<org.apache.maven.artifact.Artifact> getTypedArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact, i.e.: g:d:zip:1.0, g:a:war:1.0, g:b:jar:1.0, g:c:sources:1.0, g:e:rar:1.0
      Throws:
      IOException - if any
    • getClassifiedArtifacts

      public Set<org.apache.maven.artifact.Artifact> getClassifiedArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact, i.e.: g:c:jar:three:1.0, g:b:jar:two:1.0, g:d:jar:four:1.0, g:a:jar:one:1.0
      Throws:
      IOException - if any
    • getTypedArchiveArtifacts

      public Set<org.apache.maven.artifact.Artifact> getTypedArchiveArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact, i.e.: g:d:zip:1.0, g:a:war:1.0, g:b:jar:1.0, g:e:rar:1.0
      Throws:
      IOException - if any
    • getArtifactArtifacts

      public Set<org.apache.maven.artifact.Artifact> getArtifactArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact, i.e.: g:one:jar:a:1.0, g:two:jar:a:1.0, g:four:jar:a:1.0, g:three:jar:a:1.0
      Throws:
      IOException - if any
    • getGroupIdArtifacts

      public Set<org.apache.maven.artifact.Artifact> getGroupIdArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact, i.e.: one:group-one:jar:a:1.0, three:group-three:jar:a:1.0, four:group-four:jar:a:1.0, two:group-two:jar:a:1.0
      Throws:
      IOException - if any
    • getMixedArtifacts

      public Set<org.apache.maven.artifact.Artifact> getMixedArtifacts() throws IOException
      Returns:
      a set of DefaultArtifact
      Throws:
      IOException - if any
      See Also:
    • isCreateFiles

      public boolean isCreateFiles()
      Returns:
      Returns the createFiles.
    • setCreateFiles

      public void setCreateFiles(boolean createFiles)
      Parameters:
      createFiles - The createFiles to set.
    • getWorkingDir

      public File getWorkingDir()
      Returns:
      Returns the workingDir.
    • setWorkingDir

      public void setWorkingDir(File workingDir)
      Parameters:
      workingDir - The workingDir to set.
    • getSrcFile

      public File getSrcFile()
      Returns:
      Returns the srcFile.
    • setSrcFile

      public void setSrcFile(File srcFile)
      Parameters:
      srcFile - The srcFile to set.
    • setVariableValueToObject

      public static void setVariableValueToObject(Object object, String variable, Object value) throws IllegalAccessException
      Convenience method to set values to variables in objects that don't have setters
      Parameters:
      object -
      variable -
      value -
      Throws:
      IllegalAccessException
    • getFormattedFileName

      public static String getFormattedFileName(org.apache.maven.artifact.Artifact artifact, boolean removeVersion)
      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.
      Parameters:
      artifact - File to be formatted.
      removeVersion - Specifies if the version should be removed from the file name.
      Returns:
      Formatted file name in the format artifactId-[version]-[classifier].[type]