1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.sql; 24 25 import java.sql.Date; 26 import java.time.Instant; 27 import java.time.LocalDate; 28 import static org.testng.Assert.*; 29 import org.testng.annotations.AfterClass; 30 import org.testng.annotations.AfterMethod; 31 import org.testng.annotations.BeforeClass; 32 import org.testng.annotations.BeforeMethod; 33 import org.testng.annotations.Test; 34 35 public class DateTests { 36 37 public DateTests() { 38 } 39 40 @BeforeClass 41 public static void setUpClass() throws Exception { 42 } 43 44 @AfterClass 45 public static void tearDownClass() throws Exception { 46 } 47 48 @BeforeMethod 49 public void setUpMethod() throws Exception { 50 } 51 52 @AfterMethod 53 public void tearDownMethod() throws Exception { 54 } 55 56 /** 57 * Validate an IllegalArgumentException is thrown for an invalid Date string 58 */ 59 @Test(expectedExceptions = IllegalArgumentException.class) 60 public void testInvalid_year() throws Exception { 61 String expResult = "20009-11-01"; 62 Date.valueOf(expResult); 63 } 64 65 /** 66 * Validate an IllegalArgumentException is thrown for an invalid Date string 67 */ 68 @Test(expectedExceptions = IllegalArgumentException.class) 69 public void testInvalid_year2() throws Exception { 70 String expResult = "09-11-01"; 71 Date.valueOf(expResult); 72 } 73 74 /** 75 * Validate an IllegalArgumentException is thrown for an invalid Date string 76 */ 77 @Test(expectedExceptions = IllegalArgumentException.class) 78 public void testInvalid_year3() throws Exception { 79 String expResult = "-11-01"; 80 Date.valueOf(expResult); 81 } 82 83 /** 84 * Validate an IllegalArgumentException is thrown for an invalid Date string 85 */ 86 @Test(expectedExceptions = IllegalArgumentException.class) 87 public void testInvalid_month() throws Exception { 88 String expResult = "2009-111-01"; 89 Date.valueOf(expResult); 90 } 91 92 /** 93 * Validate an IllegalArgumentException is thrown for an invalid Date string 94 */ 95 @Test(expectedExceptions = IllegalArgumentException.class) 96 public void testInvalid_month3() throws Exception { 97 String expResult = "2009--01"; 98 Date.valueOf(expResult); 99 } 100 101 /** 102 * Validate an IllegalArgumentException is thrown for an invalid Date string 103 */ 104 @Test(expectedExceptions = IllegalArgumentException.class) 105 public void testInvalid_month4() throws Exception { 106 String expResult = "2009-13-01"; 107 Date.valueOf(expResult); 108 } 109 110 /** 111 * Validate an IllegalArgumentException is thrown for an invalid Date string 112 */ 113 @Test(expectedExceptions = IllegalArgumentException.class) 114 public void testInvalid_day() throws Exception { 115 String expResult = "2009-11-011"; 116 Date.valueOf(expResult); 117 } 118 119 /** 120 * Validate an IllegalArgumentException is thrown for an invalid Date string 121 */ 122 @Test(expectedExceptions = IllegalArgumentException.class) 123 public void testInvalid_day3() throws Exception { 124 String expResult = "2009-11-"; 125 Date.valueOf(expResult); 126 } 127 128 /** 129 * Validate an IllegalArgumentException is thrown for an invalid Date string 130 */ 131 @Test(expectedExceptions = IllegalArgumentException.class) 132 public void testInvalid_day4() throws Exception { 133 String expResult = "2009-11-00"; 134 Date.valueOf(expResult); 135 } 136 137 /** 138 * Validate an IllegalArgumentException is thrown for an invalid Date string 139 */ 140 @Test(expectedExceptions = IllegalArgumentException.class) 141 public void testInvalid_day5() throws Exception { 142 String expResult = "2009-11-33"; 143 Date.valueOf(expResult); 144 } 145 146 /** 147 * Validate an IllegalArgumentException is thrown for an invalid Date string 148 */ 149 @Test(expectedExceptions = IllegalArgumentException.class) 150 public void testInvalid_valueOf() throws Exception { 151 String expResult = "--"; 152 Date.valueOf(expResult); 153 } 154 155 /** 156 * Validate an IllegalArgumentException is thrown for an invalid Date string 157 */ 158 @Test(expectedExceptions = IllegalArgumentException.class) 159 public void testInvalid_valueOf2() throws Exception { 160 String expResult = ""; 161 Date.valueOf(expResult); 162 } 163 164 /** 165 * Validate an IllegalArgumentException is thrown for an invalid Date string 166 */ 167 @Test(expectedExceptions = IllegalArgumentException.class) 168 public void testInvalid_valueOf3() throws Exception { 169 String expResult = null; 170 Date.valueOf(expResult); 171 } 172 173 /** 174 * Validate an IllegalArgumentException is thrown for an invalid Date string 175 */ 176 @Test(expectedExceptions = IllegalArgumentException.class) 177 public void testInvalid_valueOf4() throws Exception { 178 String expResult = "-"; 179 Date.valueOf(expResult); 180 } 181 182 /** 183 * Validate an IllegalArgumentException is thrown for an invalid Date string 184 */ 185 @Test(expectedExceptions = IllegalArgumentException.class) 186 public void testInvalid_valueOf5() throws Exception { 187 String expResult = "2009"; 188 Date.valueOf(expResult); 189 } 190 191 /** 192 * Validate an IllegalArgumentException is thrown for an invalid Date string 193 */ 194 @Test(expectedExceptions = IllegalArgumentException.class) 195 public void testInvalid_valueOf6() throws Exception { 196 String expResult = "2009-01"; 197 Date.valueOf(expResult); 198 } 199 200 /** 201 * Validate an IllegalArgumentException is thrown for an invalid Date string 202 */ 203 @Test(expectedExceptions = IllegalArgumentException.class) 204 public void testInvalid_valueOf7() throws Exception { 205 String expResult = "---"; 206 Date.valueOf(expResult); 207 } 208 209 /** 210 * Validate an IllegalArgumentException is thrown for an invalid Date string 211 */ 212 @Test(expectedExceptions = IllegalArgumentException.class) 213 public void testInvalid_valueOf8() throws Exception { 214 String expResult = "2009-13--1"; 215 Date.valueOf(expResult); 216 } 217 218 /** 219 * Validate an IllegalArgumentException is thrown for an invalid Date string 220 */ 221 @Test(expectedExceptions = IllegalArgumentException.class) 222 public void testInvalid_valueOf10() { 223 String expResult = "1900-1-0"; 224 Date.valueOf(expResult); 225 } 226 227 /** 228 * Test that a date created from a date string is equal to the value 229 * returned from toString() 230 */ 231 @Test 232 public void test_valueOf() { 233 String expResult = "2009-08-30"; 234 Date d = Date.valueOf(expResult); 235 assertEquals(expResult, d.toString()); 236 } 237 238 /** 239 * Test that two dates, one with lead 0s omitted for month are equal 240 */ 241 @Test 242 public void testValid_month_single_digit() { 243 String testDate = "2009-1-01"; 244 String expResult = "2009-01-01"; 245 Date d = Date.valueOf(testDate); 246 Date d2 = Date.valueOf(expResult); 247 assertEquals(d, d2); 248 } 249 250 /** 251 * Test that two dates, one with lead 0s omitted for day are equal 252 */ 253 @Test 254 public void testValid_day_single_digit() { 255 String testDate = "2009-11-1"; 256 String expResult = "2009-11-01"; 257 Date d = Date.valueOf(testDate); 258 Date d2 = Date.valueOf(expResult); 259 assertEquals(d, d2); 260 } 261 262 /** 263 * Test that two dates, one with lead 0s omitted for month and day are equal 264 */ 265 @Test 266 public void testValid_month_day_single_digit() { 267 String testDate = "2009-1-1"; 268 String expResult = "2009-01-01"; 269 Date d = Date.valueOf(testDate); 270 Date d2 = Date.valueOf(expResult); 271 assertEquals(d, d2); 272 } 273 274 /** 275 * Validate that a Date.after() returns false when same date is compared 276 */ 277 @Test 278 public void test1() { 279 Date d = Date.valueOf("1961-08-30"); 280 assertFalse(d.after(d), "Error d.after(d) = true"); 281 } 282 283 /** 284 * Validate that a Date.after() returns true when later date is compared to 285 * earlier date 286 */ 287 @Test 288 public void test2() { 289 Date d = Date.valueOf("1961-08-30"); 290 Date d2 = new Date(System.currentTimeMillis()); 291 assertTrue(d2.after(d), "Error d2.after(d) = false"); 292 } 293 294 /** 295 * Validate that a Date.after() returns false when earlier date is compared 296 * to later date 297 */ 298 @Test 299 public void test3() { 300 Date d = Date.valueOf("1961-08-30"); 301 Date d2 = new Date(d.getTime()); 302 assertFalse(d.after(d2), "Error d.after(d2) = true"); 303 } 304 305 /** 306 * Validate that a Date.after() returns false when date compared to another 307 * date created from the original date 308 */ 309 @Test 310 public void test4() { 311 Date d = Date.valueOf("1961-08-30"); 312 Date d2 = new Date(d.getTime()); 313 assertFalse(d.after(d2), "Error d.after(d2) = true"); 314 assertFalse(d2.after(d), "Error d2.after(d) = true"); 315 } 316 317 /** 318 * Validate that a Date.before() returns false when same date is compared 319 */ 320 @Test 321 public void test5() { 322 Date d = Date.valueOf("1961-08-30"); 323 assertFalse(d.before(d), "Error d.before(d) = true"); 324 } 325 326 /** 327 * Validate that a Date.before() returns true when earlier date is compared 328 * to later date 329 */ 330 @Test 331 public void test6() { 332 Date d = Date.valueOf("1961-08-30"); 333 Date d2 = new Date(System.currentTimeMillis()); 334 assertTrue(d.before(d2), "Error d.before(d2) = false"); 335 } 336 337 /** 338 * Validate that a Date.before() returns false when later date is compared 339 * to earlier date 340 */ 341 @Test 342 public void test7() { 343 Date d = Date.valueOf("1961-08-30"); 344 Date d2 = new Date(d.getTime()); 345 assertFalse(d2.before(d), "Error d2.before(d) = true"); 346 } 347 348 /** 349 * Validate that a Date.before() returns false when date compared to another 350 * date created from the original date 351 */ 352 @Test 353 public void test8() { 354 Date d = Date.valueOf("1961-08-30"); 355 Date d2 = new Date(d.getTime()); 356 assertFalse(d.before(d2), "Error d.before(d2) = true"); 357 assertFalse(d2.before(d), "Error d2.before(d) = true"); 358 } 359 360 /** 361 * Validate that a Date.compareTo returns 0 when both Date objects are the 362 * same 363 */ 364 @Test 365 public void test9() { 366 Date d = Date.valueOf("1961-08-30"); 367 assertTrue(d.compareTo(d) == 0, "Error d.compareTo(d) !=0"); 368 } 369 370 /** 371 * Validate that a Date.compareTo returns 0 when both Date objects represent 372 * the same date 373 */ 374 @Test 375 public void test10() { 376 Date d = Date.valueOf("1961-08-30"); 377 Date d2 = new Date(d.getTime()); 378 assertTrue(d.compareTo(d2) == 0, "Error d.compareTo(d2) !=0"); 379 } 380 381 /** 382 * Validate that a Date.compareTo returns -1 when comparing a date to a 383 * later date 384 */ 385 @Test 386 public void test11() { 387 Date d = Date.valueOf("1961-08-30"); 388 Date d2 = new Date(System.currentTimeMillis()); 389 assertTrue(d.compareTo(d2) == -1, "Error d.compareTo(d2) != -1"); 390 } 391 392 /** 393 * Validate that a Date.compareTo returns 1 when comparing a date to an 394 * earlier date 395 */ 396 @Test 397 public void test12() { 398 Date d = Date.valueOf("1961-08-30"); 399 Date d2 = new Date(System.currentTimeMillis()); 400 assertTrue(d2.compareTo(d) == 1, "Error d.compareTo(d2) != 1"); 401 } 402 403 /** 404 * Validate that a Date made from a LocalDate are equal 405 */ 406 @Test 407 public void test13() { 408 Date d = Date.valueOf("1961-08-30"); 409 LocalDate ldt = d.toLocalDate(); 410 Date d2 = Date.valueOf(ldt); 411 assertTrue(d.equals(d2), "Error d != d2"); 412 } 413 414 /** 415 * Validate that a Date LocalDate value, made from a LocalDate are equal 416 */ 417 @Test 418 public void test14() { 419 LocalDate ldt = LocalDate.now(); 420 Date d = Date.valueOf(ldt); 421 assertTrue(ldt.equals(d.toLocalDate()), 422 "Error LocalDate values are not equal"); 423 } 424 425 /** 426 * Validate an NPE occurs when a null LocalDate is passed to valueOf 427 */ 428 @Test(expectedExceptions = NullPointerException.class) 429 public void test15() throws Exception { 430 LocalDate ld = null; 431 Date.valueOf(ld); 432 } 433 434 /** 435 * Validate an UnsupportedOperationException occurs when toInstant() is 436 * called 437 */ 438 @Test(expectedExceptions = UnsupportedOperationException.class) 439 public void test16() throws Exception { 440 Date d = Date.valueOf("1961-08-30"); 441 Instant instant = d.toInstant(); 442 } 443 444 /** 445 * Validate that two Date objects are equal when one is created from the 446 * toString() of the other 447 */ 448 @Test 449 public void test17() { 450 Date d = Date.valueOf("1961-08-30"); 451 Date d2 = Date.valueOf(d.toString()); 452 assertTrue(d.equals(d2) && d2.equals(d), "Error d != d2"); 453 } 454 455 /** 456 * Validate that two Date values one created using valueOf and another via a 457 * constructor are equal 458 */ 459 @Test 460 public void test18() { 461 462 Date d = Date.valueOf("1961-08-30"); 463 Date d2 = new Date(61, 7, 30); 464 assertTrue(d.equals(d2), "Error d != d2"); 465 } 466 467 /** 468 * Validate that two Date values one created using getTime() of the other 469 * are equal 470 */ 471 @Test 472 public void test19() { 473 474 Date d = Date.valueOf("1961-08-30"); 475 Date d2 = new Date(d.getTime()); 476 assertTrue(d.equals(d2), "Error d != d2"); 477 } 478 479 /** 480 * Validate that a Date value is equal to itself 481 */ 482 @Test 483 public void test20() { 484 485 Date d = Date.valueOf("1961-08-30"); 486 assertTrue(d.equals(d), "Error d != d"); 487 } 488 489 /** 490 * Validate an IllegalArgumentException is thrown for calling getHours 491 */ 492 @Test(expectedExceptions = IllegalArgumentException.class) 493 public void test21() throws Exception { 494 Date d = Date.valueOf("1961-08-30"); 495 d.getHours(); 496 } 497 498 /** 499 * Validate an IllegalArgumentException is thrown for calling getMinutes 500 */ 501 @Test(expectedExceptions = IllegalArgumentException.class) 502 public void test22() throws Exception { 503 Date d = Date.valueOf("1961-08-30"); 504 d.getMinutes(); 505 } 506 507 /** 508 * Validate an IllegalArgumentException is thrown for calling getSeconds 509 */ 510 @Test(expectedExceptions = IllegalArgumentException.class) 511 public void test23() throws Exception { 512 Date d = Date.valueOf("1961-08-30"); 513 d.getSeconds(); 514 } 515 516 /** 517 * Validate an IllegalArgumentException is thrown for calling setHours 518 */ 519 @Test(expectedExceptions = IllegalArgumentException.class) 520 public void test24() throws Exception { 521 Date d = Date.valueOf("1961-08-30"); 522 d.setHours(8); 523 } 524 525 /** 526 * Validate an IllegalArgumentException is thrown for calling setMinutes 527 */ 528 @Test(expectedExceptions = IllegalArgumentException.class) 529 public void test25() throws Exception { 530 Date d = Date.valueOf("1961-08-30"); 531 d.setMinutes(0); 532 } 533 534 /** 535 * Validate an IllegalArgumentException is thrown for calling setSeconds 536 */ 537 @Test(expectedExceptions = IllegalArgumentException.class) 538 public void test26() throws Exception { 539 Date d = Date.valueOf("1961-08-30"); 540 d.setSeconds(0); 541 } 542 }