View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.util.Map;
24  
25  import com.google.inject.Module;
26  import org.codehaus.plexus.ContainerConfiguration;
27  import org.codehaus.plexus.DefaultContainerConfiguration;
28  import org.codehaus.plexus.DefaultPlexusContainer;
29  import org.codehaus.plexus.PlexusConstants;
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  import org.junit.Rule;
38  import org.junit.rules.TestName;
39  
40  import static org.junit.Assert.fail;
41  
42  /**
43   * Based on PlexusTestCase from org.sonatype.sisu:sisu-inject-plexus.
44   * Note: this class is copied from maven-release.
45   *
46   * @author Robert Scholte
47   */
48  public class PlexusJUnit4TestCase {
49      private PlexusContainer container;
50  
51      private static String basedir;
52  
53      @Rule
54      public TestName testName = new TestName();
55  
56      @Before
57      public void setUp() throws Exception {
58          basedir = getBasedir();
59      }
60  
61      public String getName() {
62          return testName.getMethodName();
63      }
64  
65      protected void setupContainer() {
66          // ----------------------------------------------------------------------------
67          // Context Setup
68          // ----------------------------------------------------------------------------
69  
70          final DefaultContext context = new DefaultContext();
71  
72          context.put("basedir", getBasedir());
73  
74          customizeContext(context);
75  
76          final boolean hasPlexusHome = context.contains("plexus.home");
77  
78          if (!hasPlexusHome) {
79              final File f = getTestFile("target/plexus-home");
80  
81              if (!f.isDirectory()) {
82                  f.mkdir();
83              }
84  
85              context.put("plexus.home", f.getAbsolutePath());
86          }
87  
88          // ----------------------------------------------------------------------------
89          // Configuration
90          // ----------------------------------------------------------------------------
91  
92          final String config = getCustomConfigurationName();
93  
94          final ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration()
95                  .setName("test")
96                  .setContext(context.getContextData())
97                  .setAutoWiring(true)
98                  .setClassPathScanning(PlexusConstants.SCANNING_CACHE);
99  
100         if (config != null) {
101             containerConfiguration.setContainerConfiguration(config);
102         } else {
103             final String resource = getConfigurationName(null);
104 
105             containerConfiguration.setContainerConfiguration(resource);
106         }
107 
108         customizeContainerConfiguration(containerConfiguration);
109 
110         try {
111             container = new DefaultPlexusContainer(containerConfiguration, getCustomModules());
112         } catch (final PlexusContainerException e) {
113             e.printStackTrace();
114             fail("Failed to create plexus container.");
115         }
116     }
117 
118     /**
119      * Allows test to define custom modules.
120      */
121     public Module[] getCustomModules() {
122         return new Module[0];
123     }
124 
125     /**
126      * Allow custom test case implementations do augment the default container configuration before executing tests.
127      *
128      * @param containerConfiguration
129      */
130     protected void customizeContainerConfiguration(final ContainerConfiguration containerConfiguration) {}
131 
132     protected void customizeContext(final Context context) {}
133 
134     protected PlexusConfiguration customizeComponentConfiguration() {
135         return null;
136     }
137 
138     @After
139     public void tearDown() throws Exception {
140         if (container != null) {
141             container.dispose();
142 
143             container = null;
144         }
145     }
146 
147     protected PlexusContainer getContainer() {
148         if (container == null) {
149             setupContainer();
150         }
151 
152         return container;
153     }
154 
155     protected InputStream getConfiguration() throws Exception {
156         return getConfiguration(null);
157     }
158 
159     @SuppressWarnings("unused")
160     protected InputStream getConfiguration(final String subname) throws Exception {
161         return getResourceAsStream(getConfigurationName(subname));
162     }
163 
164     protected String getCustomConfigurationName() {
165         return null;
166     }
167 
168     /**
169      * Allow the retrieval of a container configuration that is based on the name of the test class being run. So if you
170      * have a test class called org.foo.FunTest, then this will produce a resource name of org/foo/FunTest.xml which
171      * would be used to configure the Plexus container before running your test.
172      *
173      * @param subname
174      * @return
175      */
176     protected String getConfigurationName(final String subname) {
177         return getClass().getName().replace('.', '/') + ".xml";
178     }
179 
180     protected InputStream getResourceAsStream(final String resource) {
181         return getClass().getResourceAsStream(resource);
182     }
183 
184     protected ClassLoader getClassLoader() {
185         return getClass().getClassLoader();
186     }
187 
188     // ----------------------------------------------------------------------
189     // Container access
190     // ----------------------------------------------------------------------
191 
192     protected Object lookup(final String componentKey) throws Exception {
193         return getContainer().lookup(componentKey);
194     }
195 
196     protected Object lookup(final String role, final String roleHint) throws Exception {
197         return getContainer().lookup(role, roleHint);
198     }
199 
200     protected <T> T lookup(final Class<T> componentClass) throws Exception {
201         return getContainer().lookup(componentClass);
202     }
203 
204     protected <T> T lookup(final Class<T> componentClass, final String roleHint) throws Exception {
205         return getContainer().lookup(componentClass, roleHint);
206     }
207 
208     protected <T> Map<String, T> lookupMap(final Class<T> componentClass) throws Exception {
209         return getContainer().lookupMap(componentClass);
210     }
211 
212     protected void release(final Object component) throws Exception {
213         getContainer().release(component);
214     }
215 
216     // ----------------------------------------------------------------------
217     // Helper methods for sub classes
218     // ----------------------------------------------------------------------
219 
220     public static File getTestFile(final String path) {
221         return new File(getBasedir(), path);
222     }
223 
224     @SuppressWarnings("hiding")
225     public static File getTestFile(final String basedir, final String path) {
226         File basedirFile = new File(basedir);
227 
228         if (!basedirFile.isAbsolute()) {
229             basedirFile = getTestFile(basedir);
230         }
231 
232         return new File(basedirFile, path);
233     }
234 
235     public static String getTestPath(final String path) {
236         return getTestFile(path).getAbsolutePath();
237     }
238 
239     @SuppressWarnings("hiding")
240     public static String getTestPath(final String basedir, final String path) {
241         return getTestFile(basedir, path).getAbsolutePath();
242     }
243 
244     public static String getBasedir() {
245         if (basedir != null) {
246             return basedir;
247         }
248 
249         basedir = System.getProperty("basedir");
250 
251         if (basedir == null) {
252             basedir = new File("").getAbsolutePath();
253         }
254 
255         return basedir;
256     }
257 
258     public String getTestConfiguration() {
259         return getTestConfiguration(getClass());
260     }
261 
262     public static String getTestConfiguration(final Class<?> clazz) {
263         final String s = clazz.getName().replace('.', '/');
264 
265         return s.substring(0, s.indexOf("$")) + ".xml";
266     }
267 }