Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Formatter/Constructors.java
41149 views
1
/*
2
* Copyright (c) 2004, 2011, 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
/* @test
25
* @bug 4981811 4984465 5064492 6240171 7000511
26
* @summary Unit test for all constructors introduced by the formatter feature
27
*/
28
29
import java.io.*;
30
import java.util.*;
31
import java.nio.charset.Charset;
32
33
public class Constructors {
34
35
private static int fail = 0;
36
private static int pass = 0;
37
38
private static Throwable first;
39
40
static void pass() {
41
pass++;
42
}
43
44
static void fail(String fs) {
45
String s = "'" + fs + "': exception not thrown";
46
if (first == null)
47
first = new RuntimeException(s);
48
System.err.println("FAILED: " + s);
49
fail++;
50
}
51
52
static void fail(String fs, Throwable ex) {
53
String s = "'" + fs + "': " + ex.getClass().getName() + " thrown";
54
if (first == null)
55
first = ex;
56
System.err.println("FAILED: " + s);
57
fail++;
58
}
59
60
static void locale(Formatter f) {
61
locale(f, Locale.getDefault(Locale.Category.FORMAT));
62
}
63
64
static void locale(Formatter f, Locale l) {
65
try {
66
if ((l != null && !l.equals(f.locale()))
67
|| (l == null && f.locale() != null))
68
throw new RuntimeException(f.locale() + " != " + l);
69
pass();
70
} catch (RuntimeException x) {
71
fail(x.getMessage());
72
}
73
}
74
75
static void out(Formatter f, Class c) {
76
try {
77
Appendable a = f.out();
78
if (!c.isInstance(a))
79
throw new RuntimeException(a.getClass().getName()
80
+ " != " + c.getName());
81
pass();
82
} catch (RuntimeException x) {
83
fail(x.getMessage());
84
}
85
}
86
87
public static void main(String [] args) {
88
// Formatter()
89
try (Formatter f = new Formatter()) {
90
pass();
91
out(f, StringBuilder.class);
92
locale(f);
93
} catch (Exception x) {
94
fail("new Formatter()", x);
95
}
96
97
// Formatter(Appendable a)
98
try (Formatter f = new Formatter((Appendable) null)) {
99
pass();
100
out(f, StringBuilder.class);
101
locale(f);
102
} catch (Exception x) {
103
fail("new Formatter((Appendable)null)", x);
104
}
105
106
// Formatter(Locale l)
107
try (Formatter f = new Formatter((Locale) null)) {
108
pass();
109
out(f, StringBuilder.class);
110
locale(f, null);
111
} catch (Exception x) {
112
fail("new Formatter((Locale)null)", x);
113
}
114
115
// Formatter(Appendable a, Locale l)
116
try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {
117
pass();
118
out(f, StringBuilder.class);
119
locale(f, null);
120
} catch (Exception x) {
121
fail("new Formatter((Appendable) null, (Locale) null)", x);
122
}
123
124
// Formatter(String fileName)
125
try (Formatter f = new Formatter("foo")) {
126
pass();
127
out(f, BufferedWriter.class);
128
locale(f);
129
} catch (Exception x) {
130
fail("new Formatter(\"foo\")", x);
131
}
132
133
try {
134
new Formatter((String)null);
135
fail("new Formatter((String)null)");
136
} catch (NullPointerException x) {
137
pass();
138
} catch (Exception x) {
139
fail("new Formatter((String)null)", x);
140
}
141
142
// Formatter(String fileName, String csn)
143
try (Formatter f = new Formatter("foo", "UTF-8")) {
144
pass();
145
out(f, BufferedWriter.class);
146
locale(f);
147
} catch (Exception x) {
148
fail("new Formatter(\"foo\", \"UTF-8\")", x);
149
}
150
151
try {
152
new Formatter("foo", "bar");
153
fail("new Formatter(\"foo\", \"bar\")");
154
} catch (UnsupportedEncodingException x) {
155
pass();
156
} catch (Exception x) {
157
fail("new Formatter(\"foo\", \"bar\")", x);
158
}
159
160
try {
161
new Formatter(".", "bar");
162
fail("new Formatter(\".\", \"bar\")");
163
} catch (FileNotFoundException|UnsupportedEncodingException x) {
164
pass();
165
} catch (Exception x) {
166
fail("new Formatter(\".\", \"bar\")", x);
167
}
168
169
// Formatter(String fileName, String csn, Locale l)
170
try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {
171
pass();
172
out(f, BufferedWriter.class);
173
locale(f, Locale.GERMANY);
174
} catch (Exception x) {
175
fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);
176
}
177
178
try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {
179
pass();
180
locale(f, null);
181
out(f, BufferedWriter.class);
182
} catch (Exception x) {
183
fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);
184
}
185
186
// Formatter(File)
187
try (Formatter f = new Formatter(new File("foo"))) {
188
pass();
189
locale(f);
190
out(f, BufferedWriter.class);
191
} catch (Exception x) {
192
fail("new Formatter(new File(\"foo\")", x);
193
}
194
195
try {
196
new Formatter((File)null);
197
fail("new Formatter((File)null)");
198
} catch (NullPointerException x) {
199
pass();
200
} catch (Exception x) {
201
fail("new Formatter((File)null)", x);
202
}
203
204
// Formatter(PrintStream ps)
205
try {
206
// ambiguity detected at compile-time
207
Formatter f = new Formatter(System.out);
208
pass();
209
out(f, PrintStream.class);
210
locale(f);
211
} catch (Exception x) {
212
fail("new Formatter(System.out)", x);
213
}
214
215
try {
216
new Formatter((PrintStream) null);
217
fail("new Formatter((PrintStream) null)");
218
} catch (NullPointerException x) {
219
pass();
220
} catch (Exception x) {
221
fail("new Formatter((PrintStream) null)", x);
222
}
223
224
try (Formatter f = new Formatter(new PrintStream("foo"))) {
225
pass();
226
locale(f);
227
out(f, PrintStream.class);
228
} catch (FileNotFoundException x) {
229
fail("new Formatter(new PrintStream(\"foo\")", x);
230
} catch (Exception x) {
231
fail("new Formatter(new PrintStream(\"foo\")", x);
232
}
233
234
try (Formatter f = new Formatter(new PrintStream("foo"),
235
Locale.JAPANESE)) {
236
pass();
237
locale(f, Locale.JAPANESE);
238
out(f, PrintStream.class);
239
} catch (FileNotFoundException x) {
240
fail("new Formatter(new PrintStream(\"foo\")", x);
241
} catch (Exception x) {
242
fail("new Formatter(new PrintStream(\"foo\")", x);
243
}
244
245
try (PrintStream ps = new PrintStream("foo")) {
246
// The cast here is necessary to avoid an ambiguity error
247
// between Formatter(Appendable a, Locale l)
248
// and Formatter(OutputStream os, String csn)
249
new Formatter(ps, (String)null);
250
fail("new Formatter(new PrintStream(\"foo\"), (String)null)");
251
} catch (FileNotFoundException x) {
252
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
253
} catch (NullPointerException x) {
254
pass();
255
} catch (UnsupportedEncodingException x) {
256
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
257
} catch (Exception x) {
258
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
259
}
260
261
// The cast here is necessary to avoid an ambiguity error
262
// between Formatter(Appendable a, Locale l)
263
// and Formatter(OutputStream os, String csn)
264
try (Formatter f = new Formatter(new PrintStream("foo"),
265
(Locale)null)) {
266
pass();
267
locale(f, null);
268
out(f, PrintStream.class);
269
} catch (FileNotFoundException x) {
270
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
271
} catch (Exception x) {
272
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
273
}
274
275
try (Formatter f = new Formatter(new PrintStream("foo"),
276
Locale.KOREAN)) {
277
pass();
278
locale(f, Locale.KOREAN);
279
out(f, PrintStream.class);
280
} catch (FileNotFoundException x) {
281
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
282
} catch (Exception x) {
283
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
284
}
285
286
try (Formatter f = new Formatter(new PrintStream("foo"),
287
"UTF-16BE", null)) {
288
pass();
289
locale(f, null);
290
out(f, BufferedWriter.class);
291
} catch (FileNotFoundException x) {
292
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
293
} catch (UnsupportedEncodingException x) {
294
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
295
} catch (Exception x) {
296
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
297
}
298
299
try (Formatter f = new Formatter(new PrintStream("foo"),
300
"UTF-16BE", Locale.ITALIAN)) {
301
pass();
302
locale(f, Locale.ITALIAN);
303
out(f, BufferedWriter.class);
304
} catch (FileNotFoundException x) {
305
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
306
} catch (UnsupportedEncodingException x) {
307
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
308
} catch (Exception x) {
309
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
310
}
311
312
String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ?
313
"ASCII" : "ISO-8859-1";
314
try {
315
ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(),
316
new ByteArrayOutputStream(),
317
new ByteArrayOutputStream()
318
};
319
new Formatter((Appendable) new PrintStream(bs[0], true, csn)).format("\u00a3");
320
new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3");
321
new Formatter( new PrintStream(bs[2], true, csn)).format("\u00a3");
322
if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) {
323
fail("arrays shouldn't match: " + bs[0].toByteArray());
324
} else {
325
pass();
326
}
327
if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) {
328
fail("arrays should match: " + bs[0].toByteArray() + " != "
329
+ bs[2].toByteArray());
330
} else {
331
pass();
332
}
333
} catch (UnsupportedEncodingException x) {
334
fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x);
335
}
336
337
// Formatter(OutputStream os)
338
try {
339
new Formatter((OutputStream) null);
340
fail("new Formatter((OutputStream) null)");
341
} catch (NullPointerException x) {
342
pass();
343
} catch (Exception x) {
344
fail("new Formatter((OutputStream) null)", x);
345
}
346
347
try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {
348
pass();
349
locale(f);
350
out(f, BufferedWriter.class);
351
} catch (Exception x) {
352
fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);
353
}
354
355
// Formatter(OutputStream os, String csn)
356
try {
357
new Formatter((OutputStream) null, "ISO-8859-1");
358
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")");
359
} catch (NullPointerException x) {
360
pass();
361
} catch (Exception x) {
362
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);
363
}
364
365
try (PrintStream ps = new PrintStream("foo")) {
366
new Formatter((OutputStream) ps, null);
367
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");
368
} catch (NullPointerException x) {
369
pass();
370
} catch (Exception x) {
371
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",
372
x);
373
}
374
375
try (PrintStream ps = new PrintStream("foo")) {
376
new Formatter(ps, "bar");
377
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");
378
} catch (UnsupportedEncodingException x) {
379
pass();
380
} catch (Exception x) {
381
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);
382
}
383
384
try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {
385
pass();
386
locale(f);
387
out(f, BufferedWriter.class);
388
} catch (Exception x) {
389
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);
390
}
391
392
// Formatter(OutputStream os, String csn, Locale l)
393
try {
394
new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK);
395
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)");
396
} catch (NullPointerException x) {
397
pass();
398
} catch (Exception x) {
399
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",
400
x);
401
}
402
403
try (PrintStream ps = new PrintStream("foo")) {
404
new Formatter(ps, (String)null, Locale.UK);
405
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)");
406
} catch (NullPointerException x) {
407
pass();
408
} catch (Exception x) {
409
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)",
410
x);
411
}
412
413
// Formatter(OutputStream os, Charset charset, Locale l)
414
try (PrintStream ps = new PrintStream("foo")) {
415
new Formatter(ps, (Charset)null, Locale.UK);
416
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)");
417
} catch (NullPointerException x) {
418
pass();
419
} catch (Exception x) {
420
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)",
421
x);
422
}
423
424
try (PrintStream ps = new PrintStream("foo")) {
425
new Formatter(ps, "bar", Locale.UK);
426
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");
427
} catch (UnsupportedEncodingException x) {
428
pass();
429
} catch (Exception x) {
430
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",
431
x);
432
}
433
434
try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {
435
pass();
436
out(f, BufferedWriter.class);
437
locale(f, Locale.UK);
438
} catch (Exception x) {
439
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",
440
x);
441
}
442
443
// PrintStream(String fileName)
444
try (PrintStream ps = new PrintStream("foo")) {
445
pass();
446
} catch (Exception x) {
447
fail("new PrintStream(\"foo\")", x);
448
}
449
450
// PrintStream(String fileName, String csn)
451
try {
452
new PrintStream("foo", (String)null);
453
fail("new PrintStream(\"foo\", (String)null)");
454
} catch (NullPointerException x) {
455
pass();
456
} catch (Exception x) {
457
fail("new PrintStream(\"foo\", (String)null)", x);
458
}
459
460
// PrintStream(String fileName, Charset charset)
461
try {
462
new PrintStream("foo", (Charset)null);
463
fail("new PrintStream(\"foo\", (Charset)null)");
464
} catch (NullPointerException x) {
465
pass();
466
} catch (Exception x) {
467
fail("new PrintStream(\"foo\", (Charset)null)", x);
468
}
469
470
// PrintStream(File file)
471
try (PrintStream ps = new PrintStream(new File("foo"))) {
472
pass();
473
} catch (Exception x) {
474
fail("new PrintStream(new File(\"foo\"))", x);
475
}
476
477
// PrintStream(File file, String csn)
478
try {
479
new PrintStream(new File("foo"), (String)null);
480
fail("new PrintStream(new File(\"foo\"), (String)null)");
481
} catch (NullPointerException x) {
482
pass();
483
} catch (Exception x) {
484
fail("new PrintStream(new File(\"foo\"), (String)null)", x);
485
}
486
487
// PrintStream(File file, Charset charset)
488
try {
489
new PrintStream(new File("foo"), (Charset)null);
490
fail("new PrintStream(new File(\"foo\"), (Charset)null)");
491
} catch (NullPointerException x) {
492
pass();
493
} catch (Exception x) {
494
fail("new PrintStream(new File(\"foo\"), (Charset)null)", x);
495
}
496
497
// PrintWriter(String fileName)
498
try (PrintWriter pw = new PrintWriter("foo")) {
499
pass();
500
} catch (Exception x) {
501
fail("new PrintWriter(\"foo\")", x);
502
}
503
504
// PrintWriter(String fileName, String csn)
505
try {
506
new PrintWriter("foo", (String)null);
507
fail("new PrintWriter(\"foo\"), (String)null");
508
} catch (NullPointerException x) {
509
pass();
510
} catch (Exception x) {
511
fail("new PrintWriter(\"foo\"), (String)null", x);
512
}
513
514
// PrintWriter(String fileName, Charset charset)
515
try {
516
new PrintWriter("foo", (Charset)null);
517
fail("new PrintWriter(\"foo\"), (Charset)null");
518
} catch (NullPointerException x) {
519
pass();
520
} catch (Exception x) {
521
fail("new PrintWriter(\"foo\"), (Charset)null", x);
522
}
523
524
// PrintWriter(File file)
525
try (PrintWriter pw = new PrintWriter(new File("foo"))) {
526
pass();
527
} catch (Exception x) {
528
fail("new PrintWriter(new File(\"foo\"))", x);
529
}
530
531
// PrintWriter(File file, String csn)
532
try {
533
new PrintWriter(new File("foo"), (String)null);
534
fail("new PrintWriter(new File(\"foo\")), (String)null");
535
} catch (NullPointerException x) {
536
pass();
537
} catch (Exception x) {
538
fail("new PrintWriter(new File(\"foo\")), (String)null", x);
539
}
540
541
// PrintWriter(File file, Charset charset)
542
try {
543
new PrintWriter(new File("foo"), (Charset)null);
544
fail("new PrintWriter(new File(\"foo\")), (Charset)null");
545
} catch (NullPointerException x) {
546
pass();
547
} catch (Exception x) {
548
fail("new PrintWriter(new File(\"foo\")), (Charset)null", x);
549
}
550
551
if (fail != 0)
552
throw new RuntimeException((fail + pass) + " tests: "
553
+ fail + " failure(s), first", first);
554
else
555
System.out.println("all " + (fail + pass) + " tests passed");
556
}
557
}
558
559