|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.qa.sql.jdbc; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.PreparedStatement; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.ResultSetMetaData; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.sql.Timestamp; |
| 15 | +import java.util.Date; |
| 16 | + |
| 17 | +public class ResultSetTestCase extends JdbcIntegrationTestCase { |
| 18 | + public void testGettingTimestamp() throws Exception { |
| 19 | + long randomMillis = randomLongBetween(0, System.currentTimeMillis()); |
| 20 | + |
| 21 | + index("library", "1", builder -> { |
| 22 | + builder.field("name", "Don Quixote"); |
| 23 | + builder.field("page_count", 1072); |
| 24 | + builder.timeField("release_date", new Date(randomMillis)); |
| 25 | + builder.timeField("republish_date", null); |
| 26 | + }); |
| 27 | + index("library", "2", builder -> { |
| 28 | + builder.field("name", "1984"); |
| 29 | + builder.field("page_count", 328); |
| 30 | + builder.timeField("release_date", new Date(-649036800000L)); |
| 31 | + builder.timeField("republish_date", new Date(599616000000L)); |
| 32 | + }); |
| 33 | + |
| 34 | + try (Connection connection = esJdbc()) { |
| 35 | + try (PreparedStatement statement = connection.prepareStatement("SELECT name, release_date, republish_date FROM library")) { |
| 36 | + try (ResultSet results = statement.executeQuery()) { |
| 37 | + ResultSetMetaData resultSetMetaData = results.getMetaData(); |
| 38 | + |
| 39 | + results.next(); |
| 40 | + assertEquals(3, resultSetMetaData.getColumnCount()); |
| 41 | + assertEquals(randomMillis, results.getTimestamp("release_date").getTime()); |
| 42 | + assertEquals(randomMillis, results.getTimestamp(2).getTime()); |
| 43 | + assertTrue(results.getObject(2) instanceof Timestamp); |
| 44 | + assertEquals(randomMillis, ((Timestamp) results.getObject("release_date")).getTime()); |
| 45 | + |
| 46 | + assertNull(results.getTimestamp(3)); |
| 47 | + assertNull(results.getObject("republish_date")); |
| 48 | + |
| 49 | + assertTrue(results.next()); |
| 50 | + assertEquals(599616000000L, results.getTimestamp("republish_date").getTime()); |
| 51 | + assertEquals(-649036800000L, ((Timestamp) results.getObject(2)).getTime()); |
| 52 | + |
| 53 | + assertFalse(results.next()); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + * Checks StackOverflowError fix for https://github.com/elastic/elasticsearch/pull/31735 |
| 61 | + */ |
| 62 | + public void testNoInfiniteRecursiveGetObjectCalls() throws SQLException, IOException { |
| 63 | + index("library", "1", builder -> { |
| 64 | + builder.field("name", "Don Quixote"); |
| 65 | + builder.field("page_count", 1072); |
| 66 | + }); |
| 67 | + Connection conn = esJdbc(); |
| 68 | + PreparedStatement statement = conn.prepareStatement("SELECT * FROM library"); |
| 69 | + ResultSet results = statement.executeQuery(); |
| 70 | + |
| 71 | + try { |
| 72 | + results.next(); |
| 73 | + results.getObject("name"); |
| 74 | + results.getObject("page_count"); |
| 75 | + results.getObject(1); |
| 76 | + results.getObject(1, String.class); |
| 77 | + results.getObject("page_count", Integer.class); |
| 78 | + } catch (StackOverflowError soe) { |
| 79 | + fail("Infinite recursive call on getObject() method"); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments