]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/framework/src/com/ibm/icu/dev/test/ModuleTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / framework / src / com / ibm / icu / dev / test / ModuleTest.java
1 /**\r
2  *******************************************************************************\r
3  * Copyright (C) 2001-2007, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test;\r
8 \r
9 import java.lang.reflect.Method;\r
10 import java.util.Iterator;\r
11 import java.util.MissingResourceException;\r
12 \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
17 \r
18 /**\r
19  * Ray: An adapter class for TestDataMoule to make it like TestFmwk\r
20  * \r
21  * A convenience extension of TestFmwk for use by data module-driven tests.\r
22  * \r
23  * Tests can implement this if they make extensive use of information in a\r
24  * TestDataModule.\r
25  * \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
30  * \r
31  * See CollationTest for an example.\r
32  */\r
33 public abstract class ModuleTest extends TestFmwk {\r
34     private TestDataModule m;\r
35 \r
36     protected TestData t = null;\r
37 \r
38     private String localeName = null;\r
39 \r
40     private String baseName = null;\r
41 \r
42     abstract protected void processModules();\r
43 \r
44     protected ModuleTest(String baseName, String locName) {\r
45         localeName = locName;\r
46         this.baseName = baseName;\r
47     }\r
48 \r
49     protected Target getTargets(String targetName) {\r
50         if (params.doMethods()) {\r
51             Target target = null;\r
52             if (!validate()) {\r
53                 return null;\r
54             }\r
55             Iterator testData = m.getTestDataIterator();\r
56             if (testData != null) {\r
57                 try {\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
63                     }\r
64                 } catch (Exception e) {\r
65                     e.printStackTrace();\r
66                     throw new IllegalStateException(e.getMessage());\r
67                 }\r
68             }\r
69             return target;\r
70         } else {\r
71             return null;\r
72         }\r
73     }\r
74 \r
75     /**\r
76      * \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
80      * data required.\r
81      */\r
82     protected boolean validate() {\r
83         try {\r
84             m = Factory.get(baseName, localeName);\r
85         } catch (DataModuleFormatError e) {\r
86             e.printStackTrace();\r
87             m = null;\r
88         } catch(MissingResourceException e){\r
89             warnln("Could not load data: "+e.getMessage());\r
90         }\r
91         return m != null;\r
92     }\r
93 \r
94     /**\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
99      * required.\r
100      */\r
101     protected boolean validateMethod(String methodName) {\r
102         return openTestData(methodName);\r
103     }\r
104 \r
105     /**\r
106      * Override of TestFmwk method to get the test suite description from the\r
107      * DESCRIPTION field of the module info.\r
108      */\r
109     protected String getDescription() {\r
110         DataMap info = moduleInfo();\r
111         if (info != null) {\r
112             // return info.getString(TestDataModule.DESCRIPTION);\r
113         }\r
114         return null;\r
115     }\r
116 \r
117     /**\r
118      * Override of TestFmwk method to get the test method description from the\r
119      * DESCRIPTION field of the test info.\r
120      */\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
126             }\r
127         }\r
128         return null;\r
129     }\r
130 \r
131     /**\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
134      * \r
135      * @throws DataModuleFormatError\r
136      */\r
137     protected boolean openTestData(String name) {\r
138         try {\r
139             t = m == null ? null : m.getTestData(name);\r
140         } catch (DataModuleFormatError e) {\r
141             return false;\r
142         }\r
143         return t != null;\r
144     }\r
145 \r
146     /**\r
147      * Get information on this module. Returns null if no module open or no info\r
148      * for the module.\r
149      */\r
150     private DataMap moduleInfo() {\r
151         return m == null ? null : m.getInfo();\r
152     }\r
153 \r
154     /**\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
157      */\r
158     private DataMap testInfo() {\r
159         return t == null ? null : t.getInfo();\r
160     }\r
161 \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
165         }\r
166         super.msg(message, level, incCount, newln);\r
167     }\r
168 \r
169 }\r