From 5d725d5fadbffaeb0c1e6e7aea7b67a85c432eeb Mon Sep 17 00:00:00 2001 From: Vinicius Avellar Date: Sun, 10 Sep 2017 22:11:43 +0100 Subject: [PATCH 1/4] Added version comparison check function --- src/main/java/org/utplsql/api/DBHelper.java | 44 +++++++++++++++++++ .../java/org/utplsql/api/DBHelperTest.java | 37 ++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/test/java/org/utplsql/api/DBHelperTest.java diff --git a/src/main/java/org/utplsql/api/DBHelper.java b/src/main/java/org/utplsql/api/DBHelper.java index bfe6c94..eca681b 100644 --- a/src/main/java/org/utplsql/api/DBHelper.java +++ b/src/main/java/org/utplsql/api/DBHelper.java @@ -12,6 +12,8 @@ */ public final class DBHelper { + public static final String UTPLSQL_VERSION = "3.0.3"; + private DBHelper() {} /** @@ -52,4 +54,46 @@ public static String getCurrentSchema(Connection conn) throws SQLException { } } + /** + * Check the utPLSQL version compatibility. + * @param conn the connection + * @return true if the requested utPLSQL version is compatible with the one installed on database + * @throws SQLException any database error + */ + public static boolean versionCompatibilityCheck(Connection conn, String requested, String current) + throws SQLException { + CallableStatement callableStatement = null; + try { + callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;"); + callableStatement.registerOutParameter(1, Types.SMALLINT); + callableStatement.setString(2, requested); + + if (current == null) + callableStatement.setNull(3, Types.VARCHAR); + else + callableStatement.setString(3, current); + + callableStatement.executeUpdate(); + return callableStatement.getInt(1) == 1; + } catch (SQLException e) { + if (e.getErrorCode() == 6550) + return false; + else + throw e; + } finally { + if (callableStatement != null) + callableStatement.close(); + } + } + + public static boolean versionCompatibilityCheck(Connection conn, String requested) + throws SQLException { + return versionCompatibilityCheck(conn, requested, null); + } + + public static boolean versionCompatibilityCheck(Connection conn) + throws SQLException { + return versionCompatibilityCheck(conn, UTPLSQL_VERSION); + } + } diff --git a/src/test/java/org/utplsql/api/DBHelperTest.java b/src/test/java/org/utplsql/api/DBHelperTest.java new file mode 100644 index 0000000..3761650 --- /dev/null +++ b/src/test/java/org/utplsql/api/DBHelperTest.java @@ -0,0 +1,37 @@ +package org.utplsql.api; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.utplsql.api.rules.DatabaseRule; + +import java.sql.SQLException; + +public class DBHelperTest { + + @Rule + public final DatabaseRule db = new DatabaseRule(); + + @Test + public void compatibleVersion() { + try { + boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.0.0", "3.0.0"); + Assert.assertTrue(isCompatible); + } catch (SQLException e) { + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void incompatibleVersion() { + try { + boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.1.0", "3.0.0"); + Assert.assertFalse(isCompatible); + } catch (SQLException e) { + e.printStackTrace(); + Assert.fail(); + } + } + +} From 0d8501b6ac915e8a03956c457952cc0ead47a847 Mon Sep 17 00:00:00 2001 From: Vinicius Avellar Date: Sun, 22 Oct 2017 17:04:51 +0100 Subject: [PATCH 2/4] Enable output_buffer before calling ut_runner.run --- src/main/java/org/utplsql/api/DBHelper.java | 34 +++++++++++++++++++ src/main/java/org/utplsql/api/TestRunner.java | 7 +++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/utplsql/api/DBHelper.java b/src/main/java/org/utplsql/api/DBHelper.java index eca681b..5c9dc0d 100644 --- a/src/main/java/org/utplsql/api/DBHelper.java +++ b/src/main/java/org/utplsql/api/DBHelper.java @@ -96,4 +96,38 @@ public static boolean versionCompatibilityCheck(Connection conn) return versionCompatibilityCheck(conn, UTPLSQL_VERSION); } + /** + * Enables the dbms_output buffer. + * @param conn the connection + * @param bufferLen the buffer length + */ + public static void enableDBMSOutput(Connection conn, int bufferLen) { + try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.enable(?); END;")) { + call.setInt(1, bufferLen); + call.execute(); + } catch (SQLException e) { + System.out.println("Failed to enable dbms_output."); + } + } + + /** + * Enables the dbms_output buffer. + * @param conn the connection + */ + public static void enableDBMSOutput(Connection conn) { + enableDBMSOutput(conn, Integer.MAX_VALUE); + } + + /** + * Disables the dbms_output buffer. + * @param conn the connection + */ + public static void disableDBMSOutput(Connection conn) { + try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.disable(); END;")) { + call.execute(); + } catch (SQLException e) { + System.out.println("Failed to disable dbms_output."); + } + } + } diff --git a/src/main/java/org/utplsql/api/TestRunner.java b/src/main/java/org/utplsql/api/TestRunner.java index 532921c..549caa4 100644 --- a/src/main/java/org/utplsql/api/TestRunner.java +++ b/src/main/java/org/utplsql/api/TestRunner.java @@ -103,6 +103,8 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException { OracleConnection oraConn = conn.unwrap(OracleConnection.class); CallableStatement callableStatement = null; try { + DBHelper.enableDBMSOutput(conn); + callableStatement = conn.prepareCall( "BEGIN " + "ut_runner.run(" + @@ -172,8 +174,11 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException { throw e; } } finally { - if (callableStatement != null) + if (callableStatement != null) { callableStatement.close(); + } + + DBHelper.disableDBMSOutput(conn); } } From 94d58a6c8f46fe38a89dee2d295a5a047488fe73 Mon Sep 17 00:00:00 2001 From: Vinicius Avellar Date: Tue, 24 Oct 2017 19:02:12 +0100 Subject: [PATCH 3/4] Enable dbms_output with unlimited size --- src/main/java/org/utplsql/api/DBHelper.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/utplsql/api/DBHelper.java b/src/main/java/org/utplsql/api/DBHelper.java index 5c9dc0d..4173e8b 100644 --- a/src/main/java/org/utplsql/api/DBHelper.java +++ b/src/main/java/org/utplsql/api/DBHelper.java @@ -97,13 +97,11 @@ public static boolean versionCompatibilityCheck(Connection conn) } /** - * Enables the dbms_output buffer. + * Enable the dbms_output buffer with unlimited size. * @param conn the connection - * @param bufferLen the buffer length */ - public static void enableDBMSOutput(Connection conn, int bufferLen) { - try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.enable(?); END;")) { - call.setInt(1, bufferLen); + public static void enableDBMSOutput(Connection conn) { + try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.enable(NULL); END;")) { call.execute(); } catch (SQLException e) { System.out.println("Failed to enable dbms_output."); @@ -111,15 +109,7 @@ public static void enableDBMSOutput(Connection conn, int bufferLen) { } /** - * Enables the dbms_output buffer. - * @param conn the connection - */ - public static void enableDBMSOutput(Connection conn) { - enableDBMSOutput(conn, Integer.MAX_VALUE); - } - - /** - * Disables the dbms_output buffer. + * Disable the dbms_output buffer. * @param conn the connection */ public static void disableDBMSOutput(Connection conn) { From e12186743871df2a92cf5baf3cc82edb7964f6ca Mon Sep 17 00:00:00 2001 From: Vinicius Avellar Date: Thu, 26 Oct 2017 22:32:19 +0100 Subject: [PATCH 4/4] Change the version constant name --- src/main/java/org/utplsql/api/DBHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/utplsql/api/DBHelper.java b/src/main/java/org/utplsql/api/DBHelper.java index 4173e8b..8b8339b 100644 --- a/src/main/java/org/utplsql/api/DBHelper.java +++ b/src/main/java/org/utplsql/api/DBHelper.java @@ -12,7 +12,7 @@ */ public final class DBHelper { - public static final String UTPLSQL_VERSION = "3.0.3"; + public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0.3"; private DBHelper() {} @@ -93,7 +93,7 @@ public static boolean versionCompatibilityCheck(Connection conn, String requeste public static boolean versionCompatibilityCheck(Connection conn) throws SQLException { - return versionCompatibilityCheck(conn, UTPLSQL_VERSION); + return versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION); } /**