Skip to content

Commit d59b88d

Browse files
committed
Add option to fail on errors
1 parent c0757c4 commit d59b88d

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

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

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

3+
import io.github.utplsql.api.exception.SomeTestsFailedException;
34
import io.github.utplsql.api.reporter.DocumentationReporter;
45
import io.github.utplsql.api.reporter.Reporter;
56
import oracle.jdbc.OracleConnection;
@@ -21,6 +22,7 @@ public class TestRunner {
2122
private List<String> testFiles = new ArrayList<>();
2223
private List<String> includeObjects = new ArrayList<>();
2324
private List<String> excludeObjects = new ArrayList<>();
25+
private boolean failOnErrors = false;
2426

2527
public TestRunner addPath(String path) {
2628
this.pathList.add(path);
@@ -72,7 +74,12 @@ public TestRunner excludeObject(String obj) {
7274
return this;
7375
}
7476

75-
public void run(Connection conn) throws SQLException {
77+
public TestRunner failOnErrors(boolean failOnErrors) {
78+
this.failOnErrors = failOnErrors;
79+
return this;
80+
}
81+
82+
public void run(Connection conn) throws SomeTestsFailedException, SQLException {
7683
for (Reporter r : this.reporterList)
7784
validateReporter(conn, r);
7885

@@ -86,16 +93,23 @@ public void run(Connection conn) throws SQLException {
8693

8794
// Workaround because Oracle JDBC doesn't support passing boolean to stored procedures.
8895
String colorConsoleStr = Boolean.toString(this.colorConsole);
96+
String failOnErrors = Boolean.toString(this.failOnErrors);
8997

9098
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
9199
CallableStatement callableStatement = null;
92100
try {
93101
callableStatement = conn.prepareCall(
94102
"BEGIN " +
95103
"ut_runner.run(" +
96-
"a_paths => ?, a_reporters => ?, a_color_console => " + colorConsoleStr + ", " +
97-
"a_coverage_schemes => ?, a_source_files => ?, a_test_files => ?, " +
98-
"a_include_objects => ?, a_exclude_objects => ?); " +
104+
"a_paths => ?, " +
105+
"a_reporters => ?, " +
106+
"a_color_console => " + colorConsoleStr + ", " +
107+
"a_coverage_schemes => ?, " +
108+
"a_source_files => ?, " +
109+
"a_test_files => ?, " +
110+
"a_include_objects => ?, " +
111+
"a_exclude_objects => ?, " +
112+
"a_fail_on_errors => " + failOnErrors + "); " +
99113
"END;");
100114

101115
int paramIdx = 0;
@@ -142,6 +156,12 @@ public void run(Connection conn) throws SQLException {
142156
}
143157

144158
callableStatement.execute();
159+
} catch (SQLException e) {
160+
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
161+
throw new SomeTestsFailedException(e.getMessage(), e);
162+
} else {
163+
throw e;
164+
}
145165
} finally {
146166
if (callableStatement != null)
147167
callableStatement.close();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.utplsql.api.exception;
2+
3+
import java.sql.SQLException;
4+
5+
/**
6+
* Custom exception class to indicate if some tests failed.
7+
*/
8+
public class SomeTestsFailedException extends SQLException {
9+
10+
public static final int ERROR_CODE = 20213;
11+
12+
public SomeTestsFailedException(String reason, Throwable cause) {
13+
super(reason, cause);
14+
}
15+
16+
}

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

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

3+
import io.github.utplsql.api.exception.SomeTestsFailedException;
34
import io.github.utplsql.api.reporter.*;
45
import io.github.utplsql.api.rules.DatabaseRule;
56
import org.junit.Assert;
@@ -32,7 +33,6 @@ public void runWithManyReporters() {
3233
try {
3334
Connection conn = db.newConnection();
3435
new TestRunner()
35-
.addPath("ut3")
3636
.addPath(db.getUser())
3737
.addReporter(new DocumentationReporter().init(conn))
3838
.addReporter(new CoverageHTMLReporter().init(conn))
@@ -47,4 +47,19 @@ public void runWithManyReporters() {
4747
}
4848
}
4949

50+
@Test
51+
public void failOnErrors() {
52+
try {
53+
Connection conn = db.newConnection();
54+
new TestRunner()
55+
.failOnErrors(true)
56+
.run(conn);
57+
Assert.fail();
58+
} catch (SomeTestsFailedException ignored) {
59+
System.out.println("Expected exception object thrown.");
60+
} catch (SQLException e) {
61+
Assert.fail("Wrong exception object thrown.");
62+
}
63+
}
64+
5065
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy