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