1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.sql.tests.java.sql; 19 20 import java.sql.Date; 21 import java.util.Calendar; 22 import java.util.TimeZone; 23 24 import junit.framework.TestCase; 25 26 /** 27 * JUnit Testcase for the java.sql.Date class 28 * 29 */ 30 public class DateTest extends TestCase { 31 32 // A calendar object created in the GMT time zone 33 static Calendar aCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); 34 35 // Some interesting millisecond time values 36 // These millisecond times are all in GMT, effectively 37 static long TIME_AN_HOUR = 3600000; // 1000 * 60 * 60 ms 38 39 static long TIME_EPOCH = 0; 40 41 static long TIME_NOW = System.currentTimeMillis(); 42 43 static long TIME_NEGATIVE = -3600001; 44 45 static long TIME_TESTDATE1 = getTime(1999, Calendar.DECEMBER, 31, 23, 59, 46 59); 47 48 static long TIME_TESTDATE2 = getTime(2010, Calendar.JUNE, 10, 20, 3, 16); 49 50 static long TIME_TESTDATE3 = getTime(1931, Calendar.APRIL, 21, 1, 25, 1); 51 52 static long TIME_LOWERLIMIT = Long.MIN_VALUE; 53 54 static long TIME_UPPERLIMIT = Long.MAX_VALUE; 55 56 // Date strings 57 static String SQL_DATESTRING1 = "1999-12-31"; 58 59 static String SQL_DATESTRING2 = "2010-06-10"; 60 61 static String SQL_DATESTRING3 = "1931-04-21"; 62 63 static String SQL_EPOCHSTRING = "1970-01-01"; 64 65 static String SQL_DATEDAY1 = "1970-01-02"; 66 67 static String SQL_NEGATIVE = "1969-12-31"; 68 69 static long[] TIME_ARRAY = new long[] { TIME_TESTDATE1, TIME_TESTDATE2, 70 TIME_TESTDATE3, TIME_NEGATIVE, TIME_EPOCH }; 71 72 // Date string array for London (GMT) 73 static String[] SQL_DATEARRAY = new String[] { SQL_DATESTRING1, 74 SQL_DATESTRING2, SQL_DATESTRING3, SQL_NEGATIVE, SQL_EPOCHSTRING }; 75 76 // Date string array for New York - sometimes a day earlier than London 77 static String[] SQL_NYARRAY = new String[] { "1999-12-31", "2010-06-10", 78 "1931-04-20", "1969-12-31", "1969-12-31" }; 79 80 // Date string for Tokyo 81 static String[] SQL_JAPANARRAY = new String[] { "2000-01-01", "2010-06-11", 82 "1931-04-21", "1970-01-01", "1970-01-01" }; 83 84 static String[][] SQL_TZ_DATEARRAYS = new String[][] { SQL_DATEARRAY, 85 SQL_NYARRAY, SQL_JAPANARRAY }; 86 87 // Timezones 88 static String TZ_LONDON = "Europe/London"; // Note: != GMT 89 90 static String TZ_PACIFIC = "America/Los_Angeles"; // GNT - 8 91 92 static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9 93 94 static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN }; 95 96 /* 97 * Helper method to create a long milliseconds time from a supplied date and 98 * time 99 */ getTime(int year, int month, int date, int hour, int minute, int second)100 static private long getTime(int year, int month, int date, int hour, 101 int minute, int second) { 102 aCal.set(year, month, date, hour, minute, second); 103 return aCal.getTimeInMillis(); 104 } // end method getTime( int, int, int, int, int, int ) 105 106 /* 107 * Test of the Date(int, int, int) constructor - now deprecated but still 108 * functioning 109 */ 110 @SuppressWarnings("deprecation") testDateintintint()111 public void testDateintintint() { 112 113 int init1[] = { 99, 8099, 9000, 99999, 99, 99, -1, -100 }; 114 int init2[] = { 11, 0, 0, 0, 999, 0, 0, -111 }; 115 int init3[] = { 31, 0, 0, 0, 0, 999, 0, -999 }; 116 117 for (int i = 0; i < init1.length; i++) { 118 Date theDate = new Date(init1[i], init2[i], init3[i]); 119 assertNotNull(theDate); 120 } // end for 121 122 } // end method testDateintintint 123 124 /* 125 * Test of the Date( long ) constructor 126 */ testDatelong()127 public void testDatelong() { 128 129 long init1[] = { TIME_TESTDATE1, TIME_TESTDATE2, TIME_TESTDATE3, 130 TIME_NEGATIVE, TIME_LOWERLIMIT, TIME_UPPERLIMIT, TIME_EPOCH, 131 TIME_NOW }; 132 133 for (long element : init1) { 134 Date theDate = new Date(element); 135 assertNotNull(theDate); 136 } // end for 137 138 } // end method testDatelong 139 140 /* 141 * Test of the (deprecated) int Date.getHours() method - which always throws 142 * an IllegalArgumentException 143 */ 144 @SuppressWarnings("deprecation") testGetHours()145 public void testGetHours() { 146 Date theDate = new Date(TIME_TESTDATE1); 147 try { 148 theDate.getHours(); 149 fail("Should throw IllegalArgumentException."); 150 } catch (IllegalArgumentException ie) { 151 // expected 152 } // end try 153 } // end method testGetHours() 154 155 /* 156 * Test of the (deprecated) int Date.getMinutes() method - which always 157 * throws an IllegalArgumentException 158 */ 159 @SuppressWarnings("deprecation") testGetMinutes()160 public void testGetMinutes() { 161 Date theDate = new Date(TIME_TESTDATE1); 162 try { 163 theDate.getMinutes(); 164 fail("Should throw IllegalArgumentException."); 165 } catch (IllegalArgumentException ie) { 166 // expected 167 } // end try 168 } // end method testGetMinutes() 169 170 /* 171 * Test of the (deprecated) int Date.getSeconds() method - which always 172 * throws an IllegalArgumentException 173 */ 174 @SuppressWarnings("deprecation") testGetSeconds()175 public void testGetSeconds() { 176 Date theDate = new Date(TIME_TESTDATE1); 177 try { 178 theDate.getSeconds(); 179 fail("Should throw IllegalArgumentException."); 180 } catch (IllegalArgumentException ie) { 181 // expected 182 } // end try 183 } // end method testGetSeconds() 184 185 /* 186 * Test of the (deprecated) Date.setHours( int ) method - which always 187 * throws an IllegalArgumentException 188 */ 189 @SuppressWarnings("deprecation") testSetHours()190 public void testSetHours() { 191 Date theDate = new Date(TIME_TESTDATE1); 192 try { 193 theDate.setHours(22); 194 fail("Should throw IllegalArgumentException."); 195 } catch (IllegalArgumentException ie) { 196 // expected 197 } // end try 198 } // end method testSetHours( int ) 199 200 /* 201 * Test of the (deprecated) Date.setMinutes( int ) method - which always 202 * throws an IllegalArgumentException 203 */ 204 @SuppressWarnings("deprecation") testSetMinutes()205 public void testSetMinutes() { 206 Date theDate = new Date(TIME_TESTDATE1); 207 try { 208 theDate.setMinutes(54); 209 fail("Should throw IllegalArgumentException."); 210 } catch (IllegalArgumentException ie) { 211 // expected 212 } // end try 213 214 } // end method testSetMinutes( int ) 215 216 /* 217 * Test of the (deprecated) Date.setSeconds( int ) method - which always 218 * throws an IllegalArgumentException 219 */ 220 @SuppressWarnings("deprecation") testSetSeconds()221 public void testSetSeconds() { 222 Date theDate = new Date(TIME_TESTDATE1); 223 try { 224 theDate.setSeconds(36); 225 fail("Should throw IllegalArgumentException."); 226 } catch (IllegalArgumentException ie) { 227 // expected 228 } // end try 229 } // end method testSetSeconds( int ) 230 231 /* 232 * Test of the String Date.toString() method This method is sensitive to the 233 * time zone setting and this test sets the time zone before calling the 234 * toString() method. 235 */ testToString()236 public void testToString() { 237 // Loop through the timezones testing the String conversion for each 238 for (int i = 0; i < TIMEZONES.length; i++) { 239 testToString(TIMEZONES[i], TIME_ARRAY, SQL_TZ_DATEARRAYS[i]); 240 } // end for 241 242 } // end method testToString() 243 testToString(String timeZone, long[] theDates, String[] theDateStrings)244 private void testToString(String timeZone, long[] theDates, String[] theDateStrings) { 245 // Set the timezone 246 TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); 247 248 for (int i = 0; i < theDates.length; i++) { 249 // Create the Date object 250 Date theDate = new Date(theDates[i]); 251 // Convert to a date string ... and compare 252 String JDBCString = theDate.toString(); 253 assertEquals(theDateStrings[i], JDBCString); 254 } // end for 255 256 } // end testToString( String, long[], String[] ) 257 258 /* 259 * Test of the void setTime(int) method This does depend on the Time Zone 260 * settings and sets up the time zone to one of a group of specific time 261 * zones and tests the method using each of these time zones in turn. 262 */ testSetTimelong()263 public void testSetTimelong() { 264 265 // Loop over the array of test timezones 266 for (int i = 0; i < TIMEZONES.length; i++) { 267 testSetTimelong(TIMEZONES[i], SQL_TZ_DATEARRAYS[i]); 268 } // end for 269 270 } // end method testSetTimelong() 271 272 /* 273 * Internal method for testing Date.setTime with a specific time zone 274 */ testSetTimelong(String timeZoneName, String[] dateArray)275 private void testSetTimelong(String timeZoneName, String[] dateArray) { 276 277 if (timeZoneName != null) { 278 TimeZone.setDefault(TimeZone.getTimeZone(timeZoneName)); 279 } // end if 280 281 Date theDate = new Date(TIME_TESTDATE1); 282 283 // Loop over the array of test times & dates 284 for (int i = 0; i < dateArray.length; i++) { 285 theDate.setTime(TIME_ARRAY[i]); 286 assertEquals(dateArray[i], theDate.toString()); 287 } // end for 288 289 } // end method testSetTimelong() 290 291 /* 292 * Test of the Date.valueOf( String ) method This test is not dependent on 293 * the default Time Zone setting 294 */ testValueOf()295 public void testValueOf() { 296 String SQL_NOTVALID1 = "ABCDEF"; // Invalid date string 297 String SQL_NOTVALID2 = "12321.43.56"; // Invalid date string 298 String SQL_NOTVALID3 = null; // Invalid date string 299 String[] SQL_INVALIDARRAY = { SQL_NOTVALID1, SQL_NOTVALID2, 300 SQL_NOTVALID3 }; 301 302 Date theDate; 303 304 for (String element : SQL_DATEARRAY) { 305 theDate = Date.valueOf(element); 306 assertEquals(element, theDate.toString()); 307 } // end for 308 309 for (String element : SQL_INVALIDARRAY) { 310 try { 311 theDate = Date.valueOf(element); 312 fail("Should throw IllegalArgumentException."); 313 } catch (IllegalArgumentException e) { 314 // expected 315 } // end try 316 } // end for 317 318 } // end method testValueOf() 319 320 /** 321 * @tests java.sql.Date#valueOf(String ) 322 */ test_valueOf_IllegalArgumentException()323 public void test_valueOf_IllegalArgumentException() { 324 try { 325 Date.valueOf("1996-10-07-01"); 326 fail("should throw NumberFormatException"); 327 } catch (NumberFormatException e) { 328 // expected 329 } 330 331 try { 332 Date.valueOf("-10-07-01"); 333 fail("should throw IllegalArgumentException"); 334 } catch (IllegalArgumentException e) { 335 // expected 336 } 337 338 try { 339 Date.valueOf("--01"); 340 fail("should throw IllegalArgumentException"); 341 } catch (IllegalArgumentException e) { 342 // expected 343 } 344 345 try { 346 Date.valueOf("1991--"); 347 fail("should throw IllegalArgumentException"); 348 } catch (IllegalArgumentException e) { 349 // expected 350 } 351 352 try { 353 Date.valueOf("-01-"); 354 fail("should throw IllegalArgumentException"); 355 } catch (IllegalArgumentException e) { 356 // expected 357 } 358 359 try { 360 Date.valueOf("-10-w2-01"); 361 fail("should throw IllegalArgumentException"); 362 } catch (IllegalArgumentException e) { 363 // expected 364 } 365 366 try { 367 Date.valueOf("07-w2-"); 368 fail("should throw IllegalArgumentException"); 369 } catch (IllegalArgumentException e) { 370 // expected 371 } 372 373 try { 374 Date.valueOf("1997-w2-w2"); 375 fail("should throw NumberFormatException"); 376 } catch (NumberFormatException e) { 377 // expected 378 } 379 380 try { 381 Date.valueOf("1996--01"); 382 fail("should throw NumberFormatException"); 383 } catch (NumberFormatException e) { 384 // expected 385 } 386 } 387 388 // Reset defualt timezone 389 static TimeZone defaultTimeZone = TimeZone.getDefault(); 390 tearDown()391 protected void tearDown(){ 392 TimeZone.setDefault(defaultTimeZone); 393 } 394 395 } // end class DateTest 396 397