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