-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestRunner.java
More file actions
153 lines (122 loc) · 4.97 KB
/
TestRunner.java
File metadata and controls
153 lines (122 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.utplsql.api;
import oracle.jdbc.OracleConnection;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.api.reporter.DocumentationReporter;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.testRunner.TestRunnerStatement;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* Created by Vinicius Avellar on 12/04/2017.
*
* @author Vinicius Avellar
* @author pesse
*/
public class TestRunner {
private TestRunnerOptions options = new TestRunnerOptions();
public TestRunner addPath(String path) {
options.pathList.add(path);
return this;
}
public TestRunner addPathList(List<String> paths) {
if (options.pathList != null) options.pathList.addAll(paths);
return this;
}
public TestRunner addReporter(Reporter reporter) {
options.reporterList.add(reporter);
return this;
}
public TestRunner colorConsole(boolean colorConsole) {
options.colorConsole = colorConsole;
return this;
}
public TestRunner addReporterList(List<Reporter> reporterList) {
if (options.reporterList != null) options.reporterList.addAll(reporterList);
return this;
}
public TestRunner addCoverageScheme(String coverageScheme) {
options.coverageSchemes.add(coverageScheme);
return this;
}
public TestRunner includeObject(String obj) {
options.includeObjects.add(obj);
return this;
}
public TestRunner excludeObject(String obj) {
options.excludeObjects.add(obj);
return this;
}
public TestRunner sourceMappingOptions(FileMapperOptions mapperOptions) {
options.sourceMappingOptions = mapperOptions;
return this;
}
public TestRunner testMappingOptions(FileMapperOptions mapperOptions) {
options.testMappingOptions = mapperOptions;
return this;
}
public TestRunner failOnErrors(boolean failOnErrors) {
options.failOnErrors = failOnErrors;
return this;
}
public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
{
options.skipCompatibilityCheck = skipCompatibilityCheck;
return this;
}
public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException {
CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
// First of all check version compatibility
compatibilityProxy.failOnNotCompatible();
for (Reporter r : options.reporterList)
validateReporter(conn, r);
if (options.pathList.isEmpty()) {
options.pathList.add(DBHelper.getCurrentSchema(conn));
}
if (options.reporterList.isEmpty()) {
options.reporterList.add(new DocumentationReporter().init(conn));
}
TestRunnerStatement testRunnerStatement = null;
try {
DBHelper.enableDBMSOutput(conn);
testRunnerStatement = compatibilityProxy.getTestRunnerStatement(options, conn);
testRunnerStatement.execute();
} catch (SQLException e) {
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
throw new SomeTestsFailedException(e.getMessage(), e);
}
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 {
if (testRunnerStatement != null) {
testRunnerStatement.close();
}
DBHelper.disableDBMSOutput(conn);
}
}
/**
* Check if the reporter was initialized, if not call reporter.init.
* @param conn the database connection
* @param reporter the reporter
* @throws SQLException any sql exception
*/
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
reporter.init(conn);
}
}