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