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.DataProvider;
  30 import org.testng.annotations.Test;
  31 import util.BaseTest;
  32 
  33 public class DateTests extends BaseTest {
  34 
  35     /*
  36      * Validate an IllegalArgumentException is thrown for an invalid Date string
  37      */
  38     @Test(dataProvider = "invalidDateValues",
  39             expectedExceptions = IllegalArgumentException.class)
  40     public void test(String d) throws Exception {
  41         Date.valueOf(d);
  42     }
  43 
  44     /*
  45      * Test that a date created from a date string is equal to the value
  46      * returned from toString()
  47      */
  48     @Test(dataProvider = "validDateValues")
  49     public void test00(String d, String expectedD) {
  50         Date d1 = Date.valueOf(d);
  51         Date d2 = Date.valueOf(expectedD);
  52         assertTrue(d1.equals(d2) && d2.equals(d1)
  53                 && d1.toString().equals(expectedD), "Error d1 != d2");
  54     }
  55 
  56     /*
  57      * Validate that a Date.after() returns false when same date is compared
  58      */
  59     @Test
  60     public void test01() {
  61         Date d = Date.valueOf("1961-08-30");
  62         assertFalse(d.after(d), "Error d.after(d) = true");
  63     }
  64 
  65     /*
  66      * Validate that a Date.after() returns true when later date is compared to
  67      * earlier date
  68      */
  69     @Test
  70     public void test2() {
  71         Date d = Date.valueOf("1961-08-30");
  72         Date d2 = new Date(System.currentTimeMillis());
  73         assertTrue(d2.after(d), "Error d2.after(d) = false");
  74     }
  75 
  76     /*
  77      * Validate that a Date.after() returns false when earlier date is compared
  78      * to later date
  79      */
  80     @Test
  81     public void test3() {
  82         Date d = Date.valueOf("1961-08-30");
  83         Date d2 = new Date(d.getTime());
  84         assertFalse(d.after(d2), "Error d.after(d2) = true");
  85     }
  86 
  87     /*
  88      * Validate that a Date.after() returns false when date compared to another
  89      * date created from the original date
  90      */
  91     @Test
  92     public void test4() {
  93         Date d = Date.valueOf("1961-08-30");
  94         Date d2 = new Date(d.getTime());
  95         assertFalse(d.after(d2), "Error d.after(d2) = true");
  96         assertFalse(d2.after(d), "Error d2.after(d) = true");
  97     }
  98 
  99     /*
 100      * Validate that a Date.before() returns false when same date is compared
 101      */
 102     @Test
 103     public void test5() {
 104         Date d = Date.valueOf("1961-08-30");
 105         assertFalse(d.before(d), "Error d.before(d) = true");
 106     }
 107 
 108     /*
 109      * Validate that a Date.before() returns true when earlier date is compared
 110      * to later date
 111      */
 112     @Test
 113     public void test6() {
 114         Date d = Date.valueOf("1961-08-30");
 115         Date d2 = new Date(System.currentTimeMillis());
 116         assertTrue(d.before(d2), "Error d.before(d2) = false");
 117     }
 118 
 119     /*
 120      * Validate that a Date.before() returns false when later date is compared
 121      * to earlier date
 122      */
 123     @Test
 124     public void test7() {
 125         Date d = Date.valueOf("1961-08-30");
 126         Date d2 = new Date(d.getTime());
 127         assertFalse(d2.before(d), "Error d2.before(d) = true");
 128     }
 129 
 130     /*
 131      * Validate that a Date.before() returns false when date compared to another
 132      * date created from the original date
 133      */
 134     @Test
 135     public void test8() {
 136         Date d = Date.valueOf("1961-08-30");
 137         Date d2 = new Date(d.getTime());
 138         assertFalse(d.before(d2), "Error d.before(d2) = true");
 139         assertFalse(d2.before(d), "Error d2.before(d) = true");
 140     }
 141 
 142     /*
 143      * Validate that a Date.compareTo returns 0 when both Date objects are the
 144      * same
 145      */
 146     @Test
 147     public void test9() {
 148         Date d = Date.valueOf("1961-08-30");
 149         assertTrue(d.compareTo(d) == 0, "Error d.compareTo(d) !=0");
 150     }
 151 
 152     /*
 153      * Validate that a Date.compareTo returns 0 when both Date objects represent
 154      * the same date
 155      */
 156     @Test
 157     public void test10() {
 158         Date d = Date.valueOf("1961-08-30");
 159         Date d2 = new Date(d.getTime());
 160         assertTrue(d.compareTo(d2) == 0, "Error d.compareTo(d2) !=0");
 161     }
 162 
 163     /*
 164      * Validate that a Date.compareTo returns -1 when comparing a date to a
 165      * later date
 166      */
 167     @Test
 168     public void test11() {
 169         Date d = Date.valueOf("1961-08-30");
 170         Date d2 = new Date(System.currentTimeMillis());
 171         assertTrue(d.compareTo(d2) == -1, "Error d.compareTo(d2) != -1");
 172     }
 173 
 174     /*
 175      * Validate that a Date.compareTo returns 1 when comparing a date to an
 176      * earlier date
 177      */
 178     @Test
 179     public void test12() {
 180         Date d = Date.valueOf("1961-08-30");
 181         Date d2 = new Date(System.currentTimeMillis());
 182         assertTrue(d2.compareTo(d) == 1, "Error d.compareTo(d2) != 1");
 183     }
 184 
 185     /*
 186      * Validate that a Date made from a LocalDate are equal
 187      */
 188     @Test
 189     public void test13() {
 190         Date d = Date.valueOf("1961-08-30");
 191         LocalDate ldt = d.toLocalDate();
 192         Date d2 = Date.valueOf(ldt);
 193         assertTrue(d.equals(d2), "Error d != d2");
 194     }
 195 
 196     /*
 197      * Validate that a Date LocalDate value, made from a LocalDate are equal
 198      */
 199     @Test
 200     public void test14() {
 201         LocalDate ldt = LocalDate.now();
 202         Date d = Date.valueOf(ldt);
 203         assertTrue(ldt.equals(d.toLocalDate()),
 204                 "Error LocalDate values are not equal");
 205     }
 206 
 207     /*
 208      * Validate an NPE occurs when a null LocalDate is passed to valueOf
 209      */
 210     @Test(expectedExceptions = NullPointerException.class)
 211     public void test15() throws Exception {
 212         LocalDate ld = null;
 213         Date.valueOf(ld);
 214     }
 215 
 216     /*
 217      * Validate an UnsupportedOperationException occurs when toInstant() is
 218      * called
 219      */
 220     @Test(expectedExceptions = UnsupportedOperationException.class)
 221     public void test16() throws Exception {
 222         Date d = Date.valueOf("1961-08-30");
 223         Instant instant = d.toInstant();
 224     }
 225 
 226     /*
 227      * Validate that two Date objects are equal when one is created from the
 228      * toString() of the other
 229      */
 230     @Test
 231     public void test17() {
 232         Date d = Date.valueOf("1961-08-30");
 233         Date d2 = Date.valueOf(d.toString());
 234         assertTrue(d.equals(d2) && d2.equals(d), "Error d != d2");
 235     }
 236 
 237     /*
 238      * Validate that two Date values one created using valueOf and another via a
 239      * constructor are equal
 240      */
 241     @Test
 242     public void test18() {
 243 
 244         Date d = Date.valueOf("1961-08-30");
 245         Date d2 = new Date(61, 7, 30);
 246         assertTrue(d.equals(d2), "Error d != d2");
 247     }
 248 
 249     /*
 250      * Validate that two Date values one created using getTime() of the other
 251      * are equal
 252      */
 253     @Test
 254     public void test19() {
 255 
 256         Date d = Date.valueOf("1961-08-30");
 257         Date d2 = new Date(d.getTime());
 258         assertTrue(d.equals(d2), "Error d != d2");
 259     }
 260 
 261     /*
 262      * Validate that a Date value is equal to itself
 263      */
 264     @Test
 265     public void test20() {
 266 
 267         Date d = Date.valueOf("1961-08-30");
 268         assertTrue(d.equals(d), "Error d != d");
 269     }
 270 
 271     /*
 272      * Validate an IllegalArgumentException is thrown for calling getHours
 273      */
 274     @Test(expectedExceptions = IllegalArgumentException.class)
 275     public void test21() throws Exception {
 276         Date d = Date.valueOf("1961-08-30");
 277         d.getHours();
 278     }
 279 
 280     /*
 281      * Validate an IllegalArgumentException is thrown for calling getMinutes
 282      */
 283     @Test(expectedExceptions = IllegalArgumentException.class)
 284     public void test22() throws Exception {
 285         Date d = Date.valueOf("1961-08-30");
 286         d.getMinutes();
 287     }
 288 
 289     /*
 290      * Validate an IllegalArgumentException is thrown for calling getSeconds
 291      */
 292     @Test(expectedExceptions = IllegalArgumentException.class)
 293     public void test23() throws Exception {
 294         Date d = Date.valueOf("1961-08-30");
 295         d.getSeconds();
 296     }
 297 
 298     /*
 299      * Validate an IllegalArgumentException is thrown for calling setHours
 300      */
 301     @Test(expectedExceptions = IllegalArgumentException.class)
 302     public void test24() throws Exception {
 303         Date d = Date.valueOf("1961-08-30");
 304         d.setHours(8);
 305     }
 306 
 307     /*
 308      * Validate an IllegalArgumentException is thrown for calling setMinutes
 309      */
 310     @Test(expectedExceptions = IllegalArgumentException.class)
 311     public void test25() throws Exception {
 312         Date d = Date.valueOf("1961-08-30");
 313         d.setMinutes(0);
 314     }
 315 
 316     /*
 317      * Validate an IllegalArgumentException is thrown for calling setSeconds
 318      */
 319     @Test(expectedExceptions = IllegalArgumentException.class)
 320     public void test26() throws Exception {
 321         Date d = Date.valueOf("1961-08-30");
 322         d.setSeconds(0);
 323     }
 324 
 325     /*
 326      * DataProvider used to provide Date which are not valid and are used
 327      * to validate that an IllegalArgumentException will be thrown from the
 328      * valueOf method
 329      */
 330     @DataProvider(name = "invalidDateValues")
 331     private Object[][] invalidDateValues() {
 332         return new Object[][]{
 333             {"20009-11-01"},
 334             {"09-11-01"},
 335             {"-11-01"},
 336             {"2009-111-01"},
 337             {"2009--01"},
 338             {"2009-13-01"},
 339             {"2009-11-011"},
 340             {"2009-11-"},
 341             {"2009-11-00"},
 342             {"2009-11-33"},
 343             {"--"},
 344             {""},
 345             {null},
 346             {"-"},
 347             {"2009"},
 348             {"2009-01"},
 349             {"---"},
 350             {"2009-13--1"},
 351             {"1900-1-0"},
 352             {"2009-01-01 10:50:01"},
 353             {"1996-12-10 12:26:19.1"},
 354             {"10:50:01"}
 355         };
 356     }
 357 
 358     /*
 359      * DataProvider used to provide Dates which are  valid and are used
 360      * to validate that an IllegalArgumentException will not be thrown from the
 361      * valueOf method and the corect value from toString() is returned
 362      */
 363     @DataProvider(name = "validDateValues")
 364     private Object[][] validDateValues() {
 365         return new Object[][]{
 366             {"2009-08-30", "2009-08-30"},
 367             {"2009-01-8", "2009-01-08"},
 368             {"2009-1-01", "2009-01-01"},
 369             {"2009-1-1", "2009-01-01"}
 370 
 371         };
 372     }
 373 }