diff --git a/src/main/java/org/utplsql/api/DBHelper.java b/src/main/java/org/utplsql/api/DBHelper.java index afef49d..f89471c 100644 --- a/src/main/java/org/utplsql/api/DBHelper.java +++ b/src/main/java/org/utplsql/api/DBHelper.java @@ -30,10 +30,13 @@ public static String newSysGuid(Connection conn) throws SQLException { /** * Return the current schema name. + * Deprecated. Use DatabaseInformation-Interface instead. + * * @param conn the connection * @return the schema name * @throws SQLException any database error */ + @Deprecated public static String getCurrentSchema(Connection conn) throws SQLException { assert conn != null; try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;")) { @@ -44,11 +47,13 @@ public static String getCurrentSchema(Connection conn) throws SQLException { } /** Returns the Frameworks version string of the given connection + * Deprecated. Use DatabaseInformation-Interface instead. * * @param conn Active db connection * @return Version-string of the utPLSQL framework * @throws SQLException any database error */ + @Deprecated public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLException { Objects.requireNonNull(conn); Version result = new Version(""); @@ -71,11 +76,13 @@ public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLE } /** Returns the Oracle database Version from a given connection object + * Deprecated. Use DatabaseInformation-Interface instead. * * @param conn Connection-Object * @return Returns version-string of the Oracle Database product component * @throws SQLException any database error */ + @Deprecated public static String getOracleDatabaseVersion( Connection conn ) throws SQLException { assert conn != null; String result = null; diff --git a/src/main/java/org/utplsql/api/JavaApiVersionInfo.java b/src/main/java/org/utplsql/api/JavaApiVersionInfo.java index 6b136bc..cd6ab82 100644 --- a/src/main/java/org/utplsql/api/JavaApiVersionInfo.java +++ b/src/main/java/org/utplsql/api/JavaApiVersionInfo.java @@ -11,7 +11,7 @@ private JavaApiVersionInfo() { } private static final String BUILD_NO = "123"; private static final String MAVEN_PROJECT_NAME = "utPLSQL-java-api"; - private static final String MAVEN_PROJECT_VERSION = "3.1.1"; + private static final String MAVEN_PROJECT_VERSION = "3.1.1.1-SNAPSHOT"; public static String getVersion() { return MAVEN_PROJECT_VERSION + "." + BUILD_NO; diff --git a/src/main/java/org/utplsql/api/TestRunner.java b/src/main/java/org/utplsql/api/TestRunner.java index 0473683..745462f 100644 --- a/src/main/java/org/utplsql/api/TestRunner.java +++ b/src/main/java/org/utplsql/api/TestRunner.java @@ -1,6 +1,8 @@ package org.utplsql.api; import org.utplsql.api.compatibility.CompatibilityProxy; +import org.utplsql.api.db.DatabaseInformation; +import org.utplsql.api.db.DefaultDatabaseInformation; import org.utplsql.api.exception.DatabaseNotCompatibleException; import org.utplsql.api.exception.SomeTestsFailedException; import org.utplsql.api.exception.UtPLSQLNotInstalledException; @@ -120,7 +122,9 @@ private void delayedAddReporters() { public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException { - compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck); + DatabaseInformation databaseInformation = new DefaultDatabaseInformation(); + + compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck, databaseInformation); if ( reporterFactory == null ) reporterFactory = ReporterFactory.createDefault(compatibilityProxy); @@ -133,7 +137,7 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException, validateReporter(conn, r); if (options.pathList.isEmpty()) { - options.pathList.add(DBHelper.getCurrentSchema(conn)); + options.pathList.add(databaseInformation.getCurrentSchema(conn)); } if (options.reporterList.isEmpty()) { diff --git a/src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java b/src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java index 0f962e0..bf817fd 100644 --- a/src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java +++ b/src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java @@ -3,6 +3,8 @@ import org.utplsql.api.DBHelper; import org.utplsql.api.TestRunnerOptions; import org.utplsql.api.Version; +import org.utplsql.api.db.DatabaseInformation; +import org.utplsql.api.db.DefaultDatabaseInformation; import org.utplsql.api.exception.DatabaseNotCompatibleException; import org.utplsql.api.outputBuffer.OutputBuffer; import org.utplsql.api.outputBuffer.OutputBufferProvider; @@ -29,14 +31,25 @@ public class CompatibilityProxy { private Version databaseVersion; private boolean compatible = false; + private DatabaseInformation databaseInformation; - public CompatibilityProxy( Connection conn ) throws SQLException - { - this(conn, false); + public CompatibilityProxy( Connection conn ) throws SQLException { + this(conn, false, null); } - public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException - { + public CompatibilityProxy( Connection conn, DatabaseInformation databaseInformation ) throws SQLException { + this(conn, false, databaseInformation); + } + + public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException { + this(conn, skipCompatibilityCheck, null); + } + + public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck, DatabaseInformation databaseInformation ) throws SQLException { + this.databaseInformation = (databaseInformation != null ) + ? databaseInformation + : new DefaultDatabaseInformation(); + if ( skipCompatibilityCheck ) doExpectCompatibility(); else @@ -51,7 +64,7 @@ public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) thr */ private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException { - databaseVersion = DBHelper.getDatabaseFrameworkVersion(conn); + databaseVersion = databaseInformation.getUtPlsqlFrameworkVersion(conn); Version clientVersion = new Version(UTPLSQL_COMPATIBILITY_VERSION); if ( databaseVersion == null ) @@ -87,17 +100,8 @@ private void doExpectCompatibility() */ private boolean versionCompatibilityCheck(Connection conn, String requested, String current) throws SQLException { - try(CallableStatement 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; + try { + return databaseInformation.frameworkCompatibilityCheck(conn, requested, current) == 1; } catch (SQLException e) { if (e.getErrorCode() == 6550) return false; diff --git a/src/main/java/org/utplsql/api/db/DatabaseInformation.java b/src/main/java/org/utplsql/api/db/DatabaseInformation.java new file mode 100644 index 0000000..5e842e9 --- /dev/null +++ b/src/main/java/org/utplsql/api/db/DatabaseInformation.java @@ -0,0 +1,21 @@ +package org.utplsql.api.db; + +import org.utplsql.api.Version; + +import java.sql.Connection; +import java.sql.SQLException; + +/** Abstraction-interface to encapsulate Database-Calls (and potentially mock them) + * + * @author pesse + */ +public interface DatabaseInformation { + + Version getUtPlsqlFrameworkVersion(Connection conn ) throws SQLException; + + String getOracleVersion( Connection conn ) throws SQLException; + + String getCurrentSchema( Connection conn ) throws SQLException; + + int frameworkCompatibilityCheck(Connection conn, String requested, String current) throws SQLException; +} diff --git a/src/main/java/org/utplsql/api/db/DefaultDatabaseInformation.java b/src/main/java/org/utplsql/api/db/DefaultDatabaseInformation.java new file mode 100644 index 0000000..27f7798 --- /dev/null +++ b/src/main/java/org/utplsql/api/db/DefaultDatabaseInformation.java @@ -0,0 +1,73 @@ +package org.utplsql.api.db; + +import org.utplsql.api.Version; +import org.utplsql.api.exception.UtPLSQLNotInstalledException; + +import java.sql.*; +import java.util.Objects; + +public class DefaultDatabaseInformation implements DatabaseInformation { + + @Override + public Version getUtPlsqlFrameworkVersion(Connection conn) throws SQLException { + Objects.requireNonNull(conn); + Version result = new Version(""); + try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual")) + { + ResultSet rs = stmt.executeQuery(); + + if ( rs.next() ) + result = new Version(rs.getString(1)); + + rs.close(); + } catch ( SQLException e ) { + if ( e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE ) + throw new UtPLSQLNotInstalledException(e); + else + throw e; + } + + return result; + } + + @Override + public String getOracleVersion(Connection conn) throws SQLException { + Objects.requireNonNull(conn); + String result = null; + try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'")) + { + ResultSet rs = stmt.executeQuery(); + + if ( rs.next() ) + result = rs.getString(1); + } + + return result; + } + + @Override + public String getCurrentSchema(Connection conn) throws SQLException { + Objects.requireNonNull(conn); + try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;")) { + callableStatement.registerOutParameter(1, Types.VARCHAR); + callableStatement.executeUpdate(); + return callableStatement.getString(1); + } + } + + @Override + public int frameworkCompatibilityCheck(Connection conn, String requested, String current) throws SQLException { + try(CallableStatement 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); + } + } +} diff --git a/src/test/java/org/utplsql/api/DatabaseInformationIT.java b/src/test/java/org/utplsql/api/DatabaseInformationIT.java new file mode 100644 index 0000000..3b7027f --- /dev/null +++ b/src/test/java/org/utplsql/api/DatabaseInformationIT.java @@ -0,0 +1,30 @@ +package org.utplsql.api; + +import org.junit.jupiter.api.Test; +import org.utplsql.api.db.DatabaseInformation; +import org.utplsql.api.db.DefaultDatabaseInformation; + +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DatabaseInformationIT extends AbstractDatabaseTest { + + @Test + public void getFrameworkVersion() throws SQLException { + DatabaseInformation databaseInformation = new DefaultDatabaseInformation(); + + Version v = databaseInformation.getUtPlsqlFrameworkVersion(getConnection()); + assertTrue(v.isValid()); + System.out.println(v.getNormalizedString() + " - " + v.toString()); + } + + @Test + public void getOracleDatabaseVersion() throws SQLException { + DatabaseInformation databaseInformation = new DefaultDatabaseInformation(); + + String databaseVersion = databaseInformation.getOracleVersion(getConnection()); + assertNotNull(databaseVersion); + } +} diff --git a/src/test/java/org/utplsql/api/OutputBufferIT.java b/src/test/java/org/utplsql/api/OutputBufferIT.java index aeb94c9..5909d57 100644 --- a/src/test/java/org/utplsql/api/OutputBufferIT.java +++ b/src/test/java/org/utplsql/api/OutputBufferIT.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.concurrent.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -137,7 +138,10 @@ public void sonarReporterHasEncodingSet() throws SQLException, InvalidVersionExc List outputLines = reporter.getOutputBuffer().fetchAll(getConnection()); - assertTrue(outputLines.get(0).contains("encoding=\"" + Charset.defaultCharset().toString() + "\"")); + String defaultCharset = Charset.defaultCharset().toString(); + String actualCharset = outputLines.get(0).replaceAll("(.*)encoding=\"([^\"]+)\"(.*)", "$2"); + + assertEquals(defaultCharset.toLowerCase(), actualCharset.toLowerCase()); } }