2 *******************************************************************************
\r
3 * Copyright (C) 2001-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test;
\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
16 public final class TestUtil {
\r
18 * Path to test data in icu4jtest.jar
\r
20 public static final String DATA_PATH = "/com/ibm/icu/dev/data/";
\r
23 * Return an input stream on the data file at path 'name' rooted at the data path
\r
25 public static final InputStream getDataStream(String name) throws IOException {
\r
26 InputStream is = null;
\r
28 is = TestUtil.class.getResourceAsStream(DATA_PATH + name);
\r
29 } catch (Throwable t) {
\r
31 new IOException("data resource '" + name + "' not found");
\r
39 * Return a buffered reader on the data file at path 'name' rooted at the data path.
\r
41 public static final BufferedReader getDataReader(String name, String charset) throws IOException {
\r
42 InputStream is = getDataStream(name);
\r
43 InputStreamReader isr =
\r
45 ? new InputStreamReader(is)
\r
46 : new InputStreamReader(is, charset);
\r
47 return new BufferedReader(isr);
\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
54 public static final BufferedReader getDataReader(String name)
\r
55 throws IOException {
\r
56 return getDataReader(name, null);
\r
59 static final char DIGITS[] =
\r
98 * Return true if the character is NOT printable ASCII. The tab,
\r
99 * newline and linefeed characters are considered unprintable.
\r
101 public static boolean isUnprintable(int c) {
\r
102 return !(c >= 0x20 && c <= 0x7E);
\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
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
121 result.append('u');
\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
132 static class Lock {
\r
135 synchronized void inc() {
\r
139 synchronized void dec() {
\r
143 synchronized int count() {
\r
149 while (count() > 0) {
\r
150 synchronized (this) {
\r
155 } catch (InterruptedException e) {
\r
160 static class TestThread extends Thread {
\r
164 TestThread(Lock lock, Runnable target) {
\r
166 this.target = target;
\r
171 public void run() {
\r
173 synchronized (lock) {
\r
177 } catch (InterruptedException e) {
\r
184 public static void runUntilDone(Runnable[] targets) {
\r
185 if (targets == null) {
\r
186 throw new IllegalArgumentException("targets is null");
\r
188 if (targets.length == 0) {
\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
199 public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {
\r
200 return openReader(dir,filename,"UTF-8");
\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