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