diff --git a/src/main/java/org/utplsql/api/CustomTypes.java b/src/main/java/org/utplsql/api/CustomTypes.java index 45dda22..bc6bb68 100644 --- a/src/main/java/org/utplsql/api/CustomTypes.java +++ b/src/main/java/org/utplsql/api/CustomTypes.java @@ -18,6 +18,9 @@ public final class CustomTypes { public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER"; public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER"; + public static final String UT_OUTPUT_BUFFER_BASE = "UT_OUTPUT_BUFFER_BASE"; + public static final String UT_OUTPUT_TABLE_BUFFER = "UT_OUTPUT_TABLE_BUFFER"; + public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING"; public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS"; diff --git a/src/main/java/org/utplsql/api/OutputBuffer.java b/src/main/java/org/utplsql/api/OutputBuffer.java index 6913bf6..62995a5 100644 --- a/src/main/java/org/utplsql/api/OutputBuffer.java +++ b/src/main/java/org/utplsql/api/OutputBuffer.java @@ -1,113 +1,41 @@ package org.utplsql.api; -import org.utplsql.api.reporter.Reporter; -import oracle.jdbc.OracleTypes; - import java.io.PrintStream; import java.sql.*; -import java.util.ArrayList; import java.util.List; -/** - * Fetches the lines produced by a reporter. - */ -public class OutputBuffer { +public abstract class OutputBuffer implements SQLData { - private Reporter reporter; + private String outputId; - /** - * Creates a new OutputBuffer. - * @param reporter the reporter to be used - */ - public OutputBuffer(Reporter reporter) { - this.reporter = reporter; + public OutputBuffer(String outputId) { + this.outputId = outputId; } - /** - * Returns the reporter used by this buffer. - * @return the reporter instance - */ - public Reporter getReporter() { - return reporter; + public String getOutputId() { + return outputId; } - /** - * Print the lines as soon as they are produced and write to a PrintStream. - * @param conn DB connection - * @param ps the PrintStream to be used, e.g: System.out - * @throws SQLException any sql errors - */ - public void printAvailable(Connection conn, PrintStream ps) throws SQLException { - List printStreams = new ArrayList<>(1); - printStreams.add(ps); - printAvailable(conn, printStreams); + private void setOutputId(String outputId) { + this.outputId = outputId; } - /** - * Print the lines as soon as they are produced and write to a list of PrintStreams. - * @param conn DB connection - * @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream) - * @throws SQLException any sql errors - */ - public void printAvailable(Connection conn, List printStreams) throws SQLException { - fetchAvailable(conn, s -> { - for (PrintStream ps : printStreams) - ps.println(s); - }); - } + public abstract void printAvailable(Connection conn, PrintStream ps) throws SQLException; - /** - * Print the lines as soon as they are produced and call the callback passing the new line. - * @param conn DB connection - * @param cb the callback to be called - * @throws SQLException any sql errors - */ - public void fetchAvailable(Connection conn, Callback cb) throws SQLException { - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - try { - preparedStatement = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))"); - preparedStatement.setString(1, getReporter().getReporterId()); - resultSet = preparedStatement.executeQuery(); + public abstract void printAvailable(Connection conn, List printStreams) throws SQLException; - while (resultSet.next()) - cb.onLineFetched(resultSet.getString(1)); - } finally { - if (resultSet != null) - resultSet.close(); - if (preparedStatement != null) - preparedStatement.close(); - } - } + public abstract void fetchAvailable(Connection conn, Callback cb) throws SQLException; - /** - * Get all lines from output buffer and return it as a list of strings. - * @param conn DB connection - * @return the lines - * @throws SQLException any sql errors - */ - public List fetchAll(Connection conn) throws SQLException { - CallableStatement callableStatement = null; - ResultSet resultSet = null; - try { - callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;"); - callableStatement.registerOutParameter(1, OracleTypes.CURSOR); - callableStatement.setString(2, getReporter().getReporterId()); - callableStatement.execute(); + public abstract List fetchAll(Connection conn) throws SQLException; - resultSet = (ResultSet) callableStatement.getObject(1); + @Override + public void readSQL(SQLInput stream, String typeName) throws SQLException { + setOutputId(stream.readString()); + } - List outputLines = new ArrayList<>(); - while (resultSet.next()) { - outputLines.add(resultSet.getString("text")); - } - return outputLines; - } finally { - if (resultSet != null) - resultSet.close(); - if (callableStatement != null) - callableStatement.close(); - } + @Override + public void writeSQL(SQLOutput stream) throws SQLException { + stream.writeString(getOutputId()); } /** diff --git a/src/main/java/org/utplsql/api/TableOutputBuffer.java b/src/main/java/org/utplsql/api/TableOutputBuffer.java new file mode 100644 index 0000000..6f4bea0 --- /dev/null +++ b/src/main/java/org/utplsql/api/TableOutputBuffer.java @@ -0,0 +1,124 @@ +package org.utplsql.api; + +import oracle.jdbc.OracleTypes; + +import java.io.PrintStream; +import java.sql.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +public class TableOutputBuffer extends OutputBuffer { + + private java.sql.Date startDate; + + public TableOutputBuffer(String outputId) { + super(outputId); + setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis())); + } + + public Date getStartDate() { + return startDate; + } + + private void setStartDate(Date startDate) { + this.startDate = startDate; + } + + @Override + public String getSQLTypeName() throws SQLException { + return CustomTypes.UT_OUTPUT_TABLE_BUFFER; + } + + @Override + public void readSQL(SQLInput stream, String typeName) throws SQLException { + super.readSQL(stream, typeName); + setStartDate(stream.readDate()); + } + + @Override + public void writeSQL(SQLOutput stream) throws SQLException { + super.writeSQL(stream); + stream.writeDate(getStartDate()); + } + + /** + * Print the lines as soon as they are produced and write to a PrintStream. + * @param conn DB connection + * @param ps the PrintStream to be used, e.g: System.out + * @throws SQLException any sql errors + */ + public void printAvailable(Connection conn, PrintStream ps) throws SQLException { + List printStreams = new ArrayList<>(1); + printStreams.add(ps); + printAvailable(conn, printStreams); + } + + /** + * Print the lines as soon as they are produced and write to a list of PrintStreams. + * @param conn DB connection + * @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream) + * @throws SQLException any sql errors + */ + public void printAvailable(Connection conn, List printStreams) throws SQLException { + fetchAvailable(conn, s -> { + for (PrintStream ps : printStreams) + ps.println(s); + }); + } + + /** + * Print the lines as soon as they are produced and call the callback passing the new line. + * @param conn DB connection + * @param cb the callback to be called + * @throws SQLException any sql errors + */ + public void fetchAvailable(Connection conn, OutputBuffer.Callback cb) throws SQLException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + try { + preparedStatement = conn.prepareStatement("SELECT * FROM table(ut_output_table_buffer(?).get_lines())"); + preparedStatement.setString(1, getOutputId()); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) + cb.onLineFetched(resultSet.getString(1)); + } finally { + if (resultSet != null) + resultSet.close(); + if (preparedStatement != null) + preparedStatement.close(); + } + } + + /** + * Get all lines from output buffer and return it as a list of strings. + * @param conn DB connection + * @return the lines + * @throws SQLException any sql errors + */ + public List fetchAll(Connection conn) throws SQLException { + CallableStatement callableStatement = null; + ResultSet resultSet = null; + try { + callableStatement = conn.prepareCall("BEGIN ? := ut_output_table_buffer(?).get_lines_cursor(); END;"); + callableStatement.registerOutParameter(1, OracleTypes.CURSOR); + callableStatement.setString(2, getOutputId()); + callableStatement.execute(); + + resultSet = (ResultSet) callableStatement.getObject(1); + + List outputLines = new ArrayList<>(); + while (resultSet.next()) { + outputLines.add(resultSet.getString("text")); + } + return outputLines; + } finally { + if (resultSet != null) + resultSet.close(); + if (callableStatement != null) + callableStatement.close(); + } + } + +} diff --git a/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java b/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java index 790112c..1f3d54d 100644 --- a/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java +++ b/src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java @@ -6,7 +6,7 @@ import java.sql.SQLInput; import java.sql.SQLOutput; -public class CoverageHTMLReporter extends Reporter { +public class CoverageHTMLReporter extends OutputReporter { // Could override Reporter.init and call ut_coverage_report_html_helper.get_default_html_assets_path from database, // but had permissions issues. diff --git a/src/main/java/org/utplsql/api/reporter/CoverageSonarReporter.java b/src/main/java/org/utplsql/api/reporter/CoverageSonarReporter.java index cdd9546..2298e60 100644 --- a/src/main/java/org/utplsql/api/reporter/CoverageSonarReporter.java +++ b/src/main/java/org/utplsql/api/reporter/CoverageSonarReporter.java @@ -4,7 +4,7 @@ import java.sql.SQLException; -public class CoverageSonarReporter extends Reporter { +public class CoverageSonarReporter extends OutputReporter { @Override public String getSQLTypeName() throws SQLException { diff --git a/src/main/java/org/utplsql/api/reporter/CoverallsReporter.java b/src/main/java/org/utplsql/api/reporter/CoverallsReporter.java index 96dfceb..3f93c2b 100644 --- a/src/main/java/org/utplsql/api/reporter/CoverallsReporter.java +++ b/src/main/java/org/utplsql/api/reporter/CoverallsReporter.java @@ -4,7 +4,7 @@ import java.sql.SQLException; -public class CoverallsReporter extends Reporter { +public class CoverallsReporter extends OutputReporter { @Override public String getSQLTypeName() throws SQLException { diff --git a/src/main/java/org/utplsql/api/reporter/DocumentationReporter.java b/src/main/java/org/utplsql/api/reporter/DocumentationReporter.java index e3e88a4..0a77758 100644 --- a/src/main/java/org/utplsql/api/reporter/DocumentationReporter.java +++ b/src/main/java/org/utplsql/api/reporter/DocumentationReporter.java @@ -6,7 +6,7 @@ import java.sql.SQLInput; import java.sql.SQLOutput; -public class DocumentationReporter extends Reporter { +public class DocumentationReporter extends OutputReporter { private int lvl; private int failed; diff --git a/src/main/java/org/utplsql/api/reporter/OutputReporter.java b/src/main/java/org/utplsql/api/reporter/OutputReporter.java new file mode 100644 index 0000000..6665f6f --- /dev/null +++ b/src/main/java/org/utplsql/api/reporter/OutputReporter.java @@ -0,0 +1,38 @@ +package org.utplsql.api.reporter; + +import org.utplsql.api.CustomTypes; +import org.utplsql.api.DBHelper; +import org.utplsql.api.OutputBuffer; +import org.utplsql.api.TableOutputBuffer; + +import java.sql.Connection; +import java.sql.SQLException; + +public class OutputReporter extends Reporter { + + private OutputBuffer outputBuffer; + + public OutputReporter init(Connection conn) throws SQLException { + return init(conn, new TableOutputBuffer(DBHelper.newSysGuid(conn))); + } + + public OutputReporter init(Connection conn, OutputBuffer outputBuffer) throws SQLException { + super.init(conn); + setOutputBuffer(outputBuffer); + return this; + } + + public OutputBuffer getOutputBuffer() { + return outputBuffer; + } + + public void setOutputBuffer(OutputBuffer outputBuffer) { + this.outputBuffer = outputBuffer; + } + + @Override + public String getSQLTypeName() throws SQLException { + return CustomTypes.UT_OUTPUT_TABLE_BUFFER; + } + +} diff --git a/src/main/java/org/utplsql/api/reporter/Reporter.java b/src/main/java/org/utplsql/api/reporter/Reporter.java index 83d201f..643796a 100644 --- a/src/main/java/org/utplsql/api/reporter/Reporter.java +++ b/src/main/java/org/utplsql/api/reporter/Reporter.java @@ -1,9 +1,6 @@ package org.utplsql.api.reporter; -import org.utplsql.api.DBHelper; - import java.sql.*; -import java.util.Calendar; /** * Created by Vinicius on 13/04/2017. @@ -12,14 +9,12 @@ public abstract class Reporter implements SQLData { private String selfType; private String reporterId; - private java.sql.Date startDate; public Reporter() {} public Reporter init(Connection conn) throws SQLException { setSelfType(getSQLTypeName()); - setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis())); - setReporterId(DBHelper.newSysGuid(conn)); + setReporterId(null); return this; } @@ -39,26 +34,16 @@ private void setReporterId(String reporterId) { this.reporterId = reporterId; } - public java.sql.Date getStartDate() { - return this.startDate; - } - - private void setStartDate(java.sql.Date startDate) { - this.startDate = startDate; - } - @Override public void readSQL(SQLInput stream, String typeName) throws SQLException { setSelfType(stream.readString()); setReporterId(stream.readString()); - setStartDate(stream.readDate()); } @Override public void writeSQL(SQLOutput stream) throws SQLException { stream.writeString(getSelfType()); stream.writeString(getReporterId()); - stream.writeDate(getStartDate()); } } diff --git a/src/main/java/org/utplsql/api/reporter/SonarTestReporter.java b/src/main/java/org/utplsql/api/reporter/SonarTestReporter.java index 66be22e..3bad03f 100644 --- a/src/main/java/org/utplsql/api/reporter/SonarTestReporter.java +++ b/src/main/java/org/utplsql/api/reporter/SonarTestReporter.java @@ -4,7 +4,7 @@ import java.sql.SQLException; -public class SonarTestReporter extends Reporter { +public class SonarTestReporter extends OutputReporter { @Override public String getSQLTypeName() throws SQLException { diff --git a/src/main/java/org/utplsql/api/reporter/TeamCityReporter.java b/src/main/java/org/utplsql/api/reporter/TeamCityReporter.java index 9c4499b..2ee797d 100644 --- a/src/main/java/org/utplsql/api/reporter/TeamCityReporter.java +++ b/src/main/java/org/utplsql/api/reporter/TeamCityReporter.java @@ -4,7 +4,7 @@ import java.sql.SQLException; -public class TeamCityReporter extends Reporter { +public class TeamCityReporter extends OutputReporter { @Override public String getSQLTypeName() throws SQLException { diff --git a/src/main/java/org/utplsql/api/reporter/XUnitReporter.java b/src/main/java/org/utplsql/api/reporter/XUnitReporter.java index cf1a9e1..4ccc560 100644 --- a/src/main/java/org/utplsql/api/reporter/XUnitReporter.java +++ b/src/main/java/org/utplsql/api/reporter/XUnitReporter.java @@ -4,7 +4,7 @@ import java.sql.SQLException; -public class XUnitReporter extends Reporter { +public class XUnitReporter extends OutputReporter { @Override public String getSQLTypeName() throws SQLException { diff --git a/src/test/java/org/utplsql/api/OutputBufferTest.java b/src/test/java/org/utplsql/api/OutputBufferTest.java index db043cb..ad9e49f 100644 --- a/src/test/java/org/utplsql/api/OutputBufferTest.java +++ b/src/test/java/org/utplsql/api/OutputBufferTest.java @@ -1,13 +1,11 @@ package org.utplsql.api; -import org.utplsql.api.reporter.DocumentationReporter; -import org.utplsql.api.reporter.Reporter; -import org.utplsql.api.rules.DatabaseRule; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; +import org.utplsql.api.reporter.DocumentationReporter; +import org.utplsql.api.reporter.OutputReporter; +import org.utplsql.api.rules.DatabaseRule; -import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; import java.sql.Connection; @@ -24,10 +22,10 @@ public class OutputBufferTest { @Rule public final DatabaseRule db = new DatabaseRule(); - public Reporter createReporter() throws SQLException { + public OutputReporter createReporter() throws SQLException { Connection conn = db.newConnection(); - Reporter reporter = new DocumentationReporter().init(conn); - System.out.println("Reporter ID: " + reporter.getReporterId()); + OutputReporter reporter = new DocumentationReporter().init(conn); + System.out.println("Output ID: " + reporter.getOutputBuffer().getOutputId()); return reporter; } @@ -36,7 +34,7 @@ public void printAvailableLines() { ExecutorService executorService = Executors.newFixedThreadPool(2); try { - final Reporter reporter = createReporter(); + final OutputReporter reporter = createReporter(); Future task1 = executorService.submit(() -> { try { @@ -63,8 +61,8 @@ public void printAvailableLines() { printStreams.add(System.out); printStreams.add(new PrintStream(fileOutStream)); - new OutputBuffer(reporter) - .printAvailable(conn, printStreams); + reporter.getOutputBuffer() + .printAvailable(conn, printStreams); return Boolean.TRUE; } catch (SQLException e) { @@ -98,14 +96,14 @@ public void printAvailableLines() { @Test public void fetchAllLines() { try { - final Reporter reporter = createReporter(); + final OutputReporter reporter = createReporter(); Connection conn = db.newConnection(); new TestRunner() .addPath(db.getUser()) .addReporter(reporter) .run(conn); - List outputLines = new OutputBuffer(reporter) + List outputLines = reporter.getOutputBuffer() .fetchAll(conn); Assert.assertTrue(outputLines.size() > 0);