View Javadoc
1   package org.codehaus.plexus;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
21  import org.codehaus.plexus.component.repository.exception.ComponentLookupRuntimeException;
22  import org.codehaus.plexus.configuration.PlexusConfiguration;
23  import org.codehaus.plexus.context.Context;
24  import org.codehaus.plexus.context.DefaultContext;
25  import org.junit.After;
26  import org.junit.Before;
27  
28  import java.io.File;
29  import java.io.InputStream;
30  
31  import static org.junit.Assert.fail;
32  
33  /**
34   * @author Jason van Zyl
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
37   * @version $Id$
38   */
39  public abstract class PlexusJUnit4TestCase
40  {
41      private PlexusContainer container;
42  
43      private static String basedir;
44  
45      @Before
46      public void beforeTest()
47          throws Exception
48      {
49          basedir = getBasedir();
50      }
51  
52      protected void setupContainer()
53      {
54          // ----------------------------------------------------------------------------
55          // Context Setup
56          // ----------------------------------------------------------------------------
57  
58          DefaultContext context = new DefaultContext();
59  
60          context.put( "basedir", getBasedir() );
61  
62          customizeContext( context );
63  
64          boolean hasPlexusHome = context.contains( "plexus.home" );
65  
66          if ( !hasPlexusHome )
67          {
68              File f = getTestFile( "target/plexus-home" );
69  
70              if ( !f.isDirectory() )
71              {
72                  f.mkdir();
73              }
74  
75              context.put( "plexus.home", f.getAbsolutePath() );
76          }
77  
78          // ----------------------------------------------------------------------------
79          // Configuration
80          // ----------------------------------------------------------------------------
81  
82          String config = getCustomConfigurationName();
83  
84          ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration()
85              .setName( "test" )
86              .setContext( context.getContextData() );
87  
88          if ( config != null )
89          {
90              containerConfiguration.setContainerConfiguration( config );
91          }
92          else
93          {
94              String resource = getConfigurationName( null );
95  
96              containerConfiguration.setContainerConfiguration( resource );
97          }
98  
99          customizeContainerConfiguration( containerConfiguration );
100 
101         try
102         {
103             container = new DefaultPlexusContainer( containerConfiguration );
104         }
105         catch ( PlexusContainerException e )
106         {
107             e.printStackTrace();
108             fail( "Failed to create plexus container." );
109         }
110     }
111 
112     /**
113      * Allow custom test case implementations do augment the default container configuration before
114      * executing tests.
115      *
116      * @param containerConfiguration
117      */
118     protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
119     {
120     }
121 
122     protected void customizeContext( Context context )
123     {
124     }
125 
126     protected PlexusConfiguration customizeComponentConfiguration()
127     {
128         return null;
129     }
130 
131     @After
132     public void afterTest()
133         throws Exception
134     {
135         if ( container != null )
136         {
137             container.dispose();
138 
139             container = null;
140         }
141     }
142 
143     protected PlexusContainer getContainer()
144     {
145         if ( container == null )
146         {
147             setupContainer();
148         }
149 
150         return container;
151     }
152 
153     protected InputStream getConfiguration()
154     {
155         return getConfiguration( null );
156     }
157 
158     protected InputStream getConfiguration( String subname )
159     {
160         return getResourceAsStream( getConfigurationName( subname ) );
161     }
162 
163     protected String getCustomConfigurationName()
164     {
165         return null;
166     }
167 
168     /**
169      * Allow the retrieval of a container configuration that is based on the name
170      * of the test class being run. So if you have a test class called org.foo.FunTest, then
171      * this will produce a resource name of org/foo/FunTest.xml which would be used to
172      * configure the Plexus container before running your test.
173      *
174      * @param subname
175      * @return
176      */
177     protected String getConfigurationName( String subname )
178     {
179         return getClass().getName().replace( '.', '/' ) + ".xml";
180     }
181 
182     protected InputStream getResourceAsStream( String resource )
183     {
184         return getClass().getResourceAsStream( resource );
185     }
186 
187     protected ClassLoader getClassLoader()
188     {
189         return getClass().getClassLoader();
190     }
191 
192     // ----------------------------------------------------------------------
193     // Container access
194     // ----------------------------------------------------------------------
195 
196     protected Object lookup( String componentKey )
197     {
198         try
199         {
200             return getContainer().lookup( componentKey );
201         }
202         catch ( ComponentLookupException e )
203         {
204             throw new ComponentLookupRuntimeException( e );
205         }
206     }
207 
208     protected Object lookup( String role,
209                              String roleHint )
210         throws ComponentLookupRuntimeException
211     {
212         try
213         {
214             return getContainer().lookup( role, roleHint );
215         }
216         catch ( ComponentLookupException e )
217         {
218             throw new ComponentLookupRuntimeException( e );
219         }
220     }
221 
222     protected <T> T lookup( Class<T> componentClass )
223         throws ComponentLookupRuntimeException
224     {
225         try
226         {
227             return getContainer().lookup( componentClass );
228         }
229         catch ( ComponentLookupException e )
230         {
231             throw new ComponentLookupRuntimeException( e );
232         }
233     }
234 
235     protected <T> T lookup( Class<T> componentClass, String roleHint )
236         throws ComponentLookupRuntimeException
237     {
238         try
239         {
240             return getContainer().lookup( componentClass, roleHint );
241         }
242         catch ( ComponentLookupException e )
243         {
244             throw new ComponentLookupRuntimeException( e );
245         }
246     }
247 
248     protected void release( Object component )
249         throws Exception
250     {
251         getContainer().release( component );
252     }
253 
254     // ----------------------------------------------------------------------
255     // Helper methods for sub classes
256     // ----------------------------------------------------------------------
257 
258     public static File getTestFile( String path )
259     {
260         return new File( getBasedir(), path );
261     }
262 
263     public static File getTestFile( String basedir,
264                                     String path )
265     {
266         File basedirFile = new File( basedir );
267 
268         if ( !basedirFile.isAbsolute() )
269         {
270             basedirFile = getTestFile( basedir );
271         }
272 
273         return new File( basedirFile, path );
274     }
275 
276     public static String getTestPath( String path )
277     {
278         return getTestFile( path ).getAbsolutePath();
279     }
280 
281     public static String getTestPath( String basedir,
282                                       String path )
283     {
284         return getTestFile( basedir, path ).getAbsolutePath();
285     }
286 
287     public static String getBasedir()
288     {
289         if ( basedir != null )
290         {
291             return basedir;
292         }
293 
294         basedir = System.getProperty( "basedir" );
295 
296         if ( basedir == null )
297         {
298             basedir = new File( "" ).getAbsolutePath();
299         }
300 
301         return basedir;
302     }
303 
304     public String getTestConfiguration()
305     {
306         return getTestConfiguration( getClass() );
307     }
308 
309     public static String getTestConfiguration( Class<?> clazz )
310     {
311         String s = clazz.getName().replace( '.', '/' );
312 
313         return s.substring( 0, s.indexOf( "$" ) ) + ".xml";
314     }
315 }