|
20 | 20 | import org.junit.Test; |
21 | 21 | import org.mockito.Mockito; |
22 | 22 |
|
| 23 | +import java.lang.reflect.InvocationTargetException; |
| 24 | +import java.lang.reflect.Method; |
23 | 25 | import java.sql.SQLException; |
| 26 | +import java.util.Collections; |
24 | 27 | import java.util.Properties; |
25 | 28 |
|
26 | 29 | /** |
@@ -71,6 +74,49 @@ public void testNumExecuteRetries() { |
71 | 74 | Assert.assertEquals(10, connection.getNumStatementRetries(props)); |
72 | 75 | } |
73 | 76 |
|
| 77 | + /** Test case for |
| 78 | + * <a href="https://issues.apache.org/jira/browse/CALCITE-6781">[CALCITE-6781] |
| 79 | + * The isUpdateCapable method of calcite.avatica will incorrectly traverse |
| 80 | + * the returned result value</a>. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void testIsUpdateCapableSkipsRowCountWhenResultSetHasNoRows() throws Exception { |
| 84 | + AvaticaConnection connection = Mockito.mock( |
| 85 | + AvaticaConnection.class, Mockito.CALLS_REAL_METHODS); |
| 86 | + AvaticaStatement statement = Mockito.mock(AvaticaStatement.class); |
| 87 | + AvaticaResultSet resultSet = Mockito.mock(AvaticaResultSet.class); |
| 88 | + |
| 89 | + Meta.Signature signature = new Meta.Signature(Collections.<ColumnMetaData>emptyList(), null, |
| 90 | + Collections.<AvaticaParameter>emptyList(), Collections.<String, Object>emptyMap(), null, |
| 91 | + Meta.StatementType.INSERT); |
| 92 | + |
| 93 | + Mockito.when(statement.getSignature()).thenReturn(signature); |
| 94 | + Mockito.when(resultSet.next()).thenReturn(false); |
| 95 | + statement.updateCount = -1; |
| 96 | + statement.openResultSet = resultSet; |
| 97 | + |
| 98 | + invokeIsUpdateCapable(connection, statement); |
| 99 | + |
| 100 | + Assert.assertEquals(-1, statement.updateCount); |
| 101 | + Assert.assertSame(resultSet, statement.openResultSet); |
| 102 | + Mockito.verify(resultSet, Mockito.never()).getObject(AvaticaConnection.ROWCOUNT_COLUMN_NAME); |
| 103 | + } |
| 104 | + |
| 105 | + private static void invokeIsUpdateCapable( |
| 106 | + AvaticaConnection connection, AvaticaStatement statement) throws Exception { |
| 107 | + Method method = AvaticaConnection.class |
| 108 | + .getDeclaredMethod("isUpdateCapable", AvaticaStatement.class); |
| 109 | + method.setAccessible(true); |
| 110 | + try { |
| 111 | + method.invoke(connection, statement); |
| 112 | + } catch (InvocationTargetException e) { |
| 113 | + if (e.getCause() instanceof SQLException) { |
| 114 | + throw (SQLException) e.getCause(); |
| 115 | + } |
| 116 | + throw e; |
| 117 | + } |
| 118 | + } |
| 119 | + |
74 | 120 | } |
75 | 121 |
|
76 | 122 | // End AvaticaConnectionTest.java |
0 commit comments