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