001: package org.andromda.maven.plugin.bootstrap;
002:
003: import java.io.File;
004:
005: import java.net.URL;
006:
007: import java.util.List;
008:
009: import junit.framework.TestResult;
010:
011: import org.andromda.core.common.ExceptionUtils;
012: import org.andromda.core.common.ResourceUtils;
013: import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo;
014: import org.andromda.translation.ocl.testsuite.TranslationTestProcessor;
015: import org.apache.maven.artifact.factory.ArtifactFactory;
016: import org.apache.maven.artifact.repository.ArtifactRepository;
017: import org.apache.maven.plugin.MojoExecutionException;
018: import org.apache.maven.plugin.MojoFailureException;
019: import org.apache.maven.project.MavenProject;
020: import org.apache.maven.settings.Settings;
021:
022: /**
023: * The bootstrap version of the translation-library test.
024: *
025: * @phase test
026: * @goal test-translation-library
027: * @requiresDependencyResolution test
028: * @description runs AndroMDA Translation-Library tests
029: * @author Chad Brandon
030: */
031: public class TranslationLibraryTestMojo extends
032: AbstractConfigurationMojo {
033: /**
034: * Base directory to which the cartridge test report is written
035: *
036: * @parameter expression="${project.build.directory}/translation-library-test/reports"
037: */
038: private String reportDirectory;
039:
040: /**
041: * Whether or not the expression shall be 'traced' (i.e. the TraceTranslator will run instead of the specified translator).
042: * This is helpful, in allowing us to see which expressions are being parsed in what order, etc.
043: *
044: * @parameter expression="${trace.expression}"
045: */
046: protected boolean traceExpression;
047:
048: /**
049: * When specified, only this translation will be tested (If more than one TestTranslation-* file is found).
050: *
051: * @parameter expression="${translation.name}"
052: */
053: protected String translationName;
054:
055: /**
056: * The directory containing the test source.
057: *
058: * @parameter expression="${basedir}"
059: * @required
060: * @readonly
061: */
062: protected String testSourceDirectory;
063:
064: /**
065: * Set this to 'true' to bypass translation-library tests entirely. Its use is NOT RECOMMENDED, but quite convenient on occasion.
066: *
067: * @parameter expression="${maven.test.skip}"
068: */
069: protected boolean skip;
070:
071: /**
072: * This is the URI to the AndroMDA configuration file.
073: *
074: * @parameter expression="file:${basedir}/conf/test/andromda.xml"
075: * @required
076: */
077: protected String configurationUri;
078:
079: /**
080: * @parameter expression="${project}"
081: * @required
082: * @readonly
083: */
084: private MavenProject project;
085:
086: /**
087: * @parameter expression="${project.build.filters}"
088: */
089: private List propertyFiles;
090:
091: /**
092: * The current user system settings for use in Maven. (allows us to pass the user
093: * settings to the AndroMDA configuration).
094: *
095: * @parameter expression="${settings}"
096: * @required
097: * @readonly
098: */
099: private Settings settings;
100:
101: /**
102: * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
103: * @required
104: * @readonly
105: */
106: private ArtifactFactory factory;
107:
108: /**
109: * The registered plugin implementations.
110: *
111: * @parameter expression="${project.build.plugins}"
112: * @required
113: * @readonlya
114: */
115: protected List plugins;
116:
117: /**
118: * @parameter expression="${localRepository}"
119: * @required
120: * @readonly
121: */
122: protected ArtifactRepository localRepository;
123:
124: /**
125: * @see org.apache.maven.plugin.Mojo#execute()
126: */
127: public void execute() throws MojoExecutionException,
128: MojoFailureException {
129: if (!this .skip) {
130: try {
131: this
132: .getLog()
133: .info(
134: "--------------------------------------------------------------------------------");
135: this
136: .getLog()
137: .info(
138: " A n d r o M D A T r a n s l a t i o n - L i b r a r y T e s t S u i t e ");
139: this
140: .getLog()
141: .info(
142: "--------------------------------------------------------------------------------");
143:
144: this
145: .initializeClasspathFromClassPathElements(this .project
146: .getTestClasspathElements());
147:
148: final TranslationTestProcessor processor = TranslationTestProcessor
149: .instance();
150: processor.setTranslationName(this .translationName);
151: processor.setUseTraceTranslator(this .traceExpression);
152: processor
153: .setTestSourceDirectory(this .testSourceDirectory);
154: final URL configurationUri = ResourceUtils
155: .toURL(this .configurationUri);
156: if (configurationUri == null) {
157: throw new MojoExecutionException(
158: "No configuration could be loaded from --> '"
159: + this .configurationUri + "'");
160: }
161: processor.setConfiguration(this
162: .getConfiguration(configurationUri));
163:
164: final TranslationLibraryTestFormatter formatter = new TranslationLibraryTestFormatter();
165:
166: // - set the report location
167: final File report = new File(this .reportDirectory, this
168: .getProject().getArtifactId()
169: + ".txt");
170: formatter.setReportFile(report);
171: final TestResult result = new TestResult();
172: formatter.startTestSuite(this .getProject().getName());
173: result.addListener(formatter);
174: processor.setResult(result);
175: processor.runSuite();
176: this .getLog().info("");
177: this .getLog().info("Results:");
178: this .getLog().info(formatter.endTestSuite());
179: if (result.failureCount() > 0
180: || result.errorCount() > 0) {
181: throw new MojoExecutionException(
182: "Test are some test failures");
183: }
184: processor.shutdown();
185: } catch (final Throwable throwable) {
186: if (throwable instanceof MojoExecutionException) {
187: throw (MojoExecutionException) throwable;
188: }
189: throw new MojoExecutionException(
190: "An error occured while testing translation-library",
191: ExceptionUtils.getRootCause(throwable));
192: }
193: } else {
194: this .getLog().info("Skipping translation-library tests");
195: }
196: }
197:
198: /**
199: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject()
200: */
201: protected MavenProject getProject() {
202: return this .project;
203: }
204:
205: /**
206: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles()
207: */
208: protected List getPropertyFiles() {
209: return this .propertyFiles;
210: }
211:
212: /**
213: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings()
214: */
215: protected Settings getSettings() {
216: return this .settings;
217: }
218:
219: /**
220: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory()
221: */
222: protected ArtifactFactory getFactory() {
223: return this .factory;
224: }
225:
226: /**
227: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins()
228: */
229: protected List getPlugins() {
230: return this .plugins;
231: }
232:
233: /**
234: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository()
235: */
236: protected ArtifactRepository getLocalRepository() {
237: return this.localRepository;
238: }
239: }
|