Skip to content

Feature/move to utplsql v3 docker and Compatibility check fixes #30

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 7 commits into from
Nov 16, 2017
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ jdk:
env:
global:
- DOCKER_CFG=$HOME/.docker
- DOCKER_REPO="viniciusam/oracledb"
- DOCKER_REPO="utplsqlv3/oracledb"
- CACHE_DIR=$HOME/.cache
- MAVEN_HOME=/usr/local/maven
- MAVEN_CFG=$HOME/.m2
- DB_URL="127.0.0.1:1521:XE"
- DB_USER=app
- DB_PASS=app
- ORACLE_VERSION="11g-xe-r2"
- ORACLE_VERSION="11g-r2-xe"
- DOCKER_OPTIONS="--shm-size=1g"
- UTPLSQL_FILE="utPLSQL"
matrix:
- UTPLSQL_VERSION="v3.0.0"
UTPLSQL_FILE="utPLSQLv3.0.0"
- UTPLSQL_VERSION="v3.0.1"
- UTPLSQL_VERSION="v3.0.2"
- UTPLSQL_VERSION="v3.0.3"
Expand Down
67 changes: 0 additions & 67 deletions src/main/java/org/utplsql/api/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
public final class DBHelper {

public static final String UTPLSQL_COMPATIBILITY_VERSION = "3";

private DBHelper() {}

/**
Expand Down Expand Up @@ -52,71 +50,6 @@ 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_COMPATIBILITY_VERSION);
}


/** Checks if actual API-version is compatible with utPLSQL database version and throws a DatabaseNotCompatibleException if not
* Throws a DatabaseNotCompatibleException if version compatibility can not be checked.
*
* @param conn Active db connection
*/
public static void failOnVersionCompatibilityCheckFailed( Connection conn ) throws DatabaseNotCompatibleException
{
try {
if (!versionCompatibilityCheck(conn))
{
// Try to find out Framework Version
Version v = DBHelper.getDatabaseFrameworkVersion(conn);

throw new DatabaseNotCompatibleException( v );
}
}
catch ( SQLException e )
{
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), new Version(UTPLSQL_COMPATIBILITY_VERSION), new Version("Unknown"), e);
}
}

/** Returns the Frameworks version string of the given connection
*
* @param conn Active db connection
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.utplsql.api;

import oracle.jdbc.OracleConnection;
import org.utplsql.api.compatibility.CompatibilityProvider;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.DocumentationReporter;
Expand Down Expand Up @@ -86,9 +86,10 @@ public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )

public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException {

CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);

// First of all check version compatibility
if ( !options.skipCompatibilityCheck )
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
compatibilityProxy.failOnNotCompatible();

for (Reporter r : options.reporterList)
validateReporter(conn, r);
Expand All @@ -106,7 +107,7 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException,
try {
DBHelper.enableDBMSOutput(conn);

testRunnerStatement = CompatibilityProvider.getTestRunnerStatement(options, conn);
testRunnerStatement = compatibilityProxy.getTestRunnerStatement(options, conn);

testRunnerStatement.execute();
} catch (SQLException e) {
Expand Down
60 changes: 59 additions & 1 deletion src/main/java/org/utplsql/api/Version.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.utplsql.api;

import org.utplsql.api.exception.InvalidVersionException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
*
* @author pesse
*/
public class Version {
public class Version implements Comparable<Version> {
private String origString;
private Integer major;
private Integer minor;
Expand Down Expand Up @@ -94,4 +96,60 @@ public String getNormalizedString()
else
return "invalid";
}

private int compareToWithNulls( Integer i1, Integer i2 ) {
if ( i1 == null && i2 == null )
return 0;
else if ( i1 == null )
return -1;
else if ( i2 == null )
return 1;
else return i1.compareTo(i2);
}

@Override
public int compareTo(Version o) {
int curResult;

if ( isValid() && o.isValid() ) {

curResult = compareToWithNulls(getMajor(), o.getMajor());
if ( curResult != 0 )
return curResult;

curResult = compareToWithNulls(getMinor(), o.getMinor());
if ( curResult != 0 )
return curResult;

curResult = compareToWithNulls(getBugfix(), o.getBugfix());
if ( curResult != 0 )
return curResult;

curResult = compareToWithNulls(getBuild(), o.getBuild());
if ( curResult != 0 )
return curResult;
}

return 0;
}

/** Compares this version to a given version and returns true if this version is greater or equal than the given one
* Throws an InvalidVersionException if either this or the given version are invalid
*
* @param v Version to compare with
* @return
* @throws InvalidVersionException
*/
public boolean isGreaterOrEqualThan( Version v ) throws InvalidVersionException {
if ( !isValid() )
throw new InvalidVersionException(this);

if ( !v.isValid() )
throw new InvalidVersionException(v);

if ( compareTo(v) >= 0 )
return true;
else
return false;
}
}

This file was deleted.

161 changes: 161 additions & 0 deletions src/main/java/org/utplsql/api/compatibility/CompatibilityProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package org.utplsql.api.compatibility;

import org.utplsql.api.DBHelper;
import org.utplsql.api.TestRunnerOptions;
import org.utplsql.api.Version;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.testRunner.TestRunnerStatement;
import org.utplsql.api.testRunner.TestRunnerStatementProvider;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

/** Class to check compatibility with database framework and also to give several specific implementations depending
* on the version of the connected framework.
* If one skips the compatibility check, the Proxy acts as like the framework has the same version as the API
*
* @author pesse
*/
public class CompatibilityProxy {

public static final String UTPLSQL_API_VERSION = "3.0.4";
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0";

private Version databaseVersion;
private boolean compatible = false;

public CompatibilityProxy( Connection conn ) throws SQLException
{
this(conn, false);
}

public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException
{
if ( skipCompatibilityCheck )
doExpectCompatibility();
else
doCompatibilityCheckWithDatabase(conn);
}

/** Receives the current framework version from database and checks - depending on the framework version - whether
* the API version is compatible or not.
*
* @param conn
* @throws SQLException
*/
private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException
{
databaseVersion = DBHelper.getDatabaseFrameworkVersion(conn);

if (frameworkHasCompatibilityCheck()) {
try {
compatible = versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION, null);
} catch (SQLException e) {
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), new Version(UTPLSQL_COMPATIBILITY_VERSION), new Version("Unknown"), e);
}
} else
compatible = versionCompatibilityCheckPre303(UTPLSQL_COMPATIBILITY_VERSION);
}

/** Just prepare the proxy to expect compatibility, expecting the database framework to be the same version as the API
*
*/
private void doExpectCompatibility()
{
databaseVersion = new Version(UTPLSQL_API_VERSION);
compatible = true;
}

/**
* 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
*/
private 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();
}
}

/** Simple fallback check for compatiblity: Major and Minor version must be equal
*
* @param requested
* @return
*/
private boolean versionCompatibilityCheckPre303( String requested )
{
Version requesteVersion = new Version(requested);

if ( databaseVersion.getMajor() == requesteVersion.getMajor() && (requesteVersion.getMinor() == null || databaseVersion.getMinor() == requesteVersion.getMinor()) )
return true;
else
return false;
}

/** Checks if framework has the compatibility check, which is since 3.0.3
*
* @return Whether framework is >= 3.0.3 or not
*/
private boolean frameworkHasCompatibilityCheck()
{
if ( databaseVersion.getMajor() >= 3 && databaseVersion.getMinor() >= 0 && databaseVersion.getBugfix() >= 3 ) // Compatibility check is included since 3.0.3
return true;
else
return false;
}

/** Checks if actual API-version is compatible with utPLSQL database version and throws a DatabaseNotCompatibleException if not
* Throws a DatabaseNotCompatibleException if version compatibility can not be checked.
*
*/
public void failOnNotCompatible() throws DatabaseNotCompatibleException
{
if ( !isCompatible() )
throw new DatabaseNotCompatibleException( databaseVersion );
}

public boolean isCompatible()
{
return compatible;
}

public Version getDatabaseVersion()
{
return databaseVersion;
}

/** Returns a TestRunnerStatement compatible with the current framework
*
* @param options
* @param conn
* @return
* @throws SQLException
*/
public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Connection conn) throws SQLException
{
return TestRunnerStatementProvider.getCompatibleTestRunnerStatement(databaseVersion, options, conn);
}
}
Loading