001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.core;
017:
018: import static org.junit.Assert.assertEquals;
019: import static org.junit.Assert.assertNotNull;
020: import static org.junit.Assert.assertTrue;
021: import static org.junit.Assert.fail;
022: import static org.unitils.core.ModulesLoader.PROPKEY_MODULES;
023: import static org.unitils.core.ModulesLoader.PROPKEY_MODULE_PREFIX;
024: import static org.unitils.core.ModulesLoader.PROPKEY_MODULE_SUFFIX_CLASS_NAME;
025: import static org.unitils.core.ModulesLoader.PROPKEY_MODULE_SUFFIX_ENABLED;
026: import static org.unitils.core.ModulesLoader.PROPKEY_MODULE_SUFFIX_RUN_AFTER;
027:
028: import java.util.List;
029: import java.util.Properties;
030:
031: import org.junit.Before;
032: import org.junit.Test;
033: import org.unitils.UnitilsJUnit4;
034:
035: /**
036: * Test for {@link ModulesLoader}.
037: */
038: public class ModulesLoaderTest extends UnitilsJUnit4 {
039:
040: /* Class under test */
041: private ModulesLoader modulesLoader;
042:
043: /* The unitils configuration settings that control the core loading */
044: private Properties configuration;
045:
046: /**
047: * Creates the test instance and initializes the fixture.
048: */
049: @Before
050: public void setUp() throws Exception {
051: modulesLoader = new ModulesLoader();
052:
053: configuration = new Properties();
054: configuration.setProperty(PROPKEY_MODULES, "a, b, c, d");
055: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
056: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, TestModuleA.class
057: .getName());
058: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
059: + PROPKEY_MODULE_SUFFIX_RUN_AFTER, "b, d");
060: configuration.setProperty(PROPKEY_MODULE_PREFIX + "b"
061: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, TestModuleB.class
062: .getName());
063: configuration.setProperty(PROPKEY_MODULE_PREFIX + "b"
064: + PROPKEY_MODULE_SUFFIX_RUN_AFTER, "d");
065: configuration.setProperty(PROPKEY_MODULE_PREFIX + "c"
066: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, TestModuleC.class
067: .getName());
068: configuration.setProperty(PROPKEY_MODULE_PREFIX + "c"
069: + PROPKEY_MODULE_SUFFIX_RUN_AFTER, "a");
070: configuration.setProperty(PROPKEY_MODULE_PREFIX + "d"
071: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, TestModuleD.class
072: .getName());
073: }
074:
075: /**
076: * Test the loading of a normal configuration.
077: */
078: @Test
079: public void testLoadModules() {
080: List<Module> result = modulesLoader.loadModules(configuration);
081:
082: assertNotNull(result);
083: assertEquals(4, result.size());
084: assertTrue(result.get(0) instanceof TestModuleD);
085: assertTrue(result.get(1) instanceof TestModuleB);
086: assertTrue(result.get(2) instanceof TestModuleA);
087: assertTrue(result.get(3) instanceof TestModuleC);
088: }
089:
090: /**
091: * Tests the loading with 1 core name left out: c. The c core should not have been loaded.
092: */
093: @Test
094: public void testLoadModules_notActive() {
095: configuration.setProperty(PROPKEY_MODULES, "a, b, d");
096:
097: List<Module> result = modulesLoader.loadModules(configuration);
098:
099: assertNotNull(result);
100: assertEquals(3, result.size());
101: assertTrue(result.get(0) instanceof TestModuleD);
102: assertTrue(result.get(1) instanceof TestModuleB);
103: assertTrue(result.get(2) instanceof TestModuleA);
104: }
105:
106: /**
107: * Tests the loading with core d disabled. The core should have been ignored
108: */
109: @Test
110: public void testLoadModules_notEnabled() {
111: configuration.setProperty(PROPKEY_MODULE_PREFIX + "d"
112: + PROPKEY_MODULE_SUFFIX_ENABLED, "false");
113:
114: List<Module> result = modulesLoader.loadModules(configuration);
115:
116: assertNotNull(result);
117: assertEquals(3, result.size());
118: assertTrue(result.get(0) instanceof TestModuleB);
119: assertTrue(result.get(1) instanceof TestModuleA);
120: assertTrue(result.get(2) instanceof TestModuleC);
121: }
122:
123: /**
124: * Tests the loading with modules (a, b) and dependencies (a -> b, d) that are declared twice. The doubles should
125: * have been ignored
126: */
127: @Test
128: public void testLoadModules_notDoubles() {
129: configuration.setProperty(PROPKEY_MODULES, "a, b, c, d, a, b");
130: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
131: + PROPKEY_MODULE_SUFFIX_RUN_AFTER, "b, b, d, b, d");
132:
133: List<Module> result = modulesLoader.loadModules(configuration);
134:
135: assertNotNull(result);
136: assertEquals(4, result.size());
137: assertTrue(result.get(0) instanceof TestModuleD);
138: assertTrue(result.get(1) instanceof TestModuleB);
139: assertTrue(result.get(2) instanceof TestModuleA);
140: assertTrue(result.get(3) instanceof TestModuleC);
141: }
142:
143: /**
144: * Tests the loading with a totally empty configuration.
145: */
146: @Test
147: public void testLoadModules_emptyConfiguration() {
148: configuration.clear();
149:
150: List<Module> result = modulesLoader.loadModules(configuration);
151:
152: assertNotNull(result);
153: assertTrue(result.isEmpty());
154: }
155:
156: /**
157: * Tests the loading of a core that is configured with a class name for a class that is not a UnitilsModule. A
158: * warning should have been logged and the other modules should have been loaded.
159: */
160: @Test
161: public void testLoadModules_wrongClassName() {
162: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
163: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, "java.lang.String");
164: try {
165: modulesLoader.loadModules(configuration);
166: fail();
167:
168: } catch (UnitilsException e) {
169: // expected
170: }
171: }
172:
173: /**
174: * Tests the loading of a core that is configured with a class name for a class that does not exist. A warning
175: * should have been logged and the other modules should have been loaded.
176: */
177: @Test
178: public void testLoadModules_classNotFound() {
179: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
180: + PROPKEY_MODULE_SUFFIX_CLASS_NAME, "xxxxx");
181:
182: List<Module> result = modulesLoader.loadModules(configuration);
183:
184: assertNotNull(result);
185: assertEquals(3, result.size());
186: assertTrue(result.get(0) instanceof TestModuleD);
187: assertTrue(result.get(1) instanceof TestModuleB);
188: assertTrue(result.get(2) instanceof TestModuleC);
189: }
190:
191: /**
192: * Tests the loading of modules that contain a circular dependency. a must run after b must run after d must run
193: * after a A runtime exception should have been thrown.
194: */
195: @Test
196: public void testLoadModules_circular() {
197: configuration.setProperty(PROPKEY_MODULE_PREFIX + "b"
198: + PROPKEY_MODULE_SUFFIX_RUN_AFTER, "c");
199: try {
200: modulesLoader.loadModules(configuration);
201: fail();
202:
203: } catch (UnitilsException e) {
204: // expected
205: }
206: }
207:
208: /**
209: * Tests the loading of a core that is configured with a class name for a class that has a private constructor.
210: */
211: @Test
212: public void testLoadModules_privateConstructor() {
213: configuration.setProperty(PROPKEY_MODULE_PREFIX + "a"
214: + PROPKEY_MODULE_SUFFIX_CLASS_NAME,
215: TestModulePrivate.class.getName());
216:
217: List<Module> result = modulesLoader.loadModules(configuration);
218:
219: assertNotNull(result);
220: assertEquals(4, result.size());
221: assertTrue(result.get(0) instanceof TestModuleD);
222: assertTrue(result.get(1) instanceof TestModuleB);
223: assertTrue(result.get(2) instanceof TestModulePrivate);
224: assertTrue(result.get(3) instanceof TestModuleC);
225: }
226:
227: /**
228: * A test unitils core type
229: */
230: public static class TestModuleA implements Module {
231:
232: public void init(Properties configuration) {
233: // do nothing
234: }
235:
236: public TestListener createTestListener() {
237: return null;
238: }
239: }
240:
241: /**
242: * A test unitils core type
243: */
244: public static class TestModuleB implements Module {
245:
246: public void init(Properties configuration) {
247: // do nothing
248: }
249:
250: public TestListener createTestListener() {
251: return null;
252: }
253: }
254:
255: /**
256: * A test unitils core type
257: */
258: public static class TestModuleC implements Module {
259:
260: public void init(Properties configuration) {
261: // do nothing
262: }
263:
264: public TestListener createTestListener() {
265: return null;
266: }
267: }
268:
269: /**
270: * A test unitils core type
271: */
272: public static class TestModuleD implements Module {
273:
274: public void init(Properties configuration) {
275: // do nothing
276: }
277:
278: public TestListener createTestListener() {
279: return null;
280: }
281: }
282:
283: /**
284: * A test unitils core type having a private constructor
285: */
286: public static class TestModulePrivate implements Module {
287:
288: public void init(Properties configuration) {
289: // do nothing
290: }
291:
292: private TestModulePrivate() {
293: }
294:
295: public TestListener createTestListener() {
296: return null;
297: }
298: }
299: }
|