Skip to content

Commit 9401da7

Browse files
committed
Change TestRunner usage
1 parent c7a5f44 commit 9401da7

File tree

4 files changed

+146
-51
lines changed

4 files changed

+146
-51
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@
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+
/**
16+
* Return a new sys_guid from database.
17+
* @param conn the connection
18+
* @return the new id string
19+
* @throws SQLException any database error
20+
*/
1421
public static String newSysGuid(Connection conn) throws SQLException {
1522
CallableStatement callableStatement = null;
1623
try {
17-
callableStatement = conn.prepareCall("BEGIN :id := sys_guid(); END;");
18-
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
24+
callableStatement = conn.prepareCall("BEGIN ? := sys_guid(); END;");
25+
callableStatement.registerOutParameter(1, OracleTypes.RAW);
1926
callableStatement.executeUpdate();
20-
return callableStatement.getString(":id");
27+
return callableStatement.getString(1);
28+
} finally {
29+
if (callableStatement != null)
30+
callableStatement.close();
31+
}
32+
}
33+
34+
/**
35+
* Return the current schema name.
36+
* @param conn the connection
37+
* @return the schema name
38+
* @throws SQLException any database error
39+
*/
40+
public static String getCurrentSchema(Connection conn) throws SQLException {
41+
CallableStatement callableStatement = null;
42+
try {
43+
callableStatement = conn.prepareCall("BEGIN ? := sys_context('userenv', 'current_schema'); END;");
44+
callableStatement.registerOutParameter(1, Types.VARCHAR);
45+
callableStatement.executeUpdate();
46+
return callableStatement.getString(1);
2147
} finally {
2248
if (callableStatement != null)
2349
callableStatement.close();

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

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

3+
import io.github.utplsql.api.reporter.DocumentationReporter;
34
import io.github.utplsql.api.reporter.Reporter;
45
import oracle.jdbc.OracleConnection;
56

6-
import java.sql.Array;
7-
import java.sql.CallableStatement;
8-
import java.sql.Connection;
9-
import java.sql.SQLException;
7+
import java.sql.*;
8+
import java.util.ArrayList;
109
import java.util.List;
1110

1211
/**
1312
* Created by Vinicius Avellar on 12/04/2017.
1413
*/
1514
public class TestRunner {
1615

17-
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<>();
1823

19-
public void run(Connection conn, String path, Reporter reporter) throws SQLException {
20-
validateReporter(conn, reporter);
21-
CallableStatement callableStatement = null;
22-
try {
23-
callableStatement = conn.prepareCall("BEGIN ut_runner.run(a_path => :path, a_reporter => :reporter); END;");
24-
callableStatement.setString(":path", path);
25-
callableStatement.setObject(":reporter", reporter);
26-
callableStatement.execute();
27-
} finally {
28-
if (callableStatement != null)
29-
callableStatement.close();
30-
}
24+
public TestRunner() {
25+
}
26+
27+
public TestRunner addPath(String path) {
28+
this.pathList.add(path);
29+
return this;
30+
}
31+
32+
public TestRunner addPathList(List<String> paths) {
33+
this.pathList.addAll(paths);
34+
return this;
35+
}
36+
37+
public TestRunner addReporter(Reporter reporter) {
38+
this.reporterList.add(reporter);
39+
return this;
40+
}
41+
42+
public TestRunner addReporterList(List<Reporter> reporterList) {
43+
this.reporterList.addAll(reporterList);
44+
return this;
3145
}
3246

33-
public void run(Connection conn, List<String> pathList, List<Reporter> reporterList) throws SQLException {
34-
for (Reporter r : reporterList)
47+
public TestRunner addCoverageScheme(String coverageScheme) {
48+
this.coverageSchemes.add(coverageScheme);
49+
return this;
50+
}
51+
52+
public TestRunner withSourceFiles(List<String> sourceFiles) {
53+
this.sourceFiles.addAll(sourceFiles);
54+
return this;
55+
}
56+
57+
public TestRunner withTestFiles(List<String> testFiles) {
58+
this.testFiles.addAll(testFiles);
59+
return this;
60+
}
61+
62+
public TestRunner includeObject(String obj) {
63+
this.includeObjects.add(obj);
64+
return this;
65+
}
66+
67+
public TestRunner excludeObject(String obj) {
68+
this.excludeObjects.add(obj);
69+
return this;
70+
}
71+
72+
public void run(Connection conn) throws SQLException {
73+
for (Reporter r : this.reporterList)
3574
validateReporter(conn, r);
3675

76+
if (this.pathList.isEmpty()) {
77+
this.pathList.add(DBHelper.getCurrentSchema(conn));
78+
}
79+
80+
if (this.reporterList.isEmpty()) {
81+
this.reporterList.add(new DocumentationReporter().init(conn));
82+
}
83+
3784
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
38-
Array pathArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, pathList.toArray());
39-
Array reporterArray = oraConn.createARRAY(CustomTypes.UT_REPORTERS, reporterList.toArray());
85+
Array pathArray = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray());
86+
Array reporterArray = oraConn.createARRAY(CustomTypes.UT_REPORTERS, this.reporterList.toArray());
87+
Array coverageSchemes = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray());
88+
Array sourceFiles = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray());
89+
Array testFiles = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray());
90+
Array includeObjects = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray());
91+
Array excludeObjects = oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray());
4092

4193
CallableStatement callableStatement = null;
4294
try {
43-
callableStatement = conn.prepareCall("BEGIN ut_runner.run(a_paths => ?, a_reporters => ?); END;");
95+
callableStatement = conn.prepareCall(
96+
"BEGIN " +
97+
"ut_runner.run(" +
98+
"a_paths => ?, a_reporters => ?, a_coverage_schemes => ?," +
99+
"a_source_files => ?, a_test_files => ?, " +
100+
"a_include_objects => ?, a_exclude_objects => ?); " +
101+
"END;");
44102
callableStatement.setArray(1, pathArray);
45103
callableStatement.setArray(2, reporterArray);
104+
callableStatement.setArray(3, coverageSchemes);
105+
callableStatement.setArray(4, sourceFiles);
106+
callableStatement.setArray(5, testFiles);
107+
callableStatement.setArray(6, includeObjects);
108+
callableStatement.setArray(7, excludeObjects);
46109
callableStatement.execute();
47110
} finally {
48111
if (callableStatement != null)

src/test/java/io/github/utplsql/api/OutputBufferTest.java

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

3-
import io.github.utplsql.api.rules.DatabaseRule;
4-
import io.github.utplsql.api.reporter.Reporter;
53
import io.github.utplsql.api.reporter.DocumentationReporter;
4+
import io.github.utplsql.api.reporter.Reporter;
5+
import io.github.utplsql.api.rules.DatabaseRule;
66
import org.junit.Assert;
77
import org.junit.Rule;
88
import org.junit.Test;
99

10+
import java.io.File;
1011
import java.io.FileOutputStream;
1112
import java.io.PrintStream;
1213
import java.sql.Connection;
@@ -40,7 +41,10 @@ public void printAvailableLines() {
4041
Future<Object> task1 = executorService.submit(() -> {
4142
try {
4243
Connection conn = db.newConnection();
43-
new TestRunner().run(conn, "", reporter);
44+
new TestRunner()
45+
.addPath(db.getUser())
46+
.addReporter(reporter)
47+
.run(conn);
4448

4549
return Boolean.TRUE;
4650
} catch (SQLException e) {
@@ -50,9 +54,10 @@ public void printAvailableLines() {
5054

5155
Future<Object> task2 = executorService.submit(() -> {
5256
FileOutputStream fileOutStream = null;
57+
File outFile = new File("output.txt");
5358
try {
5459
Connection conn = db.newConnection();
55-
fileOutStream = new FileOutputStream("output.txt");
60+
fileOutStream = new FileOutputStream(outFile);
5661

5762
List<PrintStream> printStreams = new ArrayList<>();
5863
printStreams.add(System.out);
@@ -65,8 +70,10 @@ public void printAvailableLines() {
6570
} catch (SQLException e) {
6671
return e;
6772
} finally {
68-
if (fileOutStream != null)
73+
if (fileOutStream != null) {
6974
fileOutStream.close();
75+
outFile.delete();
76+
}
7077
}
7178
});
7279

@@ -93,7 +100,10 @@ public void fetchAllLines() {
93100
try {
94101
final Reporter reporter = createReporter();
95102
Connection conn = db.newConnection();
96-
new TestRunner().run(conn, "", reporter);
103+
new TestRunner()
104+
.addPath(db.getUser())
105+
.addReporter(reporter)
106+
.run(conn);
97107

98108
List<String> outputLines = new OutputBuffer(reporter)
99109
.fetchAll(conn);

src/test/java/io/github/utplsql/api/TestRunnerTest.java

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

3+
import io.github.utplsql.api.reporter.*;
34
import io.github.utplsql.api.rules.DatabaseRule;
4-
import io.github.utplsql.api.reporter.Reporter;
5-
import io.github.utplsql.api.reporter.CoverageHTMLReporter;
6-
import io.github.utplsql.api.reporter.DocumentationReporter;
75
import org.junit.Assert;
86
import org.junit.Rule;
97
import org.junit.Test;
108

119
import java.sql.Connection;
1210
import java.sql.SQLException;
13-
import java.util.ArrayList;
14-
import java.util.List;
1511

1612
/**
1713
* Created by Vinicius on 13/04/2017.
@@ -22,30 +18,30 @@ public class TestRunnerTest {
2218
public final DatabaseRule db = new DatabaseRule();
2319

2420
@Test
25-
public void runWithDocumentationReporter() {
21+
public void runWithDefaultParameters() {
2622
try {
2723
Connection conn = db.newConnection();
28-
Reporter reporter = new DocumentationReporter();
29-
new TestRunner().run(conn, db.getUser(), reporter);
30-
Assert.assertNotNull(reporter.getReporterId());
24+
new TestRunner().run(conn);
3125
} catch (SQLException e) {
3226
Assert.fail(e.getMessage());
3327
}
3428
}
3529

3630
@Test
37-
public void runWithTwoReporters() {
31+
public void runWithManyReporters() {
3832
try {
3933
Connection conn = db.newConnection();
40-
41-
List<String> pathList = new ArrayList<>();
42-
pathList.add(db.getUser());
43-
44-
List<Reporter> reporterList = new ArrayList<>();
45-
reporterList.add(new DocumentationReporter().init(conn));
46-
reporterList.add(new CoverageHTMLReporter().init(conn));
47-
48-
new TestRunner().run(conn, pathList, reporterList);
34+
new TestRunner()
35+
.addPath("ut3")
36+
.addPath(db.getUser())
37+
.addReporter(new DocumentationReporter().init(conn))
38+
.addReporter(new CoverageHTMLReporter().init(conn))
39+
.addReporter(new CoverageSonarReporter().init(conn))
40+
.addReporter(new CoverallsReporter().init(conn))
41+
.addReporter(new SonarTestReporter().init(conn))
42+
.addReporter(new TeamCityReporter().init(conn))
43+
.addReporter(new XUnitReporter().init(conn))
44+
.run(conn);
4945
} catch (SQLException e) {
5046
Assert.fail(e.getMessage());
5147
}

0 commit comments

Comments
 (0)