2 *******************************************************************************
\r
3 * Copyright (C) 2006, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.tests;
\r
9 import java.text.FieldPosition;
\r
10 import java.text.Format;
\r
11 import java.text.ParseException;
\r
12 import java.text.ParsePosition;
\r
13 import java.util.Date;
\r
14 import java.util.Locale;
\r
16 import com.ibm.icu.text.DateFormat;
\r
17 import com.ibm.icu.text.MessageFormat;
\r
18 import com.ibm.icu.text.NumberFormat;
\r
19 import com.ibm.icu.util.ULocale;
\r
21 public class MessageFormatTest extends ICUTestCase {
\r
22 private final String pattern = "Deleted {0,number} files at {1,time,short} on {1,date}.";
\r
23 private final String altPattern = "Deleted {0, number } files at {1, time, short} on {1, date}.";
\r
24 private final Date date = new Date(716698890835L);
\r
25 private final Number num = new Long(3456);
\r
26 private final Object[] args = { num, date };
\r
27 private final Date dateOnly = new Date(716626800000L);
\r
28 private final String englishTarget = "Deleted 3,456 files at 8:01 PM on Sep 16, 1992.";
\r
29 private final String germanTarget = "Deleted 3.456 files at 20:01 on 16.09.1992.";
\r
30 private final String modifiedTarget = "Deleted 3,456 files at 8:01:30 PM PDT on Sep 16, 1992.";
\r
33 * Test method for 'com.ibm.icu.text.MessageFormat.hashCode()'
\r
35 public void testHashCode() {
\r
36 MessageFormat mf = new MessageFormat(pattern);
\r
37 MessageFormat eq = new MessageFormat(altPattern);
\r
38 MessageFormat ne = new MessageFormat("Deleted (0, number, currency} files at {1, time} on {1, date}.");
\r
39 testEHCS(mf, eq, ne);
\r
43 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(MessageFormat)'
\r
45 public void testMessageFormatMessageFormat() {
\r
46 // implicitly tested everywhere
\r
50 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String)'
\r
52 public void testMessageFormatString() {
\r
53 MessageFormat mf = new MessageFormat(pattern);
\r
54 assertEquals(englishTarget, mf.format(args));
\r
58 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, Locale)'
\r
60 public void testMessageFormatStringLocale() {
\r
61 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
62 assertEquals(englishTarget, mf.format(args));
\r
66 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, ULocale)'
\r
68 public void testMessageFormatStringULocale() {
\r
69 MessageFormat mf = new MessageFormat(pattern, ULocale.US);
\r
70 assertEquals(englishTarget, mf.format(args));
\r
74 * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(Locale)'
\r
76 public void testSetLocaleLocale() {
\r
77 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
78 mf.setLocale(Locale.GERMANY);
\r
79 mf.applyPattern(pattern);
\r
80 assertEquals(germanTarget, mf.format(args));
\r
84 * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(ULocale)'
\r
86 public void testSetLocaleULocale() {
\r
87 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
88 mf.setLocale(ULocale.GERMANY);
\r
89 mf.applyPattern(pattern);
\r
90 assertEquals(germanTarget, mf.format(args));
\r
94 * Test method for 'com.ibm.icu.text.MessageFormat.getLocale()'
\r
96 public void testGetLocale() {
\r
97 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
98 mf.setLocale(Locale.GERMANY);
\r
99 assertEquals(Locale.GERMANY, mf.getLocale());
\r
103 * Test method for 'com.ibm.icu.text.MessageFormat.getULocale()'
\r
105 public void testGetULocale() {
\r
106 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
107 mf.setLocale(ULocale.GERMANY);
\r
108 assertEquals(ULocale.GERMANY, mf.getULocale());
\r
112 * Test method for 'com.ibm.icu.text.MessageFormat.applyPattern(String)'
\r
114 public void testApplyPattern() {
\r
115 MessageFormat mf = new MessageFormat("foo");
\r
116 mf.applyPattern(pattern);
\r
117 assertEquals(englishTarget, mf.format(args));
\r
121 * Test method for 'com.ibm.icu.text.MessageFormat.toPattern()'
\r
123 public void testToPattern() {
\r
124 MessageFormat mf = new MessageFormat(altPattern);
\r
125 assertEquals(pattern, mf.toPattern());
\r
129 * Test method for 'com.ibm.icu.text.MessageFormat.setFormatsByArgumentIndex(Format[])'
\r
130 public void testSetFormatsByArgumentIndex() {
\r
131 // this api is broken. if the same argument is used twice with two different
\r
132 // formats, this can't be used, since it sets only one format per argument.
\r
133 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
134 Format[] formats = {
\r
135 NumberFormat.getIntegerInstance(),
\r
136 DateFormat.getTimeInstance(DateFormat.SHORT),
\r
137 DateFormat.getDateInstance(),
\r
139 mf.setFormatsByArgumentIndex(formats);
\r
140 assertEquals(brokenButConformantTarget, mf.format(args));
\r
145 * Test method for 'com.ibm.icu.text.MessageFormat.setFormats(Format[])'
\r
147 public void testSetFormats() {
\r
148 // this api, while it has the problem that the order of formats depends
\r
149 // on the order in the string, at least lets you set all the formats.
\r
151 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
152 Format[] formats = {
\r
153 NumberFormat.getIntegerInstance(),
\r
154 DateFormat.getTimeInstance(DateFormat.SHORT),
\r
155 DateFormat.getDateInstance(),
\r
157 mf.setFormats(formats);
\r
158 assertEquals(englishTarget, mf.format(args));
\r
162 * Test method for 'com.ibm.icu.text.MessageFormat.setFormatByArgumentIndex(int, Format)'
\r
163 public void testSetFormatByArgumentIndex() {
\r
164 // same problem, once you set a format for an argument, you've set all of them
\r
166 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
167 mf.setFormatByArgumentIndex(1, DateFormat.getTimeInstance(DateFormat.SHORT));
\r
168 assertEquals(brokenButConformantTarget, mf.format(args));
\r
174 * Test method for 'com.ibm.icu.text.MessageFormat.setFormat(int, Format)'
\r
176 public void testSetFormat() {
\r
178 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
179 mf.setFormat(1, DateFormat.getTimeInstance(DateFormat.LONG));
\r
180 assertEquals(modifiedTarget, mf.format(args));
\r
184 * Test method for 'com.ibm.icu.text.MessageFormat.getFormatsByArgumentIndex()'
\r
185 public void testGetFormatsByArgumentIndex() {
\r
186 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
187 Format[] formats = mf.getFormatsByArgumentIndex();
\r
188 NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
\r
189 assertEquals(formats[0], nf);
\r
190 DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
\r
191 assertEquals(formats[1], df);
\r
196 * Test method for 'com.ibm.icu.text.MessageFormat.getFormats()'
\r
198 public void testGetFormats() {
\r
199 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
200 Format[] formats = mf.getFormats();
\r
201 NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
\r
202 assertEquals(formats[0], nf);
\r
203 DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
\r
204 assertEquals(formats[1], tf);
\r
205 DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
\r
206 assertEquals(formats[2], df);
\r
210 * Test method for 'com.ibm.icu.text.MessageFormat.format(Object[], StringBuffer, FieldPosition)'
\r
212 public void testFormatObjectArrayStringBufferFieldPosition() {
\r
213 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
214 StringBuffer buf = new StringBuffer();
\r
215 FieldPosition fp = new FieldPosition(0);
\r
216 mf.format(args, buf, fp);
\r
217 assertEquals(englishTarget, buf.toString());
\r
221 * Test method for 'com.ibm.icu.text.MessageFormat.format(String, Object[])'
\r
223 public void testFormatStringObjectArray() {
\r
224 assertEquals(englishTarget, MessageFormat.format(pattern, args));
\r
228 * Test method for 'com.ibm.icu.text.MessageFormat.format(Object, StringBuffer, FieldPosition)'
\r
230 public void testFormatObjectStringBufferFieldPosition() {
\r
231 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
232 StringBuffer buf = new StringBuffer();
\r
233 FieldPosition fp = new FieldPosition(0);
\r
234 mf.format((Object)args, buf, fp);
\r
235 assertEquals(englishTarget, buf.toString());
\r
239 * Test method for 'com.ibm.icu.text.MessageFormat.parse(String, ParsePosition)'
\r
241 public void testParseStringParsePosition() {
\r
242 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
243 ParsePosition pp = new ParsePosition(1);
\r
244 Object[] result = mf.parse("!" + englishTarget, pp);
\r
245 assertEquals(num, result[0]);
\r
246 assertEquals(dateOnly, result[1]);
\r
250 * Test method for 'com.ibm.icu.text.MessageFormat.parse(String)'
\r
252 public void testParseString() {
\r
253 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
255 Object[] result = mf.parse(englishTarget);
\r
256 assertEquals(num, result[0]);
\r
257 assertEquals(dateOnly, result[1]);
\r
259 catch (ParseException e) {
\r
260 fail(e.getMessage());
\r
265 * Test method for 'com.ibm.icu.text.MessageFormat.parseObject(String, ParsePosition)'
\r
267 public void testParseObjectStringParsePosition() {
\r
268 MessageFormat mf = new MessageFormat(pattern, Locale.US);
\r
269 ParsePosition pp = new ParsePosition(0);
\r
270 Object result = mf.parseObject(englishTarget, pp);
\r
271 assertEquals(num, ((Object[])result)[0]);
\r
272 assertEquals(dateOnly, ((Object[])result)[1]);
\r
276 * Test method for 'com.ibm.icu.text.MessageFormat.autoQuoteApostrophe(String)'
\r
278 public void testAutoQuoteApostrophe() {
\r
279 String str = "Let's meet at {1,time,h 'o'' clock'} at l'Orange Bleue";
\r
280 String pat = MessageFormat.autoQuoteApostrophe(str);
\r
281 MessageFormat mf = new MessageFormat(pat, Locale.US);
\r
282 String result = mf.format(args);
\r
283 assertEquals("Let's meet at 8 o' clock at l'Orange Bleue", result);
\r
284 assertEquals("Let''s meet at {1,time,h 'o'' clock'} at l''Orange Bleue", pat);
\r
288 * Test method for 'com.ibm.icu.text.MessageFormat.clone()'
\r
290 public void testClone() {
\r
291 // tested already in testHashcode
\r
295 * Test method for 'com.ibm.icu.text.MessageFormat.equals(Object)'
\r
297 public void testEqualsObject() {
\r
298 // tested already in testHashcode
\r
302 * Test method for 'com.ibm.icu.text.MessageFormat.toString()'
\r
304 public void testToString() {
\r