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