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.unitils.util.ReflectionUtils.getClassWithName;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * A class for holding and retrieving modules.
027: *
028: * @author Tim Ducheyne
029: * @author Filip Neven
030: */
031: public class ModulesRepository {
032:
033: /* All modules */
034: private List<Module> modules;
035:
036: /* A map containing the test listeners of each module */
037: private Map<Module, TestListener> testListeners;
038:
039: /**
040: * Creates a repository containing the given modules.
041: *
042: * @param modules the modules, not null
043: */
044: public ModulesRepository(List<Module> modules) {
045: this .modules = modules;
046: this .testListeners = createTestListeners(modules);
047: }
048:
049: /**
050: * Gets all modules.
051: *
052: * @return the modules, not null
053: */
054: public List<Module> getModules() {
055: return modules;
056: }
057:
058: /**
059: * Gets all listeners.
060: *
061: * @return the listeners per module, not null
062: */
063: public Map<Module, TestListener> getTestListeners() {
064: return testListeners;
065: }
066:
067: /**
068: * Gets the modules that is of the given type or a sub-type.
069: * A UnitilsException is thrown when there is not exactly 1 possible match.
070: *
071: * @param <T> The module type
072: * @param type the module type, not null
073: * @return the module, not null
074: */
075: public <T extends Module> T getModuleOfType(Class<T> type) {
076: List<T> modulesOfType = getModulesOfType(type);
077: if (modulesOfType.size() > 1) {
078: throw new UnitilsException(
079: "More than one module found of type "
080: + type.getName());
081: }
082: if (modulesOfType.size() < 1) {
083: throw new UnitilsException("No module found of type "
084: + type.getName());
085: }
086: return modulesOfType.get(0);
087: }
088:
089: /**
090: * Gets all modules that are of the given type or a sub-type.
091: *
092: * @param <T> The module type
093: * @param type the type, not null
094: * @return the modules, an empty list if none found
095: */
096: @SuppressWarnings({"unchecked"})
097: public <T> List<T> getModulesOfType(Class<T> type) {
098: List<T> result = new ArrayList<T>();
099: for (Module module : modules) {
100: if (type.isAssignableFrom(module.getClass())) {
101: result.add((T) module);
102: }
103: }
104: return result;
105: }
106:
107: /**
108: * Checks whether a module of a type with the given class name exists. The class name can also be
109: * the super-type of an existing module.
110: *
111: * @param fullyQualifiedClassName The class name, not null
112: * @return True if the module exists and is enabled
113: */
114: @SuppressWarnings("unchecked")
115: public boolean isModuleEnabled(String fullyQualifiedClassName) {
116: Class<? extends Module> moduleClass;
117: try {
118: moduleClass = (Class<? extends Module>) getClassWithName(fullyQualifiedClassName);
119:
120: } catch (UnitilsException e) {
121: // class could not be loaded
122: return false;
123: }
124: return isModuleEnabled(moduleClass);
125: }
126:
127: /**
128: * Checks whether a module of a type exists. The class an also be the super-type of an existing module.
129: *
130: * @param moduleClass The class, not null
131: * @return True if the module exists and is enabled
132: */
133: public boolean isModuleEnabled(Class<? extends Module> moduleClass) {
134: List<? extends Module> modulesOfType = getModulesOfType(moduleClass);
135: if (modulesOfType.size() > 1) {
136: throw new UnitilsException(
137: "More than one module found of type "
138: + moduleClass.getName());
139: }
140: return modulesOfType.size() == 1;
141: }
142:
143: /**
144: * Gets the listener corresponding to the given module.
145: *
146: * @param module the module, not null
147: * @return the listener, null if the module could not be found
148: */
149: public TestListener getTestListener(Module module) {
150: return testListeners.get(module);
151: }
152:
153: /**
154: * Creates test listeners for each of the given modules.
155: *
156: * @param moduleList the modules, not null
157: * @return the listeners for each module, not null
158: */
159: private Map<Module, TestListener> createTestListeners(
160: List<Module> moduleList) {
161: Map<Module, TestListener> result = new HashMap<Module, TestListener>(
162: moduleList.size());
163: for (Module module : moduleList) {
164: result.put(module, module.createTestListener());
165: }
166: return result;
167: }
168:
169: }
|