001package org.andromda.maven.plugin.cartridge;
002
003import java.io.File;
004import java.io.IOException;
005import org.andromda.core.common.ExceptionUtils;
006import org.andromda.maven.plugin.AndroMDAMojo;
007import org.apache.commons.io.FilenameUtils;
008import org.apache.maven.plugin.MojoExecutionException;
009import org.apache.maven.plugin.MojoFailureException;
010import org.codehaus.plexus.archiver.Archiver;
011import org.codehaus.plexus.archiver.ArchiverException;
012
013/**
014 * Updates the cartridge expected output with the current cartridge output. Invoke it with
015 * <code>mvn andromda-cartridge:update</code> when you are inside the cartridge root directory.
016 *
017 * @goal update
018 * @requiresDependencyResolution test
019 * @description update AndroMDA Cartridge test archive
020 * @author Chad Brandon
021 * @author Peter Friese
022 * @author Bob Fields
023 */
024public class CartridgeTestUpdaterMojo
025        extends AbstractCartridgeTestMojo
026{
027    /**
028     * @see org.apache.maven.plugin.Mojo#execute()
029     */
030    public void execute() throws MojoExecutionException, MojoFailureException
031    {
032        try
033        {
034            this.getLog().info("-----------------------------------------------------------------------------");
035            this.getLog().info("  A n d r o M D A   C a r t r i d g e   T e s t   S u i t e   U p d a t e r  ");
036            this.getLog().info("-----------------------------------------------------------------------------");
037
038            // - add the cartridge test dependencies (any dependencies of the cartridge test plugin)
039            this.changeScopeForTestDependencies();
040
041            // - first run AndroMDA with the test configuration
042            final AndroMDAMojo andromdaMojo = new AndroMDAMojo();
043            andromdaMojo.setConfigurationUri(this.configurationUri);
044            andromdaMojo.setProject(this.project);
045            andromdaMojo.setSettings(this.settings);
046            andromdaMojo.setPropertyFiles(this.propertyFiles);
047            andromdaMojo.execute();
048
049            // - pack the expected output archive
050            this.pack(this.actualDirectory, this.expectedOutputArchive);
051
052        }
053        catch (final Throwable throwable)
054        {
055            if (throwable instanceof MojoExecutionException && !this.testFailureIgnore)
056            {
057                throw (MojoExecutionException)throwable;
058            }
059            else if (this.testFailureIgnore)
060            {
061                this.getLog().error("An error occurred while updating cartridge archive '"
062                        + this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
063            }
064            else
065            {
066                throw new MojoExecutionException("An error occurred while updating cartridge archive '"
067                        + this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
068            }
069        }
070
071    }
072
073    /**
074     * Packs the actual directory contents into the expected archive.
075     *
076     * @param location Location where to put the unpacked files.
077     * @param file Archive file to be created.
078     * @throws MojoExecutionException error packing directory
079     */
080    protected void pack(final File location,
081        final File file) throws MojoExecutionException
082    {
083        final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
084        try
085        {
086            final Archiver archiver;
087            archiver = this.archiverManager.getArchiver(archiveExt);
088            archiver.setDestFile(file);
089            archiver.addDirectory(location);
090            archiver.createArchive();
091        }
092        catch (Throwable throwable)
093        {
094            if (this.testFailureIgnore)
095            {
096                this.getLog().error(this.project.getArtifactId() + "Error packing directory: " + location + "to: " + file, throwable);
097            }
098            else if (throwable instanceof IOException || throwable instanceof ArchiverException)
099            {
100                throw new MojoExecutionException("Error packing directory: " + location + "to: " + file, throwable);
101            }
102        }
103    }
104
105}