|
| 1 | +package org.utplsql.api.outputBuffer; |
| 2 | + |
| 3 | +import oracle.jdbc.OracleTypes; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.utplsql.api.reporter.Reporter; |
| 7 | + |
| 8 | +import java.sql.CallableStatement; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.hamcrest.Matchers.contains; |
| 16 | +import static org.hamcrest.Matchers.sameInstance; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | +import static org.mockito.ArgumentMatchers.anyString; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +/** |
| 22 | + * Connection pools (e.g. HikariCP) return their own proxy statements, which are not |
| 23 | + * {@link oracle.jdbc.OracleCallableStatement} instances. The output buffer must work with plain JDBC statements. |
| 24 | + */ |
| 25 | +class DefaultOutputBufferTest { |
| 26 | + |
| 27 | + private Reporter reporter; |
| 28 | + private Connection connection; |
| 29 | + private CallableStatement callableStatement; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() throws SQLException { |
| 33 | + reporter = mock(Reporter.class); |
| 34 | + when(reporter.isInit()).thenReturn(true); |
| 35 | + when(reporter.getTypeName()).thenReturn("UT_DOCUMENTATION_REPORTER"); |
| 36 | + when(reporter.getId()).thenReturn("ABC123"); |
| 37 | + |
| 38 | + ResultSet resultSet = mock(ResultSet.class); |
| 39 | + when(resultSet.next()).thenReturn(true, true, false); |
| 40 | + when(resultSet.getString("text")).thenReturn("line 1", "line 2"); |
| 41 | + |
| 42 | + // A plain CallableStatement, as returned by a connection pool proxy |
| 43 | + callableStatement = mock(CallableStatement.class); |
| 44 | + when(callableStatement.getObject("lines_cursor")).thenReturn(resultSet); |
| 45 | + |
| 46 | + connection = mock(Connection.class); |
| 47 | + when(connection.prepareCall(anyString())).thenReturn(callableStatement); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void fetchAllWorksWithNonOracleCallableStatement() throws SQLException { |
| 52 | + List<String> lines = new DefaultOutputBuffer(reporter).fetchAll(connection); |
| 53 | + |
| 54 | + assertThat(lines, contains("line 1", "line 2")); |
| 55 | + verify(callableStatement).setString("reporter_id", "ABC123"); |
| 56 | + verify(callableStatement).registerOutParameter("lines_cursor", OracleTypes.CURSOR); |
| 57 | + verify(callableStatement).close(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void printAvailableWorksWithNonOracleCallableStatement() throws SQLException { |
| 62 | + new DefaultOutputBuffer(reporter).setFetchSize(1).printAvailable(connection, mock(java.io.PrintStream.class)); |
| 63 | + |
| 64 | + verify(callableStatement).setFetchSize(1); |
| 65 | + verify(callableStatement).execute(); |
| 66 | + verify(callableStatement).close(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void statementIsClosedWhenBindingFails() throws SQLException { |
| 71 | + SQLException bindError = new SQLException("bind failed"); |
| 72 | + doThrow(bindError).when(callableStatement).setString(anyString(), anyString()); |
| 73 | + |
| 74 | + SQLException thrown = assertThrows(SQLException.class, () -> new DefaultOutputBuffer(reporter).fetchAll(connection)); |
| 75 | + |
| 76 | + assertThat(thrown, sameInstance(bindError)); |
| 77 | + verify(callableStatement).close(); |
| 78 | + verify(callableStatement, never()).execute(); |
| 79 | + } |
| 80 | +} |
0 commit comments