-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestRunner.java
More file actions
287 lines (239 loc) · 9.78 KB
/
TestRunner.java
File metadata and controls
287 lines (239 loc) · 9.78 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package org.utplsql.api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.db.DatabaseInformation;
import org.utplsql.api.db.DefaultDatabaseInformation;
import org.utplsql.api.exception.OracleCreateStatmenetStuckException;
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.reporter.ReporterFactory;
import org.utplsql.api.testRunner.TestRunnerStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
/**
* Created by Vinicius Avellar on 12/04/2017.
*
* @author Vinicius Avellar
* @author pesse
*/
public class TestRunner {
private static final Logger logger = LoggerFactory.getLogger(TestRunner.class);
private final TestRunnerOptions options = new TestRunnerOptions();
private final List<String> reporterNames = new ArrayList<>();
private CompatibilityProxy compatibilityProxy;
private ReporterFactory reporterFactory;
public TestRunner addPath(String path) {
options.pathList.add(path);
return this;
}
public TestRunner addPathList(List<String> paths) {
options.pathList.addAll(paths);
return this;
}
public TestRunner addReporter(Reporter reporter) {
options.reporterList.add(reporter);
return this;
}
public TestRunner addReporter(String reporterName) {
if (reporterFactory != null) {
options.reporterList.add(reporterFactory.createReporter(reporterName));
} else {
reporterNames.add(reporterName);
}
return this;
}
public TestRunner colorConsole(boolean colorConsole) {
options.colorConsole = colorConsole;
return this;
}
public TestRunner addReporterList(List<Reporter> reporterList) {
options.reporterList.addAll(reporterList);
return this;
}
public TestRunner addCoverageScheme(String coverageScheme) {
options.coverageSchemes.add(coverageScheme);
return this;
}
public TestRunner addCoverageSchemes(Collection<String> schemaNames) {
this.options.coverageSchemes.addAll(schemaNames);
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 includeObjects(List<String> obj) {
options.includeObjects.addAll(obj);
return this;
}
public TestRunner excludeObjects(List<String> obj) {
options.excludeObjects.addAll(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 TestRunner setReporterFactory(ReporterFactory reporterFactory) {
this.reporterFactory = reporterFactory;
return this;
}
public TestRunner randomTestOrder(boolean randomTestOrder ) {
this.options.randomTestOrder = randomTestOrder;
return this;
}
public TestRunner randomTestOrderSeed( Integer seed ) {
this.options.randomTestOrderSeed = seed;
if ( seed != null ) this.options.randomTestOrder = true;
return this;
}
public TestRunner addTag( String tag ) {
this.options.tags.add(tag);
return this;
}
public TestRunner addTags(Collection<String> tags) {
this.options.tags.addAll(tags);
return this;
}
public TestRunner oraStuckTimeout(Integer oraStuckTimeout ) {
this.options.oraStuckTimeout = oraStuckTimeout;
return this;
}
public TestRunnerOptions getOptions() { return options; }
private void delayedAddReporters() {
if (reporterFactory != null) {
reporterNames.forEach(this::addReporter);
} else {
throw new IllegalStateException("ReporterFactory must be set to add delayed Reporters!");
}
}
private void handleException(Throwable e) throws SQLException {
// Just pass exceptions already categorized
if ( e instanceof UtPLSQLNotInstalledException ) throw (UtPLSQLNotInstalledException)e;
else if ( e instanceof SomeTestsFailedException ) throw (SomeTestsFailedException)e;
else if ( e instanceof OracleCreateStatmenetStuckException ) throw (OracleCreateStatmenetStuckException)e;
// Categorize exceptions
else if (e instanceof SQLException) {
SQLException sqlException = (SQLException) e;
if (sqlException.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
throw new SomeTestsFailedException(sqlException.getMessage(), e);
} else if (((SQLException) e).getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
throw new UtPLSQLNotInstalledException(sqlException);
} else {
throw sqlException;
}
} else {
throw new SQLException("Unknown exception, wrapping: " + e.getMessage(), e);
}
}
public void run(Connection conn) throws SQLException {
logger.info("TestRunner initialized");
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
if ( options.skipCompatibilityCheck ) {
compatibilityProxy = new CompatibilityProxy(conn, Version.LATEST, databaseInformation);
} else {
compatibilityProxy = new CompatibilityProxy(conn, databaseInformation);
}
logger.info("Running on utPLSQL {}", compatibilityProxy.getVersionDescription());
if (reporterFactory == null) {
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);
}
delayedAddReporters();
// First of all check version compatibility
compatibilityProxy.failOnNotCompatible();
logger.info("Initializing reporters");
for (Reporter r : options.reporterList) {
validateReporter(conn, r);
}
if (options.pathList.isEmpty()) {
options.pathList.add(databaseInformation.getCurrentSchema(conn));
}
if (options.reporterList.isEmpty()) {
logger.info("No reporter given so choosing ut_documentation_reporter");
options.reporterList.add(new DocumentationReporter().init(conn));
}
TestRunnerStatement testRunnerStatement = null;
try {
testRunnerStatement = ( options.oraStuckTimeout > 0 ) ? initStatementWithTimeout(conn, options.oraStuckTimeout) : initStatement(conn);
logger.info("Running tests");
testRunnerStatement.execute();
logger.info("Running tests finished.");
testRunnerStatement.close();
} catch (OracleCreateStatmenetStuckException e) {
// Don't close statement in this case for it will be stuck, too
throw e;
} catch (SQLException e) {
if (testRunnerStatement != null) testRunnerStatement.close();
handleException(e);
}
}
private TestRunnerStatement initStatement( Connection conn ) throws SQLException {
return compatibilityProxy.getTestRunnerStatement(options, conn);
}
private TestRunnerStatement initStatementWithTimeout( Connection conn, int timeout ) throws OracleCreateStatmenetStuckException, SQLException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<TestRunnerStatement> callable = () -> compatibilityProxy.getTestRunnerStatement(options, conn);
Future<TestRunnerStatement> future = executor.submit(callable);
// We want to leave the statement open in case of stuck scenario
TestRunnerStatement testRunnerStatement = null;
try {
testRunnerStatement = future.get(timeout, TimeUnit.SECONDS);
} catch (TimeoutException e) {
logger.error("Detected Oracle driver stuck during Statement initialization");
executor.shutdownNow();
throw new OracleCreateStatmenetStuckException(e);
} catch (InterruptedException e) {
handleException(e);
} catch (ExecutionException e) {
handleException(e.getCause());
}
return testRunnerStatement;
}
/**
* 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.isInit() || reporter.getId() == null || reporter.getId().isEmpty()) {
reporter.init(conn, compatibilityProxy, reporterFactory);
}
}
/**
* Returns the databaseVersion the TestRunner was run against
*
* @return Version of the database the TestRunner was run against
*/
public Version getUsedDatabaseVersion() {
if (compatibilityProxy != null) {
return compatibilityProxy.getUtPlsqlVersion();
} else {
return null;
}
}
}