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
22
23
24
25
26
27
28
29 public class TranslationLibraryTestMojo
30 extends AbstractConfigurationMojo
31 {
32
33
34
35
36
37 private String reportDirectory;
38
39
40
41
42
43
44
45 protected boolean traceExpression;
46
47
48
49
50
51
52 protected String translationName;
53
54
55
56
57
58
59
60
61 protected String testSourceDirectory;
62
63
64
65
66
67
68
69 protected String configurationUri;
70
71
72
73
74
75
76 private MavenProject project;
77
78
79
80
81 private List<String> propertyFiles;
82
83
84
85
86
87
88
89
90
91 private Settings settings;
92
93
94
95
96
97
98 private ArtifactFactory factory;
99
100
101
102
103
104
105
106
107 protected List<Plugin> plugins;
108
109
110
111
112
113
114 protected ArtifactRepository localRepository;
115
116
117
118
119
120
121 protected boolean skipTranslation;
122
123
124
125
126 public void execute()
127 throws MojoExecutionException, MojoFailureException
128 {
129 if (!this.skip && !this.skipTests && !this.skipTranslation)
130 {
131 try
132 {
133
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
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
201
202 protected MavenProject getProject()
203 {
204 return this.project;
205 }
206
207
208
209
210 protected List<String> getPropertyFiles()
211 {
212 return this.propertyFiles;
213 }
214
215
216
217
218 protected Settings getSettings()
219 {
220 return this.settings;
221 }
222
223
224
225
226 protected ArtifactFactory getFactory()
227 {
228 return this.factory;
229 }
230
231
232
233
234 protected List<Plugin> getPlugins()
235 {
236 return this.plugins;
237 }
238
239
240
241
242 protected ArtifactRepository getLocalRepository()
243 {
244 return this.localRepository;
245 }
246 }