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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis/create_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ VERSION=`date +%Y%m%d%H%M`

mkdir dist
mv target/appassembler utPLSQL-cli
# Remove Oracle libraries du to licensing problems
rm utPLSQL-cli/lib/ojdbc8*
rm utPLSQL-cli/lib/orai18n*

zip -r -q dist/utPLSQL-cli-${TRAVIS_BRANCH}-${VERSION}.zip utPLSQL-cli
zip -r -q utPLSQL-cli.zip utPLSQL-cli

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ You can download development versions on [Bintray](https://bintray.com/viniciusa
## Requirements
* [Java SE Runtime Environment 8](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html)
* When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.
* Due to Oracle license we can't ship the necessary oracle libraries directly with utPLSQL-cli. <b>Please download the libraries directly from oracle website and put the jars into the "lib" folder of your utPLSQL-cli installation</b>
* Oracle JDBC driver: http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html
* If you are on a 11g database you might need the orai18n library, too: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

## Compatibility
The latest CLI is always compatible with all database frameworks of the same major version.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/utplsql/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.cli.exception.DatabaseConnectionFailed;

public class Cli {

Expand Down Expand Up @@ -34,6 +37,8 @@ public static void main(String[] args) {
} else {
jc.usage();
}
} catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed e ) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/utplsql/cli/ConnectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

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) {
Expand Down Expand Up @@ -42,4 +46,40 @@ public ConnectionInfo convert(String 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;
}

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

/** Simple class to check whether needed Oracle libraries are on classpath or not
*
* @author pesse
*/
class OracleLibraryChecker {

private static boolean classExists( String classFullName ){
try
{
Class.forName(classFullName);

return true;
}
catch ( ClassNotFoundException e )
{
return false;
}
}

/** Checks if OJDBC library is on the classpath by searching for oracle.jdbc.OracleDriver class
*
* @return true or false
*/
public static boolean checkOjdbcExists() {
return classExists("oracle.jdbc.OracleDriver");
}

/** Checks if Orai18n library is on the classpath by searching for oracle.i18n.text.OraCharset
*
* @return true or false
*/
public static boolean checkOrai18nExists() {
return classExists("oracle.i18n.text.OraCharset");
}


}
51 changes: 40 additions & 11 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.utplsql.api.*;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.compatibility.OptionalFeatures;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.exception.DatabaseConnectionFailed;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -80,6 +83,8 @@ public class RunCommand {
"most actual. Use this if you use CLI with a development version of utPLSQL-framework")
private boolean skipCompatibilityCheck = false;

private CompatibilityProxy compatibilityProxy;

public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
}
Expand All @@ -89,6 +94,9 @@ public List<String> getTestPaths() {
}

public int run() throws Exception {

RunCommandChecker.checkOracleJDBCExists();

final ConnectionInfo ci = getConnectionInfo();

final List<Reporter> reporterList;
Expand All @@ -107,14 +115,27 @@ public int run() throws Exception {
// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = ci.getConnection()) {

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

// First of all do a compatibility check and fail-fast
checkFrameworkCompatibility(conn);
compatibilityProxy = checkFrameworkCompatibility(conn);

reporterList = initReporters(conn, reporterOptionsList);

} catch (SQLException e) {
System.out.println(e.getMessage());
return Cli.DEFAULT_ERROR_CODE;
if ( e.getErrorCode() == 1017 || e.getErrorCode() == 12514 ) {
throw new DatabaseConnectionFailed(e);
}
else {
throw e;
}
}

// Output a message if --failureExitCode is set but database framework is not capable of
String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getDatabaseVersion());
if ( msg != null ) {
System.out.println(msg);
}

ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());
Expand Down Expand Up @@ -256,21 +277,19 @@ public List<ReporterOptions> getReporterOptionsList() {
* @param conn Active Connection
* @throws SQLException
*/
private void checkFrameworkCompatibility(Connection conn) throws SQLException {
private CompatibilityProxy checkFrameworkCompatibility(Connection conn) throws SQLException {

if ( !skipCompatibilityCheck ) {
try {
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
} catch (DatabaseNotCompatibleException e) {
System.out.println(e.getMessage());
CompatibilityProxy proxy = new CompatibilityProxy(conn, skipCompatibilityCheck);

throw e;
}
if ( !skipCompatibilityCheck ) {
proxy.failOnNotCompatible();
}
else {
System.out.println("Skipping Compatibility check with framework version, expecting the latest version " +
"to be installed in database");
}

return proxy;
}

public FileMapperOptions getMapperOptions(List<String> mappingParams, List<String> filePaths) {
Expand Down Expand Up @@ -320,4 +339,14 @@ public FileMapperOptions getMapperOptions(List<String> mappingParams, List<Strin
return mapperOptions;
}

/** Returns the version of the database framework if available
*
* @return
*/
public Version getDatabaseVersion() {
if ( compatibilityProxy != null )
return compatibilityProxy.getDatabaseVersion();

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

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

/** 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 )
{
if ( oracleDatabaseVersion.startsWith("11.") && !OracleLibraryChecker.checkOrai18nExists() )
{
System.out.println("Warning: Could not find Oracle i18n driver in classpath. Depending on the database charset " +
"utPLSQL-cli might not run properly. It is recommended you download " +
"the i18n driver from the 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/enterprise-edition/jdbc-112010-090769.html");
}
}

/** Returns a warning message if failureExitCode is specified but database version is too low
*
* @param failureExitCode
* @param databaseVersion
*/
static String getCheckFailOnErrorMessage(int failureExitCode, Version databaseVersion) {
if ( failureExitCode != 1 && !OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(databaseVersion)) {
return "Your database framework version (" + databaseVersion.getNormalizedString() + ") is not able to " +
"redirect failureCodes. Please upgrade to a newer version if you want to use that feature.";
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.utplsql.cli.exception;

import java.sql.SQLException;

public class DatabaseConnectionFailed extends SQLException {

public DatabaseConnectionFailed(SQLException cause ) {
super( "Could not establish connection to database. Reason: " + cause.getMessage(), cause);
}
}
8 changes: 7 additions & 1 deletion src/test/java/org/utplsql/cli/RunCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Assert;
import org.junit.Test;
import org.utplsql.api.CustomTypes;
import org.utplsql.api.compatibility.OptionalFeatures;

import java.util.List;

Expand Down Expand Up @@ -119,7 +120,12 @@ public void run_Default() {

try {
int result = runCmd.run();
Assert.assertEquals(2, result);

// Only expect failure-exit-code to work on several framework versions
if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) )
Assert.assertEquals(2, result);
else
Assert.assertEquals(0, result);
}
catch ( Exception e ) {
Assert.fail(e.getMessage());
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/utplsql/cli/TestRunCommandChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.utplsql.cli;

import org.junit.Assert;
import org.junit.Test;
import org.utplsql.api.Version;

public class TestRunCommandChecker {

@Test
public void getCheckFailOnErrorMessage()
{
// FailOnError option should work since 3.0.3+ framework
Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.0")));
Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.1")));
Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.2")));
Assert.assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.3")));
Assert.assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.4")));
}
}