-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlexusExtension.java
More file actions
307 lines (255 loc) · 11.4 KB
/
PlexusExtension.java
File metadata and controls
307 lines (255 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package org.codehaus.plexus.testing;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.util.Collections;
import java.util.Optional;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.DefaultContext;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* This is a slightly modified version of the original plexus class
* available at https://raw.githubusercontent.com/codehaus-plexus/plexus-containers/master/plexus-container-default/
* src/main/java/org/codehaus/plexus/PlexusTestCase.java
* in order to migrate the tests to JUnit 5.
*
* @author Jason van Zyl
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
* @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
* @author Guillaume Nodet
*/
public class PlexusExtension implements BeforeEachCallback, AfterEachCallback {
private static class PlexusClosableWrapper implements AutoCloseable {
private final PlexusContainer container;
PlexusClosableWrapper(PlexusContainer container) {
this.container = container;
}
@Override
public void close() {
container.dispose();
}
public PlexusContainer get() {
return container;
}
}
private static final ExtensionContext.Namespace PLEXUS_EXTENSION =
ExtensionContext.Namespace.create("PlexusExtension");
public static final String BASEDIR_KEY = "basedir";
private static final ThreadLocal<ExtensionContext> extensionContextThreadLocal = new ThreadLocal<>();
static {
if (System.getProperty("guice_custom_class_loading", "").trim().isEmpty()) {
System.setProperty("guice_custom_class_loading", "CHILD");
}
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
setTestBasedir(getDefaultBasedir(), context);
context.getRequiredTestInstances().getAllInstances().forEach(testInstance -> ((DefaultPlexusContainer)
getContainer(context))
.addPlexusInjector(Collections.emptyList(), binder -> binder.requestInjection(testInstance)));
}
private PlexusContainer setupContainer(ExtensionContext context) {
// Store the context in a thread local for static access
// must be done hear as this method is always executed
extensionContextThreadLocal.set(context);
context.getStore(PLEXUS_EXTENSION)
.put("threadLocalCloseable", (AutoCloseable) extensionContextThreadLocal::remove);
// ----------------------------------------------------------------------------
// Context Setup
// ----------------------------------------------------------------------------
DefaultContext plexusContext = new DefaultContext();
plexusContext.put("basedir", getTestBasedir(context));
customizeContext(plexusContext);
boolean hasPlexusHome = plexusContext.contains("plexus.home");
if (!hasPlexusHome) {
File f = new File(getTestBasedir(context), "target/plexus-home");
if (!f.isDirectory()) {
f.mkdir();
}
plexusContext.put("plexus.home", f.getAbsolutePath());
}
// ----------------------------------------------------------------------------
// Configuration
// ----------------------------------------------------------------------------
String config = getCustomConfigurationName();
ContainerConfiguration containerConfiguration =
new DefaultContainerConfiguration().setName("test").setContext(plexusContext.getContextData());
if (config != null) {
containerConfiguration.setContainerConfiguration(config);
} else {
String resource = getConfigurationName(context);
containerConfiguration.setContainerConfiguration(resource);
}
customizeContainerConfiguration(containerConfiguration);
testInstanceCustomizeContainerConfiguration(containerConfiguration, context);
PlexusContainer container;
try {
container = new DefaultPlexusContainer(containerConfiguration);
container.addComponent(container, PlexusContainer.class.getName());
} catch (PlexusContainerException e) {
throw new IllegalArgumentException("Failed to create plexus container.", e);
}
testInstanceCustomizeContainer(container, context);
context.getStore(PLEXUS_EXTENSION).put(PlexusClosableWrapper.class, new PlexusClosableWrapper(container));
return container;
}
/**
* Allow custom test case implementations do augment the default container configuration before
* executing tests.
*
* @param containerConfiguration {@link ContainerConfiguration}.
*/
protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
containerConfiguration.setAutoWiring(true);
containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
}
private void testInstanceCustomizeContainerConfiguration(
ContainerConfiguration containerConfiguration, ExtensionContext context) {
context.getRequiredTestInstances().getAllInstances().forEach(testInstance -> {
if (testInstance instanceof PlexusTestConfiguration) {
((PlexusTestConfiguration) testInstance).customizeConfiguration(containerConfiguration);
}
});
}
private void testInstanceCustomizeContainer(PlexusContainer container, ExtensionContext context) {
context.getRequiredTestInstances().getAllInstances().forEach(testInstance -> {
if (testInstance instanceof PlexusTestConfiguration) {
((PlexusTestConfiguration) testInstance).customizeContainer(container);
}
});
}
protected void customizeContext(Context context) {}
@Override
public void afterEach(ExtensionContext context) throws Exception {
// empty method, not used
}
/**
* The base directory for the test instance. By default, this is the same as the basedir.
*
* @param context the test execution context
*
* @return the testBasedir
* @since 1.7.0
*/
protected String getTestBasedir(ExtensionContext context) {
String testBasedir = context.getStore(PLEXUS_EXTENSION).get(BASEDIR_KEY, String.class);
if (testBasedir == null) {
testBasedir = getDefaultBasedir();
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
}
return testBasedir;
}
/**
* Set the base directory for the test instance. By default, this is the same as the basedir.
*
* @param testBasedir the testBasedir for the test instance
* @param context the test execution context
* @since 1.7.0
*/
protected void setTestBasedir(String testBasedir, ExtensionContext context) {
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
}
public PlexusContainer getContainer(ExtensionContext context) {
PlexusClosableWrapper container =
context.getStore(PLEXUS_EXTENSION).get(PlexusClosableWrapper.class, PlexusClosableWrapper.class);
if (container == null) {
return setupContainer(context);
}
return container.get();
}
protected String getCustomConfigurationName() {
return null;
}
/**
* Allow the retrieval of a container configuration that is based on the name
* of the test class being run. So if you have a test class called org.foo.FunTest, then
* this will produce a resource name of org/foo/FunTest.xml which would be used to
* configure the Plexus container before running your test.
*
* @return A configruation name
*/
protected String getConfigurationName(ExtensionContext context) {
Class<?> testClass = context.getRequiredTestClass();
for (Class<?> clazz = testClass; clazz != null; clazz = clazz.getSuperclass()) {
String name = clazz.getName().replace('.', '/') + ".xml";
if (testClass.getClassLoader().getResource(name) != null) {
return name;
}
}
return null;
}
// ----------------------------------------------------------------------
// Helper methods for sub classes
// ----------------------------------------------------------------------
public static File getTestFile(String path) {
return new File(getBasedir(), path);
}
public static File getTestFile(String basedir, String path) {
File basedirFile = new File(basedir);
if (!basedirFile.isAbsolute()) {
basedirFile = getTestFile(basedir);
}
return new File(basedirFile, path);
}
public static String getTestPath(String path) {
return getTestFile(path).getAbsolutePath();
}
public static String getTestPath(String basedir, String path) {
return getTestFile(basedir, path).getAbsolutePath();
}
private static String getDefaultBasedir() {
String basedir = System.getProperty("basedir");
if (basedir == null) {
basedir = new File("").getAbsolutePath();
}
return basedir;
}
public static String getBasedir() {
return Optional.ofNullable(extensionContextThreadLocal.get())
.map(ec -> ec.getStore(PLEXUS_EXTENSION).get(BASEDIR_KEY, String.class))
.orElseGet(PlexusExtension::getDefaultBasedir);
}
public static String getTestConfiguration(Class<?> clazz) {
String s = clazz.getName().replace('.', '/');
return s.substring(0, s.indexOf("$")) + ".xml";
}
}