Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/testlib/IntlTest.java
41149 views
1
/*
2
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.io.PrintWriter;
26
import java.lang.reflect.InvocationTargetException;
27
import java.lang.reflect.Method;
28
import java.lang.reflect.Modifier;
29
import java.util.ArrayList;
30
import java.util.Map;
31
import java.util.LinkedHashMap;
32
import java.util.List;
33
34
/**
35
* IntlTest is a base class for tests that can be run conveniently from
36
* the command line as well as under the Java test harness.
37
* <p>
38
* Sub-classes implement a set of public void methods named "Test*" or
39
* "test*" with no arguments. Each of these methods performs some
40
* test. Test methods should indicate errors by calling either err() or
41
* errln(). This will increment the errorCount field and may optionally
42
* print a message to the log. Debugging information may also be added to
43
* the log via the log and logln methods. These methods will add their
44
* arguments to the log only if the test is being run in verbose mode.
45
*/
46
public abstract class IntlTest {
47
48
//------------------------------------------------------------------------
49
// Everything below here is boilerplate code that makes it possible
50
// to add a new test by simply adding a method to an existing class.
51
//------------------------------------------------------------------------
52
53
protected IntlTest() {
54
// Populate testMethods with all the test methods.
55
Method[] methods = getClass().getDeclaredMethods();
56
for (Method method : methods) {
57
if (Modifier.isPublic(method.getModifiers())
58
&& method.getReturnType() == void.class
59
&& method.getParameterCount() == 0) {
60
String name = method.getName();
61
if (name.length() > 4) {
62
if (name.startsWith("Test") || name.startsWith("test")) {
63
testMethods.put(name, method);
64
}
65
}
66
}
67
}
68
}
69
70
protected void run(String[] args) throws Exception
71
{
72
// Set up the log and reference streams. We use PrintWriters in order to
73
// take advantage of character conversion. The JavaEsc converter will
74
// convert Unicode outside the ASCII range to Java's \\uxxxx notation.
75
log = new PrintWriter(System.out, true);
76
77
// Parse the test arguments. They can be either the flag
78
// "-verbose" or names of test methods. Create a list of
79
// tests to be run.
80
List<Method> testsToRun = new ArrayList<>(args.length);
81
for (String arg : args) {
82
switch (arg) {
83
case "-verbose":
84
verbose = true;
85
break;
86
case "-prompt":
87
prompt = true;
88
break;
89
case "-nothrow":
90
nothrow = true;
91
break;
92
case "-exitcode":
93
exitCode = true;
94
break;
95
default:
96
Method m = testMethods.get(arg);
97
if (m == null) {
98
System.out.println("Method " + arg + ": not found");
99
usage();
100
return;
101
}
102
testsToRun.add(m);
103
break;
104
}
105
}
106
107
// If no test method names were given explicitly, run them all.
108
if (testsToRun.isEmpty()) {
109
testsToRun.addAll(testMethods.values());
110
}
111
112
System.out.println(getClass().getName() + " {");
113
indentLevel++;
114
115
// Run the list of tests given in the test arguments
116
for (Method testMethod : testsToRun) {
117
int oldCount = errorCount;
118
119
writeTestName(testMethod.getName());
120
121
try {
122
testMethod.invoke(this, new Object[0]);
123
} catch (IllegalAccessException e) {
124
errln("Can't acces test method " + testMethod.getName());
125
} catch (InvocationTargetException e) {
126
errln("Uncaught exception thrown in test method "
127
+ testMethod.getName());
128
e.getTargetException().printStackTrace(this.log);
129
}
130
writeTestResult(errorCount - oldCount);
131
}
132
indentLevel--;
133
writeTestResult(errorCount);
134
135
if (prompt) {
136
System.out.println("Hit RETURN to exit...");
137
try {
138
System.in.read();
139
} catch (IOException e) {
140
System.out.println("Exception: " + e.toString() + e.getMessage());
141
}
142
}
143
if (nothrow) {
144
if (exitCode) {
145
System.exit(errorCount);
146
}
147
if (errorCount > 0) {
148
throw new IllegalArgumentException("encountered " + errorCount + " errors");
149
}
150
}
151
}
152
153
/**
154
* Adds the given message to the log if we are in verbose mode.
155
*/
156
protected void log(String message) {
157
logImpl(message, false);
158
}
159
160
protected void logln(String message) {
161
logImpl(message, true);
162
}
163
164
protected void logln() {
165
logImpl(null, true);
166
}
167
168
private void logImpl(String message, boolean newline) {
169
if (verbose) {
170
if (message != null) {
171
indent(indentLevel + 1);
172
log.print(message);
173
}
174
if (newline) {
175
log.println();
176
}
177
}
178
}
179
180
protected void err(String message) {
181
errImpl(message, false);
182
}
183
184
protected void errln(String message) {
185
errImpl(message, true);
186
}
187
188
private void errImpl(String message, boolean newline) {
189
errorCount++;
190
indent(indentLevel + 1);
191
log.print(message);
192
if (newline) {
193
log.println();
194
}
195
log.flush();
196
197
if (!nothrow) {
198
throw new RuntimeException(message);
199
}
200
}
201
202
protected int getErrorCount() {
203
return errorCount;
204
}
205
206
protected void writeTestName(String testName) {
207
indent(indentLevel);
208
log.print(testName);
209
log.flush();
210
needLineFeed = true;
211
}
212
213
protected void writeTestResult(int count) {
214
if (!needLineFeed) {
215
indent(indentLevel);
216
log.print("}");
217
}
218
needLineFeed = false;
219
220
if (count != 0) {
221
log.println(" FAILED");
222
} else {
223
log.println(" Passed");
224
}
225
}
226
227
/*
228
* Returns a spece-delimited hex String.
229
*/
230
protected static String toHexString(String s) {
231
StringBuilder sb = new StringBuilder(" ");
232
233
for (int i = 0; i < s.length(); i++) {
234
sb.append(Integer.toHexString(s.charAt(i)));
235
sb.append(' ');
236
}
237
238
return sb.toString();
239
}
240
241
private void indent(int distance) {
242
if (needLineFeed) {
243
log.println(" {");
244
needLineFeed = false;
245
}
246
log.print(SPACES.substring(0, distance * 2));
247
}
248
249
/**
250
* Print a usage message for this test class.
251
*/
252
void usage() {
253
System.out.println(getClass().getName() +
254
": [-verbose] [-nothrow] [-exitcode] [-prompt] [test names]");
255
256
System.out.println(" Available test names:");
257
for (String methodName : testMethods.keySet()) {
258
System.out.println("\t" + methodName);
259
}
260
}
261
262
private boolean prompt;
263
private boolean nothrow;
264
protected boolean verbose;
265
private boolean exitCode;
266
private PrintWriter log;
267
private int indentLevel;
268
private boolean needLineFeed;
269
private int errorCount;
270
271
private final Map<String, Method> testMethods = new LinkedHashMap<>();
272
273
private static final String SPACES = " ";
274
}
275
276