Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/text/Format/MessageFormat/MessageRegression.java
47209 views
1
/*
2
* Copyright (c) 1997, 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
/*
25
* @test
26
* @bug 4031438 4058973 4074764 4094906 4104976 4105380 4106659 4106660 4106661
27
* 4111739 4112104 4113018 4114739 4114743 4116444 4118592 4118594 4120552
28
* 4142938 4169959 4232154 4293229
29
* @summary Regression tests for MessageFormat and associated classes
30
* @library /java/text/testlib
31
*/
32
/*
33
(C) Copyright Taligent, Inc. 1996 - All Rights Reserved
34
(C) Copyright IBM Corp. 1996 - All Rights Reserved
35
36
The original version of this source code and documentation is copyrighted and
37
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
38
provided under terms of a License Agreement between Taligent and Sun. This
39
technology is protected by multiple US and International patents. This notice and
40
attribution to Taligent may not be removed.
41
Taligent is a registered trademark of Taligent, Inc.
42
*/
43
44
import java.text.*;
45
import java.util.*;
46
import java.io.IOException;
47
import java.io.FileOutputStream;
48
import java.io.FileInputStream;
49
import java.io.ByteArrayInputStream;
50
import java.io.ByteArrayOutputStream;
51
import java.io.ObjectOutputStream;
52
import java.io.ObjectInputStream;
53
import java.io.Serializable;
54
55
public class MessageRegression extends IntlTest {
56
57
public static void main(String[] args) throws Exception {
58
new MessageRegression().run(args);
59
}
60
61
/* @bug 4074764
62
* Null exception when formatting pattern with MessageFormat
63
* with no parameters.
64
*/
65
public void Test4074764() {
66
String[] pattern = {"Message without param",
67
"Message with param:{0}",
68
"Longer Message with param {0}"};
69
//difference between the two param strings are that
70
//in the first one, the param position is within the
71
//length of the string without param while it is not so
72
//in the other case.
73
74
MessageFormat messageFormatter = new MessageFormat("");
75
76
try {
77
//Apply pattern with param and print the result
78
messageFormatter.applyPattern(pattern[1]);
79
Object[] params = {new String("BUG"), new Date()};
80
String tempBuffer = messageFormatter.format(params);
81
if (!tempBuffer.equals("Message with param:BUG"))
82
errln("MessageFormat with one param test failed.");
83
logln("Formatted with one extra param : " + tempBuffer);
84
85
//Apply pattern without param and print the result
86
messageFormatter.applyPattern(pattern[0]);
87
tempBuffer = messageFormatter.format(null);
88
if (!tempBuffer.equals("Message without param"))
89
errln("MessageFormat with no param test failed.");
90
logln("Formatted with no params : " + tempBuffer);
91
92
tempBuffer = messageFormatter.format(params);
93
if (!tempBuffer.equals("Message without param"))
94
errln("Formatted with arguments > subsitution failed. result = " + tempBuffer.toString());
95
logln("Formatted with extra params : " + tempBuffer);
96
//This statement gives an exception while formatting...
97
//If we use pattern[1] for the message with param,
98
//we get an NullPointerException in MessageFormat.java(617)
99
//If we use pattern[2] for the message with param,
100
//we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
101
//Both are due to maxOffset not being reset to -1
102
//in applyPattern() when the pattern does not
103
//contain any param.
104
} catch (Exception foo) {
105
errln("Exception when formatting with no params.");
106
}
107
}
108
109
/* @bug 4058973
110
* MessageFormat.toPattern has weird rounding behavior.
111
*/
112
public void Test4058973() {
113
114
MessageFormat fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
115
String pat = fmt.toPattern();
116
if (!pat.equals("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}")) {
117
errln("MessageFormat.toPattern failed");
118
}
119
}
120
/* @bug 4031438
121
* More robust message formats.
122
*/
123
public void Test4031438() {
124
String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}.";
125
String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.";
126
127
// If the current locale is hi_IN, skip this test case.
128
if (Locale.getDefault().equals(new Locale("hi", "IN"))) {
129
return;
130
}
131
132
MessageFormat messageFormatter = new MessageFormat("");
133
134
try {
135
logln("Apply with pattern : " + pattern1);
136
messageFormatter.applyPattern(pattern1);
137
Object[] params = {new Integer(7)};
138
String tempBuffer = messageFormatter.format(params);
139
if (!tempBuffer.equals("Impossible {1} has occurred -- status code is 7 and message is {2}."))
140
errln("Tests arguments < substitution failed. Formatted text=" +
141
"<" + tempBuffer + ">");
142
logln("Formatted with 7 : " + tempBuffer);
143
ParsePosition status = new ParsePosition(0);
144
Object[] objs = messageFormatter.parse(tempBuffer, status);
145
if (objs[params.length] != null)
146
errln("Parse failed with more than expected arguments");
147
for (int i = 0; i < objs.length; i++) {
148
if (objs[i] != null && !objs[i].toString().equals(params[i].toString())) {
149
errln("Parse failed on object " + objs[i] + " at index : " + i);
150
}
151
}
152
tempBuffer = messageFormatter.format(null);
153
if (!tempBuffer.equals("Impossible {1} has occurred -- status code is {0} and message is {2}."))
154
errln("Tests with no arguments failed");
155
logln("Formatted with null : " + tempBuffer);
156
logln("Apply with pattern : " + pattern2);
157
messageFormatter.applyPattern(pattern2);
158
tempBuffer = messageFormatter.format(params);
159
if (!tempBuffer.equals("Double ' Quotes 7 test and quoted {1} test plus other {2} stuff."))
160
errln("quote format test (w/ params) failed.");
161
logln("Formatted with params : " + tempBuffer);
162
tempBuffer = messageFormatter.format(null);
163
if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
164
errln("quote format test (w/ null) failed.");
165
logln("Formatted with null : " + tempBuffer);
166
logln("toPattern : " + messageFormatter.toPattern());
167
} catch (Exception foo) {
168
errln("Exception when formatting in bug 4031438. "+foo.getMessage());
169
}
170
}
171
public void Test4052223()
172
{
173
ParsePosition pos = new ParsePosition(0);
174
if (pos.getErrorIndex() != -1) {
175
errln("ParsePosition.getErrorIndex initialization failed.");
176
}
177
MessageFormat fmt = new MessageFormat("There are {0} apples growing on the {1} tree.");
178
String str = new String("There is one apple growing on the peach tree.");
179
Object[] objs = fmt.parse(str, pos);
180
logln("unparsable string , should fail at " + pos.getErrorIndex());
181
if (pos.getErrorIndex() == -1)
182
errln("Bug 4052223 failed : parsing string " + str);
183
pos.setErrorIndex(4);
184
if (pos.getErrorIndex() != 4)
185
errln("setErrorIndex failed, got " + pos.getErrorIndex() + " instead of 4");
186
ChoiceFormat f = new ChoiceFormat(
187
"-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.");
188
pos.setIndex(0); pos.setErrorIndex(-1);
189
Number obj = f.parse("are negative", pos);
190
if (pos.getErrorIndex() != -1 && obj.doubleValue() == -1.0)
191
errln("Parse with \"are negative\" failed, at " + pos.getErrorIndex());
192
pos.setIndex(0); pos.setErrorIndex(-1);
193
obj = f.parse("are no or fraction ", pos);
194
if (pos.getErrorIndex() != -1 && obj.doubleValue() == 0.0)
195
errln("Parse with \"are no or fraction\" failed, at " + pos.getErrorIndex());
196
pos.setIndex(0); pos.setErrorIndex(-1);
197
obj = f.parse("go postal", pos);
198
if (pos.getErrorIndex() == -1 && !Double.isNaN(obj.doubleValue()))
199
errln("Parse with \"go postal\" failed, at " + pos.getErrorIndex());
200
}
201
/* @bug 4104976
202
* ChoiceFormat.equals(null) throws NullPointerException
203
*/
204
public void Test4104976()
205
{
206
double[] limits = {1, 20};
207
String[] formats = {"xyz", "abc"};
208
ChoiceFormat cf = new ChoiceFormat(limits, formats);
209
try {
210
log("Compares to null is always false, returned : ");
211
logln(cf.equals(null) ? "TRUE" : "FALSE");
212
} catch (Exception foo) {
213
errln("ChoiceFormat.equals(null) throws exception.");
214
}
215
}
216
/* @bug 4106659
217
* ChoiceFormat.ctor(double[], String[]) doesn't check
218
* whether lengths of input arrays are equal.
219
*/
220
public void Test4106659()
221
{
222
double[] limits = {1, 2, 3};
223
String[] formats = {"one", "two"};
224
ChoiceFormat cf = null;
225
try {
226
cf = new ChoiceFormat(limits, formats);
227
} catch (Exception foo) {
228
logln("ChoiceFormat constructor should check for the array lengths");
229
cf = null;
230
}
231
if (cf != null) errln(cf.format(5));
232
}
233
234
/* @bug 4106660
235
* ChoiceFormat.ctor(double[], String[]) allows unordered double array.
236
* This is not a bug, added javadoc to emphasize the use of limit
237
* array must be in ascending order.
238
*/
239
public void Test4106660()
240
{
241
double[] limits = {3, 1, 2};
242
String[] formats = {"Three", "One", "Two"};
243
ChoiceFormat cf = new ChoiceFormat(limits, formats);
244
double d = 5.0;
245
String str = cf.format(d);
246
if (!str.equals("Two"))
247
errln("format(" + d + ") = " + cf.format(d));
248
}
249
250
/* @bug 4111739
251
* MessageFormat is incorrectly serialized/deserialized.
252
*/
253
public void Test4111739()
254
{
255
MessageFormat format1 = null;
256
MessageFormat format2 = null;
257
ObjectOutputStream ostream = null;
258
ByteArrayOutputStream baos = null;
259
ObjectInputStream istream = null;
260
261
try {
262
baos = new ByteArrayOutputStream();
263
ostream = new ObjectOutputStream(baos);
264
} catch(IOException e) {
265
errln("Unexpected exception : " + e.getMessage());
266
return;
267
}
268
269
try {
270
format1 = new MessageFormat("pattern{0}");
271
ostream.writeObject(format1);
272
ostream.flush();
273
274
byte bytes[] = baos.toByteArray();
275
276
istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
277
format2 = (MessageFormat)istream.readObject();
278
} catch(Exception e) {
279
errln("Unexpected exception : " + e.getMessage());
280
}
281
282
if (!format1.equals(format2)) {
283
errln("MessageFormats before and after serialization are not" +
284
" equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
285
format2 + "(" + format2.toPattern() + ")");
286
} else {
287
logln("Serialization for MessageFormat is OK.");
288
}
289
}
290
/* @bug 4114743
291
* MessageFormat.applyPattern allows illegal patterns.
292
*/
293
public void Test4114743()
294
{
295
String originalPattern = "initial pattern";
296
MessageFormat mf = new MessageFormat(originalPattern);
297
try {
298
String illegalPattern = "ab { '}' de";
299
mf.applyPattern(illegalPattern);
300
errln("illegal pattern: \"" + illegalPattern + "\"");
301
} catch (IllegalArgumentException foo) {
302
if (!originalPattern.equals(mf.toPattern()))
303
errln("pattern after: \"" + mf.toPattern() + "\"");
304
}
305
}
306
307
/* @bug 4116444
308
* MessageFormat.parse has different behavior in case of null.
309
*/
310
public void Test4116444()
311
{
312
String[] patterns = {"", "one", "{0,date,short}"};
313
MessageFormat mf = new MessageFormat("");
314
315
for (int i = 0; i < patterns.length; i++) {
316
String pattern = patterns[i];
317
mf.applyPattern(pattern);
318
try {
319
Object[] array = mf.parse(null, new ParsePosition(0));
320
logln("pattern: \"" + pattern + "\"");
321
log(" parsedObjects: ");
322
if (array != null) {
323
log("{");
324
for (int j = 0; j < array.length; j++) {
325
if (array[j] != null)
326
err("\"" + array[j].toString() + "\"");
327
else
328
log("null");
329
if (j < array.length - 1) log(",");
330
}
331
log("}") ;
332
} else {
333
log("null");
334
}
335
logln("");
336
} catch (Exception e) {
337
errln("pattern: \"" + pattern + "\"");
338
errln(" Exception: " + e.getMessage());
339
}
340
}
341
342
}
343
/* @bug 4114739 (FIX and add javadoc)
344
* MessageFormat.format has undocumented behavior about empty format objects.
345
*/
346
public void Test4114739()
347
{
348
349
MessageFormat mf = new MessageFormat("<{0}>");
350
Object[] objs1 = null;
351
Object[] objs2 = {};
352
Object[] objs3 = {null};
353
try {
354
logln("pattern: \"" + mf.toPattern() + "\"");
355
log("format(null) : ");
356
logln("\"" + mf.format(objs1) + "\"");
357
log("format({}) : ");
358
logln("\"" + mf.format(objs2) + "\"");
359
log("format({null}) :");
360
logln("\"" + mf.format(objs3) + "\"");
361
} catch (Exception e) {
362
errln("Exception thrown for null argument tests.");
363
}
364
}
365
366
/* @bug 4113018
367
* MessageFormat.applyPattern works wrong with illegal patterns.
368
*/
369
public void Test4113018()
370
{
371
String originalPattern = "initial pattern";
372
MessageFormat mf = new MessageFormat(originalPattern);
373
String illegalPattern = "format: {0, xxxYYY}";
374
logln("pattern before: \"" + mf.toPattern() + "\"");
375
logln("illegal pattern: \"" + illegalPattern + "\"");
376
try {
377
mf.applyPattern(illegalPattern);
378
errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern);
379
} catch (IllegalArgumentException e) {
380
if (!originalPattern.equals(mf.toPattern()))
381
errln("pattern after: \"" + mf.toPattern() + "\"");
382
}
383
}
384
/* @bug 4106661
385
* ChoiceFormat is silent about the pattern usage in javadoc.
386
*/
387
public void Test4106661()
388
{
389
ChoiceFormat fmt = new ChoiceFormat(
390
"-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.");
391
logln("Formatter Pattern : " + fmt.toPattern());
392
393
logln("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
394
logln("Format with -1.0 : " + fmt.format(-1.0));
395
logln("Format with 0 : " + fmt.format(0));
396
logln("Format with 0.9 : " + fmt.format(0.9));
397
logln("Format with 1.0 : " + fmt.format(1));
398
logln("Format with 1.5 : " + fmt.format(1.5));
399
logln("Format with 2 : " + fmt.format(2));
400
logln("Format with 2.1 : " + fmt.format(2.1));
401
logln("Format with NaN : " + fmt.format(Double.NaN));
402
logln("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
403
}
404
/* @bug 4094906
405
* ChoiceFormat should accept \u221E as eq. to INF.
406
*/
407
public void Test4094906()
408
{
409
ChoiceFormat fmt = new ChoiceFormat(
410
"-\u221E<are negative|0<are no or fraction|1#is one|1.0<is 1+|\u221E<are many.");
411
if (!fmt.toPattern().startsWith("-\u221E<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|\u221E<are many."))
412
errln("Formatter Pattern : " + fmt.toPattern());
413
logln("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
414
logln("Format with -1.0 : " + fmt.format(-1.0));
415
logln("Format with 0 : " + fmt.format(0));
416
logln("Format with 0.9 : " + fmt.format(0.9));
417
logln("Format with 1.0 : " + fmt.format(1));
418
logln("Format with 1.5 : " + fmt.format(1.5));
419
logln("Format with 2 : " + fmt.format(2));
420
logln("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
421
}
422
423
/* @bug 4118592
424
* MessageFormat.parse fails with ChoiceFormat.
425
*/
426
public void Test4118592()
427
{
428
MessageFormat mf = new MessageFormat("");
429
String pattern = "{0,choice,1#YES|2#NO}";
430
String prefix = "";
431
for (int i = 0; i < 5; i++) {
432
String formatted = prefix + "YES";
433
mf.applyPattern(prefix + pattern);
434
prefix += "x";
435
Object[] objs = mf.parse(formatted, new ParsePosition(0));
436
logln(i + ". pattern :\"" + mf.toPattern() + "\"");
437
log(" \"" + formatted + "\" parsed as ");
438
if (objs == null) logln(" null");
439
else logln(" " + objs[0]);
440
}
441
}
442
/* @bug 4118594
443
* MessageFormat.parse fails for some patterns.
444
*/
445
public void Test4118594()
446
{
447
MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
448
String forParsing = "x, y, z";
449
Object[] objs = mf.parse(forParsing, new ParsePosition(0));
450
logln("pattern: \"" + mf.toPattern() + "\"");
451
logln("text for parsing: \"" + forParsing + "\"");
452
if (!objs[0].toString().equals("z"))
453
errln("argument0: \"" + objs[0] + "\"");
454
mf.setLocale(Locale.US);
455
mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
456
Object[] oldobjs = {new Double(3.1415)};
457
String result = mf.format( oldobjs );
458
logln("pattern: \"" + mf.toPattern() + "\"");
459
logln("text for parsing: \"" + result + "\"");
460
// result now equals "3.14, 3.1"
461
if (!result.equals("3.14, 3.1"))
462
errln("result = " + result);
463
Object[] newobjs = mf.parse(result, new ParsePosition(0));
464
// newobjs now equals {new Double(3.1)}
465
if (((Double)newobjs[0]).doubleValue() != 3.1)
466
errln( "newobjs[0] = " + newobjs[0]);
467
}
468
/* @bug 4105380
469
* When using ChoiceFormat, MessageFormat is not good for I18n.
470
*/
471
public void Test4105380()
472
{
473
String patternText1 = "The disk \"{1}\" contains {0}.";
474
String patternText2 = "There are {0} on the disk \"{1}\"";
475
MessageFormat form1 = new MessageFormat(patternText1);
476
MessageFormat form2 = new MessageFormat(patternText2);
477
double[] filelimits = {0,1,2};
478
String[] filepart = {"no files","one file","{0,number} files"};
479
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
480
form1.setFormat(1, fileform);
481
form2.setFormat(0, fileform);
482
Object[] testArgs = {new Long(12373), "MyDisk"};
483
logln(form1.format(testArgs));
484
logln(form2.format(testArgs));
485
}
486
/* @bug 4120552
487
* MessageFormat.parse incorrectly sets errorIndex.
488
*/
489
public void Test4120552()
490
{
491
MessageFormat mf = new MessageFormat("pattern");
492
String texts[] = {"pattern", "pat", "1234"};
493
logln("pattern: \"" + mf.toPattern() + "\"");
494
for (int i = 0; i < texts.length; i++) {
495
ParsePosition pp = new ParsePosition(0);
496
Object[] objs = mf.parse(texts[i], pp);
497
log(" text for parsing: \"" + texts[i] + "\"");
498
if (objs == null) {
499
logln(" (incorrectly formatted string)");
500
if (pp.getErrorIndex() == -1)
501
errln("Incorrect error index: " + pp.getErrorIndex());
502
} else {
503
logln(" (correctly formatted string)");
504
}
505
}
506
}
507
508
/**
509
* @bug 4142938
510
* MessageFormat handles single quotes in pattern wrong.
511
* This is actually a problem in ChoiceFormat; it doesn't
512
* understand single quotes.
513
*/
514
public void Test4142938() {
515
String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 " +
516
"{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " +
517
"personnel{0,choice,0#s|1#|2#s}.";
518
MessageFormat mf = new MessageFormat(pat);
519
520
String[] PREFIX = {
521
"'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.",
522
"'Vous' avez s\u00E9lectionne\u00E9 ",
523
"'Vous' avez s\u00E9lectionne\u00E9 "
524
};
525
String[] SUFFIX = {
526
null,
527
" client personnel.",
528
" clients personnels."
529
};
530
531
for (int i=0; i<3; i++) {
532
String out = mf.format(new Object[]{new Integer(i)});
533
if (SUFFIX[i] == null) {
534
if (!out.equals(PREFIX[i]))
535
errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
536
}
537
else {
538
if (!out.startsWith(PREFIX[i]) ||
539
!out.endsWith(SUFFIX[i]))
540
errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
541
SUFFIX[i] + "\"");
542
}
543
}
544
}
545
546
/**
547
* @bug 4142938
548
* Test the applyPattern and toPattern handling of single quotes
549
* by ChoiceFormat. (This is in here because this was a bug reported
550
* against MessageFormat.) The single quote is used to quote the
551
* pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
552
* is a quote literal.
553
*/
554
public void TestChoicePatternQuote() {
555
String[] DATA = {
556
// Pattern 0 value 1 value
557
"0#can''t|1#can", "can't", "can",
558
"0#'pound(#)=''#'''|1#xyz", "pound(#)='#'", "xyz",
559
"0#'1<2 | 1\u22641'|1#''", "1<2 | 1\u22641", "'",
560
};
561
for (int i=0; i<DATA.length; i+=3) {
562
try {
563
ChoiceFormat cf = new ChoiceFormat(DATA[i]);
564
for (int j=0; j<=1; ++j) {
565
String out = cf.format(j);
566
if (!out.equals(DATA[i+1+j]))
567
errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " +
568
out + "; want \"" + DATA[i+1+j] + '"');
569
}
570
String pat = cf.toPattern();
571
String pat2 = new ChoiceFormat(pat).toPattern();
572
if (!pat.equals(pat2))
573
errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
574
else
575
logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
576
}
577
catch (IllegalArgumentException e) {
578
errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
579
}
580
}
581
}
582
583
/**
584
* @bug 4112104
585
* MessageFormat.equals(null) throws a NullPointerException. The JLS states
586
* that it should return false.
587
*/
588
public void Test4112104() {
589
MessageFormat format = new MessageFormat("");
590
try {
591
// This should NOT throw an exception
592
if (format.equals(null)) {
593
// It also should return false
594
errln("MessageFormat.equals(null) returns false");
595
}
596
}
597
catch (NullPointerException e) {
598
errln("MessageFormat.equals(null) throws " + e);
599
}
600
}
601
602
/**
603
* @bug 4169959
604
* MessageFormat does not format null objects. CANNOT REPRODUCE THIS BUG.
605
*/
606
public void Test4169959() {
607
// This works
608
logln(MessageFormat.format( "This will {0}",
609
new String[]{"work"} ) );
610
611
// This fails
612
logln(MessageFormat.format( "This will {0}",
613
new Object[]{ null } ) );
614
}
615
616
public void test4232154() {
617
boolean gotException = false;
618
try {
619
MessageFormat format = new MessageFormat("The date is {0:date}");
620
} catch (Exception e) {
621
gotException = true;
622
if (!(e instanceof IllegalArgumentException)) {
623
throw new RuntimeException("got wrong exception type");
624
}
625
if ("argument number too large at ".equals(e.getMessage())) {
626
throw new RuntimeException("got wrong exception message");
627
}
628
}
629
if (!gotException) {
630
throw new RuntimeException("didn't get exception for invalid input");
631
}
632
}
633
634
public void test4293229() {
635
MessageFormat format = new MessageFormat("'''{'0}'' '''{0}'''");
636
Object[] args = { null };
637
String expected = "'{0}' '{0}'";
638
String result = format.format(args);
639
if (!result.equals(expected)) {
640
throw new RuntimeException("wrong format result - expected \"" +
641
expected + "\", got \"" + result + "\"");
642
}
643
}
644
}
645
646