/luni/src/test/java/libcore/java/sql/TimestampTest.java

https://gitlab.com/cde/debian_android-tools_android-platform-libcore · Java · 155 lines · 107 code · 27 blank · 21 comment · 0 complexity · adc3cfea2b7d194d73c1d46b1727b0cb MD5 · raw file

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