001: /**
002: *******************************************************************************
003: * Copyright (C) 2001-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test;
007:
008: import java.lang.reflect.Method;
009: import java.util.Iterator;
010: import java.util.MissingResourceException;
011:
012: import com.ibm.icu.dev.test.TestDataModule.DataMap;
013: import com.ibm.icu.dev.test.TestDataModule.DataModuleFormatError;
014: import com.ibm.icu.dev.test.TestDataModule.Factory;
015: import com.ibm.icu.dev.test.TestDataModule.TestData;
016:
017: /**
018: * Ray: An adapter class for TestDataMoule to make it like TestFmwk
019: *
020: * A convenience extension of TestFmwk for use by data module-driven tests.
021: *
022: * Tests can implement this if they make extensive use of information in a
023: * TestDataModule.
024: *
025: * Subclasses can allow for test methods that don't use data from the modeul by
026: * overriding validateMethod to return true for these methods. Tests are also
027: * free to instantiate their own modules and run from them, though care should
028: * be taken not to interfere with the methods in this class.
029: *
030: * See CollationTest for an example.
031: */
032: public abstract class ModuleTest extends TestFmwk {
033: private TestDataModule m;
034:
035: protected TestData t = null;
036:
037: private String localeName = null;
038:
039: private String baseName = null;
040:
041: abstract protected void processModules();
042:
043: protected ModuleTest(String baseName, String locName) {
044: localeName = locName;
045: this .baseName = baseName;
046: }
047:
048: protected Target getTargets(String targetName) {
049: if (params.doMethods()) {
050: Target target = null;
051: if (!validate()) {
052: return null;
053: }
054: Iterator testData = m.getTestDataIterator();
055: if (testData != null) {
056: try {
057: Method method = getClass().getMethod(
058: "processModules", null);
059: while (testData.hasNext()) {
060: target = new MethodTarget(((TestData) testData
061: .next()).getName(), method)
062: .setNext(target);
063: }
064: } catch (Exception e) {
065: e.printStackTrace();
066: throw new IllegalStateException(e.getMessage());
067: }
068: }
069: return target;
070: } else {
071: return null;
072: }
073: }
074:
075: /**
076: *
077: * TestFmwk calls this before trying to run a suite of tests. The test suite
078: * if valid if a module whose name is the name of this class + "Data" can be
079: * opened. Subclasses can override this if there are different or additional
080: * data required.
081: */
082: protected boolean validate() {
083: try {
084: m = Factory.get(baseName, localeName);
085: } catch (DataModuleFormatError e) {
086: e.printStackTrace();
087: m = null;
088: } catch (MissingResourceException e) {
089: warnln("Could not load data: " + e.getMessage());
090: }
091: return m != null;
092: }
093:
094: /**
095: * TestFmwk calls this before trying to invoke a test method. The method is
096: * valid if there is test data with the name of this method in the module.
097: * Subclasses can override this to allow for tests that do not require test
098: * data from the module, or if there are different or additional data
099: * required.
100: */
101: protected boolean validateMethod(String methodName) {
102: return openTestData(methodName);
103: }
104:
105: /**
106: * Override of TestFmwk method to get the test suite description from the
107: * DESCRIPTION field of the module info.
108: */
109: protected String getDescription() {
110: DataMap info = moduleInfo();
111: if (info != null) {
112: // return info.getString(TestDataModule.DESCRIPTION);
113: }
114: return null;
115: }
116:
117: /**
118: * Override of TestFmwk method to get the test method description from the
119: * DESCRIPTION field of the test info.
120: */
121: protected String getMethodDescription(String methodName) {
122: if (openTestData(methodName)) {
123: DataMap info = testInfo();
124: if (info != null) {
125: // return info.getString(TestDataModule.DESCRIPTION);
126: }
127: }
128: return null;
129: }
130:
131: /**
132: * Open the test data in the module with the given name, and return true if
133: * success. The current test is reset.
134: *
135: * @throws DataModuleFormatError
136: */
137: protected boolean openTestData(String name) {
138: try {
139: t = m == null ? null : m.getTestData(name);
140: } catch (DataModuleFormatError e) {
141: return false;
142: }
143: return t != null;
144: }
145:
146: /**
147: * Get information on this module. Returns null if no module open or no info
148: * for the module.
149: */
150: private DataMap moduleInfo() {
151: return m == null ? null : m.getInfo();
152: }
153:
154: /**
155: * Get information on this test. Returns null if no module open or no test
156: * open or not info for this test.
157: */
158: private DataMap testInfo() {
159: return t == null ? null : t.getInfo();
160: }
161:
162: public void msg(String message, int level, boolean incCount,
163: boolean newln) {
164: if (level == ERR && t != null) {
165: //t.stopIteration();
166: }
167: super.msg(message, level, incCount, newln);
168: }
169:
170: }
|