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 libcore.java.text; 18 19import java.text.DateFormat; 20import java.text.FieldPosition; 21import java.text.ParseException; 22import java.text.ParsePosition; 23import java.text.SimpleDateFormat; 24import java.util.Calendar; 25import java.util.Date; 26import java.util.Locale; 27import java.util.TimeZone; 28 29public class OldDateFormatTest extends junit.framework.TestCase { 30 31 private class MockDateFormat extends DateFormat { 32 33 private static final long serialVersionUID = 1L; 34 35 public MockDateFormat() { 36 super(); 37 } 38 39 @Override 40 public Date parse(String source, ParsePosition pos) { 41 // it is a fake 42 return null; 43 } 44 45 @Override 46 public StringBuffer format(Date date, StringBuffer toAppendTo, 47 FieldPosition fieldPosition) { 48 // it is a fake 49 return null; 50 } 51 } 52 53 /** 54 * java.text.DateFormat#DateFormat() Test of method 55 * java.text.DateFormat#DateFormat(). 56 */ 57 public void test_Constructor() { 58 try { 59 new MockDateFormat(); 60 } catch (Exception e) { 61 fail("Unexpected exception " + e.toString()); 62 } 63 } 64 65 /** 66 * java.text.DateFormat#equals(java.lang.Object obj) Test of 67 * java.text.DateFormat#equals(java.lang.Object obj). 68 */ 69 public void test_equalsLjava_lang_Object() { 70 try { 71 DateFormat format = DateFormat.getInstance(); 72 DateFormat clone = (DateFormat) format.clone(); 73 assertTrue("Clone and parent are not equaled", format.equals(clone)); 74 assertTrue("Clone is equal to other object", !clone 75 .equals(DateFormat.getTimeInstance())); 76 format.setCalendar(Calendar.getInstance()); 77 assertTrue("Clone and parent are not equaled", format.equals(clone)); 78 } catch (Exception e) { 79 fail("Unexpected exception " + e.toString()); 80 } 81 } 82 83 /** 84 * java.text.DateFormat#format(java.util.Date) Test of method 85 * java.text.DateFormat#format(java.util.Date). 86 */ 87 public void test_formatLjava_util_Date() { 88 try { 89 // This test assumes a default DateFormat.is24Hour setting. 90 DateFormat.is24Hour = null; 91 DateFormat format = DateFormat.getDateTimeInstance( 92 DateFormat.SHORT, DateFormat.SHORT, Locale.US); 93 Date current = new Date(); 94 String dtf = format.format(current); 95 SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm a", Locale.US); 96 assertTrue("Incorrect date format", sdf.format(current).equals(dtf)); 97 } catch (Exception e) { 98 fail("Unexpected exception " + e.toString()); 99 } 100 } 101 102 /** 103 * java.text.DateFormat#format(Object, StringBuffer, FieldPosition) 104 * Test of method java.text.DateFormat#format(Object, StringBuffer, 105 * FieldPosition) 106 */ 107 public void test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition() { 108 try { 109 // This test assumes a default DateFormat.is24Hour setting. 110 DateFormat.is24Hour = null; 111 DateFormat format = DateFormat.getDateTimeInstance( 112 DateFormat.SHORT, DateFormat.SHORT, Locale.US); 113 Date current = new Date(); 114 StringBuffer toAppend = new StringBuffer(); 115 FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD); 116 StringBuffer sb = format.format(current, toAppend, fp); 117 SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm a", Locale.US); 118 assertTrue("Incorrect date format", sdf.format(current).equals( 119 sb.toString())); 120 assertTrue("Incorrect beginIndex of filed position", fp 121 .getBeginIndex() == sb.lastIndexOf("/") + 1); 122 assertTrue("Incorrect endIndex of filed position", 123 fp.getEndIndex() == sb.lastIndexOf("/") + 3); 124 } catch (Exception e) { 125 fail("Unexpected exception " + e.toString()); 126 } 127 } 128 129 public void test_getTimeZone() { 130 try { 131 DateFormat format = DateFormat.getInstance(); 132 TimeZone tz = format.getTimeZone(); 133 //if(1 == 1) 134 // throw new Exception(tz.getClass().getName()); 135 // We know we are not sun.util so: 136 // Redundant checking 137 //assertFalse("Incorrect zone info", tz.getClass().getName().equals( 138 // "sun.util.calendar.ZoneInfo")); 139 assertTrue("Incorrect time zone", tz.equals(format.getCalendar() 140 .getTimeZone())); 141 } catch (Exception e) { 142 fail("Unexpected exception " + e.toString()); 143 } 144 } 145 146 /** 147 * java.text.DateFormat#hashCode() Test of method 148 * java.text.DateFormat#hashCode(). 149 */ 150 public void test_hashCode() { 151 try { 152 DateFormat df1 = DateFormat.getInstance(); 153 DateFormat df2 = (DateFormat) df1.clone(); 154 assertTrue("Hash codes of clones are not equal", 155 df1.hashCode() == df2.hashCode()); 156 assertTrue("Hash codes of different objects are the same", df1 157 .hashCode() != DateFormat.getDateInstance().hashCode()); 158 } catch (Exception e) { 159 fail("Unexpected exception " + e.toString()); 160 } 161 } 162 163 /** 164 * java.text.DateFormat#isLenient() Test of method 165 * java.text.DateFormat#isLenient(). 166 */ 167 public void test_isLenient() { 168 DateFormat df = DateFormat.getInstance(); 169 Calendar c = df.getCalendar(); 170 if (df.isLenient()) { 171 try { 172 c.set(Calendar.DAY_OF_MONTH, 32); 173 c.get(Calendar.DAY_OF_MONTH); 174 } catch (Exception e) { 175 fail("Unexpected exception " + e.toString()); 176 } 177 c.setLenient(false); 178 try { 179 c.set(Calendar.DAY_OF_MONTH, 32); 180 c.get(Calendar.DAY_OF_MONTH); 181 fail("Expected IllegalArgumentException was not thrown"); 182 } catch (IllegalArgumentException e) { 183 // expected 184 } catch (Exception e) { 185 fail("Unexpected exception " + e.toString()); 186 } 187 } else { 188 try { 189 c.set(Calendar.DAY_OF_MONTH, 32); 190 c.get(Calendar.DAY_OF_MONTH); 191 fail("Expected IllegalArgumentException was not thrown"); 192 } catch (IllegalArgumentException e) { 193 // expected 194 } catch (Exception e) { 195 fail("Unexpected exception " + e.toString()); 196 } 197 c.setLenient(true); 198 try { 199 c.set(Calendar.DAY_OF_MONTH, 32); 200 c.get(Calendar.DAY_OF_MONTH); 201 } catch (Exception e) { 202 fail("Unexpected exception " + e.toString()); 203 } 204 } 205 } 206 207 /** 208 * java.text.DateFormat#parse(String) 209 */ 210 public void test_parseLString() throws Exception { 211 // This test assumes a default DateFormat.is24Hour setting. 212 DateFormat.is24Hour = null; 213 DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US); 214 215 try { 216 format.parse("not a Date"); 217 fail("should throw ParseException first"); 218 } catch (ParseException pe) { 219 assertNotNull(pe.getMessage()); 220 } 221 222 Date current = new Date(); 223 224 try { 225 Date date = format.parse(format.format(current).toString()); 226 assertEquals(current.getDate(), date.getDate()); 227 assertEquals(current.getDay(), date.getDay()); 228 assertEquals(current.getMonth(), date.getMonth()); 229 assertEquals(current.getYear(), date.getYear()); 230 assertEquals(current.getHours(), date.getHours()); 231 assertEquals(current.getMinutes(), date.getMinutes()); 232 assertEquals(0, date.getSeconds()); 233 } catch(ParseException pe) { 234 fail("ParseException was thrown for current Date."); 235 } 236 237 try { 238 format.parse("27/08/1998"); 239 fail("ParseException was not thrown."); 240 } catch(ParseException pe) { 241 //expected 242 } 243 try { 244 format.parse("30/30/908 4:50, PDT"); 245 fail("ParseException was not thrown."); 246 } catch(ParseException pe) { 247 //expected 248 } 249 try { 250 format.parse("837039928046"); 251 fail("ParseException was not thrown."); 252 } catch(ParseException pe) { 253 //expected 254 } 255 256 format = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US); 257 try { 258 Date date = format.parse(format.format(current).toString()); 259 assertEquals(current.getDate(), date.getDate()); 260 assertEquals(current.getDay(), date.getDay()); 261 assertEquals(current.getMonth(), date.getMonth()); 262 assertEquals(current.getYear(), date.getYear()); 263 assertEquals(0, date.getHours()); 264 assertEquals(0, date.getMinutes()); 265 assertEquals(0, date.getSeconds()); 266 } catch(ParseException pe) { 267 fail("ParseException was thrown for current Date."); 268 } 269 270 try { 271 format.parse("Jan 16 1970"); 272 fail("ParseException was not thrown."); 273 } catch(ParseException pe) { 274 //expected 275 } 276 277 try { 278 format.parse("27/08/1998"); 279 fail("ParseException was not thrown."); 280 } catch(ParseException pe) { 281 //expected 282 } 283 284 format = DateFormat.getDateInstance(DateFormat.LONG, Locale.US); 285 try { 286 Date date = format.parse(format.format(current).toString()); 287 assertEquals(current.getDate(), date.getDate()); 288 assertEquals(current.getDay(), date.getDay()); 289 assertEquals(current.getMonth(), date.getMonth()); 290 assertEquals(current.getYear(), date.getYear()); 291 assertEquals(0, date.getHours()); 292 assertEquals(0, date.getMinutes()); 293 assertEquals(0, date.getSeconds()); 294 } catch(ParseException pe) { 295 fail("ParseException was thrown for current Date."); 296 } 297 298 format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US); 299 try { 300 Date date = format.parse(format.format(current).toString()); 301 assertEquals(current.getDate(), date.getDate()); 302 assertEquals(current.getDay(), date.getDay()); 303 assertEquals(current.getMonth(), date.getMonth()); 304 assertEquals(current.getYear(), date.getYear()); 305 assertEquals(0, date.getHours()); 306 assertEquals(0, date.getMinutes()); 307 assertEquals(0, date.getSeconds()); 308 } catch(ParseException pe) { 309 fail("ParseException was thrown for current Date."); 310 } 311 312 format = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.US); 313 try { 314 Date date = format.parse(format.format(current).toString()); 315 assertEquals(1, date.getDate()); 316 assertEquals(0, date.getMonth()); 317 assertEquals(70, date.getYear()); 318 assertEquals(current.getHours(), date.getHours()); 319 assertEquals(current.getMinutes(), date.getMinutes()); 320 } catch(ParseException pe) { 321 fail("ParseException was thrown for current Date."); 322 } 323 324 try { 325 format.parse("8:58:44"); 326 fail("ParseException was not thrown."); 327 } catch(ParseException pe) { 328 //expected 329 } 330 331 format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.US); 332 try { 333 Date date = format.parse(format.format(current).toString()); 334 assertEquals(current.getDate(), date.getDate()); 335 assertEquals(current.getDay(), date.getDay()); 336 assertEquals(current.getMonth(), date.getMonth()); 337 assertEquals(current.getYear(), date.getYear()); 338 assertEquals(current.getHours(), date.getHours()); 339 assertEquals(current.getMinutes(), date.getMinutes()); 340 } catch(ParseException pe) { 341 fail("ParseException was thrown for current Date."); 342 } 343 344 try { 345 format.parse("January 31 1970 7:52:34 AM PST"); 346 fail("ParseException was not thrown."); 347 } catch (ParseException expected) { 348 } 349 350 try { 351 format.parse("January 31 1970"); 352 fail("ParseException was not thrown."); 353 } catch (ParseException expected) { 354 } 355 356 format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US); 357 String formatPattern = ((SimpleDateFormat) format).toPattern(); 358 String formattedCurrent = format.format(current); 359 Date date = format.parse(formattedCurrent); 360 // Date has millisecond accuracy, but humans don't use time formats that precise. 361 if (date.getTime() / 1000 != current.getTime() / 1000) { 362 fail(date.getTime() + " != " + current.getTime() + 363 "; " + formatPattern + "; " + formattedCurrent); 364 } 365 366 try { 367 format.parse("January 16, 1970 8:03:52 PM CET"); 368 fail("ParseException was not thrown."); 369 } catch (ParseException expected) { 370 } 371 } 372 373 /** 374 * java.text.DateFormat#parseObject(String, ParsePosition) Test of 375 * method java.text.DateFormat#parseObject(String, ParsePosition). 376 * Case 1: Try to parse correct data string. Case 2: Try to parse 377 * partialy correct data string. Case 3: Try to use argument 378 * ParsePosition as null. 379 */ 380 public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() { 381 DateFormat df = DateFormat.getInstance(); 382 try { 383 // case 1: Try to parse correct data string. 384 Date current = new Date(); 385 ParsePosition pp = new ParsePosition(0); 386 int parseIndex = pp.getIndex(); 387 Date result = (Date) df.parseObject(df.format(current), pp); 388 389 assertEquals("Dates are different.", current.getDate(), result.getDate()); 390 assertEquals("Days are different.", current.getDay(), result.getDay()); 391 assertEquals("Months are different.", current.getMonth(), result.getMonth()); 392 assertEquals("Years are different.", current.getYear(), result.getYear()); 393 assertEquals("Hours are different", current.getHours(), result.getHours()); 394 assertEquals("Minutes are diffetrent,", current.getMinutes(), result.getMinutes()); 395 396 assertTrue("Parse operation return null", result != null); 397 assertTrue("ParseIndex is incorrect", pp.getIndex() != parseIndex); 398 399 // case 2: Try to parse partially correct data string. 400 pp.setIndex(0); 401 char[] cur = df.format(current).toCharArray(); 402 cur[cur.length / 2] = 'Z'; 403 String partialCorrect = new String(cur); 404 result = (Date) df.parseObject(partialCorrect, pp); 405 assertTrue("Parse operation return not-null", result == null); 406 assertTrue("ParseIndex is incorrect", pp.getIndex() == 0); 407 assertTrue("ParseErrorIndex is incorrect", 408 pp.getErrorIndex() == cur.length / 2); 409 410 pp.setIndex(2); 411 char[] curDate = df.format(current).toCharArray(); 412 char [] newArray = new char[curDate.length + pp.getIndex()]; 413 for(int i = 0; i < curDate.length; i++) { 414 newArray[i + pp.getIndex()] = curDate[i]; 415 } 416 result = (Date) df.parseObject(new String(newArray), pp); 417 //assertEquals(current, result); 418 419 assertEquals("Dates are different.", current.getDate(), result.getDate()); 420 assertEquals("Days are different.", current.getDay(), result.getDay()); 421 assertEquals("Months are different.", current.getMonth(), result.getMonth()); 422 assertEquals("Years are different.", current.getYear(), result.getYear()); 423 assertEquals("Hours are different", current.getHours(), result.getHours()); 424 assertEquals("Minutes are diffetrent,", current.getMinutes(), result.getMinutes()); 425 426 // case 3: Try to use argument ParsePosition as null. 427 try { 428 df.parseObject(df.format(current), null); 429 fail("Expected NullPointerException was not thrown"); 430 } catch (NullPointerException e) { 431 // expected 432 } 433 434 assertNull(df.parseObject("test", pp)); 435 436 } catch (Exception e) { 437 fail("Unexpected exception " + e.toString()); 438 } 439 } 440 441 /** 442 * java.text.DateFormat#setLenient(boolean) Test of method 443 * java.text.DateFormat#setLenient(boolean). 444 */ 445 public void test_setLenientZ() { 446 DateFormat df = DateFormat.getInstance(); 447 Calendar c = df.getCalendar(); 448 try { 449 c.setLenient(true); 450 try { 451 c.set(Calendar.DAY_OF_MONTH, 32); 452 c.get(Calendar.DAY_OF_MONTH); 453 } catch (Exception e) { 454 fail("Unexpected exception " + e.toString()); 455 } 456 c.setLenient(false); 457 try { 458 c.set(Calendar.DAY_OF_MONTH, 32); 459 c.get(Calendar.DAY_OF_MONTH); 460 fail("Expected IllegalArgumentException was not thrown"); 461 } catch (IllegalArgumentException e) { 462 // expected 463 } catch (Exception e) { 464 fail("Unexpected exception " + e.toString()); 465 } 466 } catch (Exception e) { 467 fail("Uexpected exception " + e.toString()); 468 } 469 } 470 471 /** 472 * java.text.DateFormat#setTimeZone(TimeZone) Test of method 473 * java.text.DateFormat#setTimeZone(TimeZone). 474 */ 475 public void test_setTimeZoneLjava_util_TimeZone() { 476 try { 477 DateFormat format = DateFormat.getInstance(); 478 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); 479 format.setTimeZone(tz); 480 assertTrue("TimeZone is set incorrectly", tz.equals(format 481 .getTimeZone())); 482 } catch (Exception e) { 483 fail("Unexpected exception " + e.toString()); 484 } 485 } 486} 487