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