xborder commented on code in PR #1090:
URL: https://github.com/apache/arrow-java/pull/1090#discussion_r4527096251


##########
flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java:
##########
@@ -165,6 +182,109 @@ public void testExecuteShouldRunSelectQuery() throws 
SQLException {
         is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(-1L))));
   }
 
+  @Test
+  public void testExecuteReplacesStatementMapEntryWithPreparedStatement() 
throws SQLException {
+    final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) 
statement;
+    final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) 
connection;
+
+    assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+    final Object preparedStatement = 
arrowConnection.statementMap.get(arrowStatement.handle.id);
+
+    assertNotNull(preparedStatement);
+    assertSame(preparedStatement, 
arrowConnection.statementMap.get(arrowStatement.handle.id));
+    assertThat(preparedStatement, 
instanceOf(ArrowFlightPreparedStatement.class));
+  }
+
+  @Test
+  public void testExecutePrepareFailurePreservesServerError() {
+    final SQLException exception =
+        assertThrows(
+            SQLException.class, () -> statement.execute("SELECT * FROM 
unregistered_table"));
+
+    assertThat(exception.getMessage(), containsString("Query not found"));
+  }
+
+  @Test
+  public void testConnectionCloseClosesStatementAndResultSetAfterExecute() 
throws SQLException {
+    PRODUCER.clearActionTypeCounter();
+    final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) 
statement;
+    final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) 
connection;
+
+    assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+    final ResultSet resultSet = statement.getResultSet();
+    assertNotNull(resultSet);
+
+    connection.close();
+
+    assertThat(statement.isClosed(), is(true));
+    assertThat(resultSet.isClosed(), is(true));
+    
assertThat(arrowConnection.statementMap.containsKey(arrowStatement.handle.id), 
is(false));
+    assertThat(
+        PRODUCER
+            .getActionTypeCounter()
+            
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
+        is(1));
+  }
+
+  @Test
+  public void testExecuteQueryRestoresStatementMapEntryWithStatement() throws 
SQLException {
+    final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) 
statement;
+    final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) 
connection;
+
+    assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
+
+    try (ResultSet resultSet = statement.executeQuery(SAMPLE_QUERY_CMD)) {
+      assertThat(resultSet.next(), is(true));
+    }
+
+    assertSame(arrowStatement, 
arrowConnection.statementMap.get(arrowStatement.handle.id));
+  }
+
+  @Test
+  public void testExecuteQueryMapsPreparedStatementCloseFailureToSQLException()
+      throws SQLException {
+    final ArrowFlightStatement arrowStatement = (ArrowFlightStatement) 
statement;
+    final ArrowFlightConnection arrowConnection = (ArrowFlightConnection) 
connection;
+    final ResultSet previousResultSet = 
statement.executeQuery(SAMPLE_QUERY_CMD);
+    final ArrowFlightPreparedStatement preparedStatement = 
mock(ArrowFlightPreparedStatement.class);
+    final FlightRuntimeException closeFailure =
+        CallStatus.INTERNAL.withDescription("Prepared statement close 
failed").toRuntimeException();
+    doThrow(closeFailure).when(preparedStatement).closePreparedResources();
+    arrowConnection.statementMap.put(arrowStatement.handle.id, 
preparedStatement);
+
+    try {
+      final SQLException exception =
+          assertThrows(SQLException.class, () -> 
statement.executeQuery(SAMPLE_QUERY_CMD));
+
+      assertSame(closeFailure, exception.getCause());
+      assertThat(previousResultSet.isClosed(), is(true));
+    } finally {
+      arrowConnection.statementMap.put(arrowStatement.handle.id, 
arrowStatement);

Review Comment:
   It was used to force a prepared-statement cleanup failure and verify the 
resulting SQLException and result-set cleanup. I’ve changed the tests to inject 
the failure through the mock server instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to