]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/TestUtil.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / TestUtil.java
1 //##header\r
2 /**\r
3  *******************************************************************************\r
4  * Copyright (C) 2001-2009, International Business Machines Corporation and    *\r
5  * others. All Rights Reserved.                                                *\r
6  *******************************************************************************\r
7  */\r
8 package com.ibm.icu.dev.test;\r
9 \r
10 import java.io.BufferedReader;\r
11 import java.io.FileInputStream;\r
12 import java.io.IOException;\r
13 import java.io.InputStream;\r
14 import java.io.InputStreamReader;\r
15 import java.io.File;\r
16 \r
17 public final class TestUtil {\r
18     /**\r
19      * Path to test data in icu4jtest.jar\r
20      */\r
21     public static final String LOCAL_DATA_PATH = "/com/ibm/icu/dev/data/";\r
22 \r
23     /**\r
24      * Standard path to the test data in the file system.\r
25      */\r
26     public static final String DATA_PATH = "/src" + LOCAL_DATA_PATH;\r
27 \r
28     /**\r
29      * Property for user-defined data path.\r
30      */\r
31     public static final String DATA_PATH_PROPERTY = "ICUDataPath";\r
32 \r
33     /**\r
34      * Property for modular build.\r
35      */\r
36     public static final String DATA_MODULAR_BUILD_PROPERTY = "ICUModularBuild";\r
37 \r
38     /**\r
39      * Compute a full data path using the ICUDataPath, if defined, or the user.dir, if we\r
40      * are allowed access to it.\r
41      */\r
42     private static final String dataPath(String fileName) {\r
43         String s = System.getProperty(DATA_PATH_PROPERTY);\r
44         if (s == null) {\r
45             // assume user.dir is directly above src directory\r
46             // data path must end in '/' or '\', fileName should not start with one\r
47             s = System.getProperty("user.dir"); // protected property\r
48             s = s + DATA_PATH;\r
49         }\r
50         return s + fileName;\r
51     }\r
52 \r
53     /**\r
54      * Return an input stream on the data file at path 'name' rooted at the data path\r
55      */\r
56     public static final InputStream getDataStream(String name) throws IOException {\r
57         InputStream is = null;\r
58         try {\r
59             is = new FileInputStream(dataPath(name));\r
60         } catch (Throwable e) {\r
61             try {\r
62                 is = TestUtil.class.getResourceAsStream(LOCAL_DATA_PATH + name);\r
63             } catch (Throwable t) {\r
64                 IOException ex =\r
65                     new IOException("data resource '" + name + "' not found");\r
66 //#if defined(FOUNDATION10) || defined(J2SE13)\r
67 //##            t.printStackTrace();\r
68 //#else\r
69                 //initCause API was introduced in JDK 1.4\r
70                 ex.initCause(t);\r
71 //#endif\r
72                \r
73                 throw ex;\r
74             }\r
75         }\r
76         return is;\r
77     }\r
78 \r
79     /**\r
80      * Return a buffered reader on the data file at path 'name' rooted at the data path.\r
81      */\r
82     public static final BufferedReader getDataReader(String name, String charset) throws IOException {\r
83         InputStream is = getDataStream(name);\r
84         InputStreamReader isr =\r
85             charset == null\r
86                 ? new InputStreamReader(is)\r
87                 : new InputStreamReader(is, charset);\r
88         return new BufferedReader(isr);\r
89     }\r
90 \r
91     /**\r
92      * Return a buffered reader on the data file at path 'name' rooted at the data path,\r
93      * using the provided encoding.\r
94      */\r
95     public static final BufferedReader getDataReader(String name)\r
96         throws IOException {\r
97         return getDataReader(name, null);\r
98     }\r
99 \r
100     static final char DIGITS[] =\r
101         {\r
102             '0',\r
103             '1',\r
104             '2',\r
105             '3',\r
106             '4',\r
107             '5',\r
108             '6',\r
109             '7',\r
110             '8',\r
111             '9',\r
112             'A',\r
113             'B',\r
114             'C',\r
115             'D',\r
116             'E',\r
117             'F',\r
118             'G',\r
119             'H',\r
120             'I',\r
121             'J',\r
122             'K',\r
123             'L',\r
124             'M',\r
125             'N',\r
126             'O',\r
127             'P',\r
128             'Q',\r
129             'R',\r
130             'S',\r
131             'T',\r
132             'U',\r
133             'V',\r
134             'W',\r
135             'X',\r
136             'Y',\r
137             'Z' };\r
138     /**\r
139      * Return true if the character is NOT printable ASCII.  The tab,\r
140      * newline and linefeed characters are considered unprintable.\r
141      */\r
142     public static boolean isUnprintable(int c) {\r
143         return !(c >= 0x20 && c <= 0x7E);\r
144     }\r
145     /**\r
146      * Escape unprintable characters using <backslash>uxxxx notation\r
147      * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and\r
148      * above.  If the character is printable ASCII, then do nothing\r
149      * and return FALSE.  Otherwise, append the escaped notation and\r
150      * return TRUE.\r
151      */\r
152     public static boolean escapeUnprintable(StringBuffer result, int c) {\r
153         if (isUnprintable(c)) {\r
154             result.append('\\');\r
155             if ((c & ~0xFFFF) != 0) {\r
156                 result.append('U');\r
157                 result.append(DIGITS[0xF & (c >> 28)]);\r
158                 result.append(DIGITS[0xF & (c >> 24)]);\r
159                 result.append(DIGITS[0xF & (c >> 20)]);\r
160                 result.append(DIGITS[0xF & (c >> 16)]);\r
161             } else {\r
162                 result.append('u');\r
163             }\r
164             result.append(DIGITS[0xF & (c >> 12)]);\r
165             result.append(DIGITS[0xF & (c >> 8)]);\r
166             result.append(DIGITS[0xF & (c >> 4)]);\r
167             result.append(DIGITS[0xF & c]);\r
168             return true;\r
169         }\r
170         return false;\r
171     }\r
172 \r
173     static class Lock {\r
174         private int count;\r
175 \r
176         synchronized void inc() {\r
177             ++count;\r
178         }\r
179 \r
180         synchronized void dec() {\r
181             --count;\r
182         }\r
183 \r
184         synchronized int count() {\r
185             return count;\r
186         }\r
187 \r
188         void go() {\r
189             try {\r
190                 while (count() > 0) {\r
191                     synchronized (this) {\r
192                         notifyAll();\r
193                     }\r
194                     Thread.sleep(50);\r
195                 }\r
196             } catch (InterruptedException e) {\r
197             }\r
198         }\r
199     }\r
200 \r
201     static class TestThread extends Thread {\r
202         Lock lock;\r
203         Runnable target;\r
204 \r
205         TestThread(Lock lock, Runnable target) {\r
206             this.lock = lock;\r
207             this.target = target;\r
208 \r
209             lock.inc();\r
210         }\r
211 \r
212         public void run() {\r
213             try {\r
214                 synchronized (lock) {\r
215                     lock.wait();\r
216                 }\r
217                 target.run();\r
218             } catch (InterruptedException e) {\r
219             }\r
220 \r
221             lock.dec();\r
222         }\r
223     }\r
224 \r
225     public static void runUntilDone(Runnable[] targets) {\r
226         if (targets == null) {\r
227             throw new IllegalArgumentException("targets is null");\r
228         }\r
229         if (targets.length == 0) {\r
230             return;\r
231         }\r
232 \r
233         Lock lock = new Lock();\r
234         for (int i = 0; i < targets.length; ++i) {\r
235             new TestThread(lock, targets[i]).start();\r
236         }\r
237 \r
238         lock.go();\r
239     }\r
240     public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {\r
241         return openReader(dir,filename,"UTF-8");\r
242     }\r
243     public static BufferedReader openReader(String dir, String filename, String encoding) throws IOException {\r
244         File file = new File(dir + filename);\r
245         return new BufferedReader(\r
246             new InputStreamReader(\r
247                 new FileInputStream(file),\r
248                 encoding),\r
249             4*1024);\r
250     }\r
251 \r
252 }\r