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 java.io.File;
20  import java.io.InputStream;
21  
22  import junit.framework.TestCase;
23  
24  import org.codehaus.plexus.configuration.PlexusConfiguration;
25  import org.codehaus.plexus.context.Context;
26  import org.codehaus.plexus.context.DefaultContext;
27  
28  /**
29   * @author Jason van Zyl
30   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
31   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
32   * @version $Id$
33   */
34  public abstract class PlexusTestCase
35      extends TestCase
36  {
37      private PlexusContainer container;
38  
39      private static String basedir;
40  
41      protected void setUp()
42          throws Exception
43      {
44          basedir = getBasedir();
45      }
46  
47      protected void setupContainer()
48      {
49          // ----------------------------------------------------------------------------
50          // Context Setup
51          // ----------------------------------------------------------------------------
52  
53          DefaultContext context = new DefaultContext();
54  
55          context.put( "basedir", getBasedir() );
56  
57          customizeContext( context );
58  
59          boolean hasPlexusHome = context.contains( "plexus.home" );
60  
61          if ( !hasPlexusHome )
62          {
63              File f = getTestFile( "target/plexus-home" );
64  
65              if ( !f.isDirectory() )
66              {
67                  f.mkdir();
68              }
69  
70              context.put( "plexus.home", f.getAbsolutePath() );
71          }
72  
73          // ----------------------------------------------------------------------------
74          // Configuration
75          // ----------------------------------------------------------------------------
76  
77          String config = getCustomConfigurationName();
78  
79          ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration()
80              .setName( "test" )
81              .setContext( context.getContextData() );
82  
83          if ( config != null )
84          {
85              containerConfiguration.setContainerConfiguration( config );
86          }
87          else
88          {
89              String resource = getConfigurationName( null );
90  
91              containerConfiguration.setContainerConfiguration( resource );
92          }
93  
94          customizeContainerConfiguration( containerConfiguration );
95  
96          try
97          {
98              container = new DefaultPlexusContainer( containerConfiguration );
99          }
100         catch ( PlexusContainerException e )
101         {
102             e.printStackTrace();
103             fail( "Failed to create plexus container." );
104         }
105     }
106 
107     /**
108      * Allow custom test case implementations do augment the default container configuration before
109      * executing tests.
110      *
111      * @param containerConfiguration
112      */
113     protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
114     {
115     }
116 
117     protected void customizeContext( Context context )
118     {
119     }
120 
121     protected PlexusConfiguration customizeComponentConfiguration()
122     {
123         return null;
124     }
125 
126     protected void tearDown()
127         throws Exception
128     {
129         if ( container != null )
130         {
131             container.dispose();
132 
133             container = null;
134         }
135     }
136 
137     protected PlexusContainer getContainer()
138     {
139         if ( container == null )
140         {
141             setupContainer();
142         }
143 
144         return container;
145     }
146 
147     protected InputStream getConfiguration()
148         throws Exception
149     {
150         return getConfiguration( null );
151     }
152 
153     protected InputStream getConfiguration( String subname )
154         throws Exception
155     {
156         return getResourceAsStream( getConfigurationName( subname ) );
157     }
158 
159     protected String getCustomConfigurationName()
160     {
161         return null;
162     }
163 
164     /**
165      * Allow the retrieval of a container configuration that is based on the name
166      * of the test class being run. So if you have a test class called org.foo.FunTest, then
167      * this will produce a resource name of org/foo/FunTest.xml which would be used to
168      * configure the Plexus container before running your test.
169      *
170      * @param subname
171      * @return
172      */
173     protected String getConfigurationName( String subname )
174     {
175         return getClass().getName().replace( '.', '/' ) + ".xml";
176     }
177 
178     protected InputStream getResourceAsStream( String resource )
179     {
180         return getClass().getResourceAsStream( resource );
181     }
182 
183     protected ClassLoader getClassLoader()
184     {
185         return getClass().getClassLoader();
186     }
187 
188     // ----------------------------------------------------------------------
189     // Container access
190     // ----------------------------------------------------------------------
191 
192     protected Object lookup( String componentKey )
193         throws Exception
194     {
195         return getContainer().lookup( componentKey );
196     }
197 
198     protected Object lookup( String role,
199                              String roleHint )
200         throws Exception
201     {
202         return getContainer().lookup( role, roleHint );
203     }
204 
205     protected <T> T lookup( Class<T> componentClass )
206         throws Exception
207     {
208         return getContainer().lookup( componentClass );
209     }
210 
211     protected <T> T lookup( Class<T> componentClass, String roleHint )
212         throws Exception
213     {
214         return getContainer().lookup( componentClass, roleHint );
215     }
216 
217     protected void release( Object component )
218         throws Exception
219     {
220         getContainer().release( component );
221     }
222 
223     // ----------------------------------------------------------------------
224     // Helper methods for sub classes
225     // ----------------------------------------------------------------------
226 
227     public static File getTestFile( String path )
228     {
229         return new File( getBasedir(), path );
230     }
231 
232     public static File getTestFile( String basedir,
233                                     String path )
234     {
235         File basedirFile = new File( basedir );
236 
237         if ( !basedirFile.isAbsolute() )
238         {
239             basedirFile = getTestFile( basedir );
240         }
241 
242         return new File( basedirFile, path );
243     }
244 
245     public static String getTestPath( String path )
246     {
247         return getTestFile( path ).getAbsolutePath();
248     }
249 
250     public static String getTestPath( String basedir,
251                                       String path )
252     {
253         return getTestFile( basedir, path ).getAbsolutePath();
254     }
255 
256     public static String getBasedir()
257     {
258         if ( basedir != null )
259         {
260             return basedir;
261         }
262 
263         basedir = System.getProperty( "basedir" );
264 
265         if ( basedir == null )
266         {
267             basedir = new File( "" ).getAbsolutePath();
268         }
269 
270         return basedir;
271     }
272 
273     public String getTestConfiguration()
274     {
275         return getTestConfiguration( getClass() );
276     }
277 
278     public static String getTestConfiguration( Class<?> clazz )
279     {
280         String s = clazz.getName().replace( '.', '/' );
281 
282         return s.substring( 0, s.indexOf( "$" ) ) + ".xml";
283     }
284 }