Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
First set of refactorings around ConnectionInfo
It should really be only an Info-Object, not a DataSource wannabe
  • Loading branch information
pesse committed Jun 5, 2018
commit 2772add63931c2a6bd7f5ca55562846472be4823
69 changes: 5 additions & 64 deletions src/main/java/org/utplsql/cli/ConnectionInfo.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
package org.utplsql.cli;

import com.beust.jcommander.IStringConverter;
import com.zaxxer.hikari.HikariDataSource;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionInfo {

private String databaseVersion;

static {
String oracleHome = System.getenv("ORACLE_HOME");
if (oracleHome != null) {
System.setProperty("oracle.net.tns_admin",
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
}
}

private HikariDataSource pds = new HikariDataSource();
public static final String COMMANDLINE_PARAM_DESCRIPTION = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>";
private String connectionInfo;

public ConnectionInfo(String connectionInfo) {

pds.setJdbcUrl("jdbc:oracle:thin:" + connectionInfo);
pds.setAutoCommit(false);
this.connectionInfo = connectionInfo;
}

public void setMaxConnections( int maxConnections ) {
pds.setMaximumPoolSize(maxConnections);
}

public Connection getConnection() throws SQLException {
return pds.getConnection();
public String getConnectionString() {
return connectionInfo;
}

public static class ConnectionStringConverter implements IStringConverter<ConnectionInfo> {
Expand All @@ -44,41 +22,4 @@ public ConnectionInfo convert(String s) {
return new ConnectionInfo(s);
}
}

public String getOracleDatabaseVersion() throws SQLException
{
try ( Connection conn = getConnection() ) {
return getOracleDatabaseVersion(conn);
}
}

public String getOracleDatabaseVersion( Connection conn ) throws SQLException
{
if ( databaseVersion == null ) {
databaseVersion = getOracleDatabaseVersionFromConnection( conn );
}

return databaseVersion;
}

/** TODO: Outsource this to Java-API
*
* @param conn
* @return
* @throws SQLException
*/
public static String getOracleDatabaseVersionFromConnection( Connection conn ) throws SQLException {
assert conn != null;
String result = null;
try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'"))
{
ResultSet rs = stmt.executeQuery();

if ( rs.next() )
result = rs.getString(1);
}

return result;
}

}
44 changes: 44 additions & 0 deletions src/main/java/org/utplsql/cli/DataSourceProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.utplsql.cli;

import com.zaxxer.hikari.HikariDataSource;
import org.utplsql.api.EnvironmentVariableUtil;

import javax.sql.DataSource;
import java.io.File;

/** Helper class to give you a ready-to-use datasource
*
* @author pesse
*/
public class DataSourceProvider {

static {
String oracleHome = EnvironmentVariableUtil.getEnvValue("ORACLE_HOME");
if (oracleHome != null) {
System.setProperty("oracle.net.tns_admin",
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
}
}

public static DataSource getDataSource(ConnectionInfo info, int maxConnections ) {

requireOjdbc();

HikariDataSource pds = new HikariDataSource();
pds.setJdbcUrl("jdbc:oracle:thin:" + info.getConnectionString());
pds.setAutoCommit(false);
pds.setMaximumPoolSize(maxConnections);
return pds;
}

private static void requireOjdbc() {
if ( !OracleLibraryChecker.checkOjdbcExists() )
{
System.out.println("Could not find Oracle JDBC driver in classpath. Please download the jar from Oracle website" +
" and copy it to the 'lib' folder of your utPLSQL-cli installation.");
System.out.println("Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html");

throw new RuntimeException("Can't run utPLSQL-cli without Oracle JDBC driver");
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/utplsql/cli/ReporterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.reporters.ReporterOptionsAware;

import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -77,10 +78,10 @@ public List<Reporter> initReporters(Connection conn, ReporterFactory reporterFac
/** Starts a separate thread for each Reporter to gather its results
*
* @param executorService
* @param ci
* @param dataSource
* @param returnCode
*/
public void startReporterGatherers(ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
public void startReporterGatherers(ExecutorService executorService, final DataSource dataSource, final int[] returnCode)
{
// TODO: Implement Init-check
// Gather each reporter results on a separate thread.
Expand All @@ -89,7 +90,7 @@ public void startReporterGatherers(ExecutorService executorService, final Connec
List<PrintStream> printStreams = new ArrayList<>();
PrintStream fileOutStream = null;

try (Connection conn = ci.getConnection()) {
try (Connection conn = dataSource.getConnection()) {
if (ro.outputToScreen()) {
printStreams.add(System.out);
ro.getReporterObj().getOutputBuffer().setFetchSize(1);
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.exception.DatabaseConnectionFailed;

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

@Parameter(
Expand Down Expand Up @@ -111,9 +112,6 @@ public List<String> getTestPaths() {

public int run() throws Exception {

RunCommandChecker.checkOracleJDBCExists();


final List<Reporter> reporterList;
final List<String> testPaths = getTestPaths();

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

final ConnectionInfo ci = getConnectionInfo();
ci.setMaxConnections(getReporterManager().getNumberOfReporters()+1);
final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters()+1);

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

// Check if orai18n exists if database version is 11g
RunCommandChecker.checkOracleI18nExists(ci.getOracleDatabaseVersion(conn));
RunCommandChecker.checkOracleI18nExists(conn);

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

// Run tests.
executorService.submit(() -> {
try (Connection conn = ci.getConnection()) {
try (Connection conn = dataSource.getConnection()) {
TestRunner testRunner = new TestRunner()
.addPathList(testPaths)
.addReporterList(reporterList)
Expand All @@ -201,7 +198,7 @@ public int run() throws Exception {
});

// Gather each reporter results on a separate thread.
getReporterManager().startReporterGatherers(executorService, ci, returnCode);
getReporterManager().startReporterGatherers(executorService, dataSource, returnCode);

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
Expand Down
24 changes: 7 additions & 17 deletions src/main/java/org/utplsql/cli/RunCommandChecker.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package org.utplsql.cli;

import org.utplsql.api.DBHelper;
import org.utplsql.api.Version;
import org.utplsql.api.compatibility.OptionalFeatures;

import java.sql.Connection;
import java.sql.SQLException;

/** Helper class to check several circumstances with RunCommand. Might need refactoring.
*
* @author pesse
*/
class RunCommandChecker {

/** Checks that ojdbc library exists
*
*/
static void checkOracleJDBCExists()
{
if ( !OracleLibraryChecker.checkOjdbcExists() )
{
System.out.println("Could not find Oracle JDBC driver in classpath. Please download the jar from Oracle website" +
" and copy it to the 'lib' folder of your utPLSQL-cli installation.");
System.out.println("Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html");

throw new RuntimeException("Can't run utPLSQL-cli without Oracle JDBC driver");
}
}

/** Checks that orai18n library exists if database is an oracle 11
*
*/
static void checkOracleI18nExists(String oracleDatabaseVersion )
{
static void checkOracleI18nExists(Connection con) throws SQLException {

String oracleDatabaseVersion = DBHelper.getOracleDatabaseVersion(con);
if ( oracleDatabaseVersion.startsWith("11.") && !OracleLibraryChecker.checkOrai18nExists() )
{
System.out.println("Warning: Could not find Oracle i18n driver in classpath. Depending on the database charset " +
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/utplsql/cli/VersionInfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.utplsql.api.Version;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,7 +19,7 @@ public class VersionInfoCommand {
@Parameter(
converter = ConnectionInfo.ConnectionStringConverter.class,
variableArity = true,
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
description = ConnectionInfo.COMMANDLINE_PARAM_DESCRIPTION)
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();

public ConnectionInfo getConnectionInfo() {
Expand All @@ -28,16 +29,17 @@ public ConnectionInfo getConnectionInfo() {
return null;
}

public int run() throws Exception {
public int run() {

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

ConnectionInfo ci = getConnectionInfo();
if ( ci != null ) {
// TODO: Ora-check
ci.setMaxConnections(1);
try (Connection con = ci.getConnection()) {

DataSource dataSource = DataSourceProvider.getDataSource(ci, 1);

try (Connection con = dataSource.getConnection()) {
Version v = DBHelper.getDatabaseFrameworkVersion( con );
System.out.println("utPLSQL " + v.getNormalizedString());
}
Expand Down