001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.test;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028: import jacareto.system.EnvironmentMember;
029: import jacareto.toolkit.EnhancedHashtable;
030:
031: import java.awt.Component;
032:
033: import java.lang.reflect.Constructor;
034:
035: import java.util.Enumeration;
036:
037: /**
038: * Delivers a test for a given component.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.0
042: */
043: public class TestLoader extends EnvironmentMember {
044: /** The hashtable which maps component classes to tests. */
045: private EnhancedHashtable compToTest;
046:
047: /** The components instance. */
048: private Components components;
049:
050: /**
051: * Creates a new TestLoader.
052: *
053: * @param env the environment
054: * @param components the components instance
055: */
056: public TestLoader(Environment env, Components components) {
057: super (env);
058: this .components = components;
059: compToTest = env.getCustomization().getMap("Tests",
060: new EnhancedHashtable());
061: }
062:
063: /**
064: * Returns a test for a given component.
065: *
066: * @param component the component
067: *
068: * @return the test for the component, or <code>null</code> if no test has been found.
069: */
070: public Test getTest(Component component) {
071: Test result = null;
072:
073: Class componentClass = component.getClass();
074: Enumeration e = compToTest.keys();
075: String classname = null;
076:
077: while (e.hasMoreElements()) {
078: try {
079: classname = (String) e.nextElement();
080:
081: if (Class.forName(classname).isAssignableFrom(
082: componentClass)) {
083: Class testClass = Class.forName((String) compToTest
084: .get(classname));
085:
086: if ((result == null)
087: || result.getClass().isAssignableFrom(
088: testClass)) {
089: Class[] paramTypes = new Class[3];
090: paramTypes[0] = Class
091: .forName("jacareto.system.Environment");
092: paramTypes[1] = Class
093: .forName("jacareto.comp.Components");
094: paramTypes[2] = Class
095: .forName("java.awt.Component");
096:
097: Object[] params = new Object[3];
098: params[0] = env;
099: params[1] = components;
100: params[2] = component;
101:
102: Constructor constructor = testClass
103: .getConstructor(paramTypes);
104: result = (Test) constructor.newInstance(params);
105: }
106: }
107: } catch (ClassNotFoundException c) {
108: logger
109: .error(language
110: .getString("Tests.TestLoader.Error.ClassNotFound")
111: + " " + classname);
112: } catch (Throwable t) {
113: logger
114: .error(language
115: .getString("Tests.TestLoader.Error.BuildingTest"));
116: }
117: }
118:
119: return result;
120: }
121: }
|