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