3 *******************************************************************************
4 * Copyright (C) 2001-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
8 package com.ibm.icu.dev.test;
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;
17 public final class TestUtil {
19 * Path to test data in icu4jtest.jar
21 public static final String LOCAL_DATA_PATH = "/com/ibm/icu/dev/data/";
24 * Standard path to the test data in the file system.
26 public static final String DATA_PATH = "/src" + LOCAL_DATA_PATH;
29 * Property for user-defined data path.
31 public static final String DATA_PATH_PROPERTY = "ICUDataPath";
34 * Property for modular build.
36 public static final String DATA_MODULAR_BUILD_PROPERTY = "ICUModularBuild";
39 * Compute a full data path using the ICUDataPath, if defined, or the user.dir, if we
40 * are allowed access to it.
42 private static final String dataPath(String fileName) {
43 String s = System.getProperty(DATA_PATH_PROPERTY);
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
54 * Return an input stream on the data file at path 'name' rooted at the data path
56 public static final InputStream getDataStream(String name) throws IOException {
57 InputStream is = null;
59 is = new FileInputStream(dataPath(name));
60 } catch (Throwable e) {
62 is = TestUtil.class.getResourceAsStream(LOCAL_DATA_PATH + name);
63 } catch (Throwable t) {
65 new IOException("data resource '" + name + "' not found");
66 //#if defined(FOUNDATION10) || defined(J2SE13)
67 //## t.printStackTrace();
69 //initCause API was introduced in JDK 1.4
80 * Return a buffered reader on the data file at path 'name' rooted at the data path.
82 public static final BufferedReader getDataReader(String name, String charset) throws IOException {
83 InputStream is = getDataStream(name);
84 InputStreamReader isr =
86 ? new InputStreamReader(is)
87 : new InputStreamReader(is, charset);
88 return new BufferedReader(isr);
92 * Return a buffered reader on the data file at path 'name' rooted at the data path,
93 * using the provided encoding.
95 public static final BufferedReader getDataReader(String name)
97 return getDataReader(name, null);
100 static final char DIGITS[] =
139 * Return true if the character is NOT printable ASCII. The tab,
140 * newline and linefeed characters are considered unprintable.
142 public static boolean isUnprintable(int c) {
143 return !(c >= 0x20 && c <= 0x7E);
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
152 public static boolean escapeUnprintable(StringBuffer result, int c) {
153 if (isUnprintable(c)) {
155 if ((c & ~0xFFFF) != 0) {
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)]);
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]);
176 synchronized void inc() {
180 synchronized void dec() {
184 synchronized int count() {
190 while (count() > 0) {
191 synchronized (this) {
196 } catch (InterruptedException e) {
201 static class TestThread extends Thread {
205 TestThread(Lock lock, Runnable target) {
207 this.target = target;
214 synchronized (lock) {
218 } catch (InterruptedException e) {
225 public static void runUntilDone(Runnable[] targets) {
226 if (targets == null) {
227 throw new IllegalArgumentException("targets is null");
229 if (targets.length == 0) {
233 Lock lock = new Lock();
234 for (int i = 0; i < targets.length; ++i) {
235 new TestThread(lock, targets[i]).start();
240 public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {
241 return openReader(dir,filename,"UTF-8");
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),