Skip to content

Commit 3b98edf

Browse files
committed
Initial work on OutputBuffer abstraction
1 parent 08ffc6a commit 3b98edf

13 files changed

+203
-127
lines changed

src/main/java/org/utplsql/api/CustomTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public final class CustomTypes {
1818
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
1919
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
2020

21+
public static final String UT_OUTPUT_BUFFER_BASE = "UT_OUTPUT_BUFFER_BASE";
22+
public static final String UT_OUTPUT_TABLE_BUFFER = "UT_OUTPUT_TABLE_BUFFER";
23+
2124
public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
2225
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";
2326

src/main/java/org/utplsql/api/OutputBuffer.java

Lines changed: 19 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,41 @@
11
package org.utplsql.api;
22

3-
import org.utplsql.api.reporter.Reporter;
4-
import oracle.jdbc.OracleTypes;
5-
63
import java.io.PrintStream;
74
import java.sql.*;
8-
import java.util.ArrayList;
95
import java.util.List;
106

11-
/**
12-
* Fetches the lines produced by a reporter.
13-
*/
14-
public class OutputBuffer {
7+
public abstract class OutputBuffer implements SQLData {
158

16-
private Reporter reporter;
9+
private String outputId;
1710

18-
/**
19-
* Creates a new OutputBuffer.
20-
* @param reporter the reporter to be used
21-
*/
22-
public OutputBuffer(Reporter reporter) {
23-
this.reporter = reporter;
11+
public OutputBuffer(String outputId) {
12+
this.outputId = outputId;
2413
}
2514

26-
/**
27-
* Returns the reporter used by this buffer.
28-
* @return the reporter instance
29-
*/
30-
public Reporter getReporter() {
31-
return reporter;
15+
public String getOutputId() {
16+
return outputId;
3217
}
3318

34-
/**
35-
* Print the lines as soon as they are produced and write to a PrintStream.
36-
* @param conn DB connection
37-
* @param ps the PrintStream to be used, e.g: System.out
38-
* @throws SQLException any sql errors
39-
*/
40-
public void printAvailable(Connection conn, PrintStream ps) throws SQLException {
41-
List<PrintStream> printStreams = new ArrayList<>(1);
42-
printStreams.add(ps);
43-
printAvailable(conn, printStreams);
19+
private void setOutputId(String outputId) {
20+
this.outputId = outputId;
4421
}
4522

46-
/**
47-
* Print the lines as soon as they are produced and write to a list of PrintStreams.
48-
* @param conn DB connection
49-
* @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream)
50-
* @throws SQLException any sql errors
51-
*/
52-
public void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException {
53-
fetchAvailable(conn, s -> {
54-
for (PrintStream ps : printStreams)
55-
ps.println(s);
56-
});
57-
}
23+
public abstract void printAvailable(Connection conn, PrintStream ps) throws SQLException;
5824

59-
/**
60-
* Print the lines as soon as they are produced and call the callback passing the new line.
61-
* @param conn DB connection
62-
* @param cb the callback to be called
63-
* @throws SQLException any sql errors
64-
*/
65-
public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
66-
PreparedStatement preparedStatement = null;
67-
ResultSet resultSet = null;
68-
try {
69-
preparedStatement = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))");
70-
preparedStatement.setString(1, getReporter().getReporterId());
71-
resultSet = preparedStatement.executeQuery();
25+
public abstract void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException;
7226

73-
while (resultSet.next())
74-
cb.onLineFetched(resultSet.getString(1));
75-
} finally {
76-
if (resultSet != null)
77-
resultSet.close();
78-
if (preparedStatement != null)
79-
preparedStatement.close();
80-
}
81-
}
27+
public abstract void fetchAvailable(Connection conn, Callback cb) throws SQLException;
8228

83-
/**
84-
* Get all lines from output buffer and return it as a list of strings.
85-
* @param conn DB connection
86-
* @return the lines
87-
* @throws SQLException any sql errors
88-
*/
89-
public List<String> fetchAll(Connection conn) throws SQLException {
90-
CallableStatement callableStatement = null;
91-
ResultSet resultSet = null;
92-
try {
93-
callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
94-
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
95-
callableStatement.setString(2, getReporter().getReporterId());
96-
callableStatement.execute();
29+
public abstract List<String> fetchAll(Connection conn) throws SQLException;
9730

98-
resultSet = (ResultSet) callableStatement.getObject(1);
31+
@Override
32+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
33+
setOutputId(stream.readString());
34+
}
9935

100-
List<String> outputLines = new ArrayList<>();
101-
while (resultSet.next()) {
102-
outputLines.add(resultSet.getString("text"));
103-
}
104-
return outputLines;
105-
} finally {
106-
if (resultSet != null)
107-
resultSet.close();
108-
if (callableStatement != null)
109-
callableStatement.close();
110-
}
36+
@Override
37+
public void writeSQL(SQLOutput stream) throws SQLException {
38+
stream.writeString(getOutputId());
11139
}
11240

11341
/**
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.utplsql.api;
2+
3+
import oracle.jdbc.OracleTypes;
4+
5+
import java.io.PrintStream;
6+
import java.sql.*;
7+
import java.util.ArrayList;
8+
import java.util.Calendar;
9+
import java.util.List;
10+
11+
public class TableOutputBuffer extends OutputBuffer {
12+
13+
private java.sql.Date startDate;
14+
15+
public TableOutputBuffer(String outputId) {
16+
super(outputId);
17+
setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
18+
}
19+
20+
public Date getStartDate() {
21+
return startDate;
22+
}
23+
24+
private void setStartDate(Date startDate) {
25+
this.startDate = startDate;
26+
}
27+
28+
@Override
29+
public String getSQLTypeName() throws SQLException {
30+
return CustomTypes.UT_OUTPUT_TABLE_BUFFER;
31+
}
32+
33+
@Override
34+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
35+
super.readSQL(stream, typeName);
36+
setStartDate(stream.readDate());
37+
}
38+
39+
@Override
40+
public void writeSQL(SQLOutput stream) throws SQLException {
41+
super.writeSQL(stream);
42+
stream.writeDate(getStartDate());
43+
}
44+
45+
/**
46+
* Print the lines as soon as they are produced and write to a PrintStream.
47+
* @param conn DB connection
48+
* @param ps the PrintStream to be used, e.g: System.out
49+
* @throws SQLException any sql errors
50+
*/
51+
public void printAvailable(Connection conn, PrintStream ps) throws SQLException {
52+
List<PrintStream> printStreams = new ArrayList<>(1);
53+
printStreams.add(ps);
54+
printAvailable(conn, printStreams);
55+
}
56+
57+
/**
58+
* Print the lines as soon as they are produced and write to a list of PrintStreams.
59+
* @param conn DB connection
60+
* @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream)
61+
* @throws SQLException any sql errors
62+
*/
63+
public void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException {
64+
fetchAvailable(conn, s -> {
65+
for (PrintStream ps : printStreams)
66+
ps.println(s);
67+
});
68+
}
69+
70+
/**
71+
* Print the lines as soon as they are produced and call the callback passing the new line.
72+
* @param conn DB connection
73+
* @param cb the callback to be called
74+
* @throws SQLException any sql errors
75+
*/
76+
public void fetchAvailable(Connection conn, OutputBuffer.Callback cb) throws SQLException {
77+
PreparedStatement preparedStatement = null;
78+
ResultSet resultSet = null;
79+
try {
80+
preparedStatement = conn.prepareStatement("SELECT * FROM table(ut_output_table_buffer(?).get_lines())");
81+
preparedStatement.setString(1, getOutputId());
82+
resultSet = preparedStatement.executeQuery();
83+
84+
while (resultSet.next())
85+
cb.onLineFetched(resultSet.getString(1));
86+
} finally {
87+
if (resultSet != null)
88+
resultSet.close();
89+
if (preparedStatement != null)
90+
preparedStatement.close();
91+
}
92+
}
93+
94+
/**
95+
* Get all lines from output buffer and return it as a list of strings.
96+
* @param conn DB connection
97+
* @return the lines
98+
* @throws SQLException any sql errors
99+
*/
100+
public List<String> fetchAll(Connection conn) throws SQLException {
101+
CallableStatement callableStatement = null;
102+
ResultSet resultSet = null;
103+
try {
104+
callableStatement = conn.prepareCall("BEGIN ? := ut_output_table_buffer(?).get_lines_cursor(); END;");
105+
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
106+
callableStatement.setString(2, getOutputId());
107+
callableStatement.execute();
108+
109+
resultSet = (ResultSet) callableStatement.getObject(1);
110+
111+
List<String> outputLines = new ArrayList<>();
112+
while (resultSet.next()) {
113+
outputLines.add(resultSet.getString("text"));
114+
}
115+
return outputLines;
116+
} finally {
117+
if (resultSet != null)
118+
resultSet.close();
119+
if (callableStatement != null)
120+
callableStatement.close();
121+
}
122+
}
123+
124+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.sql.SQLInput;
77
import java.sql.SQLOutput;
88

9-
public class CoverageHTMLReporter extends Reporter {
9+
public class CoverageHTMLReporter extends OutputReporter {
1010

1111
// Could override Reporter.init and call ut_coverage_report_html_helper.get_default_html_assets_path from database,
1212
// but had permissions issues.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import java.sql.SQLException;
66

7-
public class CoverageSonarReporter extends Reporter {
7+
public class CoverageSonarReporter extends OutputReporter {
88

99
@Override
1010
public String getSQLTypeName() throws SQLException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import java.sql.SQLException;
66

7-
public class CoverallsReporter extends Reporter {
7+
public class CoverallsReporter extends OutputReporter {
88

99
@Override
1010
public String getSQLTypeName() throws SQLException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.sql.SQLInput;
77
import java.sql.SQLOutput;
88

9-
public class DocumentationReporter extends Reporter {
9+
public class DocumentationReporter extends OutputReporter {
1010

1111
private int lvl;
1212
private int failed;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.utplsql.api.reporter;
2+
3+
import org.utplsql.api.CustomTypes;
4+
import org.utplsql.api.DBHelper;
5+
import org.utplsql.api.OutputBuffer;
6+
import org.utplsql.api.TableOutputBuffer;
7+
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
11+
public class OutputReporter extends Reporter {
12+
13+
private OutputBuffer outputBuffer;
14+
15+
public OutputReporter init(Connection conn) throws SQLException {
16+
return init(conn, new TableOutputBuffer(DBHelper.newSysGuid(conn)));
17+
}
18+
19+
public OutputReporter init(Connection conn, OutputBuffer outputBuffer) throws SQLException {
20+
super.init(conn);
21+
setOutputBuffer(outputBuffer);
22+
return this;
23+
}
24+
25+
public OutputBuffer getOutputBuffer() {
26+
return outputBuffer;
27+
}
28+
29+
public void setOutputBuffer(OutputBuffer outputBuffer) {
30+
this.outputBuffer = outputBuffer;
31+
}
32+
33+
@Override
34+
public String getSQLTypeName() throws SQLException {
35+
return CustomTypes.UT_OUTPUT_TABLE_BUFFER;
36+
}
37+
38+
}
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.DBHelper;
4-
53
import java.sql.*;
6-
import java.util.Calendar;
74

85
/**
96
* Created by Vinicius on 13/04/2017.
@@ -12,14 +9,12 @@ public abstract class Reporter implements SQLData {
129

1310
private String selfType;
1411
private String reporterId;
15-
private java.sql.Date startDate;
1612

1713
public Reporter() {}
1814

1915
public Reporter init(Connection conn) throws SQLException {
2016
setSelfType(getSQLTypeName());
21-
setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
22-
setReporterId(DBHelper.newSysGuid(conn));
17+
setReporterId(null);
2318
return this;
2419
}
2520

@@ -39,26 +34,16 @@ private void setReporterId(String reporterId) {
3934
this.reporterId = reporterId;
4035
}
4136

42-
public java.sql.Date getStartDate() {
43-
return this.startDate;
44-
}
45-
46-
private void setStartDate(java.sql.Date startDate) {
47-
this.startDate = startDate;
48-
}
49-
5037
@Override
5138
public void readSQL(SQLInput stream, String typeName) throws SQLException {
5239
setSelfType(stream.readString());
5340
setReporterId(stream.readString());
54-
setStartDate(stream.readDate());
5541
}
5642

5743
@Override
5844
public void writeSQL(SQLOutput stream) throws SQLException {
5945
stream.writeString(getSelfType());
6046
stream.writeString(getReporterId());
61-
stream.writeDate(getStartDate());
6247
}
6348

6449
}

0 commit comments

Comments
 (0)