1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2006-2011, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.tests; 10 11 import java.text.FieldPosition; 12 import java.text.Format; 13 import java.text.ParseException; 14 import java.text.ParsePosition; 15 import java.util.Date; 16 import java.util.Locale; 17 18 import com.ibm.icu.text.DateFormat; 19 import com.ibm.icu.text.MessageFormat; 20 import com.ibm.icu.text.NumberFormat; 21 import com.ibm.icu.util.ULocale; 22 23 public class MessageFormatTest extends ICUTestCase { 24 private final String pattern = "Deleted {0,number} files at {1,time,short} on {1,date}."; 25 private final String altPattern = "Deleted {0, number } files at {1, time, short} on {1, date}."; 26 private final Date date = new Date(716698890835L); 27 private final Number num = new Long(3456); 28 private final Object[] args = { num, date }; 29 private final Date dateOnly = new Date(716626800000L); 30 private final String englishTarget = "Deleted 3,456 files at 8:01 PM on Sep 16, 1992."; 31 private final String germanTarget = "Deleted 3.456 files at 20:01 on 16.09.1992."; 32 private final String modifiedTarget = "Deleted 3,456 files at 8:01:30 PM PDT on Sep 16, 1992."; 33 34 /* 35 * Test method for 'com.ibm.icu.text.MessageFormat.hashCode()' 36 */ 37 public void testHashCode() { 38 MessageFormat mf = new MessageFormat(pattern); 39 MessageFormat eq = new MessageFormat(altPattern); 40 MessageFormat ne = new MessageFormat("Deleted (0, number, currency} files at {1, time} on {1, date}."); 41 testEHCS(mf, eq, ne); 42 } 43 44 /* 45 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(MessageFormat)' 46 */ 47 public void testMessageFormatMessageFormat() { 48 // implicitly tested everywhere 49 } 50 51 /* 52 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String)' 53 */ 54 public void testMessageFormatString() { 55 MessageFormat mf = new MessageFormat(pattern); 56 assertEquals(englishTarget, mf.format(args)); 57 } 58 59 /* 60 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, Locale)' 61 */ 62 public void testMessageFormatStringLocale() { 63 MessageFormat mf = new MessageFormat(pattern, Locale.US); 64 assertEquals(englishTarget, mf.format(args)); 65 } 66 67 /* 68 * Test method for 'com.ibm.icu.text.MessageFormat.MessageFormat(String, ULocale)' 69 */ 70 public void testMessageFormatStringULocale() { 71 MessageFormat mf = new MessageFormat(pattern, ULocale.US); 72 assertEquals(englishTarget, mf.format(args)); 73 } 74 75 /* 76 * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(Locale)' 77 */ 78 public void testSetLocaleLocale() { 79 MessageFormat mf = new MessageFormat(pattern, Locale.US); 80 mf.setLocale(Locale.GERMANY); 81 mf.applyPattern(pattern); 82 assertEquals(germanTarget, mf.format(args)); 83 } 84 85 /* 86 * Test method for 'com.ibm.icu.text.MessageFormat.setLocale(ULocale)' 87 */ 88 public void testSetLocaleULocale() { 89 MessageFormat mf = new MessageFormat(pattern, Locale.US); 90 mf.setLocale(ULocale.GERMANY); 91 mf.applyPattern(pattern); 92 assertEquals(germanTarget, mf.format(args)); 93 } 94 95 /* 96 * Test method for 'com.ibm.icu.text.MessageFormat.getLocale()' 97 */ 98 public void testGetLocale() { 99 MessageFormat mf = new MessageFormat(pattern, Locale.US); 100 mf.setLocale(Locale.GERMANY); 101 assertEquals(Locale.GERMANY, mf.getLocale()); 102 } 103 104 /* 105 * Test method for 'com.ibm.icu.text.MessageFormat.getULocale()' 106 */ 107 public void testGetULocale() { 108 MessageFormat mf = new MessageFormat(pattern, Locale.US); 109 mf.setLocale(ULocale.GERMANY); 110 assertEquals(ULocale.GERMANY, mf.getULocale()); 111 } 112 113 /* 114 * Test method for 'com.ibm.icu.text.MessageFormat.applyPattern(String)' 115 */ 116 public void testApplyPattern() { 117 MessageFormat mf = new MessageFormat("foo"); 118 mf.applyPattern(pattern); 119 assertEquals(englishTarget, mf.format(args)); 120 } 121 122 /* 123 * Test method for 'com.ibm.icu.text.MessageFormat.toPattern()' 124 */ 125 public void testToPattern() { 126 MessageFormat mf = new MessageFormat(altPattern); 127 assertEquals(pattern, mf.toPattern()); 128 } 129 130 /* 131 * Test method for 'com.ibm.icu.text.MessageFormat.setFormatsByArgumentIndex(Format[])' 132 public void testSetFormatsByArgumentIndex() { 133 // this api is broken. if the same argument is used twice with two different 134 // formats, this can't be used, since it sets only one format per argument. 135 MessageFormat mf = new MessageFormat(pattern, Locale.US); 136 Format[] formats = { 137 NumberFormat.getIntegerInstance(), 138 DateFormat.getTimeInstance(DateFormat.SHORT), 139 DateFormat.getDateInstance(), 140 }; 141 mf.setFormatsByArgumentIndex(formats); 142 assertEquals(brokenButConformantTarget, mf.format(args)); 143 } 144 */ 145 146 /* 147 * Test method for 'com.ibm.icu.text.MessageFormat.setFormats(Format[])' 148 */ 149 public void testSetFormats() { 150 // this api, while it has the problem that the order of formats depends 151 // on the order in the string, at least lets you set all the formats. 152 153 MessageFormat mf = new MessageFormat(pattern, Locale.US); 154 Format[] formats = { 155 NumberFormat.getIntegerInstance(), 156 DateFormat.getTimeInstance(DateFormat.SHORT), 157 DateFormat.getDateInstance(), 158 }; 159 mf.setFormats(formats); 160 assertEquals(englishTarget, mf.format(args)); 161 } 162 163 /* 164 * Test method for 'com.ibm.icu.text.MessageFormat.setFormatByArgumentIndex(int, Format)' 165 public void testSetFormatByArgumentIndex() { 166 // same problem, once you set a format for an argument, you've set all of them 167 168 MessageFormat mf = new MessageFormat(pattern, Locale.US); 169 mf.setFormatByArgumentIndex(1, DateFormat.getTimeInstance(DateFormat.SHORT)); 170 assertEquals(brokenButConformantTarget, mf.format(args)); 171 172 } 173 */ 174 175 /* 176 * Test method for 'com.ibm.icu.text.MessageFormat.setFormat(int, Format)' 177 */ 178 public void testSetFormat() { 179 // and ok again 180 MessageFormat mf = new MessageFormat(pattern, Locale.US); 181 mf.setFormat(1, DateFormat.getTimeInstance(DateFormat.LONG)); 182 assertEquals(modifiedTarget, mf.format(args)); 183 } 184 185 /* 186 * Test method for 'com.ibm.icu.text.MessageFormat.getFormatsByArgumentIndex()' 187 public void testGetFormatsByArgumentIndex() { 188 MessageFormat mf = new MessageFormat(pattern, Locale.US); 189 Format[] formats = mf.getFormatsByArgumentIndex(); 190 NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); 191 assertEquals(formats[0], nf); 192 DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US); 193 assertEquals(formats[1], df); 194 } 195 */ 196 197 /* 198 * Test method for 'com.ibm.icu.text.MessageFormat.getFormats()' 199 */ 200 public void testGetFormats() { 201 MessageFormat mf = new MessageFormat(pattern, Locale.US); 202 Format[] formats = mf.getFormats(); 203 NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); 204 assertEquals(formats[0], nf); 205 DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US); 206 assertEquals(formats[1], tf); 207 DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US); 208 assertEquals(formats[2], df); 209 } 210 211 /* 212 * Test method for 'com.ibm.icu.text.MessageFormat.format(Object[], StringBuffer, FieldPosition)' 213 */ 214 public void testFormatObjectArrayStringBufferFieldPosition() { 215 MessageFormat mf = new MessageFormat(pattern, Locale.US); 216 StringBuffer buf = new StringBuffer(); 217 FieldPosition fp = new FieldPosition(0); 218 mf.format(args, buf, fp); 219 assertEquals(englishTarget, buf.toString()); 220 } 221 222 /* 223 * Test method for 'com.ibm.icu.text.MessageFormat.format(String, Object[])' 224 */ 225 public void testFormatStringObjectArray() { 226 assertEquals(englishTarget, MessageFormat.format(pattern, args)); 227 } 228 229 /* 230 * Test method for 'com.ibm.icu.text.MessageFormat.format(Object, StringBuffer, FieldPosition)' 231 */ 232 public void testFormatObjectStringBufferFieldPosition() { 233 MessageFormat mf = new MessageFormat(pattern, Locale.US); 234 StringBuffer buf = new StringBuffer(); 235 FieldPosition fp = new FieldPosition(0); 236 mf.format((Object)args, buf, fp); 237 assertEquals(englishTarget, buf.toString()); 238 } 239 240 /* 241 * Test method for 'com.ibm.icu.text.MessageFormat.parse(String, ParsePosition)' 242 */ 243 public void testParseStringParsePosition() { 244 MessageFormat mf = new MessageFormat(pattern, Locale.US); 245 ParsePosition pp = new ParsePosition(1); 246 Object[] result = mf.parse("!" + englishTarget, pp); 247 assertEquals(num, result[0]); 248 assertEquals(dateOnly, result[1]); 249 } 250 251 /* 252 * Test method for 'com.ibm.icu.text.MessageFormat.parse(String)' 253 */ 254 public void testParseString() { 255 MessageFormat mf = new MessageFormat(pattern, Locale.US); 256 try { 257 Object[] result = mf.parse(englishTarget); 258 assertEquals(num, result[0]); 259 assertEquals(dateOnly, result[1]); 260 } 261 catch (ParseException e) { 262 fail(e.getMessage()); 263 } 264 } 265 266 /* 267 * Test method for 'com.ibm.icu.text.MessageFormat.parseObject(String, ParsePosition)' 268 */ 269 public void testParseObjectStringParsePosition() { 270 MessageFormat mf = new MessageFormat(pattern, Locale.US); 271 ParsePosition pp = new ParsePosition(0); 272 Object result = mf.parseObject(englishTarget, pp); 273 assertEquals(num, ((Object[])result)[0]); 274 assertEquals(dateOnly, ((Object[])result)[1]); 275 } 276 277 /* 278 * Test method for 'com.ibm.icu.text.MessageFormat.autoQuoteApostrophe(String)' 279 */ 280 public void testAutoQuoteApostrophe() { 281 String str = "Let's meet at {1,time,h 'o'' clock'} at l'Orange Bleue"; 282 String pat = MessageFormat.autoQuoteApostrophe(str); 283 MessageFormat mf = new MessageFormat(pat, Locale.US); 284 String result = mf.format(args); 285 assertEquals("Let's meet at 8 o' clock at l'Orange Bleue", result); 286 assertEquals("Let''s meet at {1,time,h 'o'' clock'} at l''Orange Bleue", pat); 287 } 288 289 /* 290 * Test method for 'com.ibm.icu.text.MessageFormat.clone()' 291 */ 292 public void testClone() { 293 // tested already in testHashcode 294 } 295 296 /* 297 * Test method for 'com.ibm.icu.text.MessageFormat.equals(Object)' 298 */ 299 public void testEqualsObject() { 300 // tested already in testHashcode 301 } 302 303 /* 304 * Test method for 'com.ibm.icu.text.MessageFormat.toString()' 305 */ 306 public void testToString() { 307 // no need to test 308 } 309 } 310