1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
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
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
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
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
129
130
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
172
173
174
175
176
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
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
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 }