diff --git a/src/main/java/org/utplsql/api/TestRunner.java b/src/main/java/org/utplsql/api/TestRunner.java index 96cea4e..9344abe 100644 --- a/src/main/java/org/utplsql/api/TestRunner.java +++ b/src/main/java/org/utplsql/api/TestRunner.java @@ -180,4 +180,15 @@ private void validateReporter(Connection conn, Reporter reporter) throws SQLExce reporter.init(conn, compatibilityProxy, reporterFactory); } + /** Returns the databaseVersion the TestRunner was run against + * + * @return Version of the database the TestRunner was run against + */ + public Version getUsedDatabaseVersion() { + if ( compatibilityProxy != null ) + return compatibilityProxy.getDatabaseVersion(); + else + return null; + } + } diff --git a/src/main/java/org/utplsql/api/TestRunnerOptions.java b/src/main/java/org/utplsql/api/TestRunnerOptions.java index 65ea978..b82d898 100644 --- a/src/main/java/org/utplsql/api/TestRunnerOptions.java +++ b/src/main/java/org/utplsql/api/TestRunnerOptions.java @@ -2,6 +2,8 @@ import org.utplsql.api.reporter.Reporter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -22,4 +24,5 @@ public class TestRunnerOptions { public FileMapperOptions testMappingOptions; public boolean failOnErrors = false; public boolean skipCompatibilityCheck = false; + public String clientCharacterSet = Charset.defaultCharset().toString(); } diff --git a/src/main/java/org/utplsql/api/testRunner/AbstractTestRunnerStatement.java b/src/main/java/org/utplsql/api/testRunner/AbstractTestRunnerStatement.java index 201bc3e..925d48d 100644 --- a/src/main/java/org/utplsql/api/testRunner/AbstractTestRunnerStatement.java +++ b/src/main/java/org/utplsql/api/testRunner/AbstractTestRunnerStatement.java @@ -30,7 +30,7 @@ public AbstractTestRunnerStatement(TestRunnerOptions options, Connection conn) t protected abstract String getSql(); - protected void createStatement() throws SQLException { + protected int createStatement() throws SQLException { OracleConnection oraConn = conn.unwrap(OracleConnection.class); @@ -82,6 +82,8 @@ protected void createStatement() throws SQLException { callableStatement.setArray( ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, options.excludeObjects.toArray())); } + + return paramIdx; } public void execute() throws SQLException { diff --git a/src/main/java/org/utplsql/api/testRunner/ActualTestRunnerStatement.java b/src/main/java/org/utplsql/api/testRunner/ActualTestRunnerStatement.java index 321e12b..05a05ce 100644 --- a/src/main/java/org/utplsql/api/testRunner/ActualTestRunnerStatement.java +++ b/src/main/java/org/utplsql/api/testRunner/ActualTestRunnerStatement.java @@ -33,7 +33,17 @@ protected String getSql() { "a_test_file_mappings => ?, " + "a_include_objects => ?, " + "a_exclude_objects => ?, " + - "a_fail_on_errors => " + failOnErrors + "); " + + "a_fail_on_errors => " + failOnErrors + ", " + + "a_client_character_set => ?); " + "END;"; } + + @Override + protected int createStatement() throws SQLException { + int curParamIdx = super.createStatement(); + + callableStatement.setString(++curParamIdx, options.clientCharacterSet); + + return curParamIdx; + } } diff --git a/src/main/java/org/utplsql/api/testRunner/Pre312TestRunnerStatement.java b/src/main/java/org/utplsql/api/testRunner/Pre312TestRunnerStatement.java new file mode 100644 index 0000000..c8e5f5f --- /dev/null +++ b/src/main/java/org/utplsql/api/testRunner/Pre312TestRunnerStatement.java @@ -0,0 +1,39 @@ +package org.utplsql.api.testRunner; + +import org.utplsql.api.TestRunnerOptions; + +import java.sql.Connection; +import java.sql.SQLException; + +/** TestRunner-Statement for Framework version before 3.0.3 + * Does not know about client character set + * + * @author pesse + */ +class Pre312TestRunnerStatement extends AbstractTestRunnerStatement { + + public Pre312TestRunnerStatement(TestRunnerOptions options, Connection connection ) throws SQLException { + super( options, connection); + } + + @Override + protected String getSql() { + // Workaround because Oracle JDBC doesn't support passing boolean to stored procedures. + String colorConsoleStr = Boolean.toString(options.colorConsole); + String failOnErrors = Boolean.toString(options.failOnErrors); + + return + "BEGIN " + + "ut_runner.run(" + + "a_paths => ?, " + + "a_reporters => ?, " + + "a_color_console => " + colorConsoleStr + ", " + + "a_coverage_schemes => ?, " + + "a_source_file_mappings => ?, " + + "a_test_file_mappings => ?, " + + "a_include_objects => ?, " + + "a_exclude_objects => ?, " + + "a_fail_on_errors => " + failOnErrors + "); " + + "END;"; + } +} diff --git a/src/main/java/org/utplsql/api/testRunner/TestRunnerStatementProvider.java b/src/main/java/org/utplsql/api/testRunner/TestRunnerStatementProvider.java index bbe6450..a60ea84 100644 --- a/src/main/java/org/utplsql/api/testRunner/TestRunnerStatementProvider.java +++ b/src/main/java/org/utplsql/api/testRunner/TestRunnerStatementProvider.java @@ -29,6 +29,8 @@ public static TestRunnerStatement getCompatibleTestRunnerStatement(Version datab try { if (databaseVersion.isLessThan(new Version("3.0.3"))) stmt = new Pre303TestRunnerStatement(options, conn); + else if (databaseVersion.isLessThan(new Version("3.1.2"))) + stmt = new Pre312TestRunnerStatement(options, conn); } catch ( InvalidVersionException e ) {} diff --git a/src/test/java/org/utplsql/api/OutputBufferIT.java b/src/test/java/org/utplsql/api/OutputBufferIT.java index d7b7ba0..aeb94c9 100644 --- a/src/test/java/org/utplsql/api/OutputBufferIT.java +++ b/src/test/java/org/utplsql/api/OutputBufferIT.java @@ -1,6 +1,8 @@ package org.utplsql.api; import org.junit.jupiter.api.Test; +import org.utplsql.api.compatibility.CompatibilityProxy; +import org.utplsql.api.exception.InvalidVersionException; import org.utplsql.api.reporter.CoreReporters; import org.utplsql.api.reporter.DefaultReporter; import org.utplsql.api.reporter.DocumentationReporter; @@ -9,6 +11,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; +import java.nio.charset.Charset; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -106,7 +109,7 @@ public void fetchAllLines() throws SQLException { } @Test - public void getOutputFromSonarReporter() throws SQLException { + public void getOutputFromSonarReporter() throws SQLException, InvalidVersionException { Reporter reporter = new DefaultReporter(CoreReporters.UT_SONAR_TEST_REPORTER.name(), null).init(newConnection()); new TestRunner() @@ -119,4 +122,23 @@ public void getOutputFromSonarReporter() throws SQLException { assertTrue(outputLines.size() > 0); } + @Test + public void sonarReporterHasEncodingSet() throws SQLException, InvalidVersionException { + CompatibilityProxy proxy = new CompatibilityProxy(newConnection()); + + if ( proxy.getDatabaseVersion().isGreaterOrEqualThan(new Version("3.1.2"))) { + Reporter reporter = new DefaultReporter(CoreReporters.UT_SONAR_TEST_REPORTER.name(), null).init(getConnection()); + + TestRunner tr = new TestRunner() + .addPath(getUser()) + .addReporter(reporter); + + tr.run(getConnection()); + + List outputLines = reporter.getOutputBuffer().fetchAll(getConnection()); + + assertTrue(outputLines.get(0).contains("encoding=\"" + Charset.defaultCharset().toString() + "\"")); + } + + } } diff --git a/src/test/java/org/utplsql/api/testRunner/TestRunnerStatementProviderIT.java b/src/test/java/org/utplsql/api/testRunner/TestRunnerStatementProviderIT.java index c71dd8d..9364b20 100644 --- a/src/test/java/org/utplsql/api/testRunner/TestRunnerStatementProviderIT.java +++ b/src/test/java/org/utplsql/api/testRunner/TestRunnerStatementProviderIT.java @@ -20,8 +20,20 @@ public void testGettingPre303Version() throws SQLException { @Test - public void testGettingActualVersion() throws SQLException { + public void testGettingPre312Version_from_303() throws SQLException { TestRunnerStatement stmt = TestRunnerStatementProvider.getCompatibleTestRunnerStatement(new Version("3.0.3"), new TestRunnerOptions(), getConnection()); + assertEquals(Pre312TestRunnerStatement.class, stmt.getClass()); + } + + @Test + public void testGettingPre312Version_from_311() throws SQLException { + TestRunnerStatement stmt = TestRunnerStatementProvider.getCompatibleTestRunnerStatement(new Version("3.1.1"), new TestRunnerOptions(), getConnection()); + assertEquals(Pre312TestRunnerStatement.class, stmt.getClass()); + } + + @Test + public void testGettingActualVersion() throws SQLException { + TestRunnerStatement stmt = TestRunnerStatementProvider.getCompatibleTestRunnerStatement(new Version("3.1.2"), new TestRunnerOptions(), getConnection()); assertEquals(ActualTestRunnerStatement.class, stmt.getClass()); } }