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