Skip to content

Commit bbffd35

Browse files
committed
Add Abstraction for DatabaseInformation
we should have some abstraction here to be able to mock several return values when testing for e.g. unexpected Version-return
1 parent 9fd066f commit bbffd35

File tree

7 files changed

+159
-20
lines changed

7 files changed

+159
-20
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ public static String newSysGuid(Connection conn) throws SQLException {
3030

3131
/**
3232
* Return the current schema name.
33+
* Deprecated. Use DatabaseInformation-Interface instead.
34+
*
3335
* @param conn the connection
3436
* @return the schema name
3537
* @throws SQLException any database error
3638
*/
39+
@Deprecated
3740
public static String getCurrentSchema(Connection conn) throws SQLException {
3841
assert conn != null;
3942
try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;")) {
@@ -44,11 +47,13 @@ public static String getCurrentSchema(Connection conn) throws SQLException {
4447
}
4548

4649
/** Returns the Frameworks version string of the given connection
50+
* Deprecated. Use DatabaseInformation-Interface instead.
4751
*
4852
* @param conn Active db connection
4953
* @return Version-string of the utPLSQL framework
5054
* @throws SQLException any database error
5155
*/
56+
@Deprecated
5257
public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLException {
5358
Objects.requireNonNull(conn);
5459
Version result = new Version("");
@@ -71,11 +76,13 @@ public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLE
7176
}
7277

7378
/** Returns the Oracle database Version from a given connection object
79+
* Deprecated. Use DatabaseInformation-Interface instead.
7480
*
7581
* @param conn Connection-Object
7682
* @return Returns version-string of the Oracle Database product component
7783
* @throws SQLException any database error
7884
*/
85+
@Deprecated
7986
public static String getOracleDatabaseVersion( Connection conn ) throws SQLException {
8087
assert conn != null;
8188
String result = null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private JavaApiVersionInfo() { }
1111

1212
private static final String BUILD_NO = "123";
1313
private static final String MAVEN_PROJECT_NAME = "utPLSQL-java-api";
14-
private static final String MAVEN_PROJECT_VERSION = "3.1.1";
14+
private static final String MAVEN_PROJECT_VERSION = "3.1.1.1-SNAPSHOT";
1515

1616
public static String getVersion() {
1717
return MAVEN_PROJECT_VERSION + "." + BUILD_NO;

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

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

33
import org.utplsql.api.compatibility.CompatibilityProxy;
4+
import org.utplsql.api.db.DatabaseInformation;
5+
import org.utplsql.api.db.DefaultDatabaseInformation;
46
import org.utplsql.api.exception.DatabaseNotCompatibleException;
57
import org.utplsql.api.exception.SomeTestsFailedException;
68
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
@@ -120,7 +122,9 @@ private void delayedAddReporters() {
120122

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

123-
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
125+
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
126+
127+
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck, databaseInformation);
124128
if ( reporterFactory == null )
125129
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);
126130

@@ -133,7 +137,7 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException,
133137
validateReporter(conn, r);
134138

135139
if (options.pathList.isEmpty()) {
136-
options.pathList.add(DBHelper.getCurrentSchema(conn));
140+
options.pathList.add(databaseInformation.getCurrentSchema(conn));
137141
}
138142

139143
if (options.reporterList.isEmpty()) {

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.utplsql.api.DBHelper;
44
import org.utplsql.api.TestRunnerOptions;
55
import org.utplsql.api.Version;
6+
import org.utplsql.api.db.DatabaseInformation;
7+
import org.utplsql.api.db.DefaultDatabaseInformation;
68
import org.utplsql.api.exception.DatabaseNotCompatibleException;
79
import org.utplsql.api.outputBuffer.OutputBuffer;
810
import org.utplsql.api.outputBuffer.OutputBufferProvider;
@@ -29,14 +31,25 @@ public class CompatibilityProxy {
2931

3032
private Version databaseVersion;
3133
private boolean compatible = false;
34+
private DatabaseInformation databaseInformation;
3235

33-
public CompatibilityProxy( Connection conn ) throws SQLException
34-
{
35-
this(conn, false);
36+
public CompatibilityProxy( Connection conn ) throws SQLException {
37+
this(conn, false, null);
3638
}
3739

38-
public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException
39-
{
40+
public CompatibilityProxy( Connection conn, DatabaseInformation databaseInformation ) throws SQLException {
41+
this(conn, false, databaseInformation);
42+
}
43+
44+
public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) throws SQLException {
45+
this(conn, skipCompatibilityCheck, null);
46+
}
47+
48+
public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck, DatabaseInformation databaseInformation ) throws SQLException {
49+
this.databaseInformation = (databaseInformation != null )
50+
? databaseInformation
51+
: new DefaultDatabaseInformation();
52+
4053
if ( skipCompatibilityCheck )
4154
doExpectCompatibility();
4255
else
@@ -51,7 +64,7 @@ public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck ) thr
5164
*/
5265
private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException
5366
{
54-
databaseVersion = DBHelper.getDatabaseFrameworkVersion(conn);
67+
databaseVersion = databaseInformation.getUtPlsqlFrameworkVersion(conn);
5568
Version clientVersion = new Version(UTPLSQL_COMPATIBILITY_VERSION);
5669

5770
if ( databaseVersion == null )
@@ -87,17 +100,8 @@ private void doExpectCompatibility()
87100
*/
88101
private boolean versionCompatibilityCheck(Connection conn, String requested, String current)
89102
throws SQLException {
90-
try(CallableStatement callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;")) {
91-
callableStatement.registerOutParameter(1, Types.SMALLINT);
92-
callableStatement.setString(2, requested);
93-
94-
if (current == null)
95-
callableStatement.setNull(3, Types.VARCHAR);
96-
else
97-
callableStatement.setString(3, current);
98-
99-
callableStatement.executeUpdate();
100-
return callableStatement.getInt(1) == 1;
103+
try {
104+
return databaseInformation.frameworkCompatibilityCheck(conn, requested, current) == 1;
101105
} catch (SQLException e) {
102106
if (e.getErrorCode() == 6550)
103107
return false;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.utplsql.api.db;
2+
3+
import org.utplsql.api.Version;
4+
5+
import java.sql.Connection;
6+
import java.sql.SQLException;
7+
8+
/** Abstraction-interface to encapsulate Database-Calls (and potentially mock them)
9+
*
10+
* @author pesse
11+
*/
12+
public interface DatabaseInformation {
13+
14+
Version getUtPlsqlFrameworkVersion(Connection conn ) throws SQLException;
15+
16+
String getOracleVersion( Connection conn ) throws SQLException;
17+
18+
String getCurrentSchema( Connection conn ) throws SQLException;
19+
20+
int frameworkCompatibilityCheck(Connection conn, String requested, String current) throws SQLException;
21+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.utplsql.api.db;
2+
3+
import org.utplsql.api.Version;
4+
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
5+
6+
import java.sql.*;
7+
import java.util.Objects;
8+
9+
public class DefaultDatabaseInformation implements DatabaseInformation {
10+
11+
@Override
12+
public Version getUtPlsqlFrameworkVersion(Connection conn) throws SQLException {
13+
Objects.requireNonNull(conn);
14+
Version result = new Version("");
15+
try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
16+
{
17+
ResultSet rs = stmt.executeQuery();
18+
19+
if ( rs.next() )
20+
result = new Version(rs.getString(1));
21+
22+
rs.close();
23+
} catch ( SQLException e ) {
24+
if ( e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE )
25+
throw new UtPLSQLNotInstalledException(e);
26+
else
27+
throw e;
28+
}
29+
30+
return result;
31+
}
32+
33+
@Override
34+
public String getOracleVersion(Connection conn) throws SQLException {
35+
Objects.requireNonNull(conn);
36+
String result = null;
37+
try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'"))
38+
{
39+
ResultSet rs = stmt.executeQuery();
40+
41+
if ( rs.next() )
42+
result = rs.getString(1);
43+
}
44+
45+
return result;
46+
}
47+
48+
@Override
49+
public String getCurrentSchema(Connection conn) throws SQLException {
50+
Objects.requireNonNull(conn);
51+
try (CallableStatement callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;")) {
52+
callableStatement.registerOutParameter(1, Types.VARCHAR);
53+
callableStatement.executeUpdate();
54+
return callableStatement.getString(1);
55+
}
56+
}
57+
58+
@Override
59+
public int frameworkCompatibilityCheck(Connection conn, String requested, String current) throws SQLException {
60+
try(CallableStatement callableStatement = conn.prepareCall("BEGIN ? := ut_runner.version_compatibility_check(?, ?); END;")) {
61+
callableStatement.registerOutParameter(1, Types.SMALLINT);
62+
callableStatement.setString(2, requested);
63+
64+
if (current == null)
65+
callableStatement.setNull(3, Types.VARCHAR);
66+
else
67+
callableStatement.setString(3, current);
68+
69+
callableStatement.executeUpdate();
70+
return callableStatement.getInt(1);
71+
}
72+
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.utplsql.api.db.DatabaseInformation;
5+
import org.utplsql.api.db.DefaultDatabaseInformation;
6+
7+
import java.sql.SQLException;
8+
9+
import static org.junit.jupiter.api.Assertions.assertNotNull;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
public class DatabaseInformationIT extends AbstractDatabaseTest {
13+
14+
@Test
15+
public void getFrameworkVersion() throws SQLException {
16+
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
17+
18+
Version v = databaseInformation.getUtPlsqlFrameworkVersion(getConnection());
19+
assertTrue(v.isValid());
20+
System.out.println(v.getNormalizedString() + " - " + v.toString());
21+
}
22+
23+
@Test
24+
public void getOracleDatabaseVersion() throws SQLException {
25+
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
26+
27+
String databaseVersion = databaseInformation.getOracleVersion(getConnection());
28+
assertNotNull(databaseVersion);
29+
}
30+
}

0 commit comments

Comments
 (0)