1 package org.jitr;
2
3 import java.lang.annotation.Annotation;
4
5 import javax.servlet.ServletContext;
6
7 import junit.framework.Assert;
8
9 import org.jitr.annotation.JitrConfiguration;
10 import org.jitr.container.JettyContainer;
11 import org.jitr.core.ConfigurationModel;
12 import org.jitr.core.Container;
13 import org.jitr.core.ContainerOperationException;
14 import org.jitr.core.Defaults;
15 import org.jitr.core.JitrException;
16 import org.jitr.core.OperationalMode;
17 import org.junit.After;
18 import org.junit.Ignore;
19 import org.junit.Test;
20
21 public class JitrConfigurationFactoryTest {
22
23 @Ignore
24 @JitrConfiguration(containerClass = TestContainer1.class, containerWorkPath = TEST_STRING, contextPath = TEST_STRING, mode = OperationalMode.EXTERNAL, port = 9000, warPath = JitrConfigurationFactoryTest.TEST_STRING)
25 private static final class AnnotatedClass {
26 }
27
28 @Ignore
29 private static final class TestContainer1<T> implements Container<T> {
30
31 public String getName() {
32 return null;
33 }
34
35 public T getWrappedContainer() {
36 return null;
37 }
38
39 public ServletContext initialize(final ConfigurationModel configuration,
40 final Annotation[] testClassAnnotations) {
41 return null;
42 }
43
44 public void start() throws ContainerOperationException {
45 }
46
47 public void stop() throws ContainerOperationException {
48 }
49 }
50
51 @Ignore
52 private static final class TestContainer2<T> implements Container<T> {
53
54 public String getName() {
55 return null;
56 }
57
58 public T getWrappedContainer() {
59 return null;
60 }
61
62 public ServletContext initialize(final ConfigurationModel configuration,
63 final Annotation[] testClassAnnotations) {
64 return null;
65 }
66
67 public void start() throws ContainerOperationException {
68 }
69
70 public void stop() throws ContainerOperationException {
71 }
72 }
73
74 private static final int TEST_PORT = 0;
75
76 private static final String TEST_STRING = "TEST_STRING";
77
78 @After
79 public void after() {
80
81 System.clearProperty(JitrConfigurationFactory.BASE_URI_SP_KEY);
82 System.clearProperty(JitrConfigurationFactory.CONTAINER_CLASS_SP_KEY);
83 System.clearProperty(JitrConfigurationFactory.MODE_SP_KEY);
84 System.clearProperty(JitrConfigurationFactory.PORT_SP_KEY);
85 }
86
87 @Test
88 public void testCreateConfigurationFromAnnotation() {
89 testCreateConfigurationFromAnnotation(true);
90 }
91
92 @Test
93 public void testCreateConfigurationFromAnnotation_Defaults() {
94 testCreateConfigurationFromAnnotation(false);
95 }
96
97 @Test
98 public void testDetermineBaseUri_Other() {
99 testDetermineBaseUri("http://localhost:" + TEST_PORT + TEST_STRING);
100 }
101
102 @Test
103 public void testDetermineBaseUri_SystemProperty() {
104
105 System.setProperty(JitrConfigurationFactory.BASE_URI_SP_KEY, TEST_STRING);
106 testDetermineBaseUri(TEST_STRING);
107 }
108
109 @Test
110 public void testDetermineContainerClass_Annotation() {
111 testDetermineContainerClass(TestContainer1.class, true);
112 }
113
114 @Test
115 public void testDetermineContainerClass_Default() {
116 testDetermineContainerClass(JettyContainer.class, false);
117 }
118
119 @Test
120 public void testDetermineContainerClass_SystemProperty() {
121
122 System.setProperty(JitrConfigurationFactory.CONTAINER_CLASS_SP_KEY,
123 TestContainer2.class.getName());
124 testDetermineContainerClass(TestContainer2.class, true);
125 }
126
127 @Test(expected = JitrException.class)
128 public void testDetermineContainerClass_SystemProperty_NotFound() {
129
130 System.setProperty(JitrConfigurationFactory.CONTAINER_CLASS_SP_KEY,
131 TestContainer2.class.getSimpleName());
132 testDetermineContainerClass(null, true);
133 }
134
135 @Test(expected = JitrException.class)
136 public void testDetermineContainerClass_SystemProperty_WrongType() {
137
138 System.setProperty(JitrConfigurationFactory.CONTAINER_CLASS_SP_KEY, Object.class.getName());
139 testDetermineContainerClass(null, true);
140 }
141
142 @Test
143 public void testDetermineContainerWorkPath_Annotation() {
144 testDetermineContainerWorkPath(TEST_STRING, true);
145 }
146
147 @Test
148 public void testDetermineContainerWorkPath_Default() {
149 testDetermineContainerWorkPath(Defaults.DEFAULT_CONTAINER_WORK_PATH, false);
150 }
151
152 @Test
153 public void testDetermineContextPath_Annotation() {
154 testDetermineContextPath(TEST_STRING, true);
155 }
156
157 @Test
158 public void testDetermineContextPath_Default() {
159 testDetermineContextPath(Defaults.DEFAULT_CONTEXT_PATH, false);
160 }
161
162 @Test
163 public void testDetermineOperationalMode_Annotation() {
164 testDetermineOperationalMode(OperationalMode.EXTERNAL, true);
165 }
166
167 @Test
168 public void testDetermineOperationalMode_Default() {
169 testDetermineOperationalMode(Defaults.DEFAULT_MODE, false);
170 }
171
172 @Test
173 public void testDetermineOperationalMode_SystemProperty() {
174
175 System.setProperty(JitrConfigurationFactory.MODE_SP_KEY, OperationalMode.MIXED.toString());
176 testDetermineOperationalMode(OperationalMode.MIXED, true);
177 }
178
179 @Test
180 public void testDeterminePort_Annotation() {
181 testDeterminePort(9000, true);
182 }
183
184 @Test
185 public void testDeterminePort_Default() {
186
187 JitrConfiguration annot = getAnnotation(false);
188 int port = JitrConfigurationFactory.determinePort(annot);
189 Assert.assertNotSame(Defaults.DEFAULT_PORT, port);
190 }
191
192 @Test
193 public void testDeterminePort_SystemProperty() {
194
195 System.setProperty(JitrConfigurationFactory.PORT_SP_KEY, "9001");
196 testDeterminePort(9001, true);
197 }
198
199 @Test(expected = NumberFormatException.class)
200 public void testDeterminePort_SystemProperty_CantParse() {
201
202 System.setProperty(JitrConfigurationFactory.PORT_SP_KEY, "foo");
203 testDeterminePort(0, true);
204 }
205
206 @Test
207 public void testDetermineWarPath_Annotation() {
208 testDetermineWarPath(TEST_STRING, true);
209 }
210
211 @Test
212 public void testDetermineWarPath_Default() {
213 testDetermineWarPath(Defaults.DEFAULT_WAR_PATH, false);
214 }
215
216 private void testCreateConfigurationFromAnnotation(final boolean useAnnotation) {
217
218 ConfigurationModel config =
219 JitrConfigurationFactory.createConfigurationFromAnnotation(getAnnotation(useAnnotation));
220 Assert.assertNotNull(config);
221 }
222
223 private void testDetermineBaseUri(final String expectedBaseUri) {
224
225 String baseUri = JitrConfigurationFactory.determineBaseUri(TEST_PORT, TEST_STRING);
226 Assert.assertNotNull(baseUri);
227 Assert.assertEquals(expectedBaseUri, baseUri);
228 }
229
230 private void testDetermineContainerClass(final Class<?> expectedContainerClass,
231 final boolean useAnnotation) {
232
233 JitrConfiguration annot = getAnnotation(useAnnotation);
234 Class<?> containerClass = JitrConfigurationFactory.determineContainerClass(annot);
235 Assert.assertNotNull(containerClass);
236 Assert.assertEquals(expectedContainerClass, containerClass);
237 }
238
239 private void testDetermineContainerWorkPath(final String expectedContainerWorkPath,
240 final boolean useAnnotation) {
241
242 JitrConfiguration annot = getAnnotation(useAnnotation);
243 String containerWorkPath = JitrConfigurationFactory.determineContainerWorkPath(annot);
244 Assert.assertEquals(expectedContainerWorkPath, containerWorkPath);
245 }
246
247 private void testDetermineContextPath(final String expectedContextPath,
248 final boolean useAnnotation) {
249
250 JitrConfiguration annot = getAnnotation(useAnnotation);
251 String contextPath = JitrConfigurationFactory.determineContextPath(annot);
252 Assert.assertEquals(expectedContextPath, contextPath);
253 }
254
255 private void testDetermineOperationalMode(final OperationalMode expectedMode,
256 final boolean useAnnotation) {
257
258 JitrConfiguration annot = getAnnotation(useAnnotation);
259 OperationalMode mode = JitrConfigurationFactory.determineOperationalMode(annot);
260 Assert.assertNotNull(mode);
261 Assert.assertEquals(expectedMode, mode);
262 }
263
264 private void testDeterminePort(final int expectedPort, final boolean useAnnotation) {
265
266 JitrConfiguration annot = getAnnotation(useAnnotation);
267 int port = JitrConfigurationFactory.determinePort(annot);
268 Assert.assertEquals(expectedPort, port);
269 }
270
271 private void testDetermineWarPath(final String expectedWarPath, final boolean useAnnotation) {
272
273 JitrConfiguration annot = getAnnotation(useAnnotation);
274 String warPath = JitrConfigurationFactory.determineWarPath(annot);
275 Assert.assertEquals(expectedWarPath, warPath);
276 }
277
278 private static JitrConfiguration getAnnotation(final boolean useAnnotation) {
279
280 if (!useAnnotation) {
281 return null;
282 }
283
284 JitrConfiguration annot = AnnotatedClass.class.getAnnotation(JitrConfiguration.class);
285 Assert.assertNotNull(annot);
286
287 return annot;
288 }
289 }