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.MavenSession;
27 import org.apache.maven.plugin.Mojo;
28 import org.apache.maven.plugin.MojoExecution;
29 import org.apache.maven.project.MavenProject;
30 import org.codehaus.plexus.ContainerConfiguration;
31 import org.codehaus.plexus.PlexusContainer;
32 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
33 import org.codehaus.plexus.configuration.PlexusConfiguration;
34 import org.codehaus.plexus.util.xml.Xpp3Dom;
35 import org.junit.rules.TestRule;
36 import org.junit.runner.Description;
37 import org.junit.runners.model.Statement;
38
39
40
41
42
43
44
45
46
47
48
49 public class MojoRule
50 implements TestRule
51 {
52 private final AbstractMojoTestCase testCase;
53
54 public MojoRule()
55 {
56 this( new AbstractMojoTestCase() {} );
57 }
58
59 public MojoRule(AbstractMojoTestCase testCase)
60 {
61 this.testCase = testCase;
62 }
63
64
65
66
67
68
69
70 protected void before() throws Throwable
71 {
72
73 }
74
75
76
77
78 protected void after()
79 {
80
81 }
82
83 public InputStream getPublicDescriptorStream()
84 throws Exception
85 {
86 return testCase.getPublicDescriptorStream();
87 }
88
89 public String getPluginDescriptorPath()
90 {
91 return testCase.getPluginDescriptorPath();
92 }
93
94 public String getPluginDescriptorLocation()
95 {
96 return testCase.getPluginDescriptorLocation();
97 }
98
99 public void setupContainer()
100 {
101 testCase.setupContainer();
102 }
103
104 public ContainerConfiguration setupContainerConfiguration()
105 {
106 return testCase.setupContainerConfiguration();
107 }
108
109 public PlexusContainer getContainer()
110 {
111 return testCase.getContainer();
112 }
113
114
115
116
117
118
119
120
121
122 public Mojo lookupMojo( String goal, String pluginPom )
123 throws Exception
124 {
125 return testCase.lookupMojo( goal, pluginPom );
126 }
127
128
129
130
131
132
133
134
135
136 public Mojo lookupEmptyMojo( String goal, String pluginPom )
137 throws Exception
138 {
139 return testCase.lookupEmptyMojo( goal, new File( pluginPom ) );
140 }
141
142
143
144
145
146
147
148
149
150 public Mojo lookupMojo( String goal, File pom )
151 throws Exception
152 {
153 return testCase.lookupMojo( goal, pom );
154 }
155
156
157
158
159
160
161
162
163
164 public Mojo lookupEmptyMojo( String goal, File pom )
165 throws Exception
166 {
167 return testCase.lookupEmptyMojo( goal, pom );
168 }
169
170 public Mojo lookupMojo( String groupId, String artifactId, String version, String goal,
171 PlexusConfiguration pluginConfiguration )
172 throws Exception
173 {
174 return testCase.lookupMojo( groupId, artifactId, version, goal, pluginConfiguration );
175 }
176
177 public Mojo lookupConfiguredMojo( MavenProject project, String goal )
178 throws Exception
179 {
180 return testCase.lookupConfiguredMojo( project, goal );
181 }
182
183 public Mojo lookupConfiguredMojo( MavenSession session, MojoExecution execution )
184 throws Exception, ComponentConfigurationException
185 {
186 return testCase.lookupConfiguredMojo( session, execution );
187 }
188
189 public MavenSession newMavenSession( MavenProject project )
190 {
191 return testCase.newMavenSession( project );
192 }
193
194 public MojoExecution newMojoExecution( String goal )
195 {
196 return testCase.newMojoExecution( goal );
197 }
198
199 public PlexusConfiguration extractPluginConfiguration( String artifactId, File pom )
200 throws Exception
201 {
202 return testCase.extractPluginConfiguration( artifactId, pom );
203 }
204
205 public PlexusConfiguration extractPluginConfiguration( String artifactId, Xpp3Dom pomDom )
206 throws Exception
207 {
208 return testCase.extractPluginConfiguration( artifactId, pomDom );
209 }
210
211 public Mojo configureMojo( Mojo mojo, String artifactId, File pom )
212 throws Exception
213 {
214 return testCase.configureMojo( mojo, artifactId, pom );
215 }
216
217 public Mojo configureMojo( Mojo mojo, PlexusConfiguration pluginConfiguration )
218 throws Exception
219 {
220 return testCase.configureMojo( mojo, pluginConfiguration );
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public Object getVariableValueFromObject( Object object, String variable )
234 throws IllegalAccessException
235 {
236 return testCase.getVariableValueFromObject( object, variable );
237 }
238
239
240
241
242
243
244
245
246
247 public Map<String, Object> getVariablesAndValuesFromObject( Object object )
248 throws IllegalAccessException
249 {
250 return testCase.getVariablesAndValuesFromObject( object );
251 }
252
253
254
255
256
257
258
259
260
261
262 public Map<String, Object> getVariablesAndValuesFromObject( Class<?> clazz, Object object )
263 throws IllegalAccessException
264 {
265 return testCase.getVariablesAndValuesFromObject( clazz, object );
266 }
267
268
269
270
271
272
273
274
275
276 public void setVariableValueToObject( Object object, String variable, Object value )
277 throws IllegalAccessException
278 {
279 testCase.setVariableValueToObject( object, variable, value );
280 }
281
282 @Override
283 public Statement apply(final Statement base, Description description) {
284 if (description.getAnnotation(WithoutMojo.class) != null)
285 {
286 return base;
287 }
288 return new Statement()
289 {
290 @Override
291 public void evaluate() throws Throwable
292 {
293 testCase.setUp();
294 before();
295 try
296 {
297 base.evaluate();
298 }
299 finally
300 {
301 after();
302 }
303 }
304 };
305 }
306 }