test/java/sql/test/sql/TimeTests.java

Print this page




   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 }


   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 }