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 18package libcore.java.sql; 19 20import java.sql.Timestamp; 21import java.util.TimeZone; 22import junit.framework.TestCase; 23 24public final class TimestampTest extends TestCase { 25 26 public void testToString() { 27 // Timestamp uses the current default timezone in toString() to convert to 28 // human-readable strings. 29 TimeZone defaultTimeZone = TimeZone.getDefault(); 30 TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 31 try { 32 Timestamp t1 = new Timestamp(Long.MIN_VALUE); 33 assertEquals("292278994-08-17 07:12:55.192", t1.toString()); 34 35 Timestamp t2 = new Timestamp(Long.MIN_VALUE + 1); 36 assertEquals("292278994-08-17 07:12:55.193", t2.toString()); 37 38 Timestamp t3 = new Timestamp(Long.MIN_VALUE + 807); 39 assertEquals("292278994-08-17 07:12:55.999", t3.toString()); 40 41 Timestamp t4 = new Timestamp(Long.MIN_VALUE + 808); 42 assertEquals("292269055-12-02 16:47:05.0", t4.toString()); 43 } finally { 44 TimeZone.setDefault(defaultTimeZone); 45 } 46 } 47 48 public void testValueOf() { 49 // Timestamp uses the current default timezone in valueOf(String) to convert 50 // from human-readable strings. 51 TimeZone defaultTimeZone = TimeZone.getDefault(); 52 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 53 try { 54 Timestamp t1 = Timestamp.valueOf("2001-12-31 21:45:57.123456789"); 55 assertEquals(1009835157000L + 123456789 / 1000000, t1.getTime()); 56 assertEquals(123456789, t1.getNanos()); 57 58 Timestamp t2 = Timestamp.valueOf("2001-01-02 01:05:07.123"); 59 assertEquals(978397507000L + 123000000 / 1000000, t2.getTime()); 60 assertEquals(123000000, t2.getNanos()); 61 62 Timestamp t3 = Timestamp.valueOf("2001-01-02 01:05:07"); 63 assertEquals(978397507000L, t3.getTime()); 64 assertEquals(0, t3.getNanos()); 65 } finally { 66 TimeZone.setDefault(defaultTimeZone); 67 } 68 } 69 70 public void testValueOfInvalid() { 71 try { 72 Timestamp.valueOf(""); 73 fail(); 74 } catch (IllegalArgumentException expected) { } 75 76 try { 77 Timestamp.valueOf("+2001-12-31"); 78 fail(); 79 } catch (IllegalArgumentException expected) { } 80 81 try { 82 Timestamp.valueOf("2001-+12-31"); 83 fail(); 84 } catch (IllegalArgumentException expected) { } 85 86 try { 87 Timestamp.valueOf("2001-12-+31"); 88 fail(); 89 } catch (IllegalArgumentException expected) { } 90 91 try { 92 Timestamp.valueOf("-2001-12-31"); 93 fail(); 94 } catch (IllegalArgumentException expected) { } 95 96 try { 97 Timestamp.valueOf("2001--12-31"); 98 fail(); 99 } catch (IllegalArgumentException expected) { } 100 101 try { 102 Timestamp.valueOf("2001-12--31"); 103 fail(); 104 } catch (IllegalArgumentException expected) { } 105 106 try { 107 Timestamp.valueOf("2001--"); 108 fail(); 109 } catch (IllegalArgumentException expected) { } 110 111 try { 112 Timestamp.valueOf("2001--31"); 113 fail(); 114 } catch (IllegalArgumentException expected) { } 115 116 try { 117 Timestamp.valueOf("-12-31"); 118 fail(); 119 } catch (IllegalArgumentException expected) { } 120 121 try { 122 Timestamp.valueOf("-12-"); 123 fail(); 124 } catch (IllegalArgumentException expected) { } 125 126 try { 127 Timestamp.valueOf("--31"); 128 fail(); 129 } catch (IllegalArgumentException expected) { } 130 131 try { 132 Timestamp.valueOf("2001-12-31 21:45:57.+12345678"); 133 fail(); 134 } catch (IllegalArgumentException expected) { } 135 136 try { 137 Timestamp.valueOf("2001-12-31 21:45:57.-12345678"); 138 fail(); 139 } catch (IllegalArgumentException expected) { } 140 141 try { 142 Timestamp.valueOf("2001-12-31 21:45:57.1234567891"); 143 fail(); 144 } catch (IllegalArgumentException expected) { } 145 } 146 147} 148