~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to eclipse-build/plugins.template/com.ibm.icu.base.tests/src/com/ibm/icu/tests/MessageFormatTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *******************************************************************************
 
3
 * Copyright (C) 2006-2011, International Business Machines Corporation and    *
 
4
 * others. All Rights Reserved.                                                *
 
5
 *******************************************************************************
 
6
 */
 
7
package com.ibm.icu.tests;
 
8
 
 
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;
 
15
 
 
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;
 
20
 
 
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.";
 
31
    
 
32
    /*
 
33
     * Test method for 'com.ibm.icu.text.MessageFormat.hashCode()'
 
34
     */
 
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}.");
 
39
        testEHCS(mf, eq, ne);
 
40
    }
 
41
 
 
42
    /*
 
43
     * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(MessageFormat)'
 
44
     */
 
45
    public void testMessageFormatMessageFormat() {
 
46
        // implicitly tested everywhere
 
47
    }
 
48
 
 
49
    /*
 
50
     * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String)'
 
51
     */
 
52
    public void testMessageFormatString() {
 
53
        MessageFormat mf = new MessageFormat(pattern);
 
54
        assertEquals(englishTarget, mf.format(args));
 
55
    }
 
56
 
 
57
    /*
 
58
     * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, Locale)'
 
59
     */
 
60
    public void testMessageFormatStringLocale() {
 
61
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
62
        assertEquals(englishTarget, mf.format(args));
 
63
    }
 
64
 
 
65
    /*
 
66
     * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, ULocale)'
 
67
     */
 
68
    public void testMessageFormatStringULocale() {
 
69
        MessageFormat mf = new MessageFormat(pattern, ULocale.US);
 
70
        assertEquals(englishTarget, mf.format(args));
 
71
    }
 
72
 
 
73
    /*
 
74
     * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(Locale)'
 
75
     */
 
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));
 
81
    }
 
82
 
 
83
    /*
 
84
     * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(ULocale)'
 
85
     */
 
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));
 
91
    }
 
92
 
 
93
    /*
 
94
     * Test method for 'com.ibm.icu.text.MessageFormat.getLocale()'
 
95
     */
 
96
    public void testGetLocale() {
 
97
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
98
        mf.setLocale(Locale.GERMANY);
 
99
        assertEquals(Locale.GERMANY, mf.getLocale());
 
100
    }
 
101
 
 
102
    /*
 
103
     * Test method for 'com.ibm.icu.text.MessageFormat.getULocale()'
 
104
     */
 
105
    public void testGetULocale() {
 
106
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
107
        mf.setLocale(ULocale.GERMANY);
 
108
        assertEquals(ULocale.GERMANY, mf.getULocale());
 
109
    }
 
110
 
 
111
    /*
 
112
     * Test method for 'com.ibm.icu.text.MessageFormat.applyPattern(String)'
 
113
     */
 
114
    public void testApplyPattern() {
 
115
        MessageFormat mf = new MessageFormat("foo");
 
116
        mf.applyPattern(pattern);
 
117
        assertEquals(englishTarget, mf.format(args));
 
118
    }
 
119
 
 
120
    /*
 
121
     * Test method for 'com.ibm.icu.text.MessageFormat.toPattern()'
 
122
     */
 
123
    public void testToPattern() {
 
124
        MessageFormat mf = new MessageFormat(altPattern);
 
125
        assertEquals(pattern, mf.toPattern());
 
126
    }
 
127
 
 
128
    /*
 
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);
 
134
        Format[] formats = {
 
135
            NumberFormat.getIntegerInstance(),
 
136
            DateFormat.getTimeInstance(DateFormat.SHORT),
 
137
            DateFormat.getDateInstance(),
 
138
        };
 
139
        mf.setFormatsByArgumentIndex(formats);
 
140
        assertEquals(brokenButConformantTarget, mf.format(args));
 
141
    }
 
142
     */
 
143
 
 
144
    /*
 
145
     * Test method for 'com.ibm.icu.text.MessageFormat.setFormats(Format[])'
 
146
     */
 
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.
 
150
        
 
151
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
152
        Format[] formats = {
 
153
            NumberFormat.getIntegerInstance(),
 
154
            DateFormat.getTimeInstance(DateFormat.SHORT),
 
155
            DateFormat.getDateInstance(),
 
156
        };
 
157
        mf.setFormats(formats);
 
158
        assertEquals(englishTarget, mf.format(args));
 
159
   }
 
160
 
 
161
    /*
 
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
 
165
        
 
166
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
167
        mf.setFormatByArgumentIndex(1, DateFormat.getTimeInstance(DateFormat.SHORT));
 
168
        assertEquals(brokenButConformantTarget, mf.format(args));
 
169
 
 
170
    }
 
171
    */
 
172
 
 
173
    /*
 
174
     * Test method for 'com.ibm.icu.text.MessageFormat.setFormat(int, Format)'
 
175
     */
 
176
    public void testSetFormat() {
 
177
        // and ok again        
 
178
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
179
        mf.setFormat(1, DateFormat.getTimeInstance(DateFormat.LONG));
 
180
        assertEquals(modifiedTarget, mf.format(args));
 
181
    }
 
182
 
 
183
    /*
 
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);
 
192
    }
 
193
     */
 
194
 
 
195
    /*
 
196
     * Test method for 'com.ibm.icu.text.MessageFormat.getFormats()'
 
197
     */
 
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);
 
207
    }
 
208
 
 
209
    /*
 
210
     * Test method for 'com.ibm.icu.text.MessageFormat.format(Object[], StringBuffer, FieldPosition)'
 
211
     */
 
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());
 
218
    }
 
219
 
 
220
    /*
 
221
     * Test method for 'com.ibm.icu.text.MessageFormat.format(String, Object[])'
 
222
     */
 
223
    public void testFormatStringObjectArray() {
 
224
        assertEquals(englishTarget, MessageFormat.format(pattern, args));
 
225
    }
 
226
 
 
227
    /*
 
228
     * Test method for 'com.ibm.icu.text.MessageFormat.format(Object, StringBuffer, FieldPosition)'
 
229
     */
 
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());
 
236
    }
 
237
 
 
238
    /*
 
239
     * Test method for 'com.ibm.icu.text.MessageFormat.parse(String, ParsePosition)'
 
240
     */
 
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]);
 
247
    }
 
248
 
 
249
    /*
 
250
     * Test method for 'com.ibm.icu.text.MessageFormat.parse(String)'
 
251
     */
 
252
    public void testParseString() {
 
253
        MessageFormat mf = new MessageFormat(pattern, Locale.US);
 
254
        try {
 
255
            Object[] result = mf.parse(englishTarget);
 
256
            assertEquals(num, result[0]);
 
257
            assertEquals(dateOnly, result[1]);
 
258
        }
 
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", result);
 
284
        assertEquals("Let''s meet at {1,time,h 'o'' clock'} at l''Orange Bleue", pat);
 
285
    }
 
286
 
 
287
    /*
 
288
     * Test method for 'com.ibm.icu.text.MessageFormat.clone()'
 
289
     */
 
290
    public void testClone() {
 
291
        // tested already in testHashcode
 
292
    }
 
293
 
 
294
    /*
 
295
     * Test method for 'com.ibm.icu.text.MessageFormat.equals(Object)'
 
296
     */
 
297
    public void testEqualsObject() {
 
298
        // tested already in testHashcode
 
299
    }
 
300
 
 
301
    /*
 
302
     * Test method for 'com.ibm.icu.text.MessageFormat.toString()'
 
303
     */
 
304
    public void testToString() {
 
305
        // no need to test
 
306
    }
 
307
}