Skip to content

Commit 2772add

Browse files
committed
First set of refactorings around ConnectionInfo
It should really be only an Info-Object, not a DataSource wannabe
1 parent fbbef6d commit 2772add

File tree

6 files changed

+75
-100
lines changed

6 files changed

+75
-100
lines changed
Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
package org.utplsql.cli;
22

33
import com.beust.jcommander.IStringConverter;
4-
import com.zaxxer.hikari.HikariDataSource;
5-
6-
import java.io.File;
7-
import java.sql.Connection;
8-
import java.sql.PreparedStatement;
9-
import java.sql.ResultSet;
10-
import java.sql.SQLException;
114

125
public class ConnectionInfo {
136

14-
private String databaseVersion;
15-
16-
static {
17-
String oracleHome = System.getenv("ORACLE_HOME");
18-
if (oracleHome != null) {
19-
System.setProperty("oracle.net.tns_admin",
20-
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
21-
}
22-
}
23-
24-
private HikariDataSource pds = new HikariDataSource();
7+
public static final String COMMANDLINE_PARAM_DESCRIPTION = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>";
8+
private String connectionInfo;
259

2610
public ConnectionInfo(String connectionInfo) {
27-
28-
pds.setJdbcUrl("jdbc:oracle:thin:" + connectionInfo);
29-
pds.setAutoCommit(false);
11+
this.connectionInfo = connectionInfo;
3012
}
3113

32-
public void setMaxConnections( int maxConnections ) {
33-
pds.setMaximumPoolSize(maxConnections);
34-
}
35-
36-
public Connection getConnection() throws SQLException {
37-
return pds.getConnection();
14+
public String getConnectionString() {
15+
return connectionInfo;
3816
}
3917

4018
public static class ConnectionStringConverter implements IStringConverter<ConnectionInfo> {
@@ -44,41 +22,4 @@ public ConnectionInfo convert(String s) {
4422
return new ConnectionInfo(s);
4523
}
4624
}
47-
48-
public String getOracleDatabaseVersion() throws SQLException
49-
{
50-
try ( Connection conn = getConnection() ) {
51-
return getOracleDatabaseVersion(conn);
52-
}
53-
}
54-
55-
public String getOracleDatabaseVersion( Connection conn ) throws SQLException
56-
{
57-
if ( databaseVersion == null ) {
58-
databaseVersion = getOracleDatabaseVersionFromConnection( conn );
59-
}
60-
61-
return databaseVersion;
62-
}
63-
64-
/** TODO: Outsource this to Java-API
65-
*
66-
* @param conn
67-
* @return
68-
* @throws SQLException
69-
*/
70-
public static String getOracleDatabaseVersionFromConnection( Connection conn ) throws SQLException {
71-
assert conn != null;
72-
String result = null;
73-
try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'"))
74-
{
75-
ResultSet rs = stmt.executeQuery();
76-
77-
if ( rs.next() )
78-
result = rs.getString(1);
79-
}
80-
81-
return result;
82-
}
83-
8425
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.utplsql.cli;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import org.utplsql.api.EnvironmentVariableUtil;
5+
6+
import javax.sql.DataSource;
7+
import java.io.File;
8+
9+
/** Helper class to give you a ready-to-use datasource
10+
*
11+
* @author pesse
12+
*/
13+
public class DataSourceProvider {
14+
15+
static {
16+
String oracleHome = EnvironmentVariableUtil.getEnvValue("ORACLE_HOME");
17+
if (oracleHome != null) {
18+
System.setProperty("oracle.net.tns_admin",
19+
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
20+
}
21+
}
22+
23+
public static DataSource getDataSource(ConnectionInfo info, int maxConnections ) {
24+
25+
requireOjdbc();
26+
27+
HikariDataSource pds = new HikariDataSource();
28+
pds.setJdbcUrl("jdbc:oracle:thin:" + info.getConnectionString());
29+
pds.setAutoCommit(false);
30+
pds.setMaximumPoolSize(maxConnections);
31+
return pds;
32+
}
33+
34+
private static void requireOjdbc() {
35+
if ( !OracleLibraryChecker.checkOjdbcExists() )
36+
{
37+
System.out.println("Could not find Oracle JDBC driver in classpath. Please download the jar from Oracle website" +
38+
" and copy it to the 'lib' folder of your utPLSQL-cli installation.");
39+
System.out.println("Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html");
40+
41+
throw new RuntimeException("Can't run utPLSQL-cli without Oracle JDBC driver");
42+
}
43+
}
44+
}

src/main/java/org/utplsql/cli/ReporterManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.utplsql.api.reporter.ReporterFactory;
77
import org.utplsql.cli.reporters.ReporterOptionsAware;
88

9+
import javax.sql.DataSource;
910
import java.io.FileNotFoundException;
1011
import java.io.FileOutputStream;
1112
import java.io.PrintStream;
@@ -77,10 +78,10 @@ public List<Reporter> initReporters(Connection conn, ReporterFactory reporterFac
7778
/** Starts a separate thread for each Reporter to gather its results
7879
*
7980
* @param executorService
80-
* @param ci
81+
* @param dataSource
8182
* @param returnCode
8283
*/
83-
public void startReporterGatherers(ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
84+
public void startReporterGatherers(ExecutorService executorService, final DataSource dataSource, final int[] returnCode)
8485
{
8586
// TODO: Implement Init-check
8687
// Gather each reporter results on a separate thread.
@@ -89,7 +90,7 @@ public void startReporterGatherers(ExecutorService executorService, final Connec
8990
List<PrintStream> printStreams = new ArrayList<>();
9091
PrintStream fileOutStream = null;
9192

92-
try (Connection conn = ci.getConnection()) {
93+
try (Connection conn = dataSource.getConnection()) {
9394
if (ro.outputToScreen()) {
9495
printStreams.add(System.out);
9596
ro.getReporterObj().getOutputBuffer().setFetchSize(1);

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.utplsql.api.reporter.ReporterFactory;
1313
import org.utplsql.cli.exception.DatabaseConnectionFailed;
1414

15+
import javax.sql.DataSource;
1516
import java.io.File;
1617
import java.sql.Connection;
1718
import java.sql.SQLException;
@@ -35,7 +36,7 @@ public class RunCommand {
3536
required = true,
3637
converter = ConnectionInfo.ConnectionStringConverter.class,
3738
arity = 1,
38-
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
39+
description = ConnectionInfo.COMMANDLINE_PARAM_DESCRIPTION)
3940
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
4041

4142
@Parameter(
@@ -111,9 +112,6 @@ public List<String> getTestPaths() {
111112

112113
public int run() throws Exception {
113114

114-
RunCommandChecker.checkOracleJDBCExists();
115-
116-
117115
final List<Reporter> reporterList;
118116
final List<String> testPaths = getTestPaths();
119117

@@ -144,14 +142,13 @@ public int run() throws Exception {
144142
final ArrayList<String> finalIncludeObjectsList = includeObjectsList;
145143
final ArrayList<String> finalExcludeObjectsList = excludeObjectsList;
146144

147-
final ConnectionInfo ci = getConnectionInfo();
148-
ci.setMaxConnections(getReporterManager().getNumberOfReporters()+1);
145+
final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters()+1);
149146

150147
// Do the reporters initialization, so we can use the id to run and gather results.
151-
try (Connection conn = ci.getConnection()) {
148+
try (Connection conn = dataSource.getConnection()) {
152149

153150
// Check if orai18n exists if database version is 11g
154-
RunCommandChecker.checkOracleI18nExists(ci.getOracleDatabaseVersion(conn));
151+
RunCommandChecker.checkOracleI18nExists(conn);
155152

156153
// First of all do a compatibility check and fail-fast
157154
compatibilityProxy = checkFrameworkCompatibility(conn);
@@ -178,7 +175,7 @@ public int run() throws Exception {
178175

179176
// Run tests.
180177
executorService.submit(() -> {
181-
try (Connection conn = ci.getConnection()) {
178+
try (Connection conn = dataSource.getConnection()) {
182179
TestRunner testRunner = new TestRunner()
183180
.addPathList(testPaths)
184181
.addReporterList(reporterList)
@@ -201,7 +198,7 @@ public int run() throws Exception {
201198
});
202199

203200
// Gather each reporter results on a separate thread.
204-
getReporterManager().startReporterGatherers(executorService, ci, returnCode);
201+
getReporterManager().startReporterGatherers(executorService, dataSource, returnCode);
205202

206203
executorService.shutdown();
207204
executorService.awaitTermination(60, TimeUnit.MINUTES);

src/main/java/org/utplsql/cli/RunCommandChecker.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package org.utplsql.cli;
22

3+
import org.utplsql.api.DBHelper;
34
import org.utplsql.api.Version;
45
import org.utplsql.api.compatibility.OptionalFeatures;
56

7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
610
/** Helper class to check several circumstances with RunCommand. Might need refactoring.
711
*
812
* @author pesse
913
*/
1014
class RunCommandChecker {
1115

12-
/** Checks that ojdbc library exists
13-
*
14-
*/
15-
static void checkOracleJDBCExists()
16-
{
17-
if ( !OracleLibraryChecker.checkOjdbcExists() )
18-
{
19-
System.out.println("Could not find Oracle JDBC driver in classpath. Please download the jar from Oracle website" +
20-
" and copy it to the 'lib' folder of your utPLSQL-cli installation.");
21-
System.out.println("Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html");
22-
23-
throw new RuntimeException("Can't run utPLSQL-cli without Oracle JDBC driver");
24-
}
25-
}
26-
2716
/** Checks that orai18n library exists if database is an oracle 11
2817
*
2918
*/
30-
static void checkOracleI18nExists(String oracleDatabaseVersion )
31-
{
19+
static void checkOracleI18nExists(Connection con) throws SQLException {
20+
21+
String oracleDatabaseVersion = DBHelper.getOracleDatabaseVersion(con);
3222
if ( oracleDatabaseVersion.startsWith("11.") && !OracleLibraryChecker.checkOrai18nExists() )
3323
{
3424
System.out.println("Warning: Could not find Oracle i18n driver in classpath. Depending on the database charset " +

src/main/java/org/utplsql/cli/VersionInfoCommand.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.utplsql.api.Version;
99
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
1010

11+
import javax.sql.DataSource;
1112
import java.sql.Connection;
1213
import java.util.ArrayList;
1314
import java.util.List;
@@ -18,7 +19,7 @@ public class VersionInfoCommand {
1819
@Parameter(
1920
converter = ConnectionInfo.ConnectionStringConverter.class,
2021
variableArity = true,
21-
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
22+
description = ConnectionInfo.COMMANDLINE_PARAM_DESCRIPTION)
2223
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
2324

2425
public ConnectionInfo getConnectionInfo() {
@@ -28,16 +29,17 @@ public ConnectionInfo getConnectionInfo() {
2829
return null;
2930
}
3031

31-
public int run() throws Exception {
32+
public int run() {
3233

3334
System.out.println(CliVersionInfo.getInfo());
34-
System.out.println("Java-API " + JavaApiVersionInfo.getVersion());
35+
System.out.println(JavaApiVersionInfo.getInfo());
3536

3637
ConnectionInfo ci = getConnectionInfo();
3738
if ( ci != null ) {
38-
// TODO: Ora-check
39-
ci.setMaxConnections(1);
40-
try (Connection con = ci.getConnection()) {
39+
40+
DataSource dataSource = DataSourceProvider.getDataSource(ci, 1);
41+
42+
try (Connection con = dataSource.getConnection()) {
4143
Version v = DBHelper.getDatabaseFrameworkVersion( con );
4244
System.out.println("utPLSQL " + v.getNormalizedString());
4345
}

0 commit comments

Comments
 (0)