-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestRunner.java
More file actions
191 lines (158 loc) · 7.1 KB
/
TestRunner.java
File metadata and controls
191 lines (158 loc) · 7.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package org.utplsql.api;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.DocumentationReporter;
import org.utplsql.api.reporter.Reporter;
import oracle.jdbc.OracleConnection;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Vinicius Avellar on 12/04/2017.
*/
public class TestRunner {
private List<String> pathList = new ArrayList<>();
private List<Reporter> reporterList = new ArrayList<>();
private boolean colorConsole = false;
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<>();
private FileMapperOptions sourceMappingOptions;
private FileMapperOptions testMappingOptions;
private boolean failOnErrors = false;
public TestRunner addPath(String path) {
this.pathList.add(path);
return this;
}
public TestRunner addPathList(List<String> paths) {
if (pathList != null) this.pathList.addAll(paths);
return this;
}
public TestRunner addReporter(Reporter reporter) {
this.reporterList.add(reporter);
return this;
}
public TestRunner colorConsole(boolean colorConsole) {
this.colorConsole = colorConsole;
return this;
}
public TestRunner addReporterList(List<Reporter> reporterList) {
if (reporterList != null) this.reporterList.addAll(reporterList);
return this;
}
public TestRunner addCoverageScheme(String coverageScheme) {
this.coverageSchemes.add(coverageScheme);
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 TestRunner sourceMappingOptions(FileMapperOptions mapperOptions) {
this.sourceMappingOptions = mapperOptions;
return this;
}
public TestRunner testMappingOptions(FileMapperOptions mapperOptions) {
this.testMappingOptions = mapperOptions;
return this;
}
public TestRunner failOnErrors(boolean failOnErrors) {
this.failOnErrors = failOnErrors;
return this;
}
public void run(Connection conn) throws SomeTestsFailedException, 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));
}
// Workaround because Oracle JDBC doesn't support passing boolean to stored procedures.
String colorConsoleStr = Boolean.toString(this.colorConsole);
String failOnErrors = Boolean.toString(this.failOnErrors);
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
CallableStatement callableStatement = null;
try {
callableStatement = conn.prepareCall(
"BEGIN " +
"ut_runner.run(" +
"a_paths => ?, " +
"a_reporters => ?, " +
"a_color_console => " + colorConsoleStr + ", " +
"a_coverage_schemes => ?, " +
"a_source_file_mappings => ?, " +
"a_test_file_mappings => ?, " +
"a_include_objects => ?, " +
"a_exclude_objects => ?, " +
"a_fail_on_errors => " + failOnErrors + "); " +
"END;");
int paramIdx = 0;
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray()));
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray()));
if (this.coverageSchemes.isEmpty()) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray()));
}
if (this.sourceMappingOptions == null) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
} else {
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.sourceMappingOptions);
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
}
if (this.testMappingOptions == null) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
} else {
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions);
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
}
if (this.includeObjects.isEmpty()) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray()));
}
if (this.excludeObjects.isEmpty()) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray()));
}
callableStatement.execute();
} catch (SQLException e) {
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
throw new SomeTestsFailedException(e.getMessage(), e);
} else {
throw e;
}
} finally {
if (callableStatement != null)
callableStatement.close();
}
}
/**
* 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);
}
}