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