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