Skip to content

New reporters and TestRunner api changes #7

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 6 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions src/main/java/io/github/utplsql/api/CustomTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.utplsql.api;

/**
* Database custom data reporter.
*/
public final class CustomTypes {

// Object names must be upper case.
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_VARCHAR2_LIST = "UT_VARCHAR2_LIST";

private CustomTypes() {}

}
36 changes: 32 additions & 4 deletions src/main/java/io/github/utplsql/api/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,47 @@
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

/**
* Created by Vinicius Avellar on 12/04/2017.
* Database utility functions.
*/
public final class DBHelper {

private DBHelper() {}

/**
* Return a new sys_guid from database.
* @param conn the connection
* @return the new id string
* @throws SQLException any database error
*/
public static String newSysGuid(Connection conn) throws SQLException {
CallableStatement callableStatement = null;
try {
callableStatement = conn.prepareCall("BEGIN :id := sys_guid(); END;");
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
callableStatement = conn.prepareCall("BEGIN ? := sys_guid(); END;");
callableStatement.registerOutParameter(1, OracleTypes.RAW);
callableStatement.executeUpdate();
return callableStatement.getString(1);
} finally {
if (callableStatement != null)
callableStatement.close();
}
}

/**
* Return the current schema name.
* @param conn the connection
* @return the schema name
* @throws SQLException any database error
*/
public static String getCurrentSchema(Connection conn) throws SQLException {
CallableStatement callableStatement = null;
try {
callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;");
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.executeUpdate();
return callableStatement.getString(":id");
return callableStatement.getString(1);
} finally {
if (callableStatement != null)
callableStatement.close();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/github/utplsql/api/OutputBuffer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.utplsql.api;

import io.github.utplsql.api.types.BaseReporter;
import io.github.utplsql.api.reporter.Reporter;
import oracle.jdbc.OracleTypes;

import java.io.PrintStream;
Expand All @@ -13,21 +13,21 @@
*/
public class OutputBuffer {

private BaseReporter reporter;
private Reporter reporter;

/**
* Creates a new OutputBuffer.
* @param reporter the reporter to be used
*/
public OutputBuffer(BaseReporter reporter) {
public OutputBuffer(Reporter reporter) {
this.reporter = reporter;
}

/**
* Returns the reporter used by this buffer.
* @return the reporter instance
*/
public BaseReporter getReporter() {
public Reporter getReporter() {
return reporter;
}

Expand Down Expand Up @@ -66,7 +66,7 @@ public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = conn.prepareCall("SELECT * FROM table(ut_output_buffer.get_lines(?))");
preparedStatement = conn.prepareStatement("SELECT * FROM table(ut_output_buffer.get_lines(?))");
preparedStatement.setString(1, getReporter().getReporterId());
resultSet = preparedStatement.executeQuery();

Expand Down
105 changes: 94 additions & 11 deletions src/main/java/io/github/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,108 @@
package io.github.utplsql.api;

import io.github.utplsql.api.types.BaseReporter;
import io.github.utplsql.api.reporter.DocumentationReporter;
import io.github.utplsql.api.reporter.Reporter;
import oracle.jdbc.OracleConnection;

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

/**
* Created by Vinicius Avellar on 12/04/2017.
*/
public class TestRunner {

public TestRunner() {}
private List<String> pathList = new ArrayList<>();
private List<Reporter> reporterList = new ArrayList<>();
private List<String> coverageSchemes = new ArrayList<>();
private List<String> sourceFiles = new ArrayList<>();
private List<String> testFiles = new ArrayList<>();
private List<String> includeObjects = new ArrayList<>();
private List<String> excludeObjects = new ArrayList<>();

public TestRunner addPath(String path) {
this.pathList.add(path);
return this;
}

public TestRunner addPathList(List<String> paths) {
this.pathList.addAll(paths);
return this;
}

public TestRunner addReporter(Reporter reporter) {
this.reporterList.add(reporter);
return this;
}

public TestRunner addReporterList(List<Reporter> reporterList) {
this.reporterList.addAll(reporterList);
return this;
}

public TestRunner addCoverageScheme(String coverageScheme) {
this.coverageSchemes.add(coverageScheme);
return this;
}

public TestRunner withSourceFiles(List<String> sourceFiles) {
this.sourceFiles.addAll(sourceFiles);
return this;
}

public TestRunner withTestFiles(List<String> testFiles) {
this.testFiles.addAll(testFiles);
return this;
}

public TestRunner includeObject(String obj) {
this.includeObjects.add(obj);
return this;
}

public TestRunner excludeObject(String obj) {
this.excludeObjects.add(obj);
return this;
}

public void run(Connection conn) throws SQLException {
for (Reporter r : this.reporterList)
validateReporter(conn, r);

if (this.pathList.isEmpty()) {
this.pathList.add(DBHelper.getCurrentSchema(conn));
}

if (this.reporterList.isEmpty()) {
this.reporterList.add(new DocumentationReporter().init(conn));
}

OracleConnection oraConn = conn.unwrap(OracleConnection.class);
Array pathArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray());
Array reporterArray = oraConn.createARRAY(CustomTypes.UT_REPORTERS, this.reporterList.toArray());
Array coverageSchemesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray());
Array sourceFilesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray());
Array testFilesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray());
Array includeObjectsArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray());
Array excludeObjectsArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray());

public void run(Connection conn, String path, BaseReporter reporter) throws SQLException {
validateReporter(conn, reporter);
CallableStatement callableStatement = null;
try {
callableStatement = conn.prepareCall("BEGIN ut_runner.run(a_path => :path, a_reporter => :reporter); END;");
callableStatement.setString(":path", path);
callableStatement.setObject(":reporter", reporter);
callableStatement = conn.prepareCall(
"BEGIN " +
"ut_runner.run(" +
"a_paths => ?, a_reporters => ?, a_coverage_schemes => ?," +
"a_source_files => ?, a_test_files => ?, " +
"a_include_objects => ?, a_exclude_objects => ?); " +
"END;");
callableStatement.setArray(1, pathArray);
callableStatement.setArray(2, reporterArray);
callableStatement.setArray(3, coverageSchemesArray);
callableStatement.setArray(4, sourceFilesArray);
callableStatement.setArray(5, testFilesArray);
callableStatement.setArray(6, includeObjectsArray);
callableStatement.setArray(7, excludeObjectsArray);
callableStatement.execute();
} finally {
if (callableStatement != null)
Expand All @@ -33,7 +116,7 @@ public void run(Connection conn, String path, BaseReporter reporter) throws SQLE
* @param reporter the reporter
* @throws SQLException any sql exception
*/
private void validateReporter(Connection conn, BaseReporter reporter) throws SQLException {
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
reporter.init(conn);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.CustomTypes;

import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class CoverageHTMLReporter extends Reporter {

private String projectName;
private String assetsPath;

public CoverageHTMLReporter() {

}

public CoverageHTMLReporter(String projectName, String assetsPath) {
this.projectName = projectName;
this.assetsPath = assetsPath;
}

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_COVERAGE_HTML_REPORTER;
}

public String getProjectName() {
return projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

public String getAssetsPath() {
return assetsPath;
}

public void setAssetsPath(String assetsPath) {
this.assetsPath = assetsPath;
}

@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
super.readSQL(stream, typeName);
setProjectName(stream.readString());
setAssetsPath(stream.readString());
}

@Override
public void writeSQL(SQLOutput stream) throws SQLException {
super.writeSQL(stream);
stream.writeString(getProjectName());
stream.writeString(getAssetsPath());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.CustomTypes;

import java.sql.SQLException;

public class CoverageSonarReporter extends Reporter {

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_COVERAGE_SONAR_REPORTER;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.CustomTypes;

import java.sql.SQLException;

public class CoverallsReporter extends Reporter {

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_COVERALLS_REPORTER;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.CustomTypes;

import java.sql.SQLException;

public class DocumentationReporter extends Reporter {

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_DOCUMENTATION_REPORTER;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.utplsql.api.types;
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.DBHelper;

Expand All @@ -8,15 +8,15 @@
/**
* Created by Vinicius on 13/04/2017.
*/
public abstract class BaseReporter implements SQLData {
public abstract class Reporter implements SQLData {

private String selfType;
private String reporterId;
private java.sql.Date startDate;

public BaseReporter() {}
public Reporter() {}

public BaseReporter init(Connection conn) throws SQLException {
public Reporter init(Connection conn) throws SQLException {
setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
setReporterId(DBHelper.newSysGuid(conn));
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/github/utplsql/api/reporter/ReporterFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.utplsql.api.reporter;

import io.github.utplsql.api.CustomTypes;

public final class ReporterFactory {

private ReporterFactory() {}

public static Reporter createReporter(String reporterName) {
switch (reporterName.toUpperCase()) {
case CustomTypes.UT_DOCUMENTATION_REPORTER: return new DocumentationReporter();
case CustomTypes.UT_COVERAGE_HTML_REPORTER: return new CoverageHTMLReporter();
case CustomTypes.UT_TEAMCITY_REPORTER: return new TeamCityReporter();
case CustomTypes.UT_XUNIT_REPORTER: return new XUnitReporter();
case CustomTypes.UT_COVERALLS_REPORTER: return new CoverallsReporter();
case CustomTypes.UT_COVERAGE_SONAR_REPORTER: return new CoverageSonarReporter();
case CustomTypes.UT_SONAR_TEST_REPORTER: return new SonarTestReporter();
default: throw new RuntimeException("Reporter " + reporterName + " not implemented.");
}
}

}
Loading