001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.java.sql;
019:
020: import java.sql.Time;
021: import java.util.TimeZone;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * JUnit Testcase for the java.sql.Time class
027: *
028: */
029: public class TimeTest extends TestCase {
030:
031: static long TIME_TEST1 = 38720000; // 10:45:20 GMT
032:
033: static long TIME_TEST2 = 80279000; // 22:17:59 GMT
034:
035: static long TIME_TEST3 = -38720000; // 13:14:40 GMT
036:
037: static String STRING_TEST1 = "10:45:20";
038:
039: static String STRING_TEST2 = "22:17:59";
040:
041: static String STRING_TEST3 = "13:14:40";
042:
043: static String STRING_INVALID1 = "ABCDEFGHI";
044:
045: static String STRING_INVALID2 = "233104";
046:
047: static String STRING_INVALID3 = "21-43-48";
048:
049: static String STRING_OUTRANGE = "35:99:66";
050:
051: static long[] TIME_ARRAY = { TIME_TEST1, TIME_TEST2, TIME_TEST3 };
052:
053: static String[] STRING_GMT_ARRAY = { STRING_TEST1, STRING_TEST2,
054: STRING_TEST3 };
055:
056: static String[] STRING_LA_ARRAY = { "02:45:20", "14:17:59",
057: "05:14:40" };
058:
059: static String[] STRING_JP_ARRAY = { "19:45:20", "07:17:59",
060: "22:14:40" };
061:
062: static String[] INVALID_STRINGS = { STRING_INVALID1,
063: STRING_INVALID2, STRING_INVALID3 };
064:
065: // Timezones
066: static String TZ_LONDON = "GMT"; // GMT (!) PS London != GMT (?!?)
067:
068: static String TZ_PACIFIC = "America/Los_Angeles"; // GMT - 8
069:
070: static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9
071:
072: static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN };
073:
074: static String[][] STRING_ARRAYS = { STRING_GMT_ARRAY,
075: STRING_LA_ARRAY, STRING_JP_ARRAY };
076:
077: @SuppressWarnings("deprecation")
078: public void testTimeintintint() {
079: Time theTime = new Time(10, 45, 20);
080:
081: // The date should have been created
082: assertNotNull(theTime);
083: } // end method testTimeintintint()
084:
085: public void testTime() {
086: Time theTime = new Time(TIME_TEST1);
087:
088: // The date should have been created
089: assertNotNull(theTime);
090: } // end method testTime()
091:
092: public void testToString() {
093: // Loop through the timezones testing the String conversion for each
094: for (int i = 0; i < TIME_ARRAY.length; i++) {
095: testToString(TIMEZONES[i], TIME_ARRAY, STRING_ARRAYS[i]);
096: } // end for
097:
098: } // end method test
099:
100: private void testToString(String timeZone, long[] theTimes,
101: String[] theTimeStrings) {
102: // Set the timezone
103: TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
104:
105: for (int i = 0; i < theTimes.length; i++) {
106: // Create the Time object
107: Time theTime = new Time(theTimes[i]);
108: // Convert to a time string ... and compare
109: String JDBCString = theTime.toString();
110: assertEquals(theTimeStrings[i], JDBCString);
111: } // end for
112:
113: } // end testToString( String, long[], String[] )
114:
115: /*
116: * Method test for valueOf
117: */
118: public void testValueOfString() {
119: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
120:
121: Time[] theReturns = { new Time(38720000), new Time(80279000),
122: new Time(47680000) };
123: String[] validTime = { "10:45:20", "22:17:59", "13:14:40", };
124: String[] invalidTime = { null, "ABCDEFGHI", "233104",
125: "21-43-48" };
126:
127: for (int i = 0; i < validTime.length; i++) {
128: Time theReturn = Time.valueOf(validTime[i]);
129: assertEquals(theReturns[i], theReturn);
130: } // end for
131:
132: for (String element : invalidTime) {
133: try {
134: Time.valueOf(element);
135: fail("Should throw IllegalArgumentException");
136: } catch (IllegalArgumentException e) {
137: // expected
138: }
139: }
140:
141: } // end method testValueOfString
142:
143: public void testSetTime() {
144: // Ensure that the timezone is set to GMT
145: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
146:
147: Time theTime = new Time(TIME_TEST1);
148: assertEquals(STRING_TEST1, theTime.toString());
149:
150: theTime.setTime(TIME_TEST2);
151: assertEquals(STRING_TEST2, theTime.toString());
152: } // end method testSetTime()
153:
154: @SuppressWarnings("deprecation")
155: public void testSetDate() {
156: Time theTime = new Time(TIME_TEST1);
157:
158: try {
159: theTime.setDate(10);
160: fail("Should throw IllegalArgumentException.");
161: } catch (IllegalArgumentException e) {
162: // expected
163: } // end try
164: } // end method testSetDate()
165:
166: @SuppressWarnings("deprecation")
167: public void testSetMonth() {
168: Time theTime = new Time(TIME_TEST1);
169:
170: try {
171: theTime.setMonth(2);
172: fail("Should throw IllegalArgumentException.");
173: } catch (IllegalArgumentException e) {
174: // expected
175: } // end try
176: } // end method testSetMonth()
177:
178: @SuppressWarnings("deprecation")
179: public void testSetYear() {
180: Time theTime = new Time(TIME_TEST1);
181:
182: try {
183: theTime.setYear(99);
184: fail("Should throw IllegalArgumentException.");
185: } catch (IllegalArgumentException e) {
186: // expected
187: } // end try
188: } // end method testSetYear()
189:
190: @SuppressWarnings("deprecation")
191: public void testGetDate() {
192: Time theTime = new Time(TIME_TEST1);
193:
194: try {
195: theTime.getDate();
196: fail("Should throw IllegalArgumentException.");
197: } catch (IllegalArgumentException e) {
198: // expected
199: } // end try
200: } // end method test
201:
202: @SuppressWarnings("deprecation")
203: public void testGetDay() {
204: Time theTime = new Time(TIME_TEST1);
205:
206: try {
207: theTime.getDay();
208: fail("Should throw IllegalArgumentException.");
209: } catch (IllegalArgumentException e) {
210: // expected
211: } // end try
212: } // end method test
213:
214: @SuppressWarnings("deprecation")
215: public void testGetMonth() {
216: Time theTime = new Time(TIME_TEST1);
217:
218: try {
219: theTime.getMonth();
220: fail("Should throw IllegalArgumentException.");
221: } catch (IllegalArgumentException e) {
222: // expected
223: } // end try
224: } // end method test
225:
226: @SuppressWarnings("deprecation")
227: public void testGetYear() {
228: Time theTime = new Time(TIME_TEST1);
229:
230: try {
231: theTime.getYear();
232: fail("Should throw IllegalArgumentException.");
233: } catch (IllegalArgumentException e) {
234: // expected
235: } // end try
236: } // end method test
237:
238: /**
239: * @tests java.sql.Time#valueOf(String )
240: */
241: public void test_valueOf_IllegalArgumentException() {
242: try {
243: Time.valueOf("15:43:12:34");
244: fail("should throw NumberFormatException");
245: } catch (NumberFormatException e) {
246: // expected
247: }
248:
249: try {
250: Time.valueOf(":10:07:01");
251: fail("should throw IllegalArgumentException");
252: } catch (IllegalArgumentException e) {
253: // expected
254: }
255:
256: try {
257: Time.valueOf("::01");
258: fail("should throw IllegalArgumentException");
259: } catch (IllegalArgumentException e) {
260: // expected
261: }
262:
263: try {
264: Time.valueOf("11::");
265: fail("should throw IllegalArgumentException");
266: } catch (IllegalArgumentException e) {
267: // expected
268: }
269:
270: try {
271: Time.valueOf(":01:");
272: fail("should throw IllegalArgumentException");
273: } catch (IllegalArgumentException e) {
274: // expected
275: }
276:
277: try {
278: Time.valueOf(":10:w2:01");
279: fail("should throw IllegalArgumentException");
280: } catch (IllegalArgumentException e) {
281: // expected
282: }
283:
284: try {
285: Time.valueOf("07:w2:");
286: fail("should throw IllegalArgumentException");
287: } catch (IllegalArgumentException e) {
288: // expected
289: }
290:
291: try {
292: Time.valueOf("17:w2:w2");
293: fail("should throw NumberFormatException");
294: } catch (NumberFormatException e) {
295: // expected
296: }
297:
298: try {
299: Time.valueOf("16::01");
300: fail("should throw NumberFormatException");
301: } catch (NumberFormatException e) {
302: // expected
303: }
304: }
305: } // end class TimeTest
|