Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleTypes;
import org.utplsql.api.reporter.Reporter;

import java.io.PrintStream;
Expand Down Expand Up @@ -79,6 +80,27 @@

protected abstract CallableStatement getLinesCursorStatement(Connection conn) throws SQLException;

/**
* Prepares the given PL/SQL block and binds the :reporter_id and :lines_cursor parameters.
* The returned statement must be closed by the caller. It is closed here if binding fails.
*
* @param conn DB connection
* @param plsql PL/SQL block using the :reporter_id and :lines_cursor bind variables
* @return the prepared statement
* @throws SQLException any sql errors
*/
protected CallableStatement prepareLinesCursorStatement(Connection conn, String plsql) throws SQLException {
CallableStatement cstmt = conn.prepareCall(plsql);
try {
cstmt.setString("reporter_id", reporter.getId());
cstmt.registerOutParameter("lines_cursor", OracleTypes.CURSOR);

Check failure on line 96 in src/main/java/org/utplsql/api/outputBuffer/AbstractOutputBuffer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "lines_cursor" 3 times.

See more on https://sonarcloud.io/project/issues?id=utPLSQL_utPLSQL-java-api&issues=AaD-fq7yWJzF7UC5YFLc&open=AaD-fq7yWJzF7UC5YFLc&pullRequest=111
return cstmt;
} catch (SQLException | RuntimeException e) {
cstmt.close();
throw e;
}
}

/**
* Print the lines as soon as they are produced and call the callback passing the new line.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleTypes;
import org.utplsql.api.reporter.Reporter;

import java.sql.CallableStatement;
Expand All @@ -20,9 +19,6 @@ class CompatibilityOutputBufferPre310 extends AbstractOutputBuffer {

@Override
protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLException {
CallableStatement cstmt = conn.prepareCall("begin :lines_cursor := ut_output_buffer.get_lines_cursor(:reporter_id); end;");
cstmt.registerOutParameter("lines_cursor", OracleTypes.CURSOR);
cstmt.setString("reporter_id", getReporter().getId());
return cstmt;
return prepareLinesCursorStatement(conn, "begin :lines_cursor := ut_output_buffer.get_lines_cursor(:reporter_id); end;");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import org.utplsql.api.reporter.Reporter;

import java.sql.CallableStatement;
Expand Down Expand Up @@ -34,9 +32,6 @@ protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLE
" l_rep.set_reporter_id(:reporter_id); " +
" :lines_cursor := l_rep.get_lines_cursor(); " +
"end;";
OracleCallableStatement cstmt = (OracleCallableStatement) conn.prepareCall(plsql);
cstmt.setString("reporter_id", reporter.getId());
cstmt.registerOutParameter("lines_cursor", OracleTypes.CURSOR);
return cstmt;
return prepareLinesCursorStatement(conn, plsql);
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/utplsql/api/reporter/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ public Reporter init(Connection con) throws SQLException {
* @throws SQLException if there are problems with the database access
*/
private void initDbReporter(OracleConnection oraConn, ReporterFactory reporterFactory) throws SQLException {
OracleCallableStatement callableStatement = (OracleCallableStatement) oraConn.prepareCall("{? = call " + selfType + "()}");
callableStatement.registerOutParameter(1, OracleTypes.STRUCT, "UT_REPORTER_BASE");
callableStatement.execute();
try (OracleCallableStatement callableStatement = (OracleCallableStatement) oraConn.prepareCall("{? = call " + selfType + "()}")) {
callableStatement.registerOutParameter(1, OracleTypes.STRUCT, "UT_REPORTER_BASE");
callableStatement.execute();

Reporter obj = (Reporter) callableStatement.getORAData(1, reporterFactory);
Reporter obj = (Reporter) callableStatement.getORAData(1, reporterFactory);

setAttributes(obj.getAttributes());
setAttributes(obj.getAttributes());
}

logger.debug("Database-reporter initialized, Type: {}, ID: {}", selfType, id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleTypes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.utplsql.api.reporter.Reporter;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

/**
* Connection pools (e.g. HikariCP) return their own proxy statements, which are not
* {@link oracle.jdbc.OracleCallableStatement} instances. The output buffer must work with plain JDBC statements.
*/
class DefaultOutputBufferTest {

private Reporter reporter;
private Connection connection;
private CallableStatement callableStatement;

@BeforeEach
void setUp() throws SQLException {
reporter = mock(Reporter.class);
when(reporter.isInit()).thenReturn(true);
when(reporter.getTypeName()).thenReturn("UT_DOCUMENTATION_REPORTER");
when(reporter.getId()).thenReturn("ABC123");

ResultSet resultSet = mock(ResultSet.class);
when(resultSet.next()).thenReturn(true, true, false);
when(resultSet.getString("text")).thenReturn("line 1", "line 2");

// A plain CallableStatement, as returned by a connection pool proxy
callableStatement = mock(CallableStatement.class);
when(callableStatement.getObject("lines_cursor")).thenReturn(resultSet);

connection = mock(Connection.class);
when(connection.prepareCall(anyString())).thenReturn(callableStatement);
}

@Test
void fetchAllWorksWithNonOracleCallableStatement() throws SQLException {
List<String> lines = new DefaultOutputBuffer(reporter).fetchAll(connection);

assertThat(lines, contains("line 1", "line 2"));
verify(callableStatement).setString("reporter_id", "ABC123");
verify(callableStatement).registerOutParameter("lines_cursor", OracleTypes.CURSOR);
verify(callableStatement).close();
}

@Test
void printAvailableWorksWithNonOracleCallableStatement() throws SQLException {
new DefaultOutputBuffer(reporter).setFetchSize(1).printAvailable(connection, mock(java.io.PrintStream.class));

verify(callableStatement).setFetchSize(1);
verify(callableStatement).execute();
verify(callableStatement).close();
}

@Test
void statementIsClosedWhenBindingFails() throws SQLException {
SQLException bindError = new SQLException("bind failed");
doThrow(bindError).when(callableStatement).setString(anyString(), anyString());

SQLException thrown = assertThrows(SQLException.class, () -> new DefaultOutputBuffer(reporter).fetchAll(connection));

assertThat(thrown, sameInstance(bindError));
verify(callableStatement).close();
verify(callableStatement, never()).execute();
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/utplsql/api/reporter/ReporterInitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.utplsql.api.reporter;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleConnection;
import org.junit.jupiter.api.Test;
import org.utplsql.api.compatibility.CompatibilityProxy;

import java.sql.Connection;
import java.sql.SQLException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

class ReporterInitTest {

@Test
void initClosesReporterStatement() throws SQLException {
OracleCallableStatement callableStatement = mock(OracleCallableStatement.class);
Reporter dbReporter = new DefaultReporter("UT_DOCUMENTATION_REPORTER",
new Object[]{"UT_DOCUMENTATION_REPORTER", new byte[]{0x0A, 0x0B}});
when(callableStatement.getORAData(eq(1), any())).thenReturn(dbReporter);

OracleConnection oracleConnection = mock(OracleConnection.class);
when(oracleConnection.prepareCall(anyString())).thenReturn(callableStatement);

// Connection as handed out by a connection pool
Connection connection = mock(Connection.class);
when(connection.unwrap(OracleConnection.class)).thenReturn(oracleConnection);

Reporter reporter = new DefaultReporter("UT_DOCUMENTATION_REPORTER", null)
.init(connection, mock(CompatibilityProxy.class), new ReporterFactory());

assertThat(reporter.isInit(), equalTo(true));
assertThat(reporter.getId(), equalTo("0A0B"));
verify(callableStatement).close();
}
}
Loading