|
93 | 93 | import java.util.concurrent.TimeUnit;
|
94 | 94 |
|
95 | 95 | import javax.sql.rowset.CachedRowSet;
|
| 96 | +import javax.sql.rowset.JdbcRowSet; |
| 97 | +import javax.sql.rowset.RowSetFactory; |
| 98 | +import javax.sql.rowset.RowSetProvider; |
96 | 99 |
|
97 | 100 | import org.junit.jupiter.api.Disabled;
|
98 | 101 | import org.junit.jupiter.api.Test;
|
@@ -8047,7 +8050,7 @@ void testBug102678() throws Exception {
|
8047 | 8050 | }
|
8048 | 8051 |
|
8049 | 8052 | /**
|
8050 |
| - * Tests for Bug#68608 (Bug#16690898), UpdatableResultSet does not properly handle unsigned primary key. |
| 8053 | + * Tests fix for Bug#68608 (Bug#16690898), UpdatableResultSet does not properly handle unsigned primary key. |
8051 | 8054 | *
|
8052 | 8055 | * @throws Exception
|
8053 | 8056 | */
|
@@ -8108,4 +8111,93 @@ public void testBug68608() throws Exception {
|
8108 | 8111 | assertFalse(this.rs.next());
|
8109 | 8112 | }
|
8110 | 8113 |
|
| 8114 | + /** |
| 8115 | + * Tests fix for Bug#107215 (Bug#34139593), ClassCastException: java.time.LocalDateTime cannot be cast to java.sql.Timestamp. |
| 8116 | + * |
| 8117 | + * Was failing in CachedRowSet.getDate() and CachedRowSet.getTimestamp() because ResultSet.getObject() returns java.time.LocalDateTime while the code in |
| 8118 | + * CachedRowSetImpl tries to cast the value to java.sql.Timestamp. |
| 8119 | + * See also: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/com/sun/rowset/CachedRowSetImpl.java#l2140 |
| 8120 | + * See also: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/com/sun/rowset/CachedRowSetImpl.java#l619 |
| 8121 | + * |
| 8122 | + * @throws Exception |
| 8123 | + */ |
| 8124 | + @Test |
| 8125 | + void testBug107215() throws Exception { |
| 8126 | + createTable("testBug107215", "(dt DATETIME, ts TIMESTAMP)"); |
| 8127 | + this.stmt.execute("INSERT INTO testBug107215 VALUES(NOW(), NOW())"); |
| 8128 | + |
| 8129 | + final String sql = "SELECT * FROM testBug107215"; |
| 8130 | + final String testDbUrl = dbUrl + (dbUrl.contains("?") ? "&" : "?"); |
| 8131 | + |
| 8132 | + try (Connection testConn = getConnectionWithProps("treatMysqlDatetimeAsTimestamp=false")) { |
| 8133 | + Statement testStmt = testConn.createStatement(); |
| 8134 | + this.rs = testStmt.executeQuery(sql); |
| 8135 | + assertTrue(this.rs.next()); |
| 8136 | + assertEquals(LocalDateTime.class, this.rs.getObject(1).getClass()); |
| 8137 | + assertEquals(Timestamp.class, this.rs.getObject(2).getClass()); |
| 8138 | + assertEquals(Date.class, this.rs.getDate(1).getClass()); |
| 8139 | + assertEquals(Date.class, this.rs.getDate(2).getClass()); |
| 8140 | + assertEquals(Timestamp.class, this.rs.getTimestamp(1).getClass()); |
| 8141 | + assertEquals(Timestamp.class, this.rs.getTimestamp(2).getClass()); |
| 8142 | + |
| 8143 | + RowSetFactory rowSetFact = RowSetProvider.newFactory(); |
| 8144 | + JdbcRowSet testRowSet1 = rowSetFact.createJdbcRowSet(); |
| 8145 | + testRowSet1.setCommand(sql); |
| 8146 | + testRowSet1.setUrl(testDbUrl + "treatMysqlDatetimeAsTimestamp=false"); |
| 8147 | + testRowSet1.execute(); |
| 8148 | + assertTrue(testRowSet1.next()); |
| 8149 | + assertEquals(LocalDateTime.class, testRowSet1.getObject(1).getClass()); |
| 8150 | + assertEquals(Timestamp.class, testRowSet1.getObject(2).getClass()); |
| 8151 | + assertEquals(Date.class, testRowSet1.getDate(1).getClass()); |
| 8152 | + assertEquals(Date.class, testRowSet1.getDate(2).getClass()); |
| 8153 | + assertEquals(Timestamp.class, testRowSet1.getTimestamp(1).getClass()); |
| 8154 | + assertEquals(Timestamp.class, testRowSet1.getTimestamp(2).getClass()); |
| 8155 | + |
| 8156 | + CachedRowSet testRowSet2 = rowSetFact.createCachedRowSet(); |
| 8157 | + testRowSet2.populate(testStmt.executeQuery(sql)); |
| 8158 | + assertTrue(testRowSet2.next()); |
| 8159 | + assertEquals(LocalDateTime.class, testRowSet2.getObject(1).getClass()); |
| 8160 | + assertEquals(Timestamp.class, testRowSet2.getObject(2).getClass()); |
| 8161 | + assertThrows(ClassCastException.class, () -> testRowSet2.getDate(1).getClass()); // Non-expected behavior. |
| 8162 | + assertEquals(Date.class, testRowSet2.getDate(2).getClass()); |
| 8163 | + assertThrows(ClassCastException.class, () -> testRowSet2.getTimestamp(1).getClass()); // Non-expected behavior. |
| 8164 | + assertEquals(Timestamp.class, testRowSet2.getTimestamp(2).getClass()); |
| 8165 | + } |
| 8166 | + |
| 8167 | + try (Connection testConn = getConnectionWithProps("treatMysqlDatetimeAsTimestamp=true")) { |
| 8168 | + Statement testStmt = testConn.createStatement(); |
| 8169 | + this.rs = testStmt.executeQuery(sql); |
| 8170 | + assertTrue(this.rs.next()); |
| 8171 | + assertEquals(Timestamp.class, this.rs.getObject(1).getClass()); |
| 8172 | + assertEquals(Timestamp.class, this.rs.getObject(2).getClass()); |
| 8173 | + assertEquals(Date.class, this.rs.getDate(1).getClass()); |
| 8174 | + assertEquals(Date.class, this.rs.getDate(2).getClass()); |
| 8175 | + assertEquals(Timestamp.class, this.rs.getTimestamp(1).getClass()); |
| 8176 | + assertEquals(Timestamp.class, this.rs.getTimestamp(2).getClass()); |
| 8177 | + |
| 8178 | + RowSetFactory rowSetFact = RowSetProvider.newFactory(); |
| 8179 | + JdbcRowSet testRowSet1 = rowSetFact.createJdbcRowSet(); |
| 8180 | + testRowSet1.setCommand(sql); |
| 8181 | + testRowSet1.setUrl(testDbUrl + "treatMysqlDatetimeAsTimestamp=true"); |
| 8182 | + testRowSet1.execute(); |
| 8183 | + assertTrue(testRowSet1.next()); |
| 8184 | + assertEquals(Timestamp.class, testRowSet1.getObject(1).getClass()); |
| 8185 | + assertEquals(Timestamp.class, testRowSet1.getObject(2).getClass()); |
| 8186 | + assertEquals(Date.class, testRowSet1.getDate(1).getClass()); |
| 8187 | + assertEquals(Date.class, testRowSet1.getDate(2).getClass()); |
| 8188 | + assertEquals(Timestamp.class, testRowSet1.getTimestamp(1).getClass()); |
| 8189 | + assertEquals(Timestamp.class, testRowSet1.getTimestamp(2).getClass()); |
| 8190 | + |
| 8191 | + CachedRowSet testRowSet2 = rowSetFact.createCachedRowSet(); |
| 8192 | + testRowSet2.populate(testStmt.executeQuery(sql)); |
| 8193 | + assertTrue(testRowSet2.next()); |
| 8194 | + assertEquals(Timestamp.class, testRowSet2.getObject(1).getClass()); |
| 8195 | + assertEquals(Timestamp.class, testRowSet2.getObject(2).getClass()); |
| 8196 | + assertEquals(Date.class, testRowSet2.getDate(1).getClass()); // Expected behavior. |
| 8197 | + assertEquals(Date.class, testRowSet2.getDate(2).getClass()); |
| 8198 | + assertEquals(Timestamp.class, testRowSet2.getTimestamp(1).getClass()); // Expected behavior. |
| 8199 | + assertEquals(Timestamp.class, testRowSet2.getTimestamp(2).getClass()); |
| 8200 | + } |
| 8201 | + } |
| 8202 | + |
8111 | 8203 | }
|
0 commit comments