001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.tests;
008:
009: import java.text.FieldPosition;
010: import java.text.Format;
011: import java.text.ParseException;
012: import java.text.ParsePosition;
013: import java.util.Date;
014: import java.util.Locale;
015:
016: import com.ibm.icu.text.DateFormat;
017: import com.ibm.icu.text.MessageFormat;
018: import com.ibm.icu.text.NumberFormat;
019: import com.ibm.icu.util.ULocale;
020:
021: public class MessageFormatTest extends ICUTestCase {
022: private final String pattern = "Deleted {0,number} files at {1,time,short} on {1,date}.";
023: private final String altPattern = "Deleted {0, number } files at {1, time, short} on {1, date}.";
024: private final Date date = new Date(716698890835L);
025: private final Number num = new Long(3456);
026: private final Object[] args = { num, date };
027: private final Date dateOnly = new Date(716626800000L);
028: private final String englishTarget = "Deleted 3,456 files at 8:01 PM on Sep 16, 1992.";
029: private final String germanTarget = "Deleted 3.456 files at 20:01 on 16.09.1992.";
030: private final String modifiedTarget = "Deleted 3,456 files at 8:01:30 PM PDT on Sep 16, 1992.";
031:
032: /*
033: * Test method for 'com.ibm.icu.text.MessageFormat.hashCode()'
034: */
035: public void testHashCode() {
036: MessageFormat mf = new MessageFormat(pattern);
037: MessageFormat eq = new MessageFormat(altPattern);
038: MessageFormat ne = new MessageFormat(
039: "Deleted (0, number, currency} files at {1, time} on {1, date}.");
040: testEHCS(mf, eq, ne);
041: }
042:
043: /*
044: * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(MessageFormat)'
045: */
046: public void testMessageFormatMessageFormat() {
047: // implicitly tested everywhere
048: }
049:
050: /*
051: * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String)'
052: */
053: public void testMessageFormatString() {
054: MessageFormat mf = new MessageFormat(pattern);
055: assertEquals(englishTarget, mf.format(args));
056: }
057:
058: /*
059: * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, Locale)'
060: */
061: public void testMessageFormatStringLocale() {
062: MessageFormat mf = new MessageFormat(pattern, Locale.US);
063: assertEquals(englishTarget, mf.format(args));
064: }
065:
066: /*
067: * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, ULocale)'
068: */
069: public void testMessageFormatStringULocale() {
070: MessageFormat mf = new MessageFormat(pattern, ULocale.US);
071: assertEquals(englishTarget, mf.format(args));
072: }
073:
074: /*
075: * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(Locale)'
076: */
077: public void testSetLocaleLocale() {
078: MessageFormat mf = new MessageFormat(pattern, Locale.US);
079: mf.setLocale(Locale.GERMANY);
080: mf.applyPattern(pattern);
081: assertEquals(germanTarget, mf.format(args));
082: }
083:
084: /*
085: * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(ULocale)'
086: */
087: public void testSetLocaleULocale() {
088: MessageFormat mf = new MessageFormat(pattern, Locale.US);
089: mf.setLocale(ULocale.GERMANY);
090: mf.applyPattern(pattern);
091: assertEquals(germanTarget, mf.format(args));
092: }
093:
094: /*
095: * Test method for 'com.ibm.icu.text.MessageFormat.getLocale()'
096: */
097: public void testGetLocale() {
098: MessageFormat mf = new MessageFormat(pattern, Locale.US);
099: mf.setLocale(Locale.GERMANY);
100: assertEquals(Locale.GERMANY, mf.getLocale());
101: }
102:
103: /*
104: * Test method for 'com.ibm.icu.text.MessageFormat.getULocale()'
105: */
106: public void testGetULocale() {
107: MessageFormat mf = new MessageFormat(pattern, Locale.US);
108: mf.setLocale(ULocale.GERMANY);
109: assertEquals(ULocale.GERMANY, mf.getULocale());
110: }
111:
112: /*
113: * Test method for 'com.ibm.icu.text.MessageFormat.applyPattern(String)'
114: */
115: public void testApplyPattern() {
116: MessageFormat mf = new MessageFormat("foo");
117: mf.applyPattern(pattern);
118: assertEquals(englishTarget, mf.format(args));
119: }
120:
121: /*
122: * Test method for 'com.ibm.icu.text.MessageFormat.toPattern()'
123: */
124: public void testToPattern() {
125: MessageFormat mf = new MessageFormat(altPattern);
126: assertEquals(pattern, mf.toPattern());
127: }
128:
129: /*
130: * Test method for 'com.ibm.icu.text.MessageFormat.setFormatsByArgumentIndex(Format[])'
131: public void testSetFormatsByArgumentIndex() {
132: // this api is broken. if the same argument is used twice with two different
133: // formats, this can't be used, since it sets only one format per argument.
134: MessageFormat mf = new MessageFormat(pattern, Locale.US);
135: Format[] formats = {
136: NumberFormat.getIntegerInstance(),
137: DateFormat.getTimeInstance(DateFormat.SHORT),
138: DateFormat.getDateInstance(),
139: };
140: mf.setFormatsByArgumentIndex(formats);
141: assertEquals(brokenButConformantTarget, mf.format(args));
142: }
143: */
144:
145: /*
146: * Test method for 'com.ibm.icu.text.MessageFormat.setFormats(Format[])'
147: */
148: public void testSetFormats() {
149: // this api, while it has the problem that the order of formats depends
150: // on the order in the string, at least lets you set all the formats.
151:
152: MessageFormat mf = new MessageFormat(pattern, Locale.US);
153: Format[] formats = { NumberFormat.getIntegerInstance(),
154: DateFormat.getTimeInstance(DateFormat.SHORT),
155: DateFormat.getDateInstance(), };
156: mf.setFormats(formats);
157: assertEquals(englishTarget, mf.format(args));
158: }
159:
160: /*
161: * Test method for 'com.ibm.icu.text.MessageFormat.setFormatByArgumentIndex(int, Format)'
162: public void testSetFormatByArgumentIndex() {
163: // same problem, once you set a format for an argument, you've set all of them
164:
165: MessageFormat mf = new MessageFormat(pattern, Locale.US);
166: mf.setFormatByArgumentIndex(1, DateFormat.getTimeInstance(DateFormat.SHORT));
167: assertEquals(brokenButConformantTarget, mf.format(args));
168:
169: }
170: */
171:
172: /*
173: * Test method for 'com.ibm.icu.text.MessageFormat.setFormat(int, Format)'
174: */
175: public void testSetFormat() {
176: // and ok again
177: MessageFormat mf = new MessageFormat(pattern, Locale.US);
178: mf.setFormat(1, DateFormat.getTimeInstance(DateFormat.LONG));
179: assertEquals(modifiedTarget, mf.format(args));
180: }
181:
182: /*
183: * Test method for 'com.ibm.icu.text.MessageFormat.getFormatsByArgumentIndex()'
184: public void testGetFormatsByArgumentIndex() {
185: MessageFormat mf = new MessageFormat(pattern, Locale.US);
186: Format[] formats = mf.getFormatsByArgumentIndex();
187: NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
188: assertEquals(formats[0], nf);
189: DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
190: assertEquals(formats[1], df);
191: }
192: */
193:
194: /*
195: * Test method for 'com.ibm.icu.text.MessageFormat.getFormats()'
196: */
197: public void testGetFormats() {
198: MessageFormat mf = new MessageFormat(pattern, Locale.US);
199: Format[] formats = mf.getFormats();
200: NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
201: assertEquals(formats[0], nf);
202: DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT,
203: Locale.US);
204: assertEquals(formats[1], tf);
205: DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT,
206: Locale.US);
207: assertEquals(formats[2], df);
208: }
209:
210: /*
211: * Test method for 'com.ibm.icu.text.MessageFormat.format(Object[], StringBuffer, FieldPosition)'
212: */
213: public void testFormatObjectArrayStringBufferFieldPosition() {
214: MessageFormat mf = new MessageFormat(pattern, Locale.US);
215: StringBuffer buf = new StringBuffer();
216: FieldPosition fp = new FieldPosition(0);
217: mf.format(args, buf, fp);
218: assertEquals(englishTarget, buf.toString());
219: }
220:
221: /*
222: * Test method for 'com.ibm.icu.text.MessageFormat.format(String, Object[])'
223: */
224: public void testFormatStringObjectArray() {
225: assertEquals(englishTarget, MessageFormat.format(pattern, args));
226: }
227:
228: /*
229: * Test method for 'com.ibm.icu.text.MessageFormat.format(Object, StringBuffer, FieldPosition)'
230: */
231: public void testFormatObjectStringBufferFieldPosition() {
232: MessageFormat mf = new MessageFormat(pattern, Locale.US);
233: StringBuffer buf = new StringBuffer();
234: FieldPosition fp = new FieldPosition(0);
235: mf.format((Object) args, buf, fp);
236: assertEquals(englishTarget, buf.toString());
237: }
238:
239: /*
240: * Test method for 'com.ibm.icu.text.MessageFormat.parse(String, ParsePosition)'
241: */
242: public void testParseStringParsePosition() {
243: MessageFormat mf = new MessageFormat(pattern, Locale.US);
244: ParsePosition pp = new ParsePosition(1);
245: Object[] result = mf.parse("!" + englishTarget, pp);
246: assertEquals(num, result[0]);
247: assertEquals(dateOnly, result[1]);
248: }
249:
250: /*
251: * Test method for 'com.ibm.icu.text.MessageFormat.parse(String)'
252: */
253: public void testParseString() {
254: MessageFormat mf = new MessageFormat(pattern, Locale.US);
255: try {
256: Object[] result = mf.parse(englishTarget);
257: assertEquals(num, result[0]);
258: assertEquals(dateOnly, result[1]);
259: } catch (ParseException e) {
260: fail(e.getMessage());
261: }
262: }
263:
264: /*
265: * Test method for 'com.ibm.icu.text.MessageFormat.parseObject(String, ParsePosition)'
266: */
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]);
273: }
274:
275: /*
276: * Test method for 'com.ibm.icu.text.MessageFormat.autoQuoteApostrophe(String)'
277: */
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",
284: result);
285: assertEquals(
286: "Let''s meet at {1,time,h 'o'' clock'} at l''Orange Bleue",
287: pat);
288: }
289:
290: /*
291: * Test method for 'com.ibm.icu.text.MessageFormat.clone()'
292: */
293: public void testClone() {
294: // tested already in testHashcode
295: }
296:
297: /*
298: * Test method for 'com.ibm.icu.text.MessageFormat.equals(Object)'
299: */
300: public void testEqualsObject() {
301: // tested already in testHashcode
302: }
303:
304: /*
305: * Test method for 'com.ibm.icu.text.MessageFormat.toString()'
306: */
307: public void testToString() {
308: // no need to test
309: }
310: }
|