1 package org.codehaus.plexus;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
34
35
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
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
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
99
100
101
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
142
143
144
145
146
147
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
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
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 }