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.assertTrue;
020: import static org.junit.Assert.fail;
021: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
022:
023: import java.util.Arrays;
024: import java.util.List;
025: import java.util.Properties;
026:
027: import org.junit.Before;
028: import org.junit.Test;
029: import org.unitils.UnitilsJUnit4;
030: import org.unitils.database.DatabaseModule;
031:
032: /**
033: * Test for {@link ModulesRepositoryTest}.
034: *
035: * @author Tim Ducheyne
036: * @author Filip Neven
037: */
038: public class ModulesRepositoryTest extends UnitilsJUnit4 {
039:
040: /* A test module */
041: private Module testModule1a = new TestModule1();
042:
043: /* Another test module */
044: private Module testModule1b = new TestModule1();
045:
046: /* A test module with same type as testModule1b */
047: private Module testModule2 = new TestModule2();
048:
049: /* Class under test */
050: private ModulesRepository modulesRepository;
051:
052: /**
053: * Sets up the test fixture.
054: */
055: @Before
056: public void setUp() throws Exception {
057: List<Module> modules = Arrays.asList(testModule1b,
058: testModule1a, testModule2);
059: modulesRepository = new ModulesRepository(modules);
060: }
061:
062: /**
063: * Test initialisation of repository and creation of all test listeners for the modules.
064: */
065: @Test
066: public void testCreateListeners() {
067: assertEquals(3, modulesRepository.getTestListeners().size());
068: assertTrue(modulesRepository.getTestListener(testModule1a) instanceof TestModule1.TestListener1);
069: assertTrue(modulesRepository.getTestListener(testModule1b) instanceof TestModule1.TestListener1);
070: assertTrue(modulesRepository.getTestListener(testModule2) instanceof TestModule2.TestListener2);
071: }
072:
073: /**
074: * Tests getting the first module of type TestModule1. Note: TestModule2 is a sub-type of TestModule1 and should be
075: * found first.
076: */
077: @Test
078: public void testGetModuleOfType_subType() {
079: TestModule1 result = modulesRepository
080: .getModuleOfType(TestModule2.class);
081: assertLenEquals(testModule2, result);
082: }
083:
084: /**
085: * Tests getting the first module of type DatabaseModule, but none found.
086: */
087: @Test
088: public void testGetModuleOfType_noneFound() {
089: try {
090: modulesRepository.getModuleOfType(DatabaseModule.class);
091: fail("A UnitilsException should have been thrown");
092: } catch (UnitilsException e) {
093: // Expected
094: }
095: }
096:
097: /**
098: * Tests getting the first module of type TestModule2
099: */
100: @Test
101: public void testGetModuleOfType_moreThanOneFound() {
102: try {
103: modulesRepository.getModuleOfType(TestModule1.class);
104: fail("A UnitilsException should have been thrown");
105: } catch (UnitilsException e) {
106: // Expected
107: }
108: }
109:
110: /**
111: * Tests getting all modules of type TestModule2.
112: */
113: @Test
114: public void testGetModulesOfType() {
115: List<TestModule1> result = modulesRepository
116: .getModulesOfType(TestModule1.class);
117: assertLenEquals(Arrays.asList(testModule1a, testModule1b,
118: testModule2), result);
119: }
120:
121: /**
122: * Tests getting all modules of type TestModule1. Note: TestModule2 is a sub-type of TestModule1 and should also be
123: * found.
124: */
125: @Test
126: public void testGetModulesOfType_subType() {
127: List<TestModule2> result = modulesRepository
128: .getModulesOfType(TestModule2.class);
129: assertLenEquals(Arrays.asList(testModule2), result);
130: }
131:
132: /**
133: * Tests getting all module of type DatabaseModule, but none found. An empty list should be returned.
134: */
135: @Test
136: public void testGetModulesOfType_noneFound() {
137: List<DatabaseModule> result = modulesRepository
138: .getModulesOfType(DatabaseModule.class);
139: assertTrue(result.isEmpty());
140: }
141:
142: /**
143: * A test module, creating its own test listener.
144: */
145: private static class TestModule1 implements Module {
146:
147: public void init(Properties configuration) {
148: }
149:
150: public TestListener createTestListener() {
151: return new TestListener1();
152: }
153:
154: public static class TestListener1 extends TestListener {
155: }
156: }
157:
158: /**
159: * A test module that is a subtype of TestModule1 and also creates its own test listener.
160: */
161: private static class TestModule2 extends TestModule1 implements
162: Module {
163:
164: @Override
165: public TestListener createTestListener() {
166: return new TestListener2();
167: }
168:
169: public static class TestListener2 extends TestListener {
170: }
171: }
172:
173: }
|