View Javadoc

1   package org.apache.maven.plugin.testing;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * {@link TestRule} for usage with Junit-4.10ff. This is just a wrapper for an embedded 
41   * {@link AbstractMojoTestCase}, so all <tt>protected</tt> methods of the TestCase are 
42   * exhibited as <tt>public</tt> in the rule. You may annotate single tests methods with 
43   * {@link WithoutMojo} to prevent the rule from firing.
44   *
45   * @author Mirko Friedenhagen
46   * @version $Id$
47   * @since 2.2
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       * May be overridden in the implementation to do stuff <em>after</em> the embedded test case 
66       * is set up but <em>before</em> the current test is actually run.
67       *
68       * @throws Throwable
69       */
70      protected void before() throws Throwable
71      {
72          
73      }
74      
75      /**
76       * May be overridden in the implementation to do stuff after the current test was run.
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      * Lookup the mojo leveraging the subproject pom
116      *
117      * @param goal
118      * @param pluginPom
119      * @return a Mojo instance
120      * @throws Exception
121      */
122     public Mojo lookupMojo( String goal, String pluginPom )
123         throws Exception
124     {
125         return testCase.lookupMojo( goal, pluginPom );
126     }
127 
128     /**
129      * Lookup an empty mojo
130      *
131      * @param goal
132      * @param pluginPom
133      * @return a Mojo instance
134      * @throws Exception
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      * Lookup the mojo leveraging the actual subprojects pom
144      *
145      * @param goal
146      * @param pom
147      * @return a Mojo instance
148      * @throws Exception
149      */
150     public Mojo lookupMojo( String goal, File pom )
151         throws Exception
152     {
153         return testCase.lookupMojo( goal, pom );
154     }
155 
156     /**
157      * Lookup the mojo leveraging the actual subprojects pom
158      *
159      * @param goal
160      * @param pom
161      * @return a Mojo instance
162      * @throws Exception
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      * Convenience method to obtain the value of a variable on a mojo that might not have a getter.
225      *
226      * NOTE: the caller is responsible for casting to to what the desired type is.
227      *
228      * @param object
229      * @param variable
230      * @return object value of variable
231      * @throws IllegalArgumentException
232      */
233     public Object getVariableValueFromObject( Object object, String variable )
234         throws IllegalAccessException
235     {
236         return testCase.getVariableValueFromObject( object, variable );
237     }
238 
239     /**
240      * Convenience method to obtain all variables and values from the mojo (including its superclasses)
241      *
242      * Note: the values in the map are of type Object so the caller is responsible for casting to desired types.
243      *
244      * @param object
245      * @return map of variable names and values
246      */
247     public Map<String, Object> getVariablesAndValuesFromObject( Object object )
248         throws IllegalAccessException
249     {
250         return testCase.getVariablesAndValuesFromObject( object );
251     }
252 
253     /**
254      * Convenience method to obtain all variables and values from the mojo (including its superclasses)
255      *
256      * Note: the values in the map are of type Object so the caller is responsible for casting to desired types.
257      *
258      * @param clazz
259      * @param object
260      * @return map of variable names and values
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      * Convenience method to set values to variables in objects that don't have setters
270      *
271      * @param object
272      * @param variable
273      * @param value
274      * @throws IllegalAccessException
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) // skip.
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 }