1 package org.apache.maven.plugin.testing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.InputStream;
24 import java.util.Map;
25
26 import org.apache.maven.execution.DefaultMavenExecutionRequest;
27 import org.apache.maven.execution.MavenExecutionRequest;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.plugin.Mojo;
30 import org.apache.maven.plugin.MojoExecution;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuilder;
33 import org.apache.maven.project.ProjectBuildingRequest;
34 import org.codehaus.plexus.ContainerConfiguration;
35 import org.codehaus.plexus.PlexusContainer;
36 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
37 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
38 import org.codehaus.plexus.configuration.PlexusConfiguration;
39 import org.codehaus.plexus.util.xml.Xpp3Dom;
40 import org.junit.Assert;
41 import org.junit.rules.TestRule;
42 import org.junit.runner.Description;
43 import org.junit.runners.model.Statement;
44
45
46
47
48
49
50
51
52
53
54
55 public class MojoRule
56 implements TestRule
57 {
58 private final AbstractMojoTestCase testCase;
59
60 public MojoRule()
61 {
62 this( new AbstractMojoTestCase() {} );
63 }
64
65 public MojoRule(AbstractMojoTestCase testCase)
66 {
67 this.testCase = testCase;
68 }
69
70
71
72
73
74
75
76 protected void before() throws Throwable
77 {
78
79 }
80
81
82
83
84 protected void after()
85 {
86
87 }
88
89 public InputStream getPublicDescriptorStream()
90 throws Exception
91 {
92 return testCase.getPublicDescriptorStream();
93 }
94
95 public String getPluginDescriptorPath()
96 {
97 return testCase.getPluginDescriptorPath();
98 }
99
100 public String getPluginDescriptorLocation()
101 {
102 return testCase.getPluginDescriptorLocation();
103 }
104
105 public void setupContainer()
106 {
107 testCase.setupContainer();
108 }
109
110 public ContainerConfiguration setupContainerConfiguration()
111 {
112 return testCase.setupContainerConfiguration();
113 }
114
115 public PlexusContainer getContainer()
116 {
117 return testCase.getContainer();
118 }
119
120
121
122
123
124
125
126
127
128 public Mojo lookupMojo( String goal, String pluginPom )
129 throws Exception
130 {
131 return testCase.lookupMojo( goal, pluginPom );
132 }
133
134
135
136
137
138
139
140
141
142 public Mojo lookupEmptyMojo( String goal, String pluginPom )
143 throws Exception
144 {
145 return testCase.lookupEmptyMojo( goal, new File( pluginPom ) );
146 }
147
148
149
150
151
152
153
154
155
156 public Mojo lookupMojo( String goal, File pom )
157 throws Exception
158 {
159 return testCase.lookupMojo( goal, pom );
160 }
161
162
163
164
165
166
167
168
169
170 public Mojo lookupEmptyMojo( String goal, File pom )
171 throws Exception
172 {
173 return testCase.lookupEmptyMojo( goal, pom );
174 }
175
176 public Mojo lookupMojo( String groupId, String artifactId, String version, String goal,
177 PlexusConfiguration pluginConfiguration )
178 throws Exception
179 {
180 return testCase.lookupMojo( groupId, artifactId, version, goal, pluginConfiguration );
181 }
182
183 public Mojo lookupConfiguredMojo( MavenProject project, String goal )
184 throws Exception
185 {
186 return testCase.lookupConfiguredMojo( project, goal );
187 }
188
189 public Mojo lookupConfiguredMojo( MavenSession session, MojoExecution execution )
190 throws Exception, ComponentConfigurationException
191 {
192 return testCase.lookupConfiguredMojo( session, execution );
193 }
194
195 public MavenSession newMavenSession( MavenProject project )
196 {
197 return testCase.newMavenSession( project );
198 }
199
200 public MojoExecution newMojoExecution( String goal )
201 {
202 return testCase.newMojoExecution( goal );
203 }
204
205 public PlexusConfiguration extractPluginConfiguration( String artifactId, File pom )
206 throws Exception
207 {
208 return testCase.extractPluginConfiguration( artifactId, pom );
209 }
210
211 public PlexusConfiguration extractPluginConfiguration( String artifactId, Xpp3Dom pomDom )
212 throws Exception
213 {
214 return testCase.extractPluginConfiguration( artifactId, pomDom );
215 }
216
217 public Mojo configureMojo( Mojo mojo, String artifactId, File pom )
218 throws Exception
219 {
220 return testCase.configureMojo( mojo, artifactId, pom );
221 }
222
223 public Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration )
224 throws Exception
225 {
226 return testCase.configureMojo( mojo, pluginConfiguration );
227 }
228
229
230
231
232
233
234
235
236
237
238
239 public Object getVariableValueFromObject( Object object, String variable )
240 throws IllegalAccessException
241 {
242 return testCase.getVariableValueFromObject( object, variable );
243 }
244
245
246
247
248
249
250
251
252
253 public Map<String, Object> getVariablesAndValuesFromObject( Object object )
254 throws IllegalAccessException
255 {
256 return testCase.getVariablesAndValuesFromObject( object );
257 }
258
259
260
261
262
263
264
265
266
267
268 public Map<String, Object> getVariablesAndValuesFromObject( Class<?> clazz, Object object )
269 throws IllegalAccessException
270 {
271 return testCase.getVariablesAndValuesFromObject( clazz, object );
272 }
273
274
275
276
277
278
279
280
281
282 public void setVariableValueToObject( Object object, String variable, Object value )
283 throws IllegalAccessException
284 {
285 testCase.setVariableValueToObject( object, variable, value );
286 }
287
288 @Override
289 public Statement apply(final Statement base, Description description) {
290 if (description.getAnnotation(WithoutMojo.class) != null)
291 {
292 return base;
293 }
294 return new Statement()
295 {
296 @Override
297 public void evaluate() throws Throwable
298 {
299 testCase.setUp();
300 before();
301 try
302 {
303 base.evaluate();
304 }
305 finally
306 {
307 after();
308 }
309 }
310 };
311 }
312
313
314
315
316 public MavenProject readMavenProject( File basedir )
317 throws Exception
318 {
319 File pom = new File( basedir, "pom.xml" );
320 MavenExecutionRequest request = new DefaultMavenExecutionRequest();
321 request.setBaseDirectory( basedir );
322 ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
323 MavenProject project = lookup( ProjectBuilder.class ).build( pom, configuration ).getProject();
324 Assert.assertNotNull( project );
325 return project;
326 }
327
328
329
330
331 public void executeMojo( File basedir, String goal )
332 throws Exception
333 {
334 lookupConfiguredMojo( basedir, goal ).execute();
335 }
336
337
338
339
340 public Mojo lookupConfiguredMojo( File basedir, String goal )
341 throws Exception, ComponentConfigurationException
342 {
343 MavenProject project = readMavenProject( basedir );
344 MavenSession session = newMavenSession( project );
345 MojoExecution execution = newMojoExecution( goal );
346 return lookupConfiguredMojo( session, execution );
347 }
348
349
350
351
352 public final <T> T lookup( final Class<T> role )
353 throws ComponentLookupException
354 {
355 return getContainer().lookup( role );
356 }
357
358 }