]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/framework/src/com/ibm/icu/dev/test/TestUtil.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / framework / src / com / ibm / icu / dev / test / TestUtil.java
1 /**\r
2  *******************************************************************************\r
3  * Copyright (C) 2001-2009, 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.io.BufferedReader;\r
10 import java.io.File;\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 \r
16 public final class TestUtil {\r
17     /**\r
18      * Path to test data in icu4jtest.jar\r
19      */\r
20     public static final String DATA_PATH = "/com/ibm/icu/dev/data/";\r
21 \r
22     /**\r
23      * Return an input stream on the data file at path 'name' rooted at the data path\r
24      */\r
25     public static final InputStream getDataStream(String name) throws IOException {\r
26         InputStream is = null;\r
27         try {\r
28             is = TestUtil.class.getResourceAsStream(DATA_PATH + name);\r
29         } catch (Throwable t) {\r
30             IOException ex =\r
31                 new IOException("data resource '" + name + "' not found");\r
32             ex.initCause(t);\r
33             throw ex;\r
34         }\r
35         return is;\r
36     }\r
37 \r
38     /**\r
39      * Return a buffered reader on the data file at path 'name' rooted at the data path.\r
40      */\r
41     public static final BufferedReader getDataReader(String name, String charset) throws IOException {\r
42         InputStream is = getDataStream(name);\r
43         InputStreamReader isr =\r
44             charset == null\r
45                 ? new InputStreamReader(is)\r
46                 : new InputStreamReader(is, charset);\r
47         return new BufferedReader(isr);\r
48     }\r
49 \r
50     /**\r
51      * Return a buffered reader on the data file at path 'name' rooted at the data path,\r
52      * using the provided encoding.\r
53      */\r
54     public static final BufferedReader getDataReader(String name)\r
55         throws IOException {\r
56         return getDataReader(name, null);\r
57     }\r
58 \r
59     static final char DIGITS[] =\r
60         {\r
61             '0',\r
62             '1',\r
63             '2',\r
64             '3',\r
65             '4',\r
66             '5',\r
67             '6',\r
68             '7',\r
69             '8',\r
70             '9',\r
71             'A',\r
72             'B',\r
73             'C',\r
74             'D',\r
75             'E',\r
76             'F',\r
77             'G',\r
78             'H',\r
79             'I',\r
80             'J',\r
81             'K',\r
82             'L',\r
83             'M',\r
84             'N',\r
85             'O',\r
86             'P',\r
87             'Q',\r
88             'R',\r
89             'S',\r
90             'T',\r
91             'U',\r
92             'V',\r
93             'W',\r
94             'X',\r
95             'Y',\r
96             'Z' };\r
97     /**\r
98      * Return true if the character is NOT printable ASCII.  The tab,\r
99      * newline and linefeed characters are considered unprintable.\r
100      */\r
101     public static boolean isUnprintable(int c) {\r
102         return !(c >= 0x20 && c <= 0x7E);\r
103     }\r
104     /**\r
105      * Escape unprintable characters using <backslash>uxxxx notation\r
106      * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and\r
107      * above.  If the character is printable ASCII, then do nothing\r
108      * and return FALSE.  Otherwise, append the escaped notation and\r
109      * return TRUE.\r
110      */\r
111     public static boolean escapeUnprintable(StringBuffer result, int c) {\r
112         if (isUnprintable(c)) {\r
113             result.append('\\');\r
114             if ((c & ~0xFFFF) != 0) {\r
115                 result.append('U');\r
116                 result.append(DIGITS[0xF & (c >> 28)]);\r
117                 result.append(DIGITS[0xF & (c >> 24)]);\r
118                 result.append(DIGITS[0xF & (c >> 20)]);\r
119                 result.append(DIGITS[0xF & (c >> 16)]);\r
120             } else {\r
121                 result.append('u');\r
122             }\r
123             result.append(DIGITS[0xF & (c >> 12)]);\r
124             result.append(DIGITS[0xF & (c >> 8)]);\r
125             result.append(DIGITS[0xF & (c >> 4)]);\r
126             result.append(DIGITS[0xF & c]);\r
127             return true;\r
128         }\r
129         return false;\r
130     }\r
131 \r
132     static class Lock {\r
133         private int count;\r
134 \r
135         synchronized void inc() {\r
136             ++count;\r
137         }\r
138 \r
139         synchronized void dec() {\r
140             --count;\r
141         }\r
142 \r
143         synchronized int count() {\r
144             return count;\r
145         }\r
146 \r
147         void go() {\r
148             try {\r
149                 while (count() > 0) {\r
150                     synchronized (this) {\r
151                         notifyAll();\r
152                     }\r
153                     Thread.sleep(50);\r
154                 }\r
155             } catch (InterruptedException e) {\r
156             }\r
157         }\r
158     }\r
159 \r
160     static class TestThread extends Thread {\r
161         Lock lock;\r
162         Runnable target;\r
163 \r
164         TestThread(Lock lock, Runnable target) {\r
165             this.lock = lock;\r
166             this.target = target;\r
167 \r
168             lock.inc();\r
169         }\r
170 \r
171         public void run() {\r
172             try {\r
173                 synchronized (lock) {\r
174                     lock.wait();\r
175                 }\r
176                 target.run();\r
177             } catch (InterruptedException e) {\r
178             }\r
179 \r
180             lock.dec();\r
181         }\r
182     }\r
183 \r
184     public static void runUntilDone(Runnable[] targets) {\r
185         if (targets == null) {\r
186             throw new IllegalArgumentException("targets is null");\r
187         }\r
188         if (targets.length == 0) {\r
189             return;\r
190         }\r
191 \r
192         Lock lock = new Lock();\r
193         for (int i = 0; i < targets.length; ++i) {\r
194             new TestThread(lock, targets[i]).start();\r
195         }\r
196 \r
197         lock.go();\r
198     }\r
199     public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {\r
200         return openReader(dir,filename,"UTF-8");\r
201     }\r
202     public static BufferedReader openReader(String dir, String filename, String encoding) throws IOException {\r
203         File file = new File(dir + filename);\r
204         return new BufferedReader(\r
205             new InputStreamReader(\r
206                 new FileInputStream(file),\r
207                 encoding),\r
208             4*1024);\r
209     }\r
210 \r
211 }\r