1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.text.tests.java.text; 18 19import java.text.DateFormat; 20import java.text.DateFormatSymbols; 21import java.text.NumberFormat; 22import java.text.ParseException; 23import java.text.SimpleDateFormat; 24import java.util.Calendar; 25import java.util.Date; 26import java.util.Locale; 27 28public class DateFormatTest extends junit.framework.TestCase { 29 30 /** 31 * @tests java.text.DateFormat#clone() 32 */ 33 public void test_clone() { 34 DateFormat format = DateFormat.getInstance(); 35 DateFormat clone = (DateFormat) format.clone(); 36 assertTrue("Clone not equal", format.equals(clone)); 37 clone.getNumberFormat().setMinimumFractionDigits(123); 38 assertTrue("Clone shares NumberFormat", !format.equals(clone)); 39 } 40 41 /** 42 * @tests java.text.DateFormat#getAvailableLocales() 43 */ 44 public void test_getAvailableLocales() { 45 Locale[] locales = DateFormat.getAvailableLocales(); 46 assertTrue("No locales", locales.length > 0); 47 boolean english = false, german = false; 48 for (int i = locales.length; --i >= 0;) { 49 if (locales[i].equals(Locale.ENGLISH)) 50 english = true; 51 if (locales[i].equals(Locale.GERMAN)) 52 german = true; 53 DateFormat f1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, 54 DateFormat.SHORT, locales[i]); 55 assertTrue("Doesn't work", 56 f1.format(new Date()).getClass() == String.class); 57 } 58 assertTrue("Missing locales", english && german); 59 } 60 61 /** 62 * @tests java.text.DateFormat#getCalendar() 63 */ 64 public void test_getCalendar() { 65 DateFormat format = DateFormat.getInstance(); 66 Calendar cal1 = format.getCalendar(); 67 Calendar cal2 = format.getCalendar(); 68 assertTrue("Calendars not identical", cal1 == cal2); 69 } 70 71 /** 72 * @tests java.text.DateFormat#getDateInstance() 73 */ 74 public void test_getDateInstance() { 75 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getDateInstance(); 76 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 77 assertTrue("Wrong default", f2.equals(DateFormat.getDateInstance( 78 DateFormat.DEFAULT, Locale.getDefault()))); 79 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 80 new DateFormatSymbols())); 81 assertTrue("Doesn't work", 82 f2.format(new Date()).getClass() == String.class); 83 } 84 85 /** 86 * @tests java.text.DateFormat#getDateInstance(int) 87 */ 88 public void test_getDateInstanceI() { 89 assertTrue("Default not medium", 90 DateFormat.DEFAULT == DateFormat.MEDIUM); 91 92 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat 93 .getDateInstance(DateFormat.SHORT); 94 assertTrue("Wrong class1", f2.getClass() == SimpleDateFormat.class); 95 assertTrue("Wrong default1", f2.equals(DateFormat.getDateInstance( 96 DateFormat.SHORT, Locale.getDefault()))); 97 assertTrue("Wrong symbols1", f2.getDateFormatSymbols().equals( 98 new DateFormatSymbols())); 99 assertTrue("Doesn't work1", 100 f2.format(new Date()).getClass() == String.class); 101 102 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM); 103 assertTrue("Wrong class2", f2.getClass() == SimpleDateFormat.class); 104 assertTrue("Wrong default2", f2.equals(DateFormat.getDateInstance( 105 DateFormat.MEDIUM, Locale.getDefault()))); 106 assertTrue("Wrong symbols2", f2.getDateFormatSymbols().equals( 107 new DateFormatSymbols())); 108 assertTrue("Doesn't work2", 109 f2.format(new Date()).getClass() == String.class); 110 111 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG); 112 assertTrue("Wrong class3", f2.getClass() == SimpleDateFormat.class); 113 assertTrue("Wrong default3", f2.equals(DateFormat.getDateInstance( 114 DateFormat.LONG, Locale.getDefault()))); 115 assertTrue("Wrong symbols3", f2.getDateFormatSymbols().equals( 116 new DateFormatSymbols())); 117 assertTrue("Doesn't work3", 118 f2.format(new Date()).getClass() == String.class); 119 120 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL); 121 assertTrue("Wrong class4", f2.getClass() == SimpleDateFormat.class); 122 assertTrue("Wrong default4", f2.equals(DateFormat.getDateInstance( 123 DateFormat.FULL, Locale.getDefault()))); 124 assertTrue("Wrong symbols4", f2.getDateFormatSymbols().equals( 125 new DateFormatSymbols())); 126 assertTrue("Doesn't work4", 127 f2.format(new Date()).getClass() == String.class); 128 129 // regression test for HARMONY-940 130 try { 131 DateFormat.getDateInstance(77); 132 fail("Should throw IAE"); 133 } catch (IllegalArgumentException iae) { 134 //expected 135 } 136 } 137 138 /** 139 * @tests java.text.DateFormat#getDateInstance(int, java.util.Locale) 140 */ 141 public void test_getDateInstanceILjava_util_Locale() { 142 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getDateInstance( 143 DateFormat.SHORT, Locale.GERMAN); 144 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 145 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 146 new DateFormatSymbols(Locale.GERMAN))); 147 assertTrue("Doesn't work", 148 f2.format(new Date()).getClass() == String.class); 149 150 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, 151 Locale.GERMAN); 152 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 153 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 154 new DateFormatSymbols(Locale.GERMAN))); 155 assertTrue("Doesn't work", 156 f2.format(new Date()).getClass() == String.class); 157 158 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, 159 Locale.GERMAN); 160 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 161 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 162 new DateFormatSymbols(Locale.GERMAN))); 163 assertTrue("Doesn't work", 164 f2.format(new Date()).getClass() == String.class); 165 166 f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, 167 Locale.GERMAN); 168 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 169 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 170 new DateFormatSymbols(Locale.GERMAN))); 171 assertTrue("Doesn't work", 172 f2.format(new Date()).getClass() == String.class); 173 174 // regression test for HARMONY-940 175 try { 176 DateFormat.getDateInstance(77, Locale.GERMAN); 177 fail("Should throw IAE"); 178 } catch (IllegalArgumentException iae) { 179 //expected 180 } 181 } 182 183 /** 184 * @tests java.text.DateFormat#getDateTimeInstance() 185 */ 186 public void test_getDateTimeInstance() { 187 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat 188 .getDateTimeInstance(); 189 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 190 assertTrue("Wrong default", f2.equals(DateFormat.getDateTimeInstance( 191 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault()))); 192 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 193 new DateFormatSymbols())); 194 assertTrue("Doesn't work", 195 f2.format(new Date()).getClass() == String.class); 196 } 197 198 private void testDateTime(int dStyle, int tStyle) { 199 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat 200 .getDateTimeInstance(dStyle, tStyle); 201 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 202 SimpleDateFormat date = (SimpleDateFormat) DateFormat.getDateInstance( 203 dStyle, Locale.getDefault()); 204 SimpleDateFormat time = (SimpleDateFormat) DateFormat.getTimeInstance( 205 tStyle, Locale.getDefault()); 206 assertTrue("Wrong default", f2.toPattern().equals( 207 date.toPattern() + " " + time.toPattern())); 208 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 209 new DateFormatSymbols())); 210 assertTrue("Doesn't work", 211 f2.format(new Date()).getClass() == String.class); 212 } 213 214 /** 215 * @tests java.text.DateFormat#getDateTimeInstance(int, int) 216 */ 217 public void test_getDateTimeInstanceII() { 218 testDateTime(DateFormat.SHORT, DateFormat.SHORT); 219 testDateTime(DateFormat.SHORT, DateFormat.MEDIUM); 220 testDateTime(DateFormat.SHORT, DateFormat.LONG); 221 testDateTime(DateFormat.SHORT, DateFormat.FULL); 222 223 testDateTime(DateFormat.MEDIUM, DateFormat.SHORT); 224 testDateTime(DateFormat.MEDIUM, DateFormat.MEDIUM); 225 testDateTime(DateFormat.MEDIUM, DateFormat.LONG); 226 testDateTime(DateFormat.MEDIUM, DateFormat.FULL); 227 228 testDateTime(DateFormat.LONG, DateFormat.SHORT); 229 testDateTime(DateFormat.LONG, DateFormat.MEDIUM); 230 testDateTime(DateFormat.LONG, DateFormat.LONG); 231 testDateTime(DateFormat.LONG, DateFormat.FULL); 232 233 testDateTime(DateFormat.FULL, DateFormat.SHORT); 234 testDateTime(DateFormat.FULL, DateFormat.MEDIUM); 235 testDateTime(DateFormat.FULL, DateFormat.LONG); 236 testDateTime(DateFormat.FULL, DateFormat.FULL); 237 238 // regression test for HARMONY-940 239 try { 240 DateFormat.getDateTimeInstance(77, 66); 241 fail("Should throw IAE"); 242 } catch (IllegalArgumentException iae) { 243 //expected 244 } 245 } 246 247 private void testDateTimeLocale(int dStyle, int tStyle) { 248 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat 249 .getDateTimeInstance(dStyle, tStyle, Locale.GERMAN); 250 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 251 SimpleDateFormat date = (SimpleDateFormat) DateFormat.getDateInstance( 252 dStyle, Locale.GERMAN); 253 SimpleDateFormat time = (SimpleDateFormat) DateFormat.getTimeInstance( 254 tStyle, Locale.GERMAN); 255 assertTrue("Wrong default", f2.toPattern().equals( 256 date.toPattern() + " " + time.toPattern())); 257 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 258 new DateFormatSymbols(Locale.GERMAN))); 259 assertTrue("Doesn't work", 260 f2.format(new Date()).getClass() == String.class); 261 } 262 263 /** 264 * @tests java.text.DateFormat#getDateTimeInstance(int, int, 265 * java.util.Locale) 266 */ 267 public void test_getDateTimeInstanceIILjava_util_Locale() { 268 testDateTimeLocale(DateFormat.SHORT, DateFormat.SHORT); 269 testDateTimeLocale(DateFormat.SHORT, DateFormat.MEDIUM); 270 testDateTimeLocale(DateFormat.SHORT, DateFormat.LONG); 271 testDateTimeLocale(DateFormat.SHORT, DateFormat.FULL); 272 273 testDateTimeLocale(DateFormat.MEDIUM, DateFormat.SHORT); 274 testDateTimeLocale(DateFormat.MEDIUM, DateFormat.MEDIUM); 275 testDateTimeLocale(DateFormat.MEDIUM, DateFormat.LONG); 276 testDateTimeLocale(DateFormat.MEDIUM, DateFormat.FULL); 277 278 testDateTimeLocale(DateFormat.LONG, DateFormat.SHORT); 279 testDateTimeLocale(DateFormat.LONG, DateFormat.MEDIUM); 280 testDateTimeLocale(DateFormat.LONG, DateFormat.LONG); 281 testDateTimeLocale(DateFormat.LONG, DateFormat.FULL); 282 283 testDateTimeLocale(DateFormat.FULL, DateFormat.SHORT); 284 testDateTimeLocale(DateFormat.FULL, DateFormat.MEDIUM); 285 testDateTimeLocale(DateFormat.FULL, DateFormat.LONG); 286 testDateTimeLocale(DateFormat.FULL, DateFormat.FULL); 287 288 // regression test for HARMONY-940 289 try { 290 DateFormat.getDateTimeInstance(77, 66, Locale.GERMAN); 291 fail("Should throw IAE"); 292 } catch (IllegalArgumentException iae) { 293 //expected 294 } 295 } 296 297 /** 298 * @tests java.text.DateFormat#getInstance() 299 */ 300 public void test_getInstance() { 301 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getInstance(); 302 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 303 assertTrue("Wrong default", f2.equals(DateFormat.getDateTimeInstance( 304 DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault()))); 305 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 306 new DateFormatSymbols())); 307 assertTrue("Doesn't work", 308 f2.format(new Date()).getClass() == String.class); 309 } 310 311 /** 312 * @tests java.text.DateFormat#getNumberFormat() 313 */ 314 public void test_getNumberFormat() { 315 DateFormat format = DateFormat.getInstance(); 316 NumberFormat nf1 = format.getNumberFormat(); 317 NumberFormat nf2 = format.getNumberFormat(); 318 assertTrue("NumberFormats not identical", nf1 == nf2); 319 } 320 321 /** 322 * @tests java.text.DateFormat#getTimeInstance() 323 */ 324 public void test_getTimeInstance() { 325 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getTimeInstance(); 326 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 327 assertTrue("Wrong default", f2.equals(DateFormat.getTimeInstance( 328 DateFormat.DEFAULT, Locale.getDefault()))); 329 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 330 new DateFormatSymbols())); 331 assertTrue("Doesn't work", 332 f2.format(new Date()).getClass() == String.class); 333 } 334 335 /** 336 * @tests java.text.DateFormat#getTimeInstance(int) 337 */ 338 public void test_getTimeInstanceI() { 339 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat 340 .getTimeInstance(DateFormat.SHORT); 341 assertTrue("Wrong class1", f2.getClass() == SimpleDateFormat.class); 342 assertTrue("Wrong default1", f2.equals(DateFormat.getTimeInstance( 343 DateFormat.SHORT, Locale.getDefault()))); 344 assertTrue("Wrong symbols1", f2.getDateFormatSymbols().equals( 345 new DateFormatSymbols())); 346 assertTrue("Doesn't work1", 347 f2.format(new Date()).getClass() == String.class); 348 349 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM); 350 assertTrue("Wrong class2", f2.getClass() == SimpleDateFormat.class); 351 assertTrue("Wrong default2", f2.equals(DateFormat.getTimeInstance( 352 DateFormat.MEDIUM, Locale.getDefault()))); 353 assertTrue("Wrong symbols2", f2.getDateFormatSymbols().equals( 354 new DateFormatSymbols())); 355 assertTrue("Doesn't work2", 356 f2.format(new Date()).getClass() == String.class); 357 358 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.LONG); 359 assertTrue("Wrong class3", f2.getClass() == SimpleDateFormat.class); 360 assertTrue("Wrong default3", f2.equals(DateFormat.getTimeInstance( 361 DateFormat.LONG, Locale.getDefault()))); 362 assertTrue("Wrong symbols3", f2.getDateFormatSymbols().equals( 363 new DateFormatSymbols())); 364 assertTrue("Doesn't work3", 365 f2.format(new Date()).getClass() == String.class); 366 367 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.FULL); 368 assertTrue("Wrong class4", f2.getClass() == SimpleDateFormat.class); 369 assertTrue("Wrong default4", f2.equals(DateFormat.getTimeInstance( 370 DateFormat.FULL, Locale.getDefault()))); 371 assertTrue("Wrong symbols4", f2.getDateFormatSymbols().equals( 372 new DateFormatSymbols())); 373 assertTrue("Doesn't work4", 374 f2.format(new Date()).getClass() == String.class); 375 376 // regression test for HARMONY-940 377 try { 378 DateFormat.getTimeInstance(77); 379 fail("Should throw IAE"); 380 } catch (IllegalArgumentException iae) { 381 //expected 382 } 383 } 384 385 /** 386 * @tests java.text.DateFormat#getTimeInstance(int, java.util.Locale) 387 */ 388 public void test_getTimeInstanceILjava_util_Locale() { 389 SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getTimeInstance( 390 DateFormat.SHORT, Locale.GERMAN); 391 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 392 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 393 new DateFormatSymbols(Locale.GERMAN))); 394 assertTrue("Doesn't work", 395 f2.format(new Date()).getClass() == String.class); 396 397 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM, 398 Locale.GERMAN); 399 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 400 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 401 new DateFormatSymbols(Locale.GERMAN))); 402 assertTrue("Doesn't work", 403 f2.format(new Date()).getClass() == String.class); 404 405 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.LONG, 406 Locale.GERMAN); 407 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 408 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 409 new DateFormatSymbols(Locale.GERMAN))); 410 assertTrue("Doesn't work", 411 f2.format(new Date()).getClass() == String.class); 412 413 f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.FULL, 414 Locale.GERMAN); 415 assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class); 416 assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals( 417 new DateFormatSymbols(Locale.GERMAN))); 418 assertTrue("Doesn't work", 419 f2.format(new Date()).getClass() == String.class); 420 421 // regression test for HARMONY-940 422 try { 423 DateFormat.getTimeInstance(77, Locale.GERMAN); 424 fail("Should throw IAE"); 425 } catch (IllegalArgumentException iae) { 426 //expected 427 } 428 } 429 430 /** 431 * @tests java.text.DateFormat#setCalendar(java.util.Calendar) 432 */ 433 public void test_setCalendarLjava_util_Calendar() { 434 DateFormat format = DateFormat.getInstance(); 435 Calendar cal = Calendar.getInstance(); 436 format.setCalendar(cal); 437 assertTrue("Not identical Calendar", cal == format.getCalendar()); 438 } 439 440 /** 441 * @tests java.text.DateFormat#setNumberFormat(java.text.NumberFormat) 442 */ 443 public void test_setNumberFormatLjava_text_NumberFormat() { 444 DateFormat format = DateFormat.getInstance(); 445 NumberFormat f1 = NumberFormat.getInstance(); 446 format.setNumberFormat(f1); 447 assertTrue("Not identical NumberFormat", f1 == format.getNumberFormat()); 448 } 449 450 /** 451 * @tests java.text.DateFormat#parse(String) 452 */ 453 public void test_parse_LString() { 454 DateFormat format = DateFormat.getInstance(); 455 try { 456 format.parse("not a Date"); 457 fail("should throw ParseException first"); 458 } catch (ParseException e) { 459 assertNotNull(e.getMessage()); 460 } 461 } 462 463 /** 464 * @tests java.text.DateFormat#setLenient(boolean) 465 */ 466 public void test_setLenient() { 467 Date d = null; 468 DateFormat output = new SimpleDateFormat("MM/dd/yy"); 469 output.setLenient(false); 470 try { 471 d = output.parse("01/01/-1"); 472 fail("Should throw ParseException here."); 473 } catch (ParseException e) {} 474 } 475} 476