Skip to content

Commit 4fca40a

Browse files
authored
Merge pull request #30 from utPLSQL/feature/move_to_utplsql_v3_docker
Feature/move to utplsql v3 docker and Compatibility check fixes Moved to utplsql v3 docker Fixed compatibility check problems with versions prior to 3.0.3 Refactored a bunch of classes added new Tests Prepared failOnError test to work properly with version matrix Made Version comparable
2 parents 9da472f + 05cc3b7 commit 4fca40a

14 files changed

+448
-133
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ jdk:
1010
env:
1111
global:
1212
- DOCKER_CFG=$HOME/.docker
13-
- DOCKER_REPO="viniciusam/oracledb"
13+
- DOCKER_REPO="utplsqlv3/oracledb"
1414
- CACHE_DIR=$HOME/.cache
1515
- MAVEN_HOME=/usr/local/maven
1616
- MAVEN_CFG=$HOME/.m2
1717
- DB_URL="127.0.0.1:1521:XE"
1818
- DB_USER=app
1919
- DB_PASS=app
20-
- ORACLE_VERSION="11g-xe-r2"
20+
- ORACLE_VERSION="11g-r2-xe"
2121
- DOCKER_OPTIONS="--shm-size=1g"
2222
- UTPLSQL_FILE="utPLSQL"
2323
matrix:
2424
- UTPLSQL_VERSION="v3.0.0"
25+
UTPLSQL_FILE="utPLSQLv3.0.0"
2526
- UTPLSQL_VERSION="v3.0.1"
2627
- UTPLSQL_VERSION="v3.0.2"
2728
- UTPLSQL_VERSION="v3.0.3"

src/main/java/org/utplsql/api/DBHelper.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
*/
1111
public final class DBHelper {
1212

13-
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3";
14-
1513
private DBHelper() {}
1614

1715
/**
@@ -52,71 +50,6 @@ public static String getCurrentSchema(Connection conn) throws SQLException {
5250
}
5351
}
5452

55-
/**
56-
* Check the utPLSQL version compatibility.
57-
* @param conn the connection
58-
* @return true if the requested utPLSQL version is compatible with the one installed on database
59-
* @throws SQLException any database error
60-
*/
61-
public static boolean versionCompatibilityCheck(Connection conn, String requested, String current)
62-
throws SQLException {
63-
CallableStatement callableStatement = null;
64-
try {
65-
callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;");
66-
callableStatement.registerOutParameter(1, Types.SMALLINT);
67-
callableStatement.setString(2, requested);
68-
69-
if (current == null)
70-
callableStatement.setNull(3, Types.VARCHAR);
71-
else
72-
callableStatement.setString(3, current);
73-
74-
callableStatement.executeUpdate();
75-
return callableStatement.getInt(1) == 1;
76-
} catch (SQLException e) {
77-
if (e.getErrorCode() == 6550)
78-
return false;
79-
else
80-
throw e;
81-
} finally {
82-
if (callableStatement != null)
83-
callableStatement.close();
84-
}
85-
}
86-
87-
public static boolean versionCompatibilityCheck(Connection conn, String requested)
88-
throws SQLException {
89-
return versionCompatibilityCheck(conn, requested, null);
90-
}
91-
92-
public static boolean versionCompatibilityCheck(Connection conn)
93-
throws SQLException {
94-
return versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION);
95-
}
96-
97-
98-
/** Checks if actual API-version is compatible with utPLSQL database version and throws a DatabaseNotCompatibleException if not
99-
* Throws a DatabaseNotCompatibleException if version compatibility can not be checked.
100-
*
101-
* @param conn Active db connection
102-
*/
103-
public static void failOnVersionCompatibilityCheckFailed( Connection conn ) throws DatabaseNotCompatibleException
104-
{
105-
try {
106-
if (!versionCompatibilityCheck(conn))
107-
{
108-
// Try to find out Framework Version
109-
Version v = DBHelper.getDatabaseFrameworkVersion(conn);
110-
111-
throw new DatabaseNotCompatibleException( v );
112-
}
113-
}
114-
catch ( SQLException e )
115-
{
116-
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), new Version(UTPLSQL_COMPATIBILITY_VERSION), new Version("Unknown"), e);
117-
}
118-
}
119-
12053
/** Returns the Frameworks version string of the given connection
12154
*
12255
* @param conn Active db connection

src/main/java/org/utplsql/api/TestRunner.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.utplsql.api;
22

33
import oracle.jdbc.OracleConnection;
4-
import org.utplsql.api.compatibility.CompatibilityProvider;
4+
import org.utplsql.api.compatibility.CompatibilityProxy;
55
import org.utplsql.api.exception.DatabaseNotCompatibleException;
66
import org.utplsql.api.exception.SomeTestsFailedException;
77
import org.utplsql.api.reporter.DocumentationReporter;
@@ -86,9 +86,10 @@ public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
8686

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

89+
CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
90+
8991
// First of all check version compatibility
90-
if ( !options.skipCompatibilityCheck )
91-
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
92+
compatibilityProxy.failOnNotCompatible();
9293

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

109-
testRunnerStatement = CompatibilityProvider.getTestRunnerStatement(options, conn);
110+
testRunnerStatement = compatibilityProxy.getTestRunnerStatement(options, conn);
110111

111112
testRunnerStatement.execute();
112113
} catch (SQLException e) {

src/main/java/org/utplsql/api/Version.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.utplsql.api;
22

3+
import org.utplsql.api.exception.InvalidVersionException;
4+
35
import java.util.regex.Matcher;
46
import java.util.regex.Pattern;
57

68
/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
79
*
810
* @author pesse
911
*/
10-
public class Version {
12+
public class Version implements Comparable<Version> {
1113
private String origString;
1214
private Integer major;
1315
private Integer minor;
@@ -94,4 +96,60 @@ public String getNormalizedString()
9496
else
9597
return "invalid";
9698
}
99+
100+
private int compareToWithNulls( Integer i1, Integer i2 ) {
101+
if ( i1 == null && i2 == null )
102+
return 0;
103+
else if ( i1 == null )
104+
return -1;
105+
else if ( i2 == null )
106+
return 1;
107+
else return i1.compareTo(i2);
108+
}
109+
110+
@Override
111+
public int compareTo(Version o) {
112+
int curResult;
113+
114+
if ( isValid() && o.isValid() ) {
115+
116+
curResult = compareToWithNulls(getMajor(), o.getMajor());
117+
if ( curResult != 0 )
118+
return curResult;
119+
120+
curResult = compareToWithNulls(getMinor(), o.getMinor());
121+
if ( curResult != 0 )
122+
return curResult;
123+
124+
curResult = compareToWithNulls(getBugfix(), o.getBugfix());
125+
if ( curResult != 0 )
126+
return curResult;
127+
128+
curResult = compareToWithNulls(getBuild(), o.getBuild());
129+
if ( curResult != 0 )
130+
return curResult;
131+
}
132+
133+
return 0;
134+
}
135+
136+
/** Compares this version to a given version and returns true if this version is greater or equal than the given one
137+
* Throws an InvalidVersionException if either this or the given version are invalid
138+
*
139+
* @param v Version to compare with
140+
* @return
141+
* @throws InvalidVersionException
142+
*/
143+
public boolean isGreaterOrEqualThan( Version v ) throws InvalidVersionException {
144+
if ( !isValid() )
145+
throw new InvalidVersionException(this);
146+
147+
if ( !v.isValid() )
148+
throw new InvalidVersionException(v);
149+
150+
if ( compareTo(v) >= 0 )
151+
return true;
152+
else
153+
return false;
154+
}
97155
}

src/main/java/org/utplsql/api/compatibility/CompatibilityProvider.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package org.utplsql.api.compatibility;
2+
3+
import org.utplsql.api.DBHelper;
4+
import org.utplsql.api.TestRunnerOptions;
5+
import org.utplsql.api.Version;
6+
import org.utplsql.api.exception.DatabaseNotCompatibleException;
7+
import org.utplsql.api.testRunner.TestRunnerStatement;
8+
import org.utplsql.api.testRunner.TestRunnerStatementProvider;
9+
10+
import java.sql.CallableStatement;
11+
import java.sql.Connection;
12+
import java.sql.SQLException;
13+
import java.sql.Types;
14+
15+
/** Class to check compatibility with database framework and also to give several specific implementations depending
16+
* on the version of the connected framework.
17+
* If one skips the compatibility check, the Proxy acts as like the framework has the same version as the API
18+
*
19+
* @author pesse
20+
*/
21+
public class CompatibilityProxy {
22+
23+
public static final String UTPLSQL_API_VERSION = "3.0.4";
24+
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0";
25+
26+
private Version databaseVersion;
27+
private boolean compatible = false;
28+
29+
public CompatibilityProxy( Connection conn ) throws SQLException
30+
{
31+
this(conn, false);
32+
}
33+
34+
public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException
35+
{
36+
if ( skipCompatibilityCheck )
37+
doExpectCompatibility();
38+
else
39+
doCompatibilityCheckWithDatabase(conn);
40+
}
41+
42+
/** Receives the current framework version from database and checks - depending on the framework version - whether
43+
* the API version is compatible or not.
44+
*
45+
* @param conn
46+
* @throws SQLException
47+
*/
48+
private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException
49+
{
50+
databaseVersion = DBHelper.getDatabaseFrameworkVersion(conn);
51+
52+
if (frameworkHasCompatibilityCheck()) {
53+
try {
54+
compatible = versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION, null);
55+
} catch (SQLException e) {
56+
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), new Version(UTPLSQL_COMPATIBILITY_VERSION), new Version("Unknown"), e);
57+
}
58+
} else
59+
compatible = versionCompatibilityCheckPre303(UTPLSQL_COMPATIBILITY_VERSION);
60+
}
61+
62+
/** Just prepare the proxy to expect compatibility, expecting the database framework to be the same version as the API
63+
*
64+
*/
65+
private void doExpectCompatibility()
66+
{
67+
databaseVersion = new Version(UTPLSQL_API_VERSION);
68+
compatible = true;
69+
}
70+
71+
/**
72+
* Check the utPLSQL version compatibility.
73+
* @param conn the connection
74+
* @return true if the requested utPLSQL version is compatible with the one installed on database
75+
* @throws SQLException any database error
76+
*/
77+
private boolean versionCompatibilityCheck(Connection conn, String requested, String current)
78+
throws SQLException {
79+
CallableStatement callableStatement = null;
80+
try {
81+
callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;");
82+
callableStatement.registerOutParameter(1, Types.SMALLINT);
83+
callableStatement.setString(2, requested);
84+
85+
if (current == null)
86+
callableStatement.setNull(3, Types.VARCHAR);
87+
else
88+
callableStatement.setString(3, current);
89+
90+
callableStatement.executeUpdate();
91+
return callableStatement.getInt(1) == 1;
92+
} catch (SQLException e) {
93+
if (e.getErrorCode() == 6550)
94+
return false;
95+
else
96+
throw e;
97+
} finally {
98+
if (callableStatement != null)
99+
callableStatement.close();
100+
}
101+
}
102+
103+
/** Simple fallback check for compatiblity: Major and Minor version must be equal
104+
*
105+
* @param requested
106+
* @return
107+
*/
108+
private boolean versionCompatibilityCheckPre303( String requested )
109+
{
110+
Version requesteVersion = new Version(requested);
111+
112+
if ( databaseVersion.getMajor() == requesteVersion.getMajor() && (requesteVersion.getMinor() == null || databaseVersion.getMinor() == requesteVersion.getMinor()) )
113+
return true;
114+
else
115+
return false;
116+
}
117+
118+
/** Checks if framework has the compatibility check, which is since 3.0.3
119+
*
120+
* @return Whether framework is >= 3.0.3 or not
121+
*/
122+
private boolean frameworkHasCompatibilityCheck()
123+
{
124+
if ( databaseVersion.getMajor() >= 3 && databaseVersion.getMinor() >= 0 && databaseVersion.getBugfix() >= 3 ) // Compatibility check is included since 3.0.3
125+
return true;
126+
else
127+
return false;
128+
}
129+
130+
/** Checks if actual API-version is compatible with utPLSQL database version and throws a DatabaseNotCompatibleException if not
131+
* Throws a DatabaseNotCompatibleException if version compatibility can not be checked.
132+
*
133+
*/
134+
public void failOnNotCompatible() throws DatabaseNotCompatibleException
135+
{
136+
if ( !isCompatible() )
137+
throw new DatabaseNotCompatibleException( databaseVersion );
138+
}
139+
140+
public boolean isCompatible()
141+
{
142+
return compatible;
143+
}
144+
145+
public Version getDatabaseVersion()
146+
{
147+
return databaseVersion;
148+
}
149+
150+
/** Returns a TestRunnerStatement compatible with the current framework
151+
*
152+
* @param options
153+
* @param conn
154+
* @return
155+
* @throws SQLException
156+
*/
157+
public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Connection conn) throws SQLException
158+
{
159+
return TestRunnerStatementProvider.getCompatibleTestRunnerStatement(databaseVersion, options, conn);
160+
}
161+
}

0 commit comments

Comments
 (0)