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