Skip to content

Add Abstraction for DatabaseInformation #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2018
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
7 changes: 7 additions & 0 deletions src/main/java/org/utplsql/api/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;")) {
Expand All @@ -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("");
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/utplsql/api/JavaApiVersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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()) {
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/utplsql/api/db/DatabaseInformation.java
Original file line number Diff line number Diff line change
@@ -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;
}
73 changes: 73 additions & 0 deletions src/main/java/org/utplsql/api/db/DefaultDatabaseInformation.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/utplsql/api/DatabaseInformationIT.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 5 additions & 1 deletion src/test/java/org/utplsql/api/OutputBufferIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -137,7 +138,10 @@ public void sonarReporterHasEncodingSet() throws SQLException, InvalidVersionExc

List<String> 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());
}

}
Expand Down