Skip to content

Commit 95d3fd7

Browse files
committed
Keeping compatibility with all utPLSQL 3.x-versions
1 parent e3de8de commit 95d3fd7

15 files changed

+301
-124
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.utplsql.api.TestRunnerOptions;
55
import org.utplsql.api.Version;
66
import org.utplsql.api.exception.DatabaseNotCompatibleException;
7+
import org.utplsql.api.outputBuffer.OutputBuffer;
8+
import org.utplsql.api.outputBuffer.OutputBufferProvider;
9+
import org.utplsql.api.reporter.Reporter;
710
import org.utplsql.api.testRunner.TestRunnerStatement;
811
import org.utplsql.api.testRunner.TestRunnerStatementProvider;
912

@@ -20,7 +23,7 @@
2023
*/
2124
public class CompatibilityProxy {
2225

23-
public static final String UTPLSQL_API_VERSION = "3.0.4";
26+
public static final String UTPLSQL_API_VERSION = "3.1.0";
2427
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0";
2528

2629
private Version databaseVersion;
@@ -146,4 +149,15 @@ public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Con
146149
{
147150
return TestRunnerStatementProvider.getCompatibleTestRunnerStatement(databaseVersion, options, conn);
148151
}
152+
153+
/** Returns an OutputBuffer compatible with the current framework
154+
*
155+
* @param reporter
156+
* @param conn
157+
* @return
158+
* @throws SQLException
159+
*/
160+
public OutputBuffer getOutputBuffer(Reporter reporter, Connection conn) throws SQLException {
161+
return OutputBufferProvider.getCompatibleOutputBuffer(databaseVersion, reporter, conn);
162+
}
149163
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.utplsql.api.outputBuffer;
2+
3+
import oracle.jdbc.OracleCallableStatement;
4+
import oracle.jdbc.OracleConnection;
5+
import oracle.jdbc.OraclePreparedStatement;
6+
import oracle.jdbc.OracleTypes;
7+
import org.utplsql.api.reporter.Reporter;
8+
9+
import java.io.PrintStream;
10+
import java.sql.Connection;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.function.Consumer;
16+
17+
/**
18+
* Fetches the lines produced by a reporter.
19+
*
20+
* @author vinicius
21+
* @author pesse
22+
*/
23+
abstract class AbstractOutputBuffer implements OutputBuffer {
24+
25+
private Reporter reporter;
26+
27+
/**
28+
* Creates a new DefaultOutputBuffer.
29+
* @param reporter the reporter to be used
30+
*/
31+
AbstractOutputBuffer(Reporter reporter) {
32+
33+
assert reporter.isInit() : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters";
34+
35+
this.reporter = reporter;
36+
}
37+
38+
/**
39+
* Returns the reporter used by this buffer.
40+
* @return the reporter instance
41+
*/
42+
public Reporter getReporter() {
43+
return reporter;
44+
}
45+
46+
/**
47+
* Print the lines as soon as they are produced and write to a PrintStream.
48+
* @param conn DB connection
49+
* @param ps the PrintStream to be used, e.g: System.out
50+
* @throws SQLException any sql errors
51+
*/
52+
public void printAvailable(Connection conn, PrintStream ps) throws SQLException {
53+
List<PrintStream> printStreams = new ArrayList<>(1);
54+
printStreams.add(ps);
55+
printAvailable(conn, printStreams);
56+
}
57+
58+
/**
59+
* Print the lines as soon as they are produced and write to a list of PrintStreams.
60+
* @param conn DB connection
61+
* @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream)
62+
* @throws SQLException any sql errors
63+
*/
64+
public void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException {
65+
fetchAvailable(conn, s -> {
66+
for (PrintStream ps : printStreams)
67+
ps.println(s);
68+
});
69+
}
70+
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.utplsql.api.outputBuffer;
2+
3+
import oracle.jdbc.OracleCallableStatement;
4+
import oracle.jdbc.OracleConnection;
5+
import oracle.jdbc.OraclePreparedStatement;
6+
import oracle.jdbc.OracleTypes;
7+
import org.utplsql.api.reporter.Reporter;
8+
9+
import java.io.PrintStream;
10+
import java.sql.Connection;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.function.Consumer;
16+
17+
/** Compatibility Output-Buffer for 3.0.0 - 3.0.4
18+
*
19+
* @author pesse
20+
*/
21+
class CompatibilityOutputBufferPre310 extends AbstractOutputBuffer {
22+
23+
CompatibilityOutputBufferPre310( Reporter reporter ) {
24+
super(reporter);
25+
}
26+
27+
28+
/**
29+
* Print the lines as soon as they are produced and call the callback passing the new line.
30+
* @param conn DB connection
31+
* @param onLineFetched the callback to be called
32+
* @throws SQLException any sql errors
33+
*/
34+
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {
35+
36+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
37+
38+
try (OraclePreparedStatement pstmt = (OraclePreparedStatement)oraConn.prepareStatement("select * from table(ut_output_buffer.get_lines(?))")) {
39+
40+
pstmt.setORAData(1, getReporter());
41+
try (ResultSet resultSet = pstmt.executeQuery() ) {
42+
while (resultSet.next())
43+
onLineFetched.accept(resultSet.getString(1));
44+
}
45+
}
46+
}
47+
48+
/**
49+
* Get all lines from output buffer and return it as a list of strings.
50+
* @param conn DB connection
51+
* @return the lines
52+
* @throws SQLException any sql errors
53+
*/
54+
public List<String> fetchAll(Connection conn) throws SQLException {
55+
56+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
57+
58+
try (OracleCallableStatement cstmt = (OracleCallableStatement)oraConn.prepareCall("{? = call ?.get_lines_cursor() }")) {
59+
60+
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
61+
cstmt.setORAData(2, getReporter());
62+
63+
cstmt.execute();
64+
65+
try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {
66+
67+
List<String> outputLines = new ArrayList<>();
68+
while (resultSet.next()) {
69+
outputLines.add(resultSet.getString("text"));
70+
}
71+
return outputLines;
72+
}
73+
}
74+
}
75+
}

src/main/java/org/utplsql/api/outputBuffer/DefaultOutputBuffer.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,14 @@
1919
* @author vinicius
2020
* @author pesse
2121
*/
22-
public class DefaultOutputBuffer implements OutputBuffer {
23-
24-
private Reporter reporter;
22+
class DefaultOutputBuffer extends AbstractOutputBuffer {
2523

2624
/**
2725
* Creates a new DefaultOutputBuffer.
2826
* @param reporter the reporter to be used
2927
*/
30-
public DefaultOutputBuffer(Reporter reporter) {
31-
32-
assert reporter.isInit() : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters";
33-
assert reporter.hasOutput() : "Reporter has no output. Please use NonOutputBuffer instead";
34-
35-
this.reporter = reporter;
36-
}
37-
38-
/**
39-
* Returns the reporter used by this buffer.
40-
* @return the reporter instance
41-
*/
42-
public Reporter getReporter() {
43-
return reporter;
44-
}
45-
46-
/**
47-
* Print the lines as soon as they are produced and write to a PrintStream.
48-
* @param conn DB connection
49-
* @param ps the PrintStream to be used, e.g: System.out
50-
* @throws SQLException any sql errors
51-
*/
52-
public void printAvailable(Connection conn, PrintStream ps) throws SQLException {
53-
List<PrintStream> printStreams = new ArrayList<>(1);
54-
printStreams.add(ps);
55-
printAvailable(conn, printStreams);
56-
}
57-
58-
/**
59-
* Print the lines as soon as they are produced and write to a list of PrintStreams.
60-
* @param conn DB connection
61-
* @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream)
62-
* @throws SQLException any sql errors
63-
*/
64-
public void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException {
65-
fetchAvailable(conn, s -> {
66-
for (PrintStream ps : printStreams)
67-
ps.println(s);
68-
});
28+
DefaultOutputBuffer(Reporter reporter) {
29+
super(reporter);
6930
}
7031

7132
/**

src/main/java/org/utplsql/api/outputBuffer/NonOutputBuffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*
1414
* @author pesse
1515
*/
16-
public class NonOutputBuffer implements OutputBuffer {
16+
class NonOutputBuffer implements OutputBuffer {
1717

1818
private Reporter reporter;
1919

20-
public NonOutputBuffer( Reporter reporter) {
20+
NonOutputBuffer( Reporter reporter) {
2121
this.reporter = reporter;
2222
}
2323

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.utplsql.api.outputBuffer;
2+
3+
import oracle.jdbc.OracleConnection;
4+
import org.utplsql.api.Version;
5+
import org.utplsql.api.exception.InvalidVersionException;
6+
import org.utplsql.api.reporter.Reporter;
7+
8+
import java.sql.Connection;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
13+
public class OutputBufferProvider {
14+
15+
/** Returns an OutputBuffer compatible with the given databaseVersion
16+
* If we are at 3.1.0 or greater, returns an OutputBuffer based upon the information whether the Reporter has Output or not
17+
*
18+
* @param databaseVersion
19+
* @param reporter
20+
* @param conn
21+
* @return
22+
* @throws SQLException
23+
*/
24+
public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Reporter reporter, Connection conn ) throws SQLException {
25+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
26+
27+
try {
28+
if (new Version("3.0.4.1610").isGreaterOrEqualThan(databaseVersion)) {
29+
if ( hasOutput(reporter, oraConn) ) {
30+
return new DefaultOutputBuffer(reporter);
31+
}
32+
else {
33+
return new NonOutputBuffer(reporter);
34+
}
35+
}
36+
}
37+
catch ( InvalidVersionException e ) { }
38+
39+
// If we couldn't find an appropriate OutputBuffer, return the Pre310-Compatibility-Buffer
40+
return new CompatibilityOutputBufferPre310(reporter);
41+
}
42+
43+
private static boolean hasOutput( Reporter reporter, OracleConnection oraConn ) throws SQLException {
44+
45+
try ( PreparedStatement stmt = oraConn.prepareStatement("select ut_runner.is_output_reporter(?) from dual")) {
46+
stmt.setString(1, reporter.getTypeName());
47+
48+
try ( ResultSet rs = stmt.executeQuery() ) {
49+
if ( rs.next() ) {
50+
String isReporterResult = rs.getString(1);
51+
52+
if ( isReporterResult == null )
53+
throw new IllegalArgumentException("The given type " + reporter.getTypeName() + " is not a valid Reporter!");
54+
else if (isReporterResult.equalsIgnoreCase("Y") )
55+
return true;
56+
else
57+
return false;
58+
}
59+
else
60+
throw new SQLException("Could not check Reporter validity");
61+
}
62+
}
63+
}
64+
}

src/main/java/org/utplsql/api/reporter/DefaultReporters.java renamed to src/main/java/org/utplsql/api/reporter/CoreReporters.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
*
77
* @author pesse
88
*/
9-
public enum DefaultReporters {
9+
public enum CoreReporters {
1010

1111
UT_COVERAGE_HTML_REPORTER(CoverageHTMLReporter::new, "Generates a HTML coverage report with summary and line by line information on code coverage.\n" +
1212
"Based on open-source simplecov-html coverage reporter for Ruby.\n" +
1313
"Includes source code in the report."),
1414
UT_DOCUMENTATION_REPORTER(DocumentationReporter::new, "A textual pretty-print of unit test results (usually use for console output)"),
15-
UT_TEAMCITY_REPORTER(ReporterFactory.getDefaultReporterFactoryMethod(), "For reporting live progress of test execution with Teamcity CI."),
16-
UT_XUNIT_REPORTER(ReporterFactory.getDefaultReporterFactoryMethod(), "Used for reporting test results with CI servers like Jenkins/Hudson/Teamcity."),
17-
UT_COVERALLS_REPORTER(ReporterFactory.getDefaultReporterFactoryMethod(), "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
15+
UT_TEAMCITY_REPORTER(DefaultReporter::new, "For reporting live progress of test execution with Teamcity CI."),
16+
UT_XUNIT_REPORTER(DefaultReporter::new, "Used for reporting test results with CI servers like Jenkins/Hudson/Teamcity."),
17+
UT_COVERALLS_REPORTER(DefaultReporter::new, "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
1818
"Designed for [Coveralls](https://coveralls.io/)."),
19-
UT_COVERAGE_SONAR_REPORTER(ReporterFactory.getDefaultReporterFactoryMethod(), "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
19+
UT_COVERAGE_SONAR_REPORTER(DefaultReporter::new, "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
2020
"Designed for [SonarQube](https://about.sonarqube.com/) to report coverage."),
21-
UT_SONAR_TEST_REPORTER(ReporterFactory.getDefaultReporterFactoryMethod(), "Generates a JSON report providing detailed information on test execution.\n" +
21+
UT_SONAR_TEST_REPORTER(DefaultReporter::new, "Generates a JSON report providing detailed information on test execution.\n" +
2222
"Designed for [SonarQube](https://about.sonarqube.com/) to report test execution.");
2323

2424
private BiFunction<String, Object[], ? extends Reporter> factoryMethod;
2525
private String description;
2626

27-
DefaultReporters(BiFunction<String, Object[], ? extends Reporter> factoryMethod, String description ) {
27+
CoreReporters(BiFunction<String, Object[], ? extends Reporter> factoryMethod, String description ) {
2828
this.factoryMethod = factoryMethod;
2929
this.description = description;
3030
}

src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.List;
1212
import java.util.function.Consumer;
1313

14-
public class CoverageHTMLReporter extends Reporter {
14+
public class CoverageHTMLReporter extends DefaultReporter {
1515

1616
// Could override Reporter.init and call ut_coverage_report_html_helper.get_default_html_assets_path from database,
1717
// but had permissions issues.
@@ -21,7 +21,7 @@ public class CoverageHTMLReporter extends Reporter {
2121
private String assetsPath;
2222

2323
public CoverageHTMLReporter() {
24-
super(DefaultReporters.UT_COVERAGE_HTML_REPORTER.name(), null);
24+
super(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), null);
2525
}
2626

2727
public CoverageHTMLReporter(String selfType, Object[] attributes) {

0 commit comments

Comments
 (0)