View Javadoc
1   package org.andromda.maven.plugin.translationlibrary;
2   
3   import java.io.File;
4   import java.net.URL;
5   import java.util.List;
6   import junit.framework.TestResult;
7   import org.andromda.core.common.AndroMDALogger;
8   import org.andromda.core.common.ExceptionUtils;
9   import org.andromda.core.common.ResourceUtils;
10  import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo;
11  import org.andromda.translation.ocl.testsuite.TranslationTestProcessor;
12  import org.apache.maven.artifact.factory.ArtifactFactory;
13  import org.apache.maven.artifact.repository.ArtifactRepository;
14  import org.apache.maven.model.Plugin;
15  import org.apache.maven.plugin.MojoExecutionException;
16  import org.apache.maven.plugin.MojoFailureException;
17  import org.apache.maven.project.MavenProject;
18  import org.apache.maven.settings.Settings;
19  
20  /**
21   * Provides the ability to compare cartridge output with existing output.
22   *
23   * @phase test
24   * @goal test
25   * @requiresDependencyResolution test
26   * @description runs AndroMDA Translation-Library tests
27   * @author Chad Brandon
28   */
29  public class TranslationLibraryTestMojo
30      extends AbstractConfigurationMojo
31  {
32      /**
33       * Base directory to which the cartridge test report is written
34       *
35       * @parameter expression="${project.build.directory}/translation-library-test/reports"
36       */
37      private String reportDirectory;
38  
39      /**
40       * Whether or not the expression shall be 'traced' (i.e. the TraceTranslator will run instead of the specified translator).
41       * This is helpful, in allowing us to see which expressions are being parsed in what order, etc.
42       *
43       * @parameter expression="${trace.expression}"
44       */
45      protected boolean traceExpression;
46  
47      /**
48       * When specified, only this translation will be tested (If more than one TestTranslation-* file is found).
49       *
50       * @parameter expression="${translation.name}"
51       */
52      protected String translationName;
53  
54      /**
55       * The directory containing the test source.
56       *
57       * @parameter expression="${basedir}"
58       * @required
59       * @readonly
60       */
61      protected String testSourceDirectory;
62  
63      /**
64       * This is the URI to the AndroMDA configuration file.
65       *
66       * @parameter expression="file:${basedir}/conf/test/andromda.xml"
67       * @required
68       */
69      protected String configurationUri;
70  
71      /**
72       * @parameter expression="${project}"
73       * @required
74       * @readonly
75       */
76      private MavenProject project;
77  
78      /**
79       * @parameter expression="${project.build.filters}"
80       */
81      private List<String> propertyFiles;
82  
83      /**
84       * The current user system settings for use in Maven. (allows us to pass the user
85       * settings to the AndroMDA configuration).
86       *
87       * @parameter expression="${settings}"
88       * @required
89       * @readonly
90       */
91      private Settings settings;
92  
93      /**
94       * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
95       * @required
96       * @readonly
97       */
98      private ArtifactFactory factory;
99  
100     /**
101      * The registered plugin implementations.
102      *
103      * @parameter expression="${project.build.plugins}"
104      * @required
105      * @readonly
106      */
107     protected List<Plugin> plugins;
108 
109     /**
110      * @parameter expression="${localRepository}"
111      * @required
112      * @readonly
113      */
114     protected ArtifactRepository localRepository;
115 
116     /**
117      * Set this to 'true' to bypass cartridge tests entirely. Its use is NOT RECOMMENDED, but quite convenient on occasion.
118      *
119      * @parameter expression="${andromda.translation.test.skip}" default-value="false"
120      */
121     protected boolean skipTranslation;
122 
123     /**
124      * @see org.apache.maven.plugin.Mojo#execute()
125      */
126     public void execute()
127         throws MojoExecutionException, MojoFailureException
128     {
129         if (!this.skip && !this.skipTests && !this.skipTranslation)
130         {
131             try
132             {
133                 // - initialize the AndroMDA logger instance
134                 AndroMDALogger.initialize();
135 
136                 this.getLog().info("--------------------------------------------------------------------------------");
137                 this.getLog().info("  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  ");
138                 this.getLog().info("--------------------------------------------------------------------------------");
139 
140                 this.initializeClasspathFromClassPathElements(this.project.getTestClasspathElements());
141 
142                 final TranslationTestProcessor processor = TranslationTestProcessor.instance();
143                 processor.setTranslationName(this.translationName);
144                 processor.setUseTraceTranslator(this.traceExpression);
145                 processor.setTestSourceDirectory(this.testSourceDirectory);
146                 final URL configurationUri = ResourceUtils.toURL(this.configurationUri);
147                 if (configurationUri == null)
148                 {
149                     throw new MojoExecutionException("No configuration could be loaded from --> '" +
150                         this.configurationUri + '\'');
151                 }
152                 processor.setConfiguration(this.getConfiguration(configurationUri));
153 
154                 final TranslationLibraryTestFormatter formatter = new TranslationLibraryTestFormatter();
155 
156                 // - set the report location
157                 final File report = new File(this.reportDirectory, this.getProject().getArtifactId() + ".txt");
158                 formatter.setReportFile(report);
159                 final TestResult result = new TestResult();
160                 formatter.startTestSuite(this.getProject().getName());
161                 result.addListener(formatter);
162                 processor.setResult(result);
163                 processor.runSuite();
164                 this.getLog().info("");
165                 this.getLog().info("Results:");
166                 this.getLog().info(formatter.endTestSuite());
167                 if (result.failureCount() > 0 || result.errorCount() > 0)
168                 {
169                     throw new MojoExecutionException("There are some test failures");
170                 }
171                 processor.shutdown();
172             }
173             catch (final Throwable throwable)
174             {
175                 if (throwable instanceof MojoExecutionException && !this.testFailureIgnore)
176                 {
177                     throw (MojoExecutionException)throwable;
178                 }
179                 else if (this.testFailureIgnore)
180                 {
181                     this.getLog().error("An error occurred while testing translation-library '" +
182                             this.translationName + '\'',
183                         ExceptionUtils.getRootCause(throwable));
184                 }
185                 else
186                 {
187                     throw new MojoExecutionException("An error occurred while testing translation-library '" +
188                             this.translationName + '\'',
189                             ExceptionUtils.getRootCause(throwable));
190                 }
191             }
192         }
193         else
194         {
195             this.getLog().info("Skipping translation-library tests");
196         }
197     }
198 
199     /**
200      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject()
201      */
202     protected MavenProject getProject()
203     {
204         return this.project;
205     }
206 
207     /**
208      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles()
209      */
210     protected List<String> getPropertyFiles()
211     {
212         return this.propertyFiles;
213     }
214 
215     /**
216      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings()
217      */
218     protected Settings getSettings()
219     {
220         return this.settings;
221     }
222 
223     /**
224      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory()
225      */
226     protected ArtifactFactory getFactory()
227     {
228         return this.factory;
229     }
230 
231     /**
232      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins()
233      */
234     protected List<Plugin> getPlugins()
235     {
236         return this.plugins;
237     }
238 
239     /**
240      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository()
241      */
242     protected ArtifactRepository getLocalRepository()
243     {
244         return this.localRepository;
245     }
246 }