|
1 | 1 | package org.utplsql.api;
|
2 | 2 |
|
| 3 | +import oracle.jdbc.OracleConnection; |
| 4 | +import org.utplsql.api.compatibility.CompatibilityProvider; |
| 5 | +import org.utplsql.api.exception.DatabaseNotCompatibleException; |
3 | 6 | import org.utplsql.api.exception.SomeTestsFailedException;
|
4 | 7 | import org.utplsql.api.reporter.DocumentationReporter;
|
5 | 8 | import org.utplsql.api.reporter.Reporter;
|
6 |
| -import oracle.jdbc.OracleConnection; |
| 9 | +import org.utplsql.api.testRunner.TestRunnerStatement; |
7 | 10 |
|
8 | 11 | import java.sql.CallableStatement;
|
9 | 12 | import java.sql.Connection;
|
10 | 13 | import java.sql.SQLException;
|
11 |
| -import java.sql.Types; |
12 |
| -import java.util.ArrayList; |
13 | 14 | import java.util.List;
|
14 | 15 |
|
15 | 16 | /**
|
16 | 17 | * Created by Vinicius Avellar on 12/04/2017.
|
| 18 | + * |
| 19 | + * @author Vinicius Avellar |
| 20 | + * @author pesse |
17 | 21 | */
|
18 | 22 | public class TestRunner {
|
19 | 23 |
|
20 |
| - private List<String> pathList = new ArrayList<>(); |
21 |
| - private List<Reporter> reporterList = new ArrayList<>(); |
22 |
| - private boolean colorConsole = false; |
23 |
| - private List<String> coverageSchemes = new ArrayList<>(); |
24 |
| - private List<String> sourceFiles = new ArrayList<>(); |
25 |
| - private List<String> testFiles = new ArrayList<>(); |
26 |
| - private List<String> includeObjects = new ArrayList<>(); |
27 |
| - private List<String> excludeObjects = new ArrayList<>(); |
28 |
| - private FileMapperOptions sourceMappingOptions; |
29 |
| - private FileMapperOptions testMappingOptions; |
30 |
| - private boolean failOnErrors = false; |
| 24 | + private TestRunnerOptions options = new TestRunnerOptions(); |
31 | 25 |
|
32 | 26 | public TestRunner addPath(String path) {
|
33 |
| - this.pathList.add(path); |
| 27 | + options.pathList.add(path); |
34 | 28 | return this;
|
35 | 29 | }
|
36 | 30 |
|
37 | 31 | public TestRunner addPathList(List<String> paths) {
|
38 |
| - if (pathList != null) this.pathList.addAll(paths); |
| 32 | + if (options.pathList != null) options.pathList.addAll(paths); |
39 | 33 | return this;
|
40 | 34 | }
|
41 | 35 |
|
42 | 36 | public TestRunner addReporter(Reporter reporter) {
|
43 |
| - this.reporterList.add(reporter); |
| 37 | + options.reporterList.add(reporter); |
44 | 38 | return this;
|
45 | 39 | }
|
46 | 40 |
|
47 | 41 | public TestRunner colorConsole(boolean colorConsole) {
|
48 |
| - this.colorConsole = colorConsole; |
| 42 | + options.colorConsole = colorConsole; |
49 | 43 | return this;
|
50 | 44 | }
|
51 | 45 |
|
52 | 46 | public TestRunner addReporterList(List<Reporter> reporterList) {
|
53 |
| - if (reporterList != null) this.reporterList.addAll(reporterList); |
| 47 | + if (options.reporterList != null) options.reporterList.addAll(reporterList); |
54 | 48 | return this;
|
55 | 49 | }
|
56 | 50 |
|
57 | 51 | public TestRunner addCoverageScheme(String coverageScheme) {
|
58 |
| - this.coverageSchemes.add(coverageScheme); |
| 52 | + options.coverageSchemes.add(coverageScheme); |
59 | 53 | return this;
|
60 | 54 | }
|
61 | 55 |
|
62 | 56 | public TestRunner includeObject(String obj) {
|
63 |
| - this.includeObjects.add(obj); |
| 57 | + options.includeObjects.add(obj); |
64 | 58 | return this;
|
65 | 59 | }
|
66 | 60 |
|
67 | 61 | public TestRunner excludeObject(String obj) {
|
68 |
| - this.excludeObjects.add(obj); |
| 62 | + options.excludeObjects.add(obj); |
69 | 63 | return this;
|
70 | 64 | }
|
71 | 65 |
|
72 | 66 | public TestRunner sourceMappingOptions(FileMapperOptions mapperOptions) {
|
73 |
| - this.sourceMappingOptions = mapperOptions; |
| 67 | + options.sourceMappingOptions = mapperOptions; |
74 | 68 | return this;
|
75 | 69 | }
|
76 | 70 |
|
77 | 71 | public TestRunner testMappingOptions(FileMapperOptions mapperOptions) {
|
78 |
| - this.testMappingOptions = mapperOptions; |
| 72 | + options.testMappingOptions = mapperOptions; |
79 | 73 | return this;
|
80 | 74 | }
|
81 | 75 |
|
82 | 76 | public TestRunner failOnErrors(boolean failOnErrors) {
|
83 |
| - this.failOnErrors = failOnErrors; |
| 77 | + options.failOnErrors = failOnErrors; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck ) |
| 82 | + { |
| 83 | + options.skipCompatibilityCheck = skipCompatibilityCheck; |
84 | 84 | return this;
|
85 | 85 | }
|
86 | 86 |
|
87 |
| - public void run(Connection conn) throws SomeTestsFailedException, SQLException { |
88 |
| - for (Reporter r : this.reporterList) |
| 87 | + public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException { |
| 88 | + |
| 89 | + // First of all check version compatibility |
| 90 | + if ( !options.skipCompatibilityCheck ) |
| 91 | + DBHelper.failOnVersionCompatibilityCheckFailed(conn); |
| 92 | + |
| 93 | + for (Reporter r : options.reporterList) |
89 | 94 | validateReporter(conn, r);
|
90 | 95 |
|
91 |
| - if (this.pathList.isEmpty()) { |
92 |
| - this.pathList.add(DBHelper.getCurrentSchema(conn)); |
| 96 | + if (options.pathList.isEmpty()) { |
| 97 | + options.pathList.add(DBHelper.getCurrentSchema(conn)); |
93 | 98 | }
|
94 | 99 |
|
95 |
| - if (this.reporterList.isEmpty()) { |
96 |
| - this.reporterList.add(new DocumentationReporter().init(conn)); |
| 100 | + if (options.reporterList.isEmpty()) { |
| 101 | + options.reporterList.add(new DocumentationReporter().init(conn)); |
97 | 102 | }
|
98 | 103 |
|
99 |
| - // Workaround because Oracle JDBC doesn't support passing boolean to stored procedures. |
100 |
| - String colorConsoleStr = Boolean.toString(this.colorConsole); |
101 |
| - String failOnErrors = Boolean.toString(this.failOnErrors); |
| 104 | + TestRunnerStatement testRunnerStatement = null; |
102 | 105 |
|
103 |
| - OracleConnection oraConn = conn.unwrap(OracleConnection.class); |
104 |
| - CallableStatement callableStatement = null; |
105 | 106 | try {
|
106 | 107 | DBHelper.enableDBMSOutput(conn);
|
107 | 108 |
|
108 |
| - callableStatement = conn.prepareCall( |
109 |
| - "BEGIN " + |
110 |
| - "ut_runner.run(" + |
111 |
| - "a_paths => ?, " + |
112 |
| - "a_reporters => ?, " + |
113 |
| - "a_color_console => " + colorConsoleStr + ", " + |
114 |
| - "a_coverage_schemes => ?, " + |
115 |
| - "a_source_file_mappings => ?, " + |
116 |
| - "a_test_file_mappings => ?, " + |
117 |
| - "a_include_objects => ?, " + |
118 |
| - "a_exclude_objects => ?, " + |
119 |
| - "a_fail_on_errors => " + failOnErrors + "); " + |
120 |
| - "END;"); |
121 |
| - |
122 |
| - int paramIdx = 0; |
123 |
| - |
124 |
| - callableStatement.setArray( |
125 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray())); |
126 |
| - |
127 |
| - callableStatement.setArray( |
128 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray())); |
129 |
| - |
130 |
| - if (this.coverageSchemes.isEmpty()) { |
131 |
| - callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST); |
132 |
| - } else { |
133 |
| - callableStatement.setArray( |
134 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray())); |
135 |
| - } |
136 |
| - |
137 |
| - if (this.sourceMappingOptions == null) { |
138 |
| - callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS); |
139 |
| - } else { |
140 |
| - List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.sourceMappingOptions); |
141 |
| - |
142 |
| - callableStatement.setArray( |
143 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray())); |
144 |
| - } |
145 |
| - |
146 |
| - if (this.testMappingOptions == null) { |
147 |
| - callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS); |
148 |
| - } else { |
149 |
| - List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions); |
150 |
| - |
151 |
| - callableStatement.setArray( |
152 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray())); |
153 |
| - } |
154 |
| - |
155 |
| - if (this.includeObjects.isEmpty()) { |
156 |
| - callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST); |
157 |
| - } else { |
158 |
| - callableStatement.setArray( |
159 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray())); |
160 |
| - } |
161 |
| - |
162 |
| - if (this.excludeObjects.isEmpty()) { |
163 |
| - callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST); |
164 |
| - } else { |
165 |
| - callableStatement.setArray( |
166 |
| - ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray())); |
167 |
| - } |
| 109 | + testRunnerStatement = CompatibilityProvider.getTestRunnerStatement(options, conn); |
168 | 110 |
|
169 |
| - callableStatement.execute(); |
| 111 | + testRunnerStatement.execute(); |
170 | 112 | } catch (SQLException e) {
|
171 | 113 | if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
|
172 | 114 | throw new SomeTestsFailedException(e.getMessage(), e);
|
173 | 115 | } else {
|
174 | 116 | // If the execution failed by unexpected reasons finishes all reporters,
|
175 | 117 | // this way the users don't need to care about reporters' sessions hanging.
|
| 118 | + OracleConnection oraConn = conn.unwrap(OracleConnection.class); |
| 119 | + |
176 | 120 | try (CallableStatement closeBufferStmt = conn.prepareCall("BEGIN ut_output_buffer.close(?); END;")) {
|
177 |
| - closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray())); |
| 121 | + closeBufferStmt.setArray(1, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, options.reporterList.toArray())); |
178 | 122 | closeBufferStmt.execute();
|
179 | 123 | } catch (SQLException ignored) {}
|
180 | 124 |
|
181 | 125 | throw e;
|
182 | 126 | }
|
183 | 127 | } finally {
|
184 |
| - if (callableStatement != null) { |
185 |
| - callableStatement.close(); |
| 128 | + if (testRunnerStatement != null) { |
| 129 | + testRunnerStatement.close(); |
186 | 130 | }
|
187 | 131 |
|
188 | 132 | DBHelper.disableDBMSOutput(conn);
|
|
0 commit comments