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 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
29
30
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
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
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
94
95
96
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
136
137
138
139
140
141
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
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
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 }