Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
PlexusTestCase | 39 | 54 | 0% | 34 | 90 |
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.devacfr.testing.jupiter; | |
20 | ||
21 | import java.io.File; | |
22 | import java.io.InputStream; | |
23 | ||
24 | import org.codehaus.plexus.ContainerConfiguration; | |
25 | import org.codehaus.plexus.DefaultContainerConfiguration; | |
26 | import org.codehaus.plexus.DefaultPlexusContainer; | |
27 | import org.codehaus.plexus.PlexusContainer; | |
28 | import org.codehaus.plexus.PlexusContainerException; | |
29 | import org.codehaus.plexus.configuration.PlexusConfiguration; | |
30 | import org.codehaus.plexus.context.Context; | |
31 | import org.codehaus.plexus.context.DefaultContext; | |
32 | import org.junit.jupiter.api.AfterEach; | |
33 | import org.junit.jupiter.api.Assertions; | |
34 | ||
35 | /** | |
36 | * @author Christophe Friederich | |
37 | * @since 2.4 | |
38 | */ | |
39 | public abstract class PlexusTestCase extends TestCase { | |
40 | ||
41 | private PlexusContainer container; | |
42 | ||
43 | private static String basedirPath; | |
44 | ||
45 | 2 | public PlexusTestCase() { |
46 | } | |
47 | ||
48 | 0 | public void setupContainer() { |
49 | ||
50 | 0 | final DefaultContext context = new DefaultContext(); |
51 | ||
52 | 0 | context.put("basedir", getBasedir()); |
53 | ||
54 | 0 | customizeContext(context); |
55 | ||
56 | 0 | final boolean hasPlexusHome = context.contains("plexus.home"); |
57 | ||
58 | 0 | if (!hasPlexusHome) { |
59 | 0 | final File f = getTestFile("target/plexus-home"); |
60 | ||
61 | 0 | if (!f.isDirectory()) { |
62 | 0 | f.mkdir(); |
63 | } | |
64 | ||
65 | 0 | context.put("plexus.home", f.getAbsolutePath()); |
66 | } | |
67 | ||
68 | 0 | final String config = getCustomConfigurationName(); |
69 | ||
70 | 0 | final ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setName("test") |
71 | .setContext(context.getContextData()); | |
72 | ||
73 | 0 | if (config != null) { |
74 | 0 | containerConfiguration.setContainerConfiguration(config); |
75 | } else { | |
76 | 0 | final String resource = getConfigurationName(null); |
77 | ||
78 | 0 | containerConfiguration.setContainerConfiguration(resource); |
79 | } | |
80 | ||
81 | 0 | customizeContainerConfiguration(containerConfiguration); |
82 | ||
83 | 0 | try { |
84 | 0 | container = new DefaultPlexusContainer(containerConfiguration); |
85 | } catch (final PlexusContainerException e) { | |
86 | 0 | e.printStackTrace(); |
87 | 0 | Assertions.fail("Failed to create plexus container."); |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
92 | * Allow custom test case implementations do augment the default container configuration before executing tests. | |
93 | * | |
94 | * @param containerConfiguration | |
95 | */ | |
96 | 0 | protected void customizeContainerConfiguration(final ContainerConfiguration containerConfiguration) { |
97 | } | |
98 | ||
99 | 0 | protected void customizeContext(final Context context) { |
100 | } | |
101 | ||
102 | 0 | protected PlexusConfiguration customizeComponentConfiguration() { |
103 | 0 | return null; |
104 | } | |
105 | ||
106 | 2 | @AfterEach |
107 | public void tearDown() throws Exception { | |
108 | 2 | if (container != null) { |
109 | 0 | container.dispose(); |
110 | } | |
111 | 2 | container = null; |
112 | } | |
113 | ||
114 | 0 | protected PlexusContainer getContainer() { |
115 | 0 | if (container == null) { |
116 | 0 | setupContainer(); |
117 | } | |
118 | ||
119 | 0 | return container; |
120 | } | |
121 | ||
122 | 0 | protected InputStream getConfiguration() throws Exception { |
123 | 0 | return getConfiguration(null); |
124 | } | |
125 | ||
126 | 0 | protected InputStream getConfiguration(final String subname) throws Exception { |
127 | 0 | return getResourceAsStream(getConfigurationName(subname)); |
128 | } | |
129 | ||
130 | 0 | protected String getCustomConfigurationName() { |
131 | 0 | return null; |
132 | } | |
133 | ||
134 | /** | |
135 | * Allow the retrieval of a container configuration that is based on the name of the test class being run. So if you | |
136 | * have a test class called org.foo.FunTest, then this will produce a resource name of org/foo/FunTest.xml which | |
137 | * would be used to configure the Plexus container before running your test. | |
138 | * | |
139 | * @param subname | |
140 | * the subname | |
141 | * @return A configruation name | |
142 | */ | |
143 | 0 | protected String getConfigurationName(final String subname) { |
144 | 0 | return getClass().getName().replace('.', '/') + ".xml"; |
145 | } | |
146 | ||
147 | 0 | protected InputStream getResourceAsStream(final String resource) { |
148 | 0 | return getClass().getResourceAsStream(resource); |
149 | } | |
150 | ||
151 | 0 | protected ClassLoader getClassLoader() { |
152 | 0 | return getClass().getClassLoader(); |
153 | } | |
154 | ||
155 | // ---------------------------------------------------------------------- | |
156 | // Container access | |
157 | // ---------------------------------------------------------------------- | |
158 | ||
159 | 0 | @SuppressWarnings("unchecked") |
160 | protected <T> T lookup(final String componentKey) throws Exception { | |
161 | 0 | return (T) getContainer().lookup(componentKey); |
162 | } | |
163 | ||
164 | 0 | @SuppressWarnings("unchecked") |
165 | protected <T> T lookup(final String role, final String roleHint) throws Exception { | |
166 | 0 | return (T) getContainer().lookup(role, roleHint); |
167 | } | |
168 | ||
169 | 0 | protected <T> T lookup(final Class<T> componentClass) throws Exception { |
170 | 0 | return getContainer().lookup(componentClass); |
171 | } | |
172 | ||
173 | 0 | protected <T> T lookup(final Class<T> componentClass, final String roleHint) throws Exception { |
174 | 0 | return getContainer().lookup(componentClass, roleHint); |
175 | } | |
176 | ||
177 | 0 | protected void release(final Object component) throws Exception { |
178 | 0 | getContainer().release(component); |
179 | } | |
180 | ||
181 | // ---------------------------------------------------------------------- | |
182 | // Helper methods for sub classes | |
183 | // ---------------------------------------------------------------------- | |
184 | ||
185 | 0 | public static File getTestFile(final String path) { |
186 | 0 | return new File(getBasedir(), path); |
187 | } | |
188 | ||
189 | 0 | public static File getTestFile(final String basedir, final String path) { |
190 | 0 | File basedirFile = new File(basedir); |
191 | ||
192 | 0 | if (!basedirFile.isAbsolute()) { |
193 | 0 | basedirFile = getTestFile(basedir); |
194 | } | |
195 | ||
196 | 0 | return new File(basedirFile, path); |
197 | } | |
198 | ||
199 | 0 | public static String getTestPath(final String path) { |
200 | 0 | return getTestFile(path).getAbsolutePath(); |
201 | } | |
202 | ||
203 | 0 | public static String getTestPath(final String basedir, final String path) { |
204 | 0 | return getTestFile(basedir, path).getAbsolutePath(); |
205 | } | |
206 | ||
207 | 0 | public static String getBasedir() { |
208 | 0 | if (basedirPath != null) { |
209 | 0 | return basedirPath; |
210 | } | |
211 | ||
212 | 0 | basedirPath = System.getProperty("basedir"); |
213 | 0 | if (basedirPath == null) { |
214 | 0 | basedirPath = new File("").getAbsolutePath(); |
215 | } | |
216 | 0 | return basedirPath; |
217 | } | |
218 | ||
219 | 0 | public String getTestConfiguration() { |
220 | 0 | return getTestConfiguration(getClass()); |
221 | } | |
222 | ||
223 | 0 | public static String getTestConfiguration(final Class<?> clazz) { |
224 | 0 | final String s = clazz.getName().replace('.', '/'); |
225 | ||
226 | 0 | return s.substring(0, s.indexOf("$")) + ".xml"; |
227 | } | |
228 | } |