Skip to content

Feature/refactor reporter api #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
19782c3
First attempt to refactor and improve Reporter API
pesse Feb 10, 2018
aa83de6
We want to have description for all our reporters.
pesse Feb 11, 2018
fdb940b
Started to rewrite the whole reporter API
pesse Feb 27, 2018
2593fd7
Refactored Reporter to be more clear
pesse Feb 28, 2018
e3de8de
Refactored Reporter, ReporterFactory and OutputBuffers to be more
pesse Feb 28, 2018
95d3fd7
Keeping compatibility with all utPLSQL 3.x-versions
pesse Mar 1, 2018
1514834
Cleanup of similar OutputBuffers
pesse Mar 1, 2018
facc543
java-api should be compatible with all major core versions
pesse Mar 1, 2018
2cbd11f
ReporterFactory is no longer a singleton
pesse Mar 6, 2018
0183367
Bumped version to 3.1.0
pesse Mar 6, 2018
d255454
We should convert the Reporter-ID correctly I guess...
pesse Mar 7, 2018
6bd7fda
Delete static resources, we want to get those dynamically during build
pesse Mar 7, 2018
fe22a70
New public interface for ReporterFactory and CoreReporters
pesse Mar 7, 2018
41ef81c
Maybe we need this test as IntegrationTest (after package-phase)
pesse Mar 7, 2018
14689ae
Revert "Maybe we need this test as IntegrationTest (after package-pha…
pesse Mar 7, 2018
09701b8
Trying to get more info about the NPE
pesse Mar 7, 2018
7ea7a84
Full error stack from maven
pesse Mar 7, 2018
6c02223
Some tracer bullets... can't reproduce the NPE locally.
pesse Mar 7, 2018
0bddb6d
let's just get that stacktrace
pesse Mar 7, 2018
a328ad9
mavens resource-plugin is skipping all resources if the folder is empty
pesse Mar 7, 2018
c52e259
Semms it has something to do with order of resource-phase
pesse Mar 7, 2018
d7af0fc
Added 3.1.0 to test-matrix
pesse Mar 8, 2018
055b205
Try to get actual develop-branch for test-matrix
pesse Mar 8, 2018
6648276
Naming and shell stuff
pesse Mar 8, 2018
9a70d24
First simple version of ReporterInspector working
pesse Mar 8, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ env:
- UTPLSQL_VERSION="v3.0.2"
- UTPLSQL_VERSION="v3.0.3"
- UTPLSQL_VERSION="v3.0.4"
- UTPLSQL_VERSION="develop"
UTPLSQL_FILE="utPLSQL"

cache:
directories:
Expand Down
8 changes: 7 additions & 1 deletion .travis/install_utplsql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ set -ev
cd $(dirname $(readlink -f $0))

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

# Download develop branch of utPLSQL.
#UTPLSQL_VERSION="develop"
Expand Down
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>utPLSQL-java-api</name>
Expand Down Expand Up @@ -128,6 +128,18 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -136,6 +148,7 @@
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
<trimStackTrace>false</trimStackTrace>
</configuration>
<dependencies>
<dependency>
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/utplsql/api/CustomTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ public final class CustomTypes {
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";

public static final String UT_REPORTERS = "UT_REPORTERS";
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
public static final String UT_TEAMCITY_REPORTER = "UT_TEAMCITY_REPORTER";
public static final String UT_XUNIT_REPORTER = "UT_XUNIT_REPORTER";
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
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_FILE_MAPPING = "UT_FILE_MAPPING";
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/utplsql/api/ResourceUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.utplsql.api;

import com.sun.nio.zipfs.ZipFileSystem;
import com.sun.nio.zipfs.ZipPath;
import org.utplsql.api.reporter.CoverageHTMLReporter;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down
44 changes: 32 additions & 12 deletions src/main/java/org/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.api.reporter.DocumentationReporter;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.api.testRunner.TestRunnerStatement;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -23,6 +25,9 @@
public class TestRunner {

private TestRunnerOptions options = new TestRunnerOptions();
private CompatibilityProxy compatibilityProxy;
private ReporterFactory reporterFactory;
private List<String> reporterNames = new ArrayList<>();

public TestRunner addPath(String path) {
options.pathList.add(path);
Expand All @@ -39,6 +44,14 @@ public TestRunner addReporter(Reporter reporter) {
return this;
}

public TestRunner addReporter( String reporterName ) {
if ( reporterFactory != null )
options.reporterList.add(reporterFactory.createReporter(reporterName));
else
reporterNames.add(reporterName);
return this;
}

public TestRunner colorConsole(boolean colorConsole) {
options.colorConsole = colorConsole;
return this;
Expand Down Expand Up @@ -95,9 +108,25 @@ public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
return this;
}

public TestRunner setReporterFactory( ReporterFactory reporterFactory ) {
this.reporterFactory = reporterFactory;
return this;
}

private void delayedAddReporters() {
if ( reporterFactory != null )
reporterNames.stream().forEach( this::addReporter );
else
throw new IllegalStateException("ReporterFactory must be set to add delayed Reporters!");
}

public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException {

CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
if ( reporterFactory == null )
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);

delayedAddReporters();

// First of all check version compatibility
compatibilityProxy.failOnNotCompatible();
Expand Down Expand Up @@ -129,15 +158,6 @@ else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
throw new UtPLSQLNotInstalledException(e);
}
else {
// If the execution failed by unexpected reasons finishes all reporters,
// this way the users don't need to care about reporters' sessions hanging.
OracleConnection oraConn = conn.unwrap(OracleConnection.class);

try (CallableStatement closeBufferStmt = conn.prepareCall("BEGIN ut_output_buffer.close(?); END;")) {
closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, options.reporterList.toArray()));
closeBufferStmt.execute();
} catch (SQLException ignored) {}

throw e;
}
} finally {
Expand All @@ -156,8 +176,8 @@ else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
* @throws SQLException any sql exception
*/
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
reporter.init(conn);
if (!reporter.isInit() || reporter.getId() == null || reporter.getId().isEmpty())
reporter.init(conn, compatibilityProxy, reporterFactory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.utplsql.api.TestRunnerOptions;
import org.utplsql.api.Version;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.outputBuffer.OutputBuffer;
import org.utplsql.api.outputBuffer.OutputBufferProvider;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.testRunner.TestRunnerStatement;
import org.utplsql.api.testRunner.TestRunnerStatementProvider;

Expand All @@ -20,8 +23,8 @@
*/
public class CompatibilityProxy {

public static final String UTPLSQL_API_VERSION = "3.0.4";
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3.0";
public static final String UTPLSQL_API_VERSION = "3.1.0";
public static final String UTPLSQL_COMPATIBILITY_VERSION = "3";

private Version databaseVersion;
private boolean compatible = false;
Expand Down Expand Up @@ -146,4 +149,15 @@ public TestRunnerStatement getTestRunnerStatement(TestRunnerOptions options, Con
{
return TestRunnerStatementProvider.getCompatibleTestRunnerStatement(databaseVersion, options, conn);
}

/** Returns an OutputBuffer compatible with the current framework
*
* @param reporter
* @param conn
* @return
* @throws SQLException
*/
public OutputBuffer getOutputBuffer(Reporter reporter, Connection conn) throws SQLException {
return OutputBufferProvider.getCompatibleOutputBuffer(databaseVersion, reporter, conn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public enum OptionalFeatures {

FAIL_ON_ERROR("3.0.3", null),
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null);
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null),
CUSTOM_REPORTERS("3.1.0", null);

private Version minVersion;
private Version maxVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package org.utplsql.api;
package org.utplsql.api.outputBuffer;

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;
import java.util.function.Consumer;

/**
* Fetches the lines produced by a reporter.
*
* @author vinicius
* @author pesse
*/
public class OutputBuffer {
abstract class AbstractOutputBuffer implements OutputBuffer {

private Reporter reporter;

/**
* Creates a new OutputBuffer.
* Creates a new DefaultOutputBuffer.
* @param reporter the reporter to be used
*/
public OutputBuffer(Reporter reporter) {
AbstractOutputBuffer(Reporter reporter) {

assert reporter.isInit() : "Reporter is not initialized! You can only create OutputBuffers for initialized Reporters";

this.reporter = reporter;
}

Expand Down Expand Up @@ -56,27 +62,24 @@ public void printAvailable(Connection conn, List<PrintStream> printStreams) thro
});
}

protected abstract PreparedStatement getLinesStatement( Connection conn ) throws SQLException;

protected abstract CallableStatement getLinesCursorStatement( Connection conn ) 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
* @param onLineFetched 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();

while (resultSet.next())
cb.onLineFetched(resultSet.getString(1));
} finally {
if (resultSet != null)
resultSet.close();
if (preparedStatement != null)
preparedStatement.close();
public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) throws SQLException {

try (PreparedStatement pstmt = getLinesStatement(conn)) {

try (ResultSet resultSet = pstmt.executeQuery() ) {
while (resultSet.next())
onLineFetched.accept(resultSet.getString(1));
}
}
}

Expand All @@ -87,34 +90,22 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
* @throws SQLException any sql errors
*/
public List<String> 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();

resultSet = (ResultSet) callableStatement.getObject(1);

List<String> outputLines = new ArrayList<>();
while (resultSet.next()) {
outputLines.add(resultSet.getString("text"));

try (CallableStatement cstmt = getLinesCursorStatement(conn)) {

cstmt.execute();

try ( ResultSet resultSet = (ResultSet) cstmt.getObject(1)) {

List<String> outputLines = new ArrayList<>();
while (resultSet.next()) {
outputLines.add(resultSet.getString("text"));
}
return outputLines;
}
return outputLines;
} finally {
if (resultSet != null)
resultSet.close();
if (callableStatement != null)
callableStatement.close();
}
}

/**
* Callback to be called when a new line is available from the output buffer.
*/
public interface Callback {
void onLineFetched(String s);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.utplsql.api.outputBuffer;

import oracle.jdbc.OracleTypes;
import org.utplsql.api.reporter.Reporter;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/** Compatibility Output-Buffer for 3.0.0 - 3.0.4
*
* @author pesse
*/
class CompatibilityOutputBufferPre310 extends AbstractOutputBuffer {

CompatibilityOutputBufferPre310( Reporter reporter ) {
super(reporter);
}

@Override
protected PreparedStatement getLinesStatement(Connection conn) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))");
pstmt.setString(1, getReporter().getId());
return pstmt;
}

@Override
protected CallableStatement getLinesCursorStatement(Connection conn) throws SQLException {
CallableStatement cstmt = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.setString(2, getReporter().getId());
return cstmt;
}
}
Loading