Skip to content

Commit 390980e

Browse files
authored
Merge pull request #22 from utPLSQL/feature/version_check
Enable dbms_output before calling ut_runner.run. Add the version compatibility check function (work in progress).
2 parents 08ffc6a + e121867 commit 390980e

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
public final class DBHelper {
1414

15+
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0.3";
16+
1517
private DBHelper() {}
1618

1719
/**
@@ -52,4 +54,70 @@ public static String getCurrentSchema(Connection conn) throws SQLException {
5254
}
5355
}
5456

57+
/**
58+
* Check the utPLSQL version compatibility.
59+
* @param conn the connection
60+
* @return true if the requested utPLSQL version is compatible with the one installed on database
61+
* @throws SQLException any database error
62+
*/
63+
public static boolean versionCompatibilityCheck(Connection conn, String requested, String current)
64+
throws SQLException {
65+
CallableStatement callableStatement = null;
66+
try {
67+
callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;");
68+
callableStatement.registerOutParameter(1, Types.SMALLINT);
69+
callableStatement.setString(2, requested);
70+
71+
if (current == null)
72+
callableStatement.setNull(3, Types.VARCHAR);
73+
else
74+
callableStatement.setString(3, current);
75+
76+
callableStatement.executeUpdate();
77+
return callableStatement.getInt(1) == 1;
78+
} catch (SQLException e) {
79+
if (e.getErrorCode() == 6550)
80+
return false;
81+
else
82+
throw e;
83+
} finally {
84+
if (callableStatement != null)
85+
callableStatement.close();
86+
}
87+
}
88+
89+
public static boolean versionCompatibilityCheck(Connection conn, String requested)
90+
throws SQLException {
91+
return versionCompatibilityCheck(conn, requested, null);
92+
}
93+
94+
public static boolean versionCompatibilityCheck(Connection conn)
95+
throws SQLException {
96+
return versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION);
97+
}
98+
99+
/**
100+
* Enable the dbms_output buffer with unlimited size.
101+
* @param conn the connection
102+
*/
103+
public static void enableDBMSOutput(Connection conn) {
104+
try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.enable(NULL); END;")) {
105+
call.execute();
106+
} catch (SQLException e) {
107+
System.out.println("Failed to enable dbms_output.");
108+
}
109+
}
110+
111+
/**
112+
* Disable the dbms_output buffer.
113+
* @param conn the connection
114+
*/
115+
public static void disableDBMSOutput(Connection conn) {
116+
try (CallableStatement call = conn.prepareCall("BEGIN dbms_output.disable(); END;")) {
117+
call.execute();
118+
} catch (SQLException e) {
119+
System.out.println("Failed to disable dbms_output.");
120+
}
121+
}
122+
55123
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
103103
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
104104
CallableStatement callableStatement = null;
105105
try {
106+
DBHelper.enableDBMSOutput(conn);
107+
106108
callableStatement = conn.prepareCall(
107109
"BEGIN " +
108110
"ut_runner.run(" +
@@ -172,8 +174,11 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
172174
throw e;
173175
}
174176
} finally {
175-
if (callableStatement != null)
177+
if (callableStatement != null) {
176178
callableStatement.close();
179+
}
180+
181+
DBHelper.disableDBMSOutput(conn);
177182
}
178183
}
179184

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.utplsql.api.rules.DatabaseRule;
7+
8+
import java.sql.SQLException;
9+
10+
public class DBHelperTest {
11+
12+
@Rule
13+
public final DatabaseRule db = new DatabaseRule();
14+
15+
@Test
16+
public void compatibleVersion() {
17+
try {
18+
boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.0.0", "3.0.0");
19+
Assert.assertTrue(isCompatible);
20+
} catch (SQLException e) {
21+
e.printStackTrace();
22+
Assert.fail();
23+
}
24+
}
25+
26+
@Test
27+
public void incompatibleVersion() {
28+
try {
29+
boolean isCompatible = DBHelper.versionCompatibilityCheck(db.newConnection(), "3.1.0", "3.0.0");
30+
Assert.assertFalse(isCompatible);
31+
} catch (SQLException e) {
32+
e.printStackTrace();
33+
Assert.fail();
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)