• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.harmony.tests.java.text;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.text.ChoiceFormat;
26 import java.text.DateFormat;
27 import java.text.DecimalFormat;
28 import java.text.DecimalFormatSymbols;
29 import java.text.FieldPosition;
30 import java.text.Format;
31 import java.text.MessageFormat;
32 import java.text.NumberFormat;
33 import java.text.ParseException;
34 import java.text.ParsePosition;
35 import java.text.SimpleDateFormat;
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.Locale;
40 import java.util.TimeZone;
41 
42 import junit.framework.TestCase;
43 
44 public class MessageFormatTest extends TestCase {
45 
46   private MessageFormat format1, format2, format3;
47 
48   private Locale defaultLocale;
49 
checkSerialization(MessageFormat format)50   private void checkSerialization(MessageFormat format) {
51     try {
52       ByteArrayOutputStream ba = new ByteArrayOutputStream();
53       ObjectOutputStream out = new ObjectOutputStream(ba);
54       out.writeObject(format);
55       out.close();
56       ObjectInputStream in = new ObjectInputStream(
57           new ByteArrayInputStream(ba.toByteArray()));
58       MessageFormat read = (MessageFormat) in.readObject();
59       assertTrue("Not equal: " + format.toPattern(), format.equals(read));
60     } catch (IOException e) {
61       fail("Format: " + format.toPattern()
62           + " caused IOException: " + e);
63     } catch (ClassNotFoundException e) {
64       fail("Format: " + format.toPattern()
65           + " caused ClassNotFoundException: " + e);
66     }
67   }
68 
test_formatToCharacterIteratorLjava_lang_Object()69   public void test_formatToCharacterIteratorLjava_lang_Object() {
70     new Support_MessageFormat("test_formatToCharacterIteratorLjava_lang_Object").t_formatToCharacterIterator();
71 
72     try {
73       new MessageFormat("{1, number}").formatToCharacterIterator(null);
74       fail();
75     } catch (NullPointerException expected) {
76     }
77 
78     try {
79       new MessageFormat("{0, time}").formatToCharacterIterator(new Object[]{""});
80       fail();
81     } catch (IllegalArgumentException expected) {
82     }
83   }
84 
85 
test_getLocale()86   public void test_getLocale() throws Exception {
87     Locale[] l = {
88       Locale.FRANCE,
89       Locale.KOREA,
90       new Locale("FR", "fr"), // Deliberately backwards.
91       new Locale("mk"),
92       new Locale("mk", "MK"),
93       Locale.US,
94       new Locale("#ru", "@31230") // Deliberately nonsense.
95     };
96 
97     String pattern = "getLocale test {0,number,#,####}";
98 
99     for (int i = 0; i < 0; i++) {
100       MessageFormat mf = new MessageFormat(pattern, l[i]);
101       Locale result = mf.getLocale();
102       assertEquals(l[i], result);
103       assertEquals(l[i].getLanguage(), result.getLanguage());
104       assertEquals(l[i].getCountry(), result.getCountry());
105     }
106 
107     MessageFormat mf = new MessageFormat(pattern);
108     mf.setLocale(null);
109     Locale result = mf.getLocale();
110     assertEquals(null, result);
111   }
112 
test_setFormatILjava_text_Format()113   public void test_setFormatILjava_text_Format() throws Exception {
114     // case 1: Compare getFormats() results after calls to setFormat()
115     MessageFormat f1 = (MessageFormat) format1.clone();
116     f1.setFormat(0, DateFormat.getTimeInstance());
117     f1.setFormat(1, DateFormat.getTimeInstance());
118     f1.setFormat(2, NumberFormat.getInstance());
119     f1.setFormat(3, new ChoiceFormat("0#off|1#on"));
120     f1.setFormat(4, new ChoiceFormat("1#few|2#ok|3#a lot"));
121     f1.setFormat(5, DateFormat.getTimeInstance());
122 
123     Format[] formats = f1.getFormats();
124     formats = f1.getFormats();
125 
126     Format[] correctFormats = new Format[] {
127       DateFormat.getTimeInstance(),
128       DateFormat.getTimeInstance(),
129       NumberFormat.getInstance(),
130       new ChoiceFormat("0#off|1#on"),
131       new ChoiceFormat("1#few|2#ok|3#a lot"),
132       DateFormat.getTimeInstance()
133     };
134 
135     assertEquals(correctFormats.length, formats.length);
136     for (int i = 0; i < correctFormats.length; i++) {
137       assertEquals("Test1B:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
138     }
139 
140     // case 2: Try to setFormat using incorrect index
141     try {
142       f1.setFormat(-1, DateFormat.getDateInstance());
143       fail();
144     } catch (ArrayIndexOutOfBoundsException expected) {
145     }
146     try {
147       f1.setFormat(f1.getFormats().length, DateFormat.getDateInstance());
148       fail();
149     } catch (ArrayIndexOutOfBoundsException expected) {
150     }
151   }
152 
test_parseObjectLjava_lang_StringLjavajava_text_ParsePosition()153   public void test_parseObjectLjava_lang_StringLjavajava_text_ParsePosition() throws Exception {
154     MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
155     // case 1: Try to parse correct data string.
156     Object[] objs = { Double.valueOf(3.1415) };
157     String result = mf.format(objs);
158     // result now equals "3.14, 3.1"
159     Object[] res = null;
160     ParsePosition pp = new ParsePosition(0);
161     int parseIndex = pp.getIndex();
162     res = (Object[]) mf.parseObject(result, pp);
163     assertTrue("Parse operation return null", res != null);
164     assertTrue("parse operation return array with incorrect length", 1 == res.length);
165     assertTrue("ParseIndex is incorrect", pp.getIndex() != parseIndex);
166     assertTrue("Result object is incorrect", Double.valueOf(3.1).equals(res[0]));
167 
168     // case 2: Try to parse partially correct data string.
169     pp.setIndex(0);
170     char[] cur = result.toCharArray();
171     cur[cur.length / 2] = 'Z';
172     String partialCorrect = new String(cur);
173     res = (Object[]) mf.parseObject(partialCorrect, pp);
174     assertTrue("Parse operation return null", res == null);
175     assertTrue("ParseIndex is incorrect", pp.getIndex() == 0);
176     assertTrue("ParseErrorIndex is incorrect", pp.getErrorIndex() == cur.length / 2);
177 
178     // case 3: Try to use argument ParsePosition as null.
179     try {
180       mf.parseObject(result, null);
181       fail();
182     } catch (NullPointerException expected) {
183     }
184   }
185 
test_parseLjava_lang_String()186   public void test_parseLjava_lang_String() throws ParseException {
187     // This test assumes a default DateFormat.is24Hour setting.
188     DateFormat.is24Hour = null;
189 
190     String pattern = "A {3, number, currency} B {2, time} C {0, number, percent} D {4}  E {1,choice,0#off|1#on} F {0, date}";
191     MessageFormat mf = new MessageFormat(pattern);
192     String sToParse = "A $12,345.00 B 9:56:07" + Support_MessageFormat.AM_PM_SPACE_CHAR
193             + "AM C 3,200% D 1/15/70 9:56 AM  E on F Jan 1, 1970";
194     Object[] result = mf.parse(sToParse);
195 
196     assertTrue("No result: " + result.length, result.length == 5);
197     assertTrue("Object 0 is not date", result[0] instanceof Date);
198     assertEquals("Object 1 is not stringr", result[1].toString(), "1.0");
199     assertTrue("Object 2 is not date", result[2] instanceof Date);
200     assertEquals("Object 3 is not number", result[3].toString(), "12345");
201     assertEquals("Object 4 is not string", result[4].toString(), "1/15/70 9:56 AM");
202 
203     sToParse = "xxdate is Feb 28, 1999";
204     try {
205       result = format1.parse(sToParse);
206       fail();
207     } catch (java.text.ParseException expected) {
208     }
209 
210     sToParse = "vm=Test, @3 4 6, 3   ";
211     mf = new MessageFormat("vm={0},{1},{2}");
212     result = mf.parse(sToParse);
213     assertTrue("No result: " + result.length, result.length == 3);
214     assertEquals("Object 0 is not string", result[0].toString(), "Test");
215     assertEquals("Object 1 is not string", result[1].toString(), " @3 4 6");
216     assertEquals("Object 2 is not string", result[2].toString(), " 3   ");
217 
218     try {
219       result = mf.parse(null);
220       fail();
221     } catch (java.text.ParseException expected) {
222     }
223   }
224 
test_setFormats$Ljava_text_Format()225   public void test_setFormats$Ljava_text_Format() throws Exception {
226     MessageFormat f1 = (MessageFormat) format1.clone();
227 
228     // case 1: Test with repeating formats and max argument index < max
229     // offset
230     // compare getFormats() results after calls to setFormats(Format[])
231     Format[] correctFormats = new Format[] {
232       DateFormat.getTimeInstance(),
233       new ChoiceFormat("0#off|1#on"),
234       DateFormat.getTimeInstance(),
235       NumberFormat.getCurrencyInstance(),
236       new ChoiceFormat("1#few|2#ok|3#a lot")
237     };
238 
239     f1.setFormats(correctFormats);
240     Format[] formats = f1.getFormats();
241 
242     assertTrue("Test1A:Returned wrong number of formats:", correctFormats.length <= formats.length);
243     for (int i = 0; i < correctFormats.length; i++) {
244       assertEquals("Test1B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
245     }
246 
247     // case 2: Try to pass null argument to setFormats().
248     try {
249       f1.setFormats(null);
250       fail();
251     } catch (NullPointerException expected) {
252     }
253   }
254 
test_formatLjava_lang_StringLjava_lang_Object()255   public void test_formatLjava_lang_StringLjava_lang_Object() {
256     TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
257     int iCurrency = 123;
258     int iInteger  = Integer.MIN_VALUE;
259 
260     Date     date     = new Date(12345678);
261     Object[] args     = { date, iCurrency, iInteger };
262     String   resStr   = "Date: Jan 1, 1970 Currency: $" + iCurrency + ".00 Integer: -2,147,483,648";
263     String   pattern  = "Date: {0,date} Currency: {1, number, currency} Integer: {2, number, integer}";
264     String   sFormat  = MessageFormat.format(pattern, (Object[]) args);
265     assertEquals(
266         "format(String, Object[]) with valid parameters returns incorrect string: case 1",
267         sFormat, resStr);
268 
269     pattern = "abc {4, number, integer} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}";
270     resStr  = "abc -2,147,483,648 def Jan 1, 1970 ghi -2,147,483,648 jkl high mnop -2,147,483,648";
271     Object[] args_ = { iInteger, 1, iInteger, date, iInteger };
272     sFormat = MessageFormat.format(pattern, args_);
273     assertEquals(
274         "format(String, Object[]) with valid parameters returns incorrect string: case 1",
275         sFormat, resStr);
276 
277     try {
278       args = null;
279       MessageFormat.format(null, args);
280       fail();
281     } catch (Exception expected) {
282     }
283 
284     try {
285       MessageFormat.format("Invalid {1,foobar} format descriptor!", new Object[] {iInteger} );
286       fail();
287     } catch (IllegalArgumentException expected) {
288     }
289 
290     try {
291       MessageFormat.format("Invalid {1,date,invalid-spec} format descriptor!", new Object[]{""});
292       fail();
293     } catch (IllegalArgumentException expected) {
294     }
295 
296     try {
297       MessageFormat.format("{0,number,integer", new Object[] {iInteger});
298       fail();
299     } catch (IllegalArgumentException expected) {
300     }
301 
302     try {
303       MessageFormat.format("Valid {1, date} format {0, number} descriptor!", new Object[]{ "" } );
304       fail();
305     } catch (IllegalArgumentException expected) {
306     }
307   }
308 
test_ConstructorLjava_lang_StringLjava_util_Locale()309   public void test_ConstructorLjava_lang_StringLjava_util_Locale() {
310     Locale mk = new Locale("mk", "MK");
311     MessageFormat format = new MessageFormat("Date: {0,date} Currency: {1, number, currency} Integer: {2, number, integer}", mk);
312     assertEquals(format.getLocale(), mk);
313     assertEquals(format.getFormats()[0], DateFormat.getDateInstance(DateFormat.DEFAULT, mk));
314     assertEquals(format.getFormats()[1], NumberFormat.getCurrencyInstance(mk));
315     assertEquals(format.getFormats()[2], NumberFormat.getIntegerInstance(mk));
316   }
317 
test_ConstructorLjava_lang_String()318   public void test_ConstructorLjava_lang_String() {
319     MessageFormat format = new MessageFormat(
320         "abc {4,time} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}");
321     assertTrue("Not a MessageFormat",
322                format.getClass() == MessageFormat.class);
323     Format[] formats = format.getFormats();
324     assertNotNull("null formats", formats);
325     assertTrue("Wrong format count: " + formats.length, formats.length >= 5);
326     assertTrue("Wrong time format", formats[0].equals(DateFormat
327                                                           .getTimeInstance()));
328     assertTrue("Wrong date format", formats[1].equals(DateFormat
329                                                           .getDateInstance()));
330     assertTrue("Wrong number format", formats[2].equals(NumberFormat
331                                                             .getInstance()));
332     assertTrue("Wrong choice format", formats[3].equals(new ChoiceFormat(
333                                                             "0.0#low|1.0#high")));
334     assertNull("Wrong string format", formats[4]);
335 
336     Date date = new Date();
337     FieldPosition pos = new FieldPosition(-1);
338     StringBuffer buffer = new StringBuffer();
339     format.format(new Object[] { "123", Double.valueOf(1.6), Double.valueOf(7.2),
340         date, date }, buffer, pos);
341     String result = buffer.toString();
342     buffer.setLength(0);
343     buffer.append("abc ");
344     buffer.append(DateFormat.getTimeInstance().format(date));
345     buffer.append(" def ");
346     buffer.append(DateFormat.getDateInstance().format(date));
347     buffer.append(" ghi ");
348     buffer.append(NumberFormat.getInstance().format(Double.valueOf(7.2)));
349     buffer.append(" jkl high mnop 123");
350     assertTrue("Wrong answer:\n" + result + "\n" + buffer, result
351                    .equals(buffer.toString()));
352 
353     assertEquals("Simple string", "Test message", new MessageFormat("Test message").format(
354         new Object[0]));
355 
356     result = new MessageFormat("Don't").format(new Object[0]);
357     assertTrue("Should not throw IllegalArgumentException: " + result,
358                "Dont".equals(result));
359 
360     try {
361       new MessageFormat("Invalid {1,foobar} format descriptor!");
362       fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
363     } catch (IllegalArgumentException ex) {
364       // expected
365     }
366 
367     try {
368       new MessageFormat(
369           "Invalid {1,date,invalid-spec} format descriptor!");
370     } catch (IllegalArgumentException ex) {
371       // expected
372     }
373 
374     checkSerialization(new MessageFormat(""));
375     checkSerialization(new MessageFormat("noargs"));
376     checkSerialization(new MessageFormat("{0}"));
377     checkSerialization(new MessageFormat("a{0}"));
378     checkSerialization(new MessageFormat("{0}b"));
379     checkSerialization(new MessageFormat("a{0}b"));
380 
381     // Regression for HARMONY-65
382     try {
383       new MessageFormat("{0,number,integer");
384       fail("Assert 0: Failed to detect unmatched brackets.");
385     } catch (IllegalArgumentException e) {
386       // expected
387     }
388   }
389 
test_applyPatternLjava_lang_String()390     public void test_applyPatternLjava_lang_String() {
391         MessageFormat format = new MessageFormat("test");
392         format.applyPattern("xx {0}");
393 		assertEquals("Invalid number", "xx 46", format.format(
394 				new Object[] { Integer.valueOf(46) }));
395         Date date = new Date();
396         String result = format.format(new Object[] { date });
397         String expected = "xx " + DateFormat.getInstance().format(date);
398         assertTrue("Invalid date:\n" + result + "\n" + expected, result
399                 .equals(expected));
400         format = new MessageFormat("{0,date}{1,time}{2,number,integer}");
401         format.applyPattern("nothing");
402 		assertEquals("Found formats", "nothing", format.toPattern());
403 
404         format.applyPattern("{0}");
405 		assertNull("Wrong format", format.getFormats()[0]);
406 		assertEquals("Wrong pattern", "{0}", format.toPattern());
407 
408         format.applyPattern("{0, \t\u001ftime }");
409         assertTrue("Wrong time format", format.getFormats()[0]
410                 .equals(DateFormat.getTimeInstance()));
411 		assertEquals("Wrong time pattern", "{0,time}", format.toPattern());
412         format.applyPattern("{0,Time, Short\n}");
413         assertTrue("Wrong short time format", format.getFormats()[0]
414                 .equals(DateFormat.getTimeInstance(DateFormat.SHORT)));
415 		assertEquals("Wrong short time pattern",
416 				"{0,time,short}", format.toPattern());
417         format.applyPattern("{0,TIME,\nmedium  }");
418         assertTrue("Wrong medium time format", format.getFormats()[0]
419                 .equals(DateFormat.getTimeInstance(DateFormat.MEDIUM)));
420 		assertEquals("Wrong medium time pattern",
421 				"{0,time}", format.toPattern());
422         format.applyPattern("{0,time,LONG}");
423         assertTrue("Wrong long time format", format.getFormats()[0]
424                 .equals(DateFormat.getTimeInstance(DateFormat.LONG)));
425 		assertEquals("Wrong long time pattern",
426 				"{0,time,long}", format.toPattern());
427         format.setLocale(Locale.FRENCH); // use French since English has the
428         // same LONG and FULL time patterns
429         format.applyPattern("{0,time, Full}");
430         assertTrue("Wrong full time format", format.getFormats()[0]
431                 .equals(DateFormat.getTimeInstance(DateFormat.FULL,
432                         Locale.FRENCH)));
433 		assertEquals("Wrong full time pattern",
434 				"{0,time,full}", format.toPattern());
435         format.setLocale(Locale.getDefault());
436 
437         format.applyPattern("{0, date}");
438         assertTrue("Wrong date format", format.getFormats()[0]
439                 .equals(DateFormat.getDateInstance()));
440 		assertEquals("Wrong date pattern", "{0,date}", format.toPattern());
441         format.applyPattern("{0, date, short}");
442         assertTrue("Wrong short date format", format.getFormats()[0]
443                 .equals(DateFormat.getDateInstance(DateFormat.SHORT)));
444 		assertEquals("Wrong short date pattern",
445 				"{0,date,short}", format.toPattern());
446         format.applyPattern("{0, date, medium}");
447         assertTrue("Wrong medium date format", format.getFormats()[0]
448                 .equals(DateFormat.getDateInstance(DateFormat.MEDIUM)));
449 		assertEquals("Wrong medium date pattern",
450 				"{0,date}", format.toPattern());
451         format.applyPattern("{0, date, long}");
452         assertTrue("Wrong long date format", format.getFormats()[0]
453                 .equals(DateFormat.getDateInstance(DateFormat.LONG)));
454 		assertEquals("Wrong long date pattern",
455 				"{0,date,long}", format.toPattern());
456         format.applyPattern("{0, date, full}");
457         assertTrue("Wrong full date format", format.getFormats()[0]
458                 .equals(DateFormat.getDateInstance(DateFormat.FULL)));
459 		assertEquals("Wrong full date pattern",
460 				"{0,date,full}", format.toPattern());
461 
462         format.applyPattern("{0, date, MMM d {hh:mm:ss}}");
463 		assertEquals("Wrong time/date format", " MMM d {hh:mm:ss}", ((SimpleDateFormat) (format
464 				.getFormats()[0])).toPattern());
465 		assertEquals("Wrong time/date pattern",
466 				"{0,date, MMM d {hh:mm:ss}}", format.toPattern());
467 
468         format.applyPattern("{0, number}");
469         assertTrue("Wrong number format", format.getFormats()[0]
470                 .equals(NumberFormat.getNumberInstance()));
471 		assertEquals("Wrong number pattern",
472 				"{0,number}", format.toPattern());
473         format.applyPattern("{0, number, currency}");
474         assertTrue("Wrong currency number format", format.getFormats()[0]
475                 .equals(NumberFormat.getCurrencyInstance()));
476 		assertEquals("Wrong currency number pattern",
477 				"{0,number,currency}", format.toPattern());
478         format.applyPattern("{0, number, percent}");
479         assertTrue("Wrong percent number format", format.getFormats()[0]
480                 .equals(NumberFormat.getPercentInstance()));
481 		assertEquals("Wrong percent number pattern",
482 				"{0,number,percent}", format.toPattern());
483         format.applyPattern("{0, number, integer}");
484 
485         DecimalFormat expectedNumberFormat = (DecimalFormat) NumberFormat.getIntegerInstance();
486         DecimalFormat actualNumberFormat = (DecimalFormat) format.getFormats()[0];
487         assertEquals(expectedNumberFormat.getDecimalFormatSymbols(), actualNumberFormat.getDecimalFormatSymbols());
488         assertEquals(expectedNumberFormat.isParseIntegerOnly(), actualNumberFormat.isParseIntegerOnly());
489         assertEquals("Wrong integer number pattern", "{0,number,integer}", format.toPattern());
490         assertEquals(expectedNumberFormat, format.getFormats()[0]);
491 
492         format.applyPattern("{0, number, {'#'}##0.0E0}");
493 
494         /*
495          * TODO validate these assertions
496          * String actual = ((DecimalFormat)(format.getFormats()[0])).toPattern();
497          * assertEquals("Wrong pattern number format", "' {#}'##0.0E0", actual);
498          * assertEquals("Wrong pattern number pattern", "{0,number,' {#}'##0.0E0}", format.toPattern());
499          *
500          */
501 
502         format.applyPattern("{0, choice,0#no|1#one|2#{1,number}}");
503 		assertEquals("Wrong choice format",
504 
505 						"0.0#no|1.0#one|2.0#{1,number}", ((ChoiceFormat) format.getFormats()[0]).toPattern());
506 		assertEquals("Wrong choice pattern",
507 				"{0,choice,0.0#no|1.0#one|2.0#{1,number}}", format.toPattern());
508 		assertEquals("Wrong formatted choice", "3.6", format.format(
509 				new Object[] { Integer.valueOf(2), Float.valueOf(3.6f) }));
510 
511         try {
512             format.applyPattern("WRONG MESSAGE FORMAT {0,number,{}");
513             fail("Expected IllegalArgumentException for invalid pattern");
514         } catch (IllegalArgumentException e) {
515         }
516 
517         // Regression for HARMONY-65
518         MessageFormat mf = new MessageFormat("{0,number,integer}");
519         String badpattern = "{0,number,#";
520         try {
521             mf.applyPattern(badpattern);
522             fail("Assert 0: Failed to detect unmatched brackets.");
523         } catch (IllegalArgumentException e) {
524             // expected
525         }
526     }
527 
test_clone()528     public void test_clone() {
529         MessageFormat format = new MessageFormat("'{'choice'}'{0}");
530         MessageFormat clone = (MessageFormat) format.clone();
531         assertTrue("Clone not equal", format.equals(clone));
532 		assertEquals("Wrong answer",
533 				"{choice}{0}", format.format(new Object[] {}));
534         clone.setFormat(0, DateFormat.getInstance());
535         assertTrue("Clone shares format data", !format.equals(clone));
536         format = (MessageFormat) clone.clone();
537         Format[] formats = clone.getFormats();
538         ((SimpleDateFormat) formats[0]).applyPattern("adk123");
539         assertTrue("Clone shares format data", !format.equals(clone));
540     }
541 
test_equalsLjava_lang_Object()542     public void test_equalsLjava_lang_Object() {
543         MessageFormat format1 = new MessageFormat("{0}");
544         MessageFormat format2 = new MessageFormat("{1}");
545         assertTrue("Should not be equal", !format1.equals(format2));
546         format2.applyPattern("{0}");
547         assertTrue("Should be equal", format1.equals(format2));
548         SimpleDateFormat date = (SimpleDateFormat) DateFormat.getTimeInstance();
549         format1.setFormat(0, DateFormat.getTimeInstance());
550         format2.setFormat(0, new SimpleDateFormat(date.toPattern()));
551         assertTrue("Should be equal2", format1.equals(format2));
552     }
553 
test_hashCode()554   public void test_hashCode() {
555       assertEquals("Should be equal", 3648, new MessageFormat("rr", null).hashCode());
556   }
557 
test_format$Ljava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition()558   public void test_format$Ljava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition() {
559     MessageFormat format = new MessageFormat("{1,number,integer}");
560     StringBuffer buffer = new StringBuffer();
561     format.format(new Object[] { "0", Double.valueOf(53.863) }, buffer,
562                   new FieldPosition(0));
563     assertEquals("Wrong result", "54", buffer.toString());
564 
565     format.format(new Object[] { "0", Double.valueOf(53.863) }, buffer,
566                   new FieldPosition(MessageFormat.Field.ARGUMENT));
567     assertEquals("Wrong result", "5454", buffer.toString());
568 
569     buffer = new StringBuffer();
570     format.applyPattern("{0,choice,0#zero|1#one '{1,choice,2#two {2,time}}'}");
571     Date date = new Date();
572     String expectedText = "one two " + DateFormat.getTimeInstance().format(date);
573     format.format(new Object[] { Double.valueOf(1.6), Integer.valueOf(3), date }, buffer, new FieldPosition(MessageFormat.Field.ARGUMENT));
574     assertEquals("Choice not recursive:\n" + expectedText + "\n" + buffer, expectedText, buffer.toString());
575 
576     StringBuffer str = format.format(new Object[] { Double.valueOf(0.6),
577         Integer.valueOf(3)}, buffer, null);
578     assertEquals(expectedText + "zero", str.toString());
579     assertEquals(expectedText + "zero", buffer.toString());
580 
581     try {
582       format.format(new Object[] { "0", Double.valueOf(1), "" }, buffer,
583                     new FieldPosition(MessageFormat.Field.ARGUMENT));
584       fail();
585     } catch (IllegalArgumentException expected) {
586     }
587 
588     try {
589       format.format(new Object[] { "",  Integer.valueOf(3)}, buffer,
590                     new FieldPosition(MessageFormat.Field.ARGUMENT));
591       fail();
592     } catch (IllegalArgumentException expected) {
593     }
594   }
595 
test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition()596   public void test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition() {
597     new Support_MessageFormat(
598         "test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition")
599         .t_format_with_FieldPosition();
600 
601     String pattern = "On {4,date} at {3,time}, he ate {2,number, integer} hamburger{2,choice,1#|1<s}.";
602     MessageFormat format = new MessageFormat(pattern, Locale.US);
603     Object[] objects = new Object[] { "", Integer.valueOf(3), 8, ""};
604     try {
605       format.format(objects, new StringBuffer(), new FieldPosition(DateFormat.Field.AM_PM));
606       fail();
607     } catch (IllegalArgumentException expected) {
608     }
609   }
610 
test_getFormats()611   public void test_getFormats() {
612     // test with repeating formats and max argument index < max offset
613     Format[] formats = format1.getFormats();
614     Format[] correctFormats = new Format[] {
615       NumberFormat.getCurrencyInstance(),
616       DateFormat.getTimeInstance(),
617       NumberFormat.getPercentInstance(), null,
618       new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance(), };
619 
620     assertEquals("Test1:Returned wrong number of formats:",
621                  correctFormats.length, formats.length);
622     for (int i = 0; i < correctFormats.length; i++) {
623       assertEquals("Test1:wrong format for pattern index " + i + ":",
624                    correctFormats[i], formats[i]);
625     }
626 
627     // test with max argument index > max offset
628     formats = format2.getFormats();
629     correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
630         DateFormat.getTimeInstance(),
631         NumberFormat.getPercentInstance(), null,
632         new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance() };
633 
634     assertEquals("Test2:Returned wrong number of formats:",
635                  correctFormats.length, formats.length);
636     for (int i = 0; i < correctFormats.length; i++) {
637       assertEquals("Test2:wrong format for pattern index " + i + ":",
638                    correctFormats[i], formats[i]);
639     }
640 
641     // test with argument number being zero
642     formats = format3.getFormats();
643     assertEquals("Test3: Returned wrong number of formats:", 0,
644                  formats.length);
645   }
646 
test_getFormatsByArgumentIndex()647     public void test_getFormatsByArgumentIndex() {
648         // test with repeating formats and max argument index < max offset
649         Format[] formats = format1.getFormatsByArgumentIndex();
650         Format[] correctFormats = new Format[] { DateFormat.getDateInstance(),
651                 new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
652                 NumberFormat.getCurrencyInstance(), null };
653 
654         assertEquals("Test1:Returned wrong number of formats:",
655                 correctFormats.length, formats.length);
656         for (int i = 0; i < correctFormats.length; i++) {
657             assertEquals("Test1:wrong format for argument index " + i + ":",
658                     correctFormats[i], formats[i]);
659         }
660 
661         // test with max argument index > max offset
662         formats = format2.getFormatsByArgumentIndex();
663         correctFormats = new Format[] { DateFormat.getDateInstance(),
664                 new ChoiceFormat("0#off|1#on"), null,
665                 NumberFormat.getCurrencyInstance(), null, null, null, null,
666                 DateFormat.getTimeInstance() };
667 
668         assertEquals("Test2:Returned wrong number of formats:",
669                 correctFormats.length, formats.length);
670         for (int i = 0; i < correctFormats.length; i++) {
671             assertEquals("Test2:wrong format for argument index " + i + ":",
672                     correctFormats[i], formats[i]);
673         }
674 
675         // test with argument number being zero
676         formats = format3.getFormatsByArgumentIndex();
677         assertEquals("Test3: Returned wrong number of formats:", 0,
678                 formats.length);
679     }
680 
test_setFormatByArgumentIndexILjava_text_Format()681     public void test_setFormatByArgumentIndexILjava_text_Format() {
682         // test for method setFormatByArgumentIndex(int, Format)
683         MessageFormat f1 = (MessageFormat) format1.clone();
684         f1.setFormatByArgumentIndex(0, DateFormat.getTimeInstance());
685         f1.setFormatByArgumentIndex(4, new ChoiceFormat("1#few|2#ok|3#a lot"));
686 
687         // test with repeating formats and max argument index < max offset
688         // compare getFormatsByArgumentIndex() results after calls to
689         // setFormatByArgumentIndex()
690         Format[] formats = f1.getFormatsByArgumentIndex();
691 
692         Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
693                 new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
694                 NumberFormat.getCurrencyInstance(),
695                 new ChoiceFormat("1#few|2#ok|3#a lot") };
696 
697         assertEquals("Test1A:Returned wrong number of formats:",
698                 correctFormats.length, formats.length);
699         for (int i = 0; i < correctFormats.length; i++) {
700             assertEquals("Test1B:wrong format for argument index " + i + ":",
701                     correctFormats[i], formats[i]);
702         }
703 
704         // compare getFormats() results after calls to
705         // setFormatByArgumentIndex()
706         formats = f1.getFormats();
707 
708         correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
709                 DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
710                 new ChoiceFormat("1#few|2#ok|3#a lot"),
711                 new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };
712 
713         assertEquals("Test1C:Returned wrong number of formats:",
714                 correctFormats.length, formats.length);
715         for (int i = 0; i < correctFormats.length; i++) {
716             assertEquals("Test1D:wrong format for pattern index " + i + ":",
717                     correctFormats[i], formats[i]);
718         }
719 
720         // test setting argumentIndexes that are not used
721         MessageFormat f2 = (MessageFormat) format2.clone();
722         f2.setFormatByArgumentIndex(2, NumberFormat.getPercentInstance());
723         f2.setFormatByArgumentIndex(4, DateFormat.getTimeInstance());
724 
725         formats = f2.getFormatsByArgumentIndex();
726         correctFormats = format2.getFormatsByArgumentIndex();
727 
728         assertEquals("Test2A:Returned wrong number of formats:",
729                 correctFormats.length, formats.length);
730         for (int i = 0; i < correctFormats.length; i++) {
731             assertEquals("Test2B:wrong format for argument index " + i + ":",
732                     correctFormats[i], formats[i]);
733         }
734 
735         formats = f2.getFormats();
736         correctFormats = format2.getFormats();
737 
738         assertEquals("Test2C:Returned wrong number of formats:",
739                 correctFormats.length, formats.length);
740         for (int i = 0; i < correctFormats.length; i++) {
741             assertEquals("Test2D:wrong format for pattern index " + i + ":",
742                     correctFormats[i], formats[i]);
743         }
744 
745         // test exceeding the argumentIndex number
746         MessageFormat f3 = (MessageFormat) format3.clone();
747         f3.setFormatByArgumentIndex(1, NumberFormat.getCurrencyInstance());
748 
749         formats = f3.getFormatsByArgumentIndex();
750         assertEquals("Test3A:Returned wrong number of formats:", 0,
751                 formats.length);
752 
753         formats = f3.getFormats();
754         assertEquals("Test3B:Returned wrong number of formats:", 0,
755                 formats.length);
756     }
757 
test_setFormatsByArgumentIndex$Ljava_text_Format()758     public void test_setFormatsByArgumentIndex$Ljava_text_Format() {
759         MessageFormat f1 = (MessageFormat) format1.clone();
760 
761         // test with repeating formats and max argument index < max offset
762         // compare getFormatsByArgumentIndex() results after calls to
763         // setFormatsByArgumentIndex(Format[])
764         Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
765                 new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
766                 NumberFormat.getCurrencyInstance(),
767                 new ChoiceFormat("1#few|2#ok|3#a lot") };
768 
769         f1.setFormatsByArgumentIndex(correctFormats);
770         Format[] formats = f1.getFormatsByArgumentIndex();
771 
772         assertEquals("Test1A:Returned wrong number of formats:",
773                 correctFormats.length, formats.length);
774         for (int i = 0; i < correctFormats.length; i++) {
775             assertEquals("Test1B:wrong format for argument index " + i + ":",
776                     correctFormats[i], formats[i]);
777         }
778 
779         // compare getFormats() results after calls to
780         // setFormatByArgumentIndex()
781         formats = f1.getFormats();
782         correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
783                 DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
784                 new ChoiceFormat("1#few|2#ok|3#a lot"),
785                 new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };
786 
787         assertEquals("Test1C:Returned wrong number of formats:",
788                 correctFormats.length, formats.length);
789         for (int i = 0; i < correctFormats.length; i++) {
790             assertEquals("Test1D:wrong format for pattern index " + i + ":",
791                     correctFormats[i], formats[i]);
792         }
793 
794         // test setting argumentIndexes that are not used
795         MessageFormat f2 = (MessageFormat) format2.clone();
796         Format[] inputFormats = new Format[] { DateFormat.getDateInstance(),
797                 new ChoiceFormat("0#off|1#on"),
798                 NumberFormat.getPercentInstance(),
799                 NumberFormat.getCurrencyInstance(),
800                 DateFormat.getTimeInstance(), null, null, null,
801                 DateFormat.getTimeInstance() };
802         f2.setFormatsByArgumentIndex(inputFormats);
803 
804         formats = f2.getFormatsByArgumentIndex();
805         correctFormats = format2.getFormatsByArgumentIndex();
806 
807         assertEquals("Test2A:Returned wrong number of formats:",
808                 correctFormats.length, formats.length);
809         for (int i = 0; i < correctFormats.length; i++) {
810             assertEquals("Test2B:wrong format for argument index " + i + ":",
811                     correctFormats[i], formats[i]);
812         }
813 
814         formats = f2.getFormats();
815         correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
816                 DateFormat.getTimeInstance(), DateFormat.getDateInstance(),
817                 null, new ChoiceFormat("0#off|1#on"),
818                 DateFormat.getDateInstance() };
819 
820         assertEquals("Test2C:Returned wrong number of formats:",
821                 correctFormats.length, formats.length);
822         for (int i = 0; i < correctFormats.length; i++) {
823             assertEquals("Test2D:wrong format for pattern index " + i + ":",
824                     correctFormats[i], formats[i]);
825         }
826 
827         // test exceeding the argumentIndex number
828         MessageFormat f3 = (MessageFormat) format3.clone();
829         f3.setFormatsByArgumentIndex(inputFormats);
830 
831         formats = f3.getFormatsByArgumentIndex();
832         assertEquals("Test3A:Returned wrong number of formats:", 0,
833                 formats.length);
834 
835         formats = f3.getFormats();
836         assertEquals("Test3B:Returned wrong number of formats:", 0,
837                 formats.length);
838 
839     }
840 
test_parseLjava_lang_StringLjava_text_ParsePosition()841     public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
842         MessageFormat format = new MessageFormat("date is {0,date,MMM d, yyyy}");
843         ParsePosition pos = new ParsePosition(2);
844         Object[] result = (Object[]) format
845                 .parse("xxdate is Feb 28, 1999", pos);
846         assertTrue("No result: " + result.length, result.length >= 1);
847         assertTrue("Wrong answer", ((Date) result[0])
848                 .equals(new GregorianCalendar(1999, Calendar.FEBRUARY, 28)
849                         .getTime()));
850 
851         MessageFormat mf = new MessageFormat("vm={0},{1},{2}");
852         result = mf.parse("vm=win,foo,bar", new ParsePosition(0));
853         assertTrue("Invalid parse", result[0].equals("win")
854                 && result[1].equals("foo") && result[2].equals("bar"));
855 
856         mf = new MessageFormat("{0}; {0}; {0}");
857         String parse = "a; b; c";
858         result = mf.parse(parse, new ParsePosition(0));
859         assertEquals("Wrong variable result", "c", result[0]);
860 
861         mf = new MessageFormat("before {0}, after {1,number}");
862         parse = "before you, after 42";
863         pos.setIndex(0);
864         pos.setErrorIndex(8);
865         result = mf.parse(parse, pos);
866         assertEquals(2, result.length);
867 
868         try {
869             mf.parse(parse, null);
870             fail();
871         } catch (NullPointerException expected) {
872         }
873 
874         // This should _not_ throw.
875         mf.parse(null, pos);
876     }
877 
test_setLocaleLjava_util_Locale()878   public void test_setLocaleLjava_util_Locale() {
879     MessageFormat format = new MessageFormat("date {0,date}");
880     format.setLocale(Locale.CHINA);
881     assertEquals("Wrong locale1", Locale.CHINA, format.getLocale());
882     format.applyPattern("{1,date}");
883     assertEquals("Wrong locale3", DateFormat.getDateInstance(DateFormat.DEFAULT,
884                                                              Locale.CHINA), format.getFormats()[0]);
885   }
886 
test_toPattern()887   public void test_toPattern() {
888     String pattern = "[{0}]";
889     MessageFormat mf = new MessageFormat(pattern);
890     assertTrue("Wrong pattern", mf.toPattern().equals(pattern));
891 
892     // Regression for HARMONY-59
893     new MessageFormat("CHOICE {1,choice}").toPattern();
894   }
895 
setUp()896   protected void setUp() {
897     defaultLocale = Locale.getDefault();
898     Locale.setDefault(Locale.US);
899 
900     // test with repeating formats and max argument index < max offset
901     String pattern = "A {3, number, currency} B {2, time} C {0, number, percent} D {4}  E {1,choice,0#off|1#on} F {0, date}";
902     format1 = new MessageFormat(pattern);
903 
904     // test with max argument index > max offset
905     pattern = "A {3, number, currency} B {8, time} C {0, number, percent} D {6}  E {1,choice,0#off|1#on} F {0, date}";
906     format2 = new MessageFormat(pattern);
907 
908     // test with argument number being zero
909     pattern = "A B C D E F";
910     format3 = new MessageFormat(pattern);
911   }
912 
tearDown()913   protected void tearDown() {
914     Locale.setDefault(defaultLocale);
915   }
916 
test_ConstructorLjava_util_Locale()917   public void test_ConstructorLjava_util_Locale() {
918     // Regression for HARMONY-65
919     try {
920       new MessageFormat("{0,number,integer", Locale.US);
921       fail("Assert 0: Failed to detect unmatched brackets.");
922     } catch (IllegalArgumentException e) {
923       // expected
924     }
925   }
926 
test_parse()927   public void test_parse() throws ParseException {
928     // Regression for HARMONY-63
929     MessageFormat mf = new MessageFormat("{0,number,#,##}", Locale.US);
930     Object[] res = mf.parse("1,00,00");
931     assertEquals("Assert 0: incorrect size of parsed data ", 1, res.length);
932     assertEquals("Assert 1: parsed value incorrectly", Long.valueOf(10000), (Long)res[0]);
933   }
934 
test_format_Object()935   public void test_format_Object() {
936     // Regression for HARMONY-1875
937     Locale.setDefault(Locale.CANADA);
938     TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
939     String pat="text here {0, date, yyyyyyyyy } and here";
940     String etalon="text here  000002007  and here";
941     MessageFormat obj = new MessageFormat(pat);
942     assertEquals(etalon, obj.format(new Object[]{new Date(1198141737640L)}));
943 
944     assertEquals("{0}", MessageFormat.format("{0}", (Object[]) null));
945     assertEquals("nullABC", MessageFormat.format("{0}{1}", (Object[]) new String[]{null, "ABC"}));
946   }
947 
testHARMONY5323()948   public void testHARMONY5323() {
949     Object[] messageArgs = new Object[11];
950     for (int i = 0; i < messageArgs.length; i++) {
951       messageArgs[i] = "example" + i;
952     }
953 
954     String res = MessageFormat.format("bgcolor=\"{10}\"", messageArgs);
955     assertEquals(res, "bgcolor=\"example10\"");
956   }
957 
958   // http://b/19011159
test19011159()959   public void test19011159() {
960     final String pattern = "ab{0,choice,0#1'2''3'''4''''.}yz";
961     final MessageFormat format = new MessageFormat(pattern, Locale.ENGLISH);
962     final Object[] zero0 = new Object[] { 0 };
963     assertEquals("ab12'3'4''.yz", format.format(zero0));
964   }
965 }
966