Skip to content

Commit 3f244a1

Browse files
authored
Merge pull request #7 from viniciusam/develop
New reporters and TestRunner api changes
2 parents e453df0 + 625a25f commit 3f244a1

20 files changed

+407
-92
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.utplsql.api;
2+
3+
/**
4+
* Database custom data reporter.
5+
*/
6+
public final class CustomTypes {
7+
8+
// Object names must be upper case.
9+
public static final String UT_REPORTERS = "UT_REPORTERS";
10+
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
11+
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
12+
public static final String UT_TEAMCITY_REPORTER = "UT_TEAMCITY_REPORTER";
13+
public static final String UT_XUNIT_REPORTER = "UT_XUNIT_REPORTER";
14+
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
15+
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
16+
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
17+
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
18+
19+
private CustomTypes() {}
20+
21+
}

src/main/java/io/github/utplsql/api/DBHelper.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,47 @@
55
import java.sql.CallableStatement;
66
import java.sql.Connection;
77
import java.sql.SQLException;
8+
import java.sql.Types;
89

910
/**
10-
* Created by Vinicius Avellar on 12/04/2017.
11+
* Database utility functions.
1112
*/
1213
public final class DBHelper {
1314

15+
private DBHelper() {}
16+
17+
/**
18+
* Return a new sys_guid from database.
19+
* @param conn the connection
20+
* @return the new id string
21+
* @throws SQLException any database error
22+
*/
1423
public static String newSysGuid(Connection conn) throws SQLException {
1524
CallableStatement callableStatement = null;
1625
try {
17-
callableStatement = conn.prepareCall("BEGIN :id := sys_guid(); END;");
18-
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
26+
callableStatement = conn.prepareCall("BEGIN ? := sys_guid(); END;");
27+
callableStatement.registerOutParameter(1, OracleTypes.RAW);
28+
callableStatement.executeUpdate();
29+
return callableStatement.getString(1);
30+
} finally {
31+
if (callableStatement != null)
32+
callableStatement.close();
33+
}
34+
}
35+
36+
/**
37+
* Return the current schema name.
38+
* @param conn the connection
39+
* @return the schema name
40+
* @throws SQLException any database error
41+
*/
42+
public static String getCurrentSchema(Connection conn) throws SQLException {
43+
CallableStatement callableStatement = null;
44+
try {
45+
callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;");
46+
callableStatement.registerOutParameter(1, Types.VARCHAR);
1947
callableStatement.executeUpdate();
20-
return callableStatement.getString(":id");
48+
return callableStatement.getString(1);
2149
} finally {
2250
if (callableStatement != null)
2351
callableStatement.close();

src/main/java/io/github/utplsql/api/OutputBuffer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.utplsql.api;
22

3-
import io.github.utplsql.api.types.BaseReporter;
3+
import io.github.utplsql.api.reporter.Reporter;
44
import oracle.jdbc.OracleTypes;
55

66
import java.io.PrintStream;
@@ -13,21 +13,21 @@
1313
*/
1414
public class OutputBuffer {
1515

16-
private BaseReporter reporter;
16+
private Reporter reporter;
1717

1818
/**
1919
* Creates a new OutputBuffer.
2020
* @param reporter the reporter to be used
2121
*/
22-
public OutputBuffer(BaseReporter reporter) {
22+
public OutputBuffer(Reporter reporter) {
2323
this.reporter = reporter;
2424
}
2525

2626
/**
2727
* Returns the reporter used by this buffer.
2828
* @return the reporter instance
2929
*/
30-
public BaseReporter getReporter() {
30+
public Reporter getReporter() {
3131
return reporter;
3232
}
3333

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

src/main/java/io/github/utplsql/api/TestRunner.java

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,108 @@
11
package io.github.utplsql.api;
22

3-
import io.github.utplsql.api.types.BaseReporter;
3+
import io.github.utplsql.api.reporter.DocumentationReporter;
4+
import io.github.utplsql.api.reporter.Reporter;
5+
import oracle.jdbc.OracleConnection;
46

5-
import java.sql.CallableStatement;
6-
import java.sql.Connection;
7-
import java.sql.SQLException;
7+
import java.sql.*;
8+
import java.util.ArrayList;
9+
import java.util.List;
810

911
/**
1012
* Created by Vinicius Avellar on 12/04/2017.
1113
*/
1214
public class TestRunner {
1315

14-
public TestRunner() {}
16+
private List<String> pathList = new ArrayList<>();
17+
private List<Reporter> reporterList = new ArrayList<>();
18+
private List<String> coverageSchemes = new ArrayList<>();
19+
private List<String> sourceFiles = new ArrayList<>();
20+
private List<String> testFiles = new ArrayList<>();
21+
private List<String> includeObjects = new ArrayList<>();
22+
private List<String> excludeObjects = new ArrayList<>();
23+
24+
public TestRunner addPath(String path) {
25+
this.pathList.add(path);
26+
return this;
27+
}
28+
29+
public TestRunner addPathList(List<String> paths) {
30+
this.pathList.addAll(paths);
31+
return this;
32+
}
33+
34+
public TestRunner addReporter(Reporter reporter) {
35+
this.reporterList.add(reporter);
36+
return this;
37+
}
38+
39+
public TestRunner addReporterList(List<Reporter> reporterList) {
40+
this.reporterList.addAll(reporterList);
41+
return this;
42+
}
43+
44+
public TestRunner addCoverageScheme(String coverageScheme) {
45+
this.coverageSchemes.add(coverageScheme);
46+
return this;
47+
}
48+
49+
public TestRunner withSourceFiles(List<String> sourceFiles) {
50+
this.sourceFiles.addAll(sourceFiles);
51+
return this;
52+
}
53+
54+
public TestRunner withTestFiles(List<String> testFiles) {
55+
this.testFiles.addAll(testFiles);
56+
return this;
57+
}
58+
59+
public TestRunner includeObject(String obj) {
60+
this.includeObjects.add(obj);
61+
return this;
62+
}
63+
64+
public TestRunner excludeObject(String obj) {
65+
this.excludeObjects.add(obj);
66+
return this;
67+
}
68+
69+
public void run(Connection conn) throws SQLException {
70+
for (Reporter r : this.reporterList)
71+
validateReporter(conn, r);
72+
73+
if (this.pathList.isEmpty()) {
74+
this.pathList.add(DBHelper.getCurrentSchema(conn));
75+
}
76+
77+
if (this.reporterList.isEmpty()) {
78+
this.reporterList.add(new DocumentationReporter().init(conn));
79+
}
80+
81+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
82+
Array pathArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray());
83+
Array reporterArray = oraConn.createARRAY(CustomTypes.UT_REPORTERS, this.reporterList.toArray());
84+
Array coverageSchemesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray());
85+
Array sourceFilesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray());
86+
Array testFilesArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray());
87+
Array includeObjectsArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray());
88+
Array excludeObjectsArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray());
1589

16-
public void run(Connection conn, String path, BaseReporter reporter) throws SQLException {
17-
validateReporter(conn, reporter);
1890
CallableStatement callableStatement = null;
1991
try {
20-
callableStatement = conn.prepareCall("BEGIN ut_runner.run(a_path => :path, a_reporter => :reporter); END;");
21-
callableStatement.setString(":path", path);
22-
callableStatement.setObject(":reporter", reporter);
92+
callableStatement = conn.prepareCall(
93+
"BEGIN " +
94+
"ut_runner.run(" +
95+
"a_paths => ?, a_reporters => ?, a_coverage_schemes => ?," +
96+
"a_source_files => ?, a_test_files => ?, " +
97+
"a_include_objects => ?, a_exclude_objects => ?); " +
98+
"END;");
99+
callableStatement.setArray(1, pathArray);
100+
callableStatement.setArray(2, reporterArray);
101+
callableStatement.setArray(3, coverageSchemesArray);
102+
callableStatement.setArray(4, sourceFilesArray);
103+
callableStatement.setArray(5, testFilesArray);
104+
callableStatement.setArray(6, includeObjectsArray);
105+
callableStatement.setArray(7, excludeObjectsArray);
23106
callableStatement.execute();
24107
} finally {
25108
if (callableStatement != null)
@@ -33,7 +116,7 @@ public void run(Connection conn, String path, BaseReporter reporter) throws SQLE
33116
* @param reporter the reporter
34117
* @throws SQLException any sql exception
35118
*/
36-
private void validateReporter(Connection conn, BaseReporter reporter) throws SQLException {
119+
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
37120
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
38121
reporter.init(conn);
39122
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.utplsql.api.reporter;
2+
3+
import io.github.utplsql.api.CustomTypes;
4+
5+
import java.sql.SQLException;
6+
import java.sql.SQLInput;
7+
import java.sql.SQLOutput;
8+
9+
public class CoverageHTMLReporter extends Reporter {
10+
11+
private String projectName;
12+
private String assetsPath;
13+
14+
public CoverageHTMLReporter() {
15+
16+
}
17+
18+
public CoverageHTMLReporter(String projectName, String assetsPath) {
19+
this.projectName = projectName;
20+
this.assetsPath = assetsPath;
21+
}
22+
23+
@Override
24+
public String getSQLTypeName() throws SQLException {
25+
return CustomTypes.UT_COVERAGE_HTML_REPORTER;
26+
}
27+
28+
public String getProjectName() {
29+
return projectName;
30+
}
31+
32+
public void setProjectName(String projectName) {
33+
this.projectName = projectName;
34+
}
35+
36+
public String getAssetsPath() {
37+
return assetsPath;
38+
}
39+
40+
public void setAssetsPath(String assetsPath) {
41+
this.assetsPath = assetsPath;
42+
}
43+
44+
@Override
45+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
46+
super.readSQL(stream, typeName);
47+
setProjectName(stream.readString());
48+
setAssetsPath(stream.readString());
49+
}
50+
51+
@Override
52+
public void writeSQL(SQLOutput stream) throws SQLException {
53+
super.writeSQL(stream);
54+
stream.writeString(getProjectName());
55+
stream.writeString(getAssetsPath());
56+
}
57+
58+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.utplsql.api.reporter;
2+
3+
import io.github.utplsql.api.CustomTypes;
4+
5+
import java.sql.SQLException;
6+
7+
public class CoverageSonarReporter extends Reporter {
8+
9+
@Override
10+
public String getSQLTypeName() throws SQLException {
11+
return CustomTypes.UT_COVERAGE_SONAR_REPORTER;
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.utplsql.api.reporter;
2+
3+
import io.github.utplsql.api.CustomTypes;
4+
5+
import java.sql.SQLException;
6+
7+
public class CoverallsReporter extends Reporter {
8+
9+
@Override
10+
public String getSQLTypeName() throws SQLException {
11+
return CustomTypes.UT_COVERALLS_REPORTER;
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.utplsql.api.reporter;
2+
3+
import io.github.utplsql.api.CustomTypes;
4+
5+
import java.sql.SQLException;
6+
7+
public class DocumentationReporter extends Reporter {
8+
9+
@Override
10+
public String getSQLTypeName() throws SQLException {
11+
return CustomTypes.UT_DOCUMENTATION_REPORTER;
12+
}
13+
14+
}

src/main/java/io/github/utplsql/api/types/BaseReporter.java renamed to src/main/java/io/github/utplsql/api/reporter/Reporter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.utplsql.api.types;
1+
package io.github.utplsql.api.reporter;
22

33
import io.github.utplsql.api.DBHelper;
44

@@ -8,15 +8,15 @@
88
/**
99
* Created by Vinicius on 13/04/2017.
1010
*/
11-
public abstract class BaseReporter implements SQLData {
11+
public abstract class Reporter implements SQLData {
1212

1313
private String selfType;
1414
private String reporterId;
1515
private java.sql.Date startDate;
1616

17-
public BaseReporter() {}
17+
public Reporter() {}
1818

19-
public BaseReporter init(Connection conn) throws SQLException {
19+
public Reporter init(Connection conn) throws SQLException {
2020
setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
2121
setReporterId(DBHelper.newSysGuid(conn));
2222
return this;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.utplsql.api.reporter;
2+
3+
import io.github.utplsql.api.CustomTypes;
4+
5+
public final class ReporterFactory {
6+
7+
private ReporterFactory() {}
8+
9+
public static Reporter createReporter(String reporterName) {
10+
switch (reporterName.toUpperCase()) {
11+
case CustomTypes.UT_DOCUMENTATION_REPORTER: return new DocumentationReporter();
12+
case CustomTypes.UT_COVERAGE_HTML_REPORTER: return new CoverageHTMLReporter();
13+
case CustomTypes.UT_TEAMCITY_REPORTER: return new TeamCityReporter();
14+
case CustomTypes.UT_XUNIT_REPORTER: return new XUnitReporter();
15+
case CustomTypes.UT_COVERALLS_REPORTER: return new CoverallsReporter();
16+
case CustomTypes.UT_COVERAGE_SONAR_REPORTER: return new CoverageSonarReporter();
17+
case CustomTypes.UT_SONAR_TEST_REPORTER: return new SonarTestReporter();
18+
default: throw new RuntimeException("Reporter " + reporterName + " not implemented.");
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)