1 package org.andromda.maven.plugin.cartridge;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8 import org.apache.commons.io.FilenameUtils;
9 import org.apache.maven.artifact.Artifact;
10 import org.apache.maven.model.Dependency;
11 import org.apache.maven.plugin.AbstractMojo;
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.maven.project.MavenProject;
14 import org.apache.maven.settings.Settings;
15 import org.codehaus.plexus.archiver.ArchiverException;
16 import org.codehaus.plexus.archiver.UnArchiver;
17 import org.codehaus.plexus.archiver.manager.ArchiverManager;
18
19 /**
20 * Abstract base mojo for cartridge tests. Must be subclassed.
21 *
22 * @author Chad Brandon
23 * @author Peter Friese
24 * @author Bob Fields
25 */
26 public abstract class AbstractCartridgeTestMojo
27 extends AbstractMojo
28 {
29 /**
30 * Base directory to which the cartridge test report is written
31 *
32 * @parameter expression="${project.build.directory}/cartridge-test/reports"
33 */
34 protected File reportDirectory;
35
36 /**
37 * Specifies the directory that contains the "actual" output (meaning the output
38 * that was currently generated)
39 * @parameter expression="${project.build.directory}/cartridge-test/actual"
40 * @required
41 */
42 protected File actualDirectory;
43
44 /**
45 * Specifies the directory that contains the "expected" output.
46 * @parameter expression="${project.build.directory}/cartridge-test/expected"
47 * @required
48 */
49 protected File expectedDirectory;
50
51 /**
52 * The location of the archive storing the expected output.
53 * @parameter expression="${basedir}/src/test/expected/cartridge-outputUML2.zip"
54 * @required
55 */
56 protected File expectedOutputArchive;
57
58 /**
59 * This is the URI to the AndroMDA configuration file.
60 *
61 * @parameter expression="file:${basedir}/conf/test/andromdaUML2.xml"
62 * @required
63 */
64 protected String configurationUri;
65
66 /**
67 * @parameter expression="${project}"
68 * @required
69 * @readonly
70 */
71 protected MavenProject project;
72
73 /**
74 * @parameter expression="${project.build.filters}"
75 */
76 protected List<String> propertyFiles;
77
78 /**
79 * The current user system settings for use in Maven. (allows us to pass the user
80 * settings to the AndroMDA configuration).
81 *
82 * @parameter expression="${settings}"
83 * @required
84 * @readonly
85 */
86 protected Settings settings;
87
88 /**
89 * Defines the extensions of binary files, binary files are checked for presence
90 * and equality, however they aren't compared as strings, like every other file.
91 *
92 * @parameter expression="jpg,jpeg,gif,png,jar,zip"
93 */
94 protected String binaryOutputSuffixes;
95
96 /**
97 * To look up Archiver/UnArchiver implementations
98 *
99 * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager"
100 * @required
101 */
102 protected ArchiverManager archiverManager;
103
104 /**
105 * Set this to 'true' to bypass cartridge tests entirely. Its use is NOT RECOMMENDED, but quite convenient on occasion.
106 *
107 * @parameter expression="${maven.test.skip}"
108 */
109 protected boolean skip;
110
111 /**
112 * Set this to 'true' to skip running tests, but still compile them. Its use is NOT RECOMMENDED, but quite convenient on occasion.
113 *
114 * @parameter expression="${skipTests}"
115 */
116 protected boolean skipTests;
117
118 /**
119 * Set this to true to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion.
120 *
121 * @parameter expression="${maven.test.failure.ignore}" default-value="false"
122 */
123 protected boolean testFailureIgnore;
124
125 /**
126 * Set this to 'true' to skip code generation if code has not changed since the latest model date.
127 *
128 * @parameter expression="${lastModifiedCheck}"
129 */
130 protected boolean lastModifiedCheck;
131
132 /**
133 * Adds any dependencies for the cartridge plugin
134 * to the current dependencies of the project.
135 */
136 protected void changeScopeForTestDependencies()
137 {
138 // - get all test dependencies, change the scope and add them to them to the dependencies of this
139 // project as runtime scope so that the AndroMDA plugin can see them.
140 for (final Dependency dependency : (Iterable<Dependency>) this.project.getTestDependencies())
141 {
142 //process only test dependencies
143 if (dependency.getScope().equals(Artifact.SCOPE_TEST))
144 {
145 dependency.setScope(Artifact.SCOPE_RUNTIME);
146 project.getDependencies().add(dependency);
147 }
148 }
149
150 final Set artifacts = new HashSet<Artifact>(this.project.getArtifacts());
151 for (final Artifact artifact : (Iterable<Artifact>) this.project.getTestArtifacts())
152 {
153 //process only test dependencies
154 if (artifact.getScope().equals(Artifact.SCOPE_TEST))
155 {
156 artifact.setScope(Artifact.SCOPE_RUNTIME);
157 artifacts.add(artifact);
158 }
159 }
160 project.setArtifacts(artifacts);
161 }
162
163 /**
164 * Unpacks the expected archive file to the expected directory
165 *
166 * @param file File to be unpacked.
167 * @param location Location where to put the unpacked files.
168 * @throws MojoExecutionException Error unpacking file
169 */
170 protected void unpack(
171 final File file,
172 final File location)
173 throws MojoExecutionException
174 {
175 final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
176 try
177 {
178 final UnArchiver unArchiver;
179 unArchiver = this.archiverManager.getUnArchiver(archiveExt);
180 unArchiver.setSourceFile(file);
181 location.mkdirs();
182 unArchiver.setDestDirectory(location);
183 unArchiver.extract();
184 }
185 catch (Throwable throwable)
186 {
187 if (this.testFailureIgnore)
188 {
189 this.getLog().error(this.project.getArtifactId() + ": Error unpacking file: " + file + " to " + location, throwable);
190 }
191 else if (throwable instanceof IOException || throwable instanceof ArchiverException)
192 {
193 throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location, throwable);
194 }
195 }
196 }
197 }