Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +1,5 @@
package org.utplsql.api.outputBuffer;

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

Expand Down Expand Up @@ -34,7 +33,7 @@
" l_rep.set_reporter_id(:reporter_id); " +
" :lines_cursor := l_rep.get_lines_cursor(); " +
"end;";
OracleCallableStatement cstmt = (OracleCallableStatement) conn.prepareCall(plsql);
CallableStatement cstmt = conn.prepareCall(plsql);

Check failure on line 36 in src/main/java/org/utplsql/api/outputBuffer/DefaultOutputBuffer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use try-with-resources or close this "CallableStatement" in a "finally" clause.

See more on https://sonarcloud.io/project/issues?id=utPLSQL_utPLSQL-java-api&issues=AaD-WlHU_9h-13omTiLw&open=AaD-WlHU_9h-13omTiLw&pullRequest=111

Check warning on line 36 in src/main/java/org/utplsql/api/outputBuffer/DefaultOutputBuffer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure using a dynamically formatted SQL query is safe here.

See more on https://sonarcloud.io/project/issues?id=utPLSQL_utPLSQL-java-api&issues=AaD-WlHU_9h-13omTiLx&open=AaD-WlHU_9h-13omTiLx&pullRequest=111
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
cstmt.setString("reporter_id", reporter.getId());
cstmt.registerOutParameter("lines_cursor", OracleTypes.CURSOR);
return cstmt;
Expand Down
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,66 @@
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.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();
}
}
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