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