Skip to content

Commit 6bdd4f9

Browse files
authored
Merge pull request #28 from utPLSQL/feature/version_check
Feature/version check
2 parents 51ac9db + 799792d commit 6bdd4f9

16 files changed

+588
-116
lines changed

.travis/install_utplsql.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ set -ev
33
cd $(dirname $(readlink -f $0))
44

55
# Download the specified version of utPLSQL.
6-
# UTPLSQL_VERSION="v3.0.0-beta"
7-
# UTPLSQL_FILE="utPLSQLv3.0.0.562-beta"
8-
# curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"
6+
UTPLSQL_VERSION="v3.0.4"
7+
UTPLSQL_FILE="utPLSQL"
8+
curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"
99

1010
# Download develop branch of utPLSQL.
11-
UTPLSQL_VERSION="develop"
12-
UTPLSQL_FILE="utPLSQL"
13-
git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git
11+
#UTPLSQL_VERSION="develop"
12+
#UTPLSQL_FILE="utPLSQL"
13+
#git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git
1414
# tar -czf $UTPLSQL_FILE.tar.gz $UTPLSQL_FILE && rm -rf $UTPLSQL_FILE
1515

1616
# Create a temporary install script.
1717
cat > install.sh.tmp <<EOF
18-
# tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz
18+
tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz
1919
cd ${UTPLSQL_FILE}/source
2020
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA @install_headless.sql ut3 ut3 users
2121
EOF
2222

2323
# Copy utPLSQL files to the container and install it.
24-
# docker cp ./$UTPLSQL_FILE.tar.gz $ORACLE_VERSION:/$UTPLSQL_FILE.tar.gz
25-
docker cp ./$UTPLSQL_FILE $ORACLE_VERSION:/$UTPLSQL_FILE
24+
docker cp ./$UTPLSQL_FILE.tar.gz $ORACLE_VERSION:/$UTPLSQL_FILE.tar.gz
25+
# docker cp ./$UTPLSQL_FILE $ORACLE_VERSION:/$UTPLSQL_FILE
2626
docker cp ./install.sh.tmp $ORACLE_VERSION:/install.sh
2727
docker cp ./create_api_user.sh $ORACLE_VERSION:/create_api_user.sh
2828

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.utplsql</groupId>
66
<artifactId>java-api</artifactId>
7-
<version>1.0-SNAPSHOT</version>
7+
<version>3.0.4-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>utPLSQL-java-api</name>

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package org.utplsql.api;
22

33
import oracle.jdbc.OracleTypes;
4+
import org.utplsql.api.exception.DatabaseNotCompatibleException;
45

5-
import java.sql.CallableStatement;
6-
import java.sql.Connection;
7-
import java.sql.SQLException;
8-
import java.sql.Types;
6+
import java.sql.*;
97

108
/**
119
* Database utility functions.
1210
*/
1311
public final class DBHelper {
1412

15-
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0.3";
13+
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3";
1614

1715
private DBHelper() {}
1816

@@ -96,6 +94,51 @@ public static boolean versionCompatibilityCheck(Connection conn)
9694
return versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION);
9795
}
9896

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+
120+
/** Returns the Frameworks version string of the given connection
121+
*
122+
* @param conn Active db connection
123+
* @return
124+
* @throws SQLException
125+
*/
126+
public static Version getDatabaseFrameworkVersion( Connection conn )
127+
throws SQLException {
128+
Version result = new Version("");
129+
try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
130+
{
131+
ResultSet rs = stmt.executeQuery();
132+
133+
if ( rs.next() )
134+
result = new Version(rs.getString(1));
135+
136+
rs.close();
137+
}
138+
139+
return result;
140+
}
141+
99142
/**
100143
* Enable the dbms_output buffer with unlimited size.
101144
* @param conn the connection
@@ -119,5 +162,4 @@ public static void disableDBMSOutput(Connection conn) {
119162
System.out.println("Failed to disable dbms_output.");
120163
}
121164
}
122-
123165
}

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

Lines changed: 44 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,132 @@
11
package org.utplsql.api;
22

3+
import oracle.jdbc.OracleConnection;
4+
import org.utplsql.api.compatibility.CompatibilityProvider;
5+
import org.utplsql.api.exception.DatabaseNotCompatibleException;
36
import org.utplsql.api.exception.SomeTestsFailedException;
47
import org.utplsql.api.reporter.DocumentationReporter;
58
import org.utplsql.api.reporter.Reporter;
6-
import oracle.jdbc.OracleConnection;
9+
import org.utplsql.api.testRunner.TestRunnerStatement;
710

811
import java.sql.CallableStatement;
912
import java.sql.Connection;
1013
import java.sql.SQLException;
11-
import java.sql.Types;
12-
import java.util.ArrayList;
1314
import java.util.List;
1415

1516
/**
1617
* Created by Vinicius Avellar on 12/04/2017.
18+
*
19+
* @author Vinicius Avellar
20+
* @author pesse
1721
*/
1822
public class TestRunner {
1923

20-
private List<String> pathList = new ArrayList<>();
21-
private List<Reporter> reporterList = new ArrayList<>();
22-
private boolean colorConsole = false;
23-
private List<String> coverageSchemes = new ArrayList<>();
24-
private List<String> sourceFiles = new ArrayList<>();
25-
private List<String> testFiles = new ArrayList<>();
26-
private List<String> includeObjects = new ArrayList<>();
27-
private List<String> excludeObjects = new ArrayList<>();
28-
private FileMapperOptions sourceMappingOptions;
29-
private FileMapperOptions testMappingOptions;
30-
private boolean failOnErrors = false;
24+
private TestRunnerOptions options = new TestRunnerOptions();
3125

3226
public TestRunner addPath(String path) {
33-
this.pathList.add(path);
27+
options.pathList.add(path);
3428
return this;
3529
}
3630

3731
public TestRunner addPathList(List<String> paths) {
38-
if (pathList != null) this.pathList.addAll(paths);
32+
if (options.pathList != null) options.pathList.addAll(paths);
3933
return this;
4034
}
4135

4236
public TestRunner addReporter(Reporter reporter) {
43-
this.reporterList.add(reporter);
37+
options.reporterList.add(reporter);
4438
return this;
4539
}
4640

4741
public TestRunner colorConsole(boolean colorConsole) {
48-
this.colorConsole = colorConsole;
42+
options.colorConsole = colorConsole;
4943
return this;
5044
}
5145

5246
public TestRunner addReporterList(List<Reporter> reporterList) {
53-
if (reporterList != null) this.reporterList.addAll(reporterList);
47+
if (options.reporterList != null) options.reporterList.addAll(reporterList);
5448
return this;
5549
}
5650

5751
public TestRunner addCoverageScheme(String coverageScheme) {
58-
this.coverageSchemes.add(coverageScheme);
52+
options.coverageSchemes.add(coverageScheme);
5953
return this;
6054
}
6155

6256
public TestRunner includeObject(String obj) {
63-
this.includeObjects.add(obj);
57+
options.includeObjects.add(obj);
6458
return this;
6559
}
6660

6761
public TestRunner excludeObject(String obj) {
68-
this.excludeObjects.add(obj);
62+
options.excludeObjects.add(obj);
6963
return this;
7064
}
7165

7266
public TestRunner sourceMappingOptions(FileMapperOptions mapperOptions) {
73-
this.sourceMappingOptions = mapperOptions;
67+
options.sourceMappingOptions = mapperOptions;
7468
return this;
7569
}
7670

7771
public TestRunner testMappingOptions(FileMapperOptions mapperOptions) {
78-
this.testMappingOptions = mapperOptions;
72+
options.testMappingOptions = mapperOptions;
7973
return this;
8074
}
8175

8276
public TestRunner failOnErrors(boolean failOnErrors) {
83-
this.failOnErrors = failOnErrors;
77+
options.failOnErrors = failOnErrors;
78+
return this;
79+
}
80+
81+
public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
82+
{
83+
options.skipCompatibilityCheck = skipCompatibilityCheck;
8484
return this;
8585
}
8686

87-
public void run(Connection conn) throws SomeTestsFailedException, SQLException {
88-
for (Reporter r : this.reporterList)
87+
public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException {
88+
89+
// First of all check version compatibility
90+
if ( !options.skipCompatibilityCheck )
91+
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
92+
93+
for (Reporter r : options.reporterList)
8994
validateReporter(conn, r);
9095

91-
if (this.pathList.isEmpty()) {
92-
this.pathList.add(DBHelper.getCurrentSchema(conn));
96+
if (options.pathList.isEmpty()) {
97+
options.pathList.add(DBHelper.getCurrentSchema(conn));
9398
}
9499

95-
if (this.reporterList.isEmpty()) {
96-
this.reporterList.add(new DocumentationReporter().init(conn));
100+
if (options.reporterList.isEmpty()) {
101+
options.reporterList.add(new DocumentationReporter().init(conn));
97102
}
98103

99-
// Workaround because Oracle JDBC doesn't support passing boolean to stored procedures.
100-
String colorConsoleStr = Boolean.toString(this.colorConsole);
101-
String failOnErrors = Boolean.toString(this.failOnErrors);
104+
TestRunnerStatement testRunnerStatement = null;
102105

103-
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
104-
CallableStatement callableStatement = null;
105106
try {
106107
DBHelper.enableDBMSOutput(conn);
107108

108-
callableStatement = conn.prepareCall(
109-
"BEGIN " +
110-
"ut_runner.run(" +
111-
"a_paths => ?, " +
112-
"a_reporters => ?, " +
113-
"a_color_console => " + colorConsoleStr + ", " +
114-
"a_coverage_schemes => ?, " +
115-
"a_source_file_mappings => ?, " +
116-
"a_test_file_mappings => ?, " +
117-
"a_include_objects => ?, " +
118-
"a_exclude_objects => ?, " +
119-
"a_fail_on_errors => " + failOnErrors + "); " +
120-
"END;");
121-
122-
int paramIdx = 0;
123-
124-
callableStatement.setArray(
125-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray()));
126-
127-
callableStatement.setArray(
128-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray()));
129-
130-
if (this.coverageSchemes.isEmpty()) {
131-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
132-
} else {
133-
callableStatement.setArray(
134-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray()));
135-
}
136-
137-
if (this.sourceMappingOptions == null) {
138-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
139-
} else {
140-
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.sourceMappingOptions);
141-
142-
callableStatement.setArray(
143-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
144-
}
145-
146-
if (this.testMappingOptions == null) {
147-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
148-
} else {
149-
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions);
150-
151-
callableStatement.setArray(
152-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
153-
}
154-
155-
if (this.includeObjects.isEmpty()) {
156-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
157-
} else {
158-
callableStatement.setArray(
159-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray()));
160-
}
161-
162-
if (this.excludeObjects.isEmpty()) {
163-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
164-
} else {
165-
callableStatement.setArray(
166-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray()));
167-
}
109+
testRunnerStatement = CompatibilityProvider.getTestRunnerStatement(options, conn);
168110

169-
callableStatement.execute();
111+
testRunnerStatement.execute();
170112
} catch (SQLException e) {
171113
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
172114
throw new SomeTestsFailedException(e.getMessage(), e);
173115
} else {
174116
// If the execution failed by unexpected reasons finishes all reporters,
175117
// this way the users don't need to care about reporters' sessions hanging.
118+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
119+
176120
try (CallableStatement closeBufferStmt = conn.prepareCall("BEGIN ut_output_buffer.close(?); END;")) {
177-
closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray()));
121+
closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, options.reporterList.toArray()));
178122
closeBufferStmt.execute();
179123
} catch (SQLException ignored) {}
180124

181125
throw e;
182126
}
183127
} finally {
184-
if (callableStatement != null) {
185-
callableStatement.close();
128+
if (testRunnerStatement != null) {
129+
testRunnerStatement.close();
186130
}
187131

188132
DBHelper.disableDBMSOutput(conn);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.utplsql.api;
2+
3+
import org.utplsql.api.reporter.Reporter;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/** Holds the various possible options of TestRunner
9+
*
10+
* @author pesse
11+
*/
12+
public class TestRunnerOptions {
13+
public List<String> pathList = new ArrayList<>();
14+
public List<Reporter> reporterList = new ArrayList<>();
15+
public boolean colorConsole = false;
16+
public List<String> coverageSchemes = new ArrayList<>();
17+
public List<String> sourceFiles = new ArrayList<>();
18+
public List<String> testFiles = new ArrayList<>();
19+
public List<String> includeObjects = new ArrayList<>();
20+
public List<String> excludeObjects = new ArrayList<>();
21+
public FileMapperOptions sourceMappingOptions;
22+
public FileMapperOptions testMappingOptions;
23+
public boolean failOnErrors = false;
24+
public boolean skipCompatibilityCheck = false;
25+
}

0 commit comments

Comments
 (0)