-//##header\r
-/**\r
- *******************************************************************************\r
- * Copyright (C) 2001-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.InputStreamReader;\r
-import java.io.File;\r
-\r
-public final class TestUtil {\r
- /**\r
- * Path to test data in icu4jtest.jar\r
- */\r
- public static final String LOCAL_DATA_PATH = "/com/ibm/icu/dev/data/";\r
-\r
- /**\r
- * Standard path to the test data in the file system.\r
- */\r
- public static final String DATA_PATH = "/src" + LOCAL_DATA_PATH;\r
-\r
- /**\r
- * Property for user-defined data path.\r
- */\r
- public static final String DATA_PATH_PROPERTY = "ICUDataPath";\r
-\r
- /**\r
- * Property for modular build.\r
- */\r
- public static final String DATA_MODULAR_BUILD_PROPERTY = "ICUModularBuild";\r
-\r
- /**\r
- * Compute a full data path using the ICUDataPath, if defined, or the user.dir, if we\r
- * are allowed access to it.\r
- */\r
- private static final String dataPath(String fileName) {\r
- String s = System.getProperty(DATA_PATH_PROPERTY);\r
- if (s == null) {\r
- // assume user.dir is directly above src directory\r
- // data path must end in '/' or '\', fileName should not start with one\r
- s = System.getProperty("user.dir"); // protected property\r
- s = s + DATA_PATH;\r
- }\r
- return s + fileName;\r
- }\r
-\r
- /**\r
- * Return an input stream on the data file at path 'name' rooted at the data path\r
- */\r
- public static final InputStream getDataStream(String name) throws IOException {\r
- InputStream is = null;\r
- try {\r
- is = new FileInputStream(dataPath(name));\r
- } catch (Throwable e) {\r
- try {\r
- is = TestUtil.class.getResourceAsStream(LOCAL_DATA_PATH + name);\r
- } catch (Throwable t) {\r
- IOException ex =\r
- new IOException("data resource '" + name + "' not found");\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//## t.printStackTrace();\r
-//#else\r
- //initCause API was introduced in JDK 1.4\r
- ex.initCause(t);\r
-//#endif\r
- \r
- throw ex;\r
- }\r
- }\r
- return is;\r
- }\r
-\r
- /**\r
- * Return a buffered reader on the data file at path 'name' rooted at the data path.\r
- */\r
- public static final BufferedReader getDataReader(String name, String charset) throws IOException {\r
- InputStream is = getDataStream(name);\r
- InputStreamReader isr =\r
- charset == null\r
- ? new InputStreamReader(is)\r
- : new InputStreamReader(is, charset);\r
- return new BufferedReader(isr);\r
- }\r
-\r
- /**\r
- * Return a buffered reader on the data file at path 'name' rooted at the data path,\r
- * using the provided encoding.\r
- */\r
- public static final BufferedReader getDataReader(String name)\r
- throws IOException {\r
- return getDataReader(name, null);\r
- }\r
-\r
- static final char DIGITS[] =\r
- {\r
- '0',\r
- '1',\r
- '2',\r
- '3',\r
- '4',\r
- '5',\r
- '6',\r
- '7',\r
- '8',\r
- '9',\r
- 'A',\r
- 'B',\r
- 'C',\r
- 'D',\r
- 'E',\r
- 'F',\r
- 'G',\r
- 'H',\r
- 'I',\r
- 'J',\r
- 'K',\r
- 'L',\r
- 'M',\r
- 'N',\r
- 'O',\r
- 'P',\r
- 'Q',\r
- 'R',\r
- 'S',\r
- 'T',\r
- 'U',\r
- 'V',\r
- 'W',\r
- 'X',\r
- 'Y',\r
- 'Z' };\r
- /**\r
- * Return true if the character is NOT printable ASCII. The tab,\r
- * newline and linefeed characters are considered unprintable.\r
- */\r
- public static boolean isUnprintable(int c) {\r
- return !(c >= 0x20 && c <= 0x7E);\r
- }\r
- /**\r
- * Escape unprintable characters using <backslash>uxxxx notation\r
- * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and\r
- * above. If the character is printable ASCII, then do nothing\r
- * and return FALSE. Otherwise, append the escaped notation and\r
- * return TRUE.\r
- */\r
- public static boolean escapeUnprintable(StringBuffer result, int c) {\r
- if (isUnprintable(c)) {\r
- result.append('\\');\r
- if ((c & ~0xFFFF) != 0) {\r
- result.append('U');\r
- result.append(DIGITS[0xF & (c >> 28)]);\r
- result.append(DIGITS[0xF & (c >> 24)]);\r
- result.append(DIGITS[0xF & (c >> 20)]);\r
- result.append(DIGITS[0xF & (c >> 16)]);\r
- } else {\r
- result.append('u');\r
- }\r
- result.append(DIGITS[0xF & (c >> 12)]);\r
- result.append(DIGITS[0xF & (c >> 8)]);\r
- result.append(DIGITS[0xF & (c >> 4)]);\r
- result.append(DIGITS[0xF & c]);\r
- return true;\r
- }\r
- return false;\r
- }\r
-\r
- static class Lock {\r
- private int count;\r
-\r
- synchronized void inc() {\r
- ++count;\r
- }\r
-\r
- synchronized void dec() {\r
- --count;\r
- }\r
-\r
- synchronized int count() {\r
- return count;\r
- }\r
-\r
- void go() {\r
- try {\r
- while (count() > 0) {\r
- synchronized (this) {\r
- notifyAll();\r
- }\r
- Thread.sleep(50);\r
- }\r
- } catch (InterruptedException e) {\r
- }\r
- }\r
- }\r
-\r
- static class TestThread extends Thread {\r
- Lock lock;\r
- Runnable target;\r
-\r
- TestThread(Lock lock, Runnable target) {\r
- this.lock = lock;\r
- this.target = target;\r
-\r
- lock.inc();\r
- }\r
-\r
- public void run() {\r
- try {\r
- synchronized (lock) {\r
- lock.wait();\r
- }\r
- target.run();\r
- } catch (InterruptedException e) {\r
- }\r
-\r
- lock.dec();\r
- }\r
- }\r
-\r
- public static void runUntilDone(Runnable[] targets) {\r
- if (targets == null) {\r
- throw new IllegalArgumentException("targets is null");\r
- }\r
- if (targets.length == 0) {\r
- return;\r
- }\r
-\r
- Lock lock = new Lock();\r
- for (int i = 0; i < targets.length; ++i) {\r
- new TestThread(lock, targets[i]).start();\r
- }\r
-\r
- lock.go();\r
- }\r
- public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {\r
- return openReader(dir,filename,"UTF-8");\r
- }\r
- public static BufferedReader openReader(String dir, String filename, String encoding) throws IOException {\r
- File file = new File(dir + filename);\r
- return new BufferedReader(\r
- new InputStreamReader(\r
- new FileInputStream(file),\r
- encoding),\r
- 4*1024);\r
- }\r
-\r
-}\r
+//##header J2SE15
+/**
+ *******************************************************************************
+ * Copyright (C) 2001-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.File;
+
+public final class TestUtil {
+ /**
+ * Path to test data in icu4jtest.jar
+ */
+ public static final String LOCAL_DATA_PATH = "/com/ibm/icu/dev/data/";
+
+ /**
+ * Standard path to the test data in the file system.
+ */
+ public static final String DATA_PATH = "/src" + LOCAL_DATA_PATH;
+
+ /**
+ * Property for user-defined data path.
+ */
+ public static final String DATA_PATH_PROPERTY = "ICUDataPath";
+
+ /**
+ * Property for modular build.
+ */
+ public static final String DATA_MODULAR_BUILD_PROPERTY = "ICUModularBuild";
+
+ /**
+ * Compute a full data path using the ICUDataPath, if defined, or the user.dir, if we
+ * are allowed access to it.
+ */
+ private static final String dataPath(String fileName) {
+ String s = System.getProperty(DATA_PATH_PROPERTY);
+ if (s == null) {
+ // assume user.dir is directly above src directory
+ // data path must end in '/' or '\', fileName should not start with one
+ s = System.getProperty("user.dir"); // protected property
+ s = s + DATA_PATH;
+ }
+ return s + fileName;
+ }
+
+ /**
+ * Return an input stream on the data file at path 'name' rooted at the data path
+ */
+ public static final InputStream getDataStream(String name) throws IOException {
+ InputStream is = null;
+ try {
+ is = new FileInputStream(dataPath(name));
+ } catch (Throwable e) {
+ try {
+ is = TestUtil.class.getResourceAsStream(LOCAL_DATA_PATH + name);
+ } catch (Throwable t) {
+ IOException ex =
+ new IOException("data resource '" + name + "' not found");
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//## t.printStackTrace();
+//#else
+ //initCause API was introduced in JDK 1.4
+ ex.initCause(t);
+//#endif
+
+ throw ex;
+ }
+ }
+ return is;
+ }
+
+ /**
+ * Return a buffered reader on the data file at path 'name' rooted at the data path.
+ */
+ public static final BufferedReader getDataReader(String name, String charset) throws IOException {
+ InputStream is = getDataStream(name);
+ InputStreamReader isr =
+ charset == null
+ ? new InputStreamReader(is)
+ : new InputStreamReader(is, charset);
+ return new BufferedReader(isr);
+ }
+
+ /**
+ * Return a buffered reader on the data file at path 'name' rooted at the data path,
+ * using the provided encoding.
+ */
+ public static final BufferedReader getDataReader(String name)
+ throws IOException {
+ return getDataReader(name, null);
+ }
+
+ static final char DIGITS[] =
+ {
+ '0',
+ '1',
+ '2',
+ '3',
+ '4',
+ '5',
+ '6',
+ '7',
+ '8',
+ '9',
+ 'A',
+ 'B',
+ 'C',
+ 'D',
+ 'E',
+ 'F',
+ 'G',
+ 'H',
+ 'I',
+ 'J',
+ 'K',
+ 'L',
+ 'M',
+ 'N',
+ 'O',
+ 'P',
+ 'Q',
+ 'R',
+ 'S',
+ 'T',
+ 'U',
+ 'V',
+ 'W',
+ 'X',
+ 'Y',
+ 'Z' };
+ /**
+ * Return true if the character is NOT printable ASCII. The tab,
+ * newline and linefeed characters are considered unprintable.
+ */
+ public static boolean isUnprintable(int c) {
+ return !(c >= 0x20 && c <= 0x7E);
+ }
+ /**
+ * Escape unprintable characters using <backslash>uxxxx notation
+ * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and
+ * above. If the character is printable ASCII, then do nothing
+ * and return FALSE. Otherwise, append the escaped notation and
+ * return TRUE.
+ */
+ public static boolean escapeUnprintable(StringBuffer result, int c) {
+ if (isUnprintable(c)) {
+ result.append('\\');
+ if ((c & ~0xFFFF) != 0) {
+ result.append('U');
+ result.append(DIGITS[0xF & (c >> 28)]);
+ result.append(DIGITS[0xF & (c >> 24)]);
+ result.append(DIGITS[0xF & (c >> 20)]);
+ result.append(DIGITS[0xF & (c >> 16)]);
+ } else {
+ result.append('u');
+ }
+ result.append(DIGITS[0xF & (c >> 12)]);
+ result.append(DIGITS[0xF & (c >> 8)]);
+ result.append(DIGITS[0xF & (c >> 4)]);
+ result.append(DIGITS[0xF & c]);
+ return true;
+ }
+ return false;
+ }
+
+ static class Lock {
+ private int count;
+
+ synchronized void inc() {
+ ++count;
+ }
+
+ synchronized void dec() {
+ --count;
+ }
+
+ synchronized int count() {
+ return count;
+ }
+
+ void go() {
+ try {
+ while (count() > 0) {
+ synchronized (this) {
+ notifyAll();
+ }
+ Thread.sleep(50);
+ }
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+
+ static class TestThread extends Thread {
+ Lock lock;
+ Runnable target;
+
+ TestThread(Lock lock, Runnable target) {
+ this.lock = lock;
+ this.target = target;
+
+ lock.inc();
+ }
+
+ public void run() {
+ try {
+ synchronized (lock) {
+ lock.wait();
+ }
+ target.run();
+ } catch (InterruptedException e) {
+ }
+
+ lock.dec();
+ }
+ }
+
+ public static void runUntilDone(Runnable[] targets) {
+ if (targets == null) {
+ throw new IllegalArgumentException("targets is null");
+ }
+ if (targets.length == 0) {
+ return;
+ }
+
+ Lock lock = new Lock();
+ for (int i = 0; i < targets.length; ++i) {
+ new TestThread(lock, targets[i]).start();
+ }
+
+ lock.go();
+ }
+ public static BufferedReader openUTF8Reader(String dir, String filename) throws IOException {
+ return openReader(dir,filename,"UTF-8");
+ }
+ public static BufferedReader openReader(String dir, String filename, String encoding) throws IOException {
+ File file = new File(dir + filename);
+ return new BufferedReader(
+ new InputStreamReader(
+ new FileInputStream(file),
+ encoding),
+ 4*1024);
+ }
+
+}