2 *******************************************************************************
3 * Copyright (C) 2001-2007, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test;
9 import java.lang.reflect.Method;
10 import java.util.Iterator;
11 import java.util.MissingResourceException;
13 import com.ibm.icu.dev.test.TestDataModule.DataMap;
14 import com.ibm.icu.dev.test.TestDataModule.DataModuleFormatError;
15 import com.ibm.icu.dev.test.TestDataModule.Factory;
16 import com.ibm.icu.dev.test.TestDataModule.TestData;
19 * Ray: An adapter class for TestDataMoule to make it like TestFmwk
21 * A convenience extension of TestFmwk for use by data module-driven tests.
23 * Tests can implement this if they make extensive use of information in a
26 * Subclasses can allow for test methods that don't use data from the module by
27 * overriding validateMethod to return true for these methods. Tests are also
28 * free to instantiate their own modules and run from them, though care should
29 * be taken not to interfere with the methods in this class.
31 * See CollationTest for an example.
33 public abstract class ModuleTest extends TestFmwk {
34 private TestDataModule m;
36 protected TestData t = null;
38 private String localeName = null;
40 private String baseName = null;
42 abstract protected void processModules();
44 protected ModuleTest(String baseName, String locName) {
46 this.baseName = baseName;
49 protected Target getTargets(String targetName) {
50 if (params.doMethods()) {
55 Iterator testData = m.getTestDataIterator();
56 if (testData != null) {
58 Method method = getClass()
59 .getMethod("processModules", (Class[])null);
60 while (testData.hasNext()) {
61 target = new MethodTarget(((TestData) testData.next())
62 .getName(), method).setNext(target);
64 } catch (Exception e) {
66 throw new IllegalStateException(e.getMessage());
77 * TestFmwk calls this before trying to run a suite of tests. The test suite
78 * if valid if a module whose name is the name of this class + "Data" can be
79 * opened. Subclasses can override this if there are different or additional
82 protected boolean validate() {
84 m = Factory.get(baseName, localeName);
85 } catch (DataModuleFormatError e) {
88 } catch(MissingResourceException e){
89 warnln("Could not load data: "+e.getMessage());
95 * TestFmwk calls this before trying to invoke a test method. The method is
96 * valid if there is test data with the name of this method in the module.
97 * Subclasses can override this to allow for tests that do not require test
98 * data from the module, or if there are different or additional data
101 protected boolean validateMethod(String methodName) {
102 return openTestData(methodName);
106 * Override of TestFmwk method to get the test suite description from the
107 * DESCRIPTION field of the module info.
109 protected String getDescription() {
110 DataMap info = moduleInfo();
112 // return info.getString(TestDataModule.DESCRIPTION);
118 * Override of TestFmwk method to get the test method description from the
119 * DESCRIPTION field of the test info.
121 protected String getMethodDescription(String methodName) {
122 if (openTestData(methodName)) {
123 DataMap info = testInfo();
125 // return info.getString(TestDataModule.DESCRIPTION);
132 * Open the test data in the module with the given name, and return true if
133 * success. The current test is reset.
135 * @throws DataModuleFormatError
137 protected boolean openTestData(String name) {
139 t = m == null ? null : m.getTestData(name);
140 } catch (DataModuleFormatError e) {
147 * Get information on this module. Returns null if no module open or no info
150 private DataMap moduleInfo() {
151 return m == null ? null : m.getInfo();
155 * Get information on this test. Returns null if no module open or no test
156 * open or not info for this test.
158 private DataMap testInfo() {
159 return t == null ? null : t.getInfo();
162 public void msg(String message, int level, boolean incCount, boolean newln) {
163 if (level == ERR && t != null) {
166 super.msg(message, level, incCount, newln);