Skip to content

Commit 0a13dd1

Browse files
authored
Merge pull request #45 from utPLSQL/feature/refactor_reporter_api
Feature/refactor reporter api
2 parents f995384 + 9a70d24 commit 0a13dd1

File tree

63 files changed

+1158
-2853
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1158
-2853
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ env:
2727
- UTPLSQL_VERSION="v3.0.2"
2828
- UTPLSQL_VERSION="v3.0.3"
2929
- UTPLSQL_VERSION="v3.0.4"
30+
- UTPLSQL_VERSION="develop"
31+
UTPLSQL_FILE="utPLSQL"
3032

3133
cache:
3234
directories:

.travis/install_utplsql.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ set -ev
33
cd $(dirname $(readlink -f $0))
44

55
# Download the specified version of utPLSQL.
6-
curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"
6+
if [ "$UTPLSQL_VERSION" == "develop" ]
7+
then
8+
git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git
9+
tar -czf $UTPLSQL_FILE.tar.gz $UTPLSQL_FILE && rm -rf $UTPLSQL_FILE
10+
else
11+
curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"
12+
fi
713

814
# Download develop branch of utPLSQL.
915
#UTPLSQL_VERSION="develop"

pom.xml

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

55
<groupId>org.utplsql</groupId>
66
<artifactId>java-api</artifactId>
7-
<version>3.0.5-SNAPSHOT</version>
7+
<version>3.1.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>utPLSQL-java-api</name>
@@ -128,6 +128,18 @@
128128
</execution>
129129
</executions>
130130
</plugin>
131+
<plugin>
132+
<artifactId>maven-resources-plugin</artifactId>
133+
<version>2.6</version>
134+
<executions>
135+
<execution>
136+
<phase>process-resources</phase>
137+
<goals>
138+
<goal>resources</goal>
139+
</goals>
140+
</execution>
141+
</executions>
142+
</plugin>
131143
<plugin>
132144
<groupId>org.apache.maven.plugins</groupId>
133145
<artifactId>maven-surefire-plugin</artifactId>
@@ -136,6 +148,7 @@
136148
<excludes>
137149
<exclude>**/*IT.java</exclude>
138150
</excludes>
151+
<trimStackTrace>false</trimStackTrace>
139152
</configuration>
140153
<dependencies>
141154
<dependency>

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ public final class CustomTypes {
1010
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
1111

1212
public static final String UT_REPORTERS = "UT_REPORTERS";
13-
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
14-
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
15-
public static final String UT_TEAMCITY_REPORTER = "UT_TEAMCITY_REPORTER";
16-
public static final String UT_XUNIT_REPORTER = "UT_XUNIT_REPORTER";
17-
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
18-
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
19-
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
2013

2114
public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
2215
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.utplsql.api;
22

3-
import com.sun.nio.zipfs.ZipFileSystem;
43
import com.sun.nio.zipfs.ZipPath;
54
import org.utplsql.api.reporter.CoverageHTMLReporter;
65

7-
import java.io.File;
86
import java.io.IOException;
97
import java.net.URI;
108
import java.net.URISyntaxException;

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
88
import org.utplsql.api.reporter.DocumentationReporter;
99
import org.utplsql.api.reporter.Reporter;
10+
import org.utplsql.api.reporter.ReporterFactory;
1011
import org.utplsql.api.testRunner.TestRunnerStatement;
1112

1213
import java.sql.CallableStatement;
1314
import java.sql.Connection;
1415
import java.sql.SQLException;
16+
import java.util.ArrayList;
1517
import java.util.List;
1618

1719
/**
@@ -23,6 +25,9 @@
2325
public class TestRunner {
2426

2527
private TestRunnerOptions options = new TestRunnerOptions();
28+
private CompatibilityProxy compatibilityProxy;
29+
private ReporterFactory reporterFactory;
30+
private List<String> reporterNames = new ArrayList<>();
2631

2732
public TestRunner addPath(String path) {
2833
options.pathList.add(path);
@@ -39,6 +44,14 @@ public TestRunner addReporter(Reporter reporter) {
3944
return this;
4045
}
4146

47+
public TestRunner addReporter( String reporterName ) {
48+
if ( reporterFactory != null )
49+
options.reporterList.add(reporterFactory.createReporter(reporterName));
50+
else
51+
reporterNames.add(reporterName);
52+
return this;
53+
}
54+
4255
public TestRunner colorConsole(boolean colorConsole) {
4356
options.colorConsole = colorConsole;
4457
return this;
@@ -95,9 +108,25 @@ public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
95108
return this;
96109
}
97110

111+
public TestRunner setReporterFactory( ReporterFactory reporterFactory ) {
112+
this.reporterFactory = reporterFactory;
113+
return this;
114+
}
115+
116+
private void delayedAddReporters() {
117+
if ( reporterFactory != null )
118+
reporterNames.stream().forEach( this::addReporter );
119+
else
120+
throw new IllegalStateException("ReporterFactory must be set to add delayed Reporters!");
121+
}
122+
98123
public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException {
99124

100-
CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
125+
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
126+
if ( reporterFactory == null )
127+
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);
128+
129+
delayedAddReporters();
101130

102131
// First of all check version compatibility
103132
compatibilityProxy.failOnNotCompatible();
@@ -129,15 +158,6 @@ else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
129158
throw new UtPLSQLNotInstalledException(e);
130159
}
131160
else {
132-
// If the execution failed by unexpected reasons finishes all reporters,
133-
// this way the users don't need to care about reporters' sessions hanging.
134-
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
135-
136-
try (CallableStatement closeBufferStmt = conn.prepareCall("BEGIN ut_output_buffer.close(?); END;")) {
137-
closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, options.reporterList.toArray()));
138-
closeBufferStmt.execute();
139-
} catch (SQLException ignored) {}
140-
141161
throw e;
142162
}
143163
} finally {
@@ -156,8 +176,8 @@ else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
156176
* @throws SQLException any sql exception
157177
*/
158178
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
159-
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
160-
reporter.init(conn);
179+
if (!reporter.isInit() || reporter.getId() == null || reporter.getId().isEmpty())
180+
reporter.init(conn, compatibilityProxy, reporterFactory);
161181
}
162182

163183
}

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

Lines changed: 16 additions & 2 deletions
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,8 +23,8 @@
2023
*/
2124
public class CompatibilityProxy {
2225

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

2629
private Version databaseVersion;
2730
private boolean compatible = false;
@@ -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
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
public enum OptionalFeatures {
77

88
FAIL_ON_ERROR("3.0.3", null),
9-
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null);
9+
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null),
10+
CUSTOM_REPORTERS("3.1.0", null);
1011

1112
private Version minVersion;
1213
private Version maxVersion;
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
package org.utplsql.api;
1+
package org.utplsql.api.outputBuffer;
22

33
import org.utplsql.api.reporter.Reporter;
4-
import oracle.jdbc.OracleTypes;
54

65
import java.io.PrintStream;
76
import java.sql.*;
87
import java.util.ArrayList;
98
import java.util.List;
9+
import java.util.function.Consumer;
1010

1111
/**
1212
* Fetches the lines produced by a reporter.
13+
*
14+
* @author vinicius
15+
* @author pesse
1316
*/
14-
public class OutputBuffer {
17+
abstract class AbstractOutputBuffer implements OutputBuffer {
1518

1619
private Reporter reporter;
1720

1821
/**
19-
* Creates a new OutputBuffer.
22+
* Creates a new DefaultOutputBuffer.
2023
* @param reporter the reporter to be used
2124
*/
22-
public OutputBuffer(Reporter reporter) {
25+
AbstractOutputBuffer(Reporter reporter) {
26+
27+
assert reporter.isInit() : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters";
28+
2329
this.reporter = reporter;
2430
}
2531

@@ -56,27 +62,24 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
5662
});
5763
}
5864

65+
protected abstract PreparedStatement getLinesStatement( Connection conn ) throws SQLException;
66+
67+
protected abstract CallableStatement getLinesCursorStatement( Connection conn ) throws SQLException;
68+
5969
/**
6070
* Print the lines as soon as they are produced and call the callback passing the new line.
6171
* @param conn DB connection
62-
* @param cb the callback to be called
72+
* @param onLineFetched the callback to be called
6373
* @throws SQLException any sql errors
6474
*/
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();
72-
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();
75+
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {
76+
77+
try (PreparedStatement pstmt = getLinesStatement(conn)) {
78+
79+
try (ResultSet resultSet = pstmt.executeQuery() ) {
80+
while (resultSet.next())
81+
onLineFetched.accept(resultSet.getString(1));
82+
}
8083
}
8184
}
8285

@@ -87,34 +90,22 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
8790
* @throws SQLException any sql errors
8891
*/
8992
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();
97-
98-
resultSet = (ResultSet) callableStatement.getObject(1);
99-
100-
List<String> outputLines = new ArrayList<>();
101-
while (resultSet.next()) {
102-
outputLines.add(resultSet.getString("text"));
93+
94+
try (CallableStatement cstmt = getLinesCursorStatement(conn)) {
95+
96+
cstmt.execute();
97+
98+
try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {
99+
100+
List<String> outputLines = new ArrayList<>();
101+
while (resultSet.next()) {
102+
outputLines.add(resultSet.getString("text"));
103+
}
104+
return outputLines;
103105
}
104-
return outputLines;
105-
} finally {
106-
if (resultSet != null)
107-
resultSet.close();
108-
if (callableStatement != null)
109-
callableStatement.close();
110106
}
111107
}
112108

113-
/**
114-
* Callback to be called when a new line is available from the output buffer.
115-
*/
116-
public interface Callback {
117-
void onLineFetched(String s);
118-
}
109+
119110

120111
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.utplsql.api.outputBuffer;
2+
3+
import oracle.jdbc.OracleTypes;
4+
import org.utplsql.api.reporter.Reporter;
5+
6+
import java.sql.CallableStatement;
7+
import java.sql.Connection;
8+
import java.sql.PreparedStatement;
9+
import java.sql.SQLException;
10+
11+
/** Compatibility Output-Buffer for 3.0.0 - 3.0.4
12+
*
13+
* @author pesse
14+
*/
15+
class CompatibilityOutputBufferPre310 extends AbstractOutputBuffer {
16+
17+
CompatibilityOutputBufferPre310( Reporter reporter ) {
18+
super(reporter);
19+
}
20+
21+
@Override
22+
protected PreparedStatement getLinesStatement(Connection conn) throws SQLException {
23+
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))");
24+
pstmt.setString(1, getReporter().getId());
25+
return pstmt;
26+
}
27+
28+
@Override
29+
protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLException {
30+
CallableStatement cstmt = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
31+
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
32+
cstmt.setString(2, getReporter().getId());
33+
return cstmt;
34+
}
35+
}

0 commit comments

Comments
 (0)