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