]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/framework/src/com/ibm/icu/dev/test/ModuleTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / framework / src / com / ibm / icu / dev / test / ModuleTest.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2007, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test;
8
9 import java.lang.reflect.Method;
10 import java.util.Iterator;
11 import java.util.MissingResourceException;
12
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;
17
18 /**
19  * Ray: An adapter class for TestDataMoule to make it like TestFmwk
20  * 
21  * A convenience extension of TestFmwk for use by data module-driven tests.
22  * 
23  * Tests can implement this if they make extensive use of information in a
24  * TestDataModule.
25  * 
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.
30  * 
31  * See CollationTest for an example.
32  */
33 public abstract class ModuleTest extends TestFmwk {
34     private TestDataModule m;
35
36     protected TestData t = null;
37
38     private String localeName = null;
39
40     private String baseName = null;
41
42     abstract protected void processModules();
43
44     protected ModuleTest(String baseName, String locName) {
45         localeName = locName;
46         this.baseName = baseName;
47     }
48
49     protected Target getTargets(String targetName) {
50         if (params.doMethods()) {
51             Target target = null;
52             if (!validate()) {
53                 return null;
54             }
55             Iterator testData = m.getTestDataIterator();
56             if (testData != null) {
57                 try {
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);
63                     }
64                 } catch (Exception e) {
65                     e.printStackTrace();
66                     throw new IllegalStateException(e.getMessage());
67                 }
68             }
69             return target;
70         } else {
71             return null;
72         }
73     }
74
75     /**
76      * 
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
80      * data required.
81      */
82     protected boolean validate() {
83         try {
84             m = Factory.get(baseName, localeName);
85         } catch (DataModuleFormatError e) {
86             e.printStackTrace();
87             m = null;
88         } catch(MissingResourceException e){
89             warnln("Could not load data: "+e.getMessage());
90         }
91         return m != null;
92     }
93
94     /**
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
99      * 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, boolean newln) {
163         if (level == ERR && t != null) {
164            //t.stopIteration();
165         }
166         super.msg(message, level, incCount, newln);
167     }
168
169 }