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