|
7 | 7 | import org.utplsql.api.TestRunner; |
8 | 8 | import org.utplsql.api.Version; |
9 | 9 | import org.utplsql.api.compatibility.CompatibilityProxy; |
| 10 | +import org.utplsql.api.exception.DatabaseNotCompatibleException; |
10 | 11 | import org.utplsql.api.exception.SomeTestsFailedException; |
| 12 | +import org.utplsql.api.exception.UtPLSQLNotInstalledException; |
11 | 13 | import org.utplsql.api.reporter.Reporter; |
12 | 14 | import org.utplsql.api.reporter.ReporterFactory; |
13 | 15 | import org.utplsql.cli.exception.DatabaseConnectionFailed; |
|
30 | 32 | * @author pesse |
31 | 33 | */ |
32 | 34 | @Parameters(separators = "=", commandDescription = "run tests") |
33 | | -public class RunCommand { |
| 35 | +public class RunCommand implements ICommand { |
34 | 36 |
|
35 | 37 | @Parameter( |
36 | 38 | required = true, |
@@ -110,102 +112,113 @@ public List<String> getTestPaths() { |
110 | 112 | return testPaths; |
111 | 113 | } |
112 | 114 |
|
113 | | - public int run() throws Exception { |
| 115 | + public int run() { |
114 | 116 |
|
115 | | - final List<Reporter> reporterList; |
116 | | - final List<String> testPaths = getTestPaths(); |
| 117 | + try { |
117 | 118 |
|
118 | | - final File baseDir = new File("").getAbsoluteFile(); |
119 | | - final FileMapperOptions[] sourceMappingOptions = {null}; |
120 | | - final FileMapperOptions[] testMappingOptions = {null}; |
| 119 | + final List<Reporter> reporterList; |
| 120 | + final List<String> testPaths = getTestPaths(); |
121 | 121 |
|
122 | | - final int[] returnCode = {0}; |
| 122 | + final File baseDir = new File("").getAbsoluteFile(); |
| 123 | + final FileMapperOptions[] sourceMappingOptions = {null}; |
| 124 | + final FileMapperOptions[] testMappingOptions = {null}; |
123 | 125 |
|
124 | | - sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir); |
125 | | - testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir); |
| 126 | + final int[] returnCode = {0}; |
126 | 127 |
|
127 | | - ArrayList<String> includeObjectsList; |
128 | | - ArrayList<String> excludeObjectsList; |
| 128 | + sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir); |
| 129 | + testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir); |
129 | 130 |
|
130 | | - if (includeObjects != null && !includeObjects.isEmpty()) { |
131 | | - includeObjectsList = new ArrayList<>(Arrays.asList(includeObjects.split(","))); |
132 | | - } else { |
133 | | - includeObjectsList = new ArrayList<>(); |
134 | | - } |
135 | | - |
136 | | - if (excludeObjects != null && !excludeObjects.isEmpty()) { |
137 | | - excludeObjectsList = new ArrayList<>(Arrays.asList(excludeObjects.split(","))); |
138 | | - } else { |
139 | | - excludeObjectsList = new ArrayList<>(); |
140 | | - } |
| 131 | + ArrayList<String> includeObjectsList; |
| 132 | + ArrayList<String> excludeObjectsList; |
141 | 133 |
|
142 | | - final ArrayList<String> finalIncludeObjectsList = includeObjectsList; |
143 | | - final ArrayList<String> finalExcludeObjectsList = excludeObjectsList; |
144 | | - |
145 | | - final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters()+1); |
| 134 | + if (includeObjects != null && !includeObjects.isEmpty()) { |
| 135 | + includeObjectsList = new ArrayList<>(Arrays.asList(includeObjects.split(","))); |
| 136 | + } else { |
| 137 | + includeObjectsList = new ArrayList<>(); |
| 138 | + } |
146 | 139 |
|
147 | | - // Do the reporters initialization, so we can use the id to run and gather results. |
148 | | - try (Connection conn = dataSource.getConnection()) { |
| 140 | + if (excludeObjects != null && !excludeObjects.isEmpty()) { |
| 141 | + excludeObjectsList = new ArrayList<>(Arrays.asList(excludeObjects.split(","))); |
| 142 | + } else { |
| 143 | + excludeObjectsList = new ArrayList<>(); |
| 144 | + } |
149 | 145 |
|
150 | | - // Check if orai18n exists if database version is 11g |
151 | | - RunCommandChecker.checkOracleI18nExists(conn); |
| 146 | + final ArrayList<String> finalIncludeObjectsList = includeObjectsList; |
| 147 | + final ArrayList<String> finalExcludeObjectsList = excludeObjectsList; |
152 | 148 |
|
153 | | - // First of all do a compatibility check and fail-fast |
154 | | - compatibilityProxy = checkFrameworkCompatibility(conn); |
155 | | - reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy); |
| 149 | + final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 1); |
156 | 150 |
|
157 | | - reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy); |
| 151 | + // Do the reporters initialization, so we can use the id to run and gather results. |
| 152 | + try (Connection conn = dataSource.getConnection()) { |
158 | 153 |
|
159 | | - } catch (SQLException e) { |
160 | | - if ( e.getErrorCode() == 1017 || e.getErrorCode() == 12514 ) { |
161 | | - throw new DatabaseConnectionFailed(e); |
162 | | - } |
163 | | - else { |
164 | | - throw e; |
165 | | - } |
166 | | - } |
| 154 | + // Check if orai18n exists if database version is 11g |
| 155 | + RunCommandChecker.checkOracleI18nExists(conn); |
167 | 156 |
|
168 | | - // Output a message if --failureExitCode is set but database framework is not capable of |
169 | | - String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getDatabaseVersion()); |
170 | | - if ( msg != null ) { |
171 | | - System.out.println(msg); |
172 | | - } |
| 157 | + // First of all do a compatibility check and fail-fast |
| 158 | + compatibilityProxy = checkFrameworkCompatibility(conn); |
| 159 | + reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy); |
173 | 160 |
|
174 | | - ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size()); |
| 161 | + reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy); |
175 | 162 |
|
176 | | - // Run tests. |
177 | | - executorService.submit(() -> { |
178 | | - try (Connection conn = dataSource.getConnection()) { |
179 | | - TestRunner testRunner = new TestRunner() |
180 | | - .addPathList(testPaths) |
181 | | - .addReporterList(reporterList) |
182 | | - .sourceMappingOptions(sourceMappingOptions[0]) |
183 | | - .testMappingOptions(testMappingOptions[0]) |
184 | | - .colorConsole(this.colorConsole) |
185 | | - .failOnErrors(true) |
186 | | - .skipCompatibilityCheck(skipCompatibilityCheck) |
187 | | - .includeObjects(finalIncludeObjectsList) |
188 | | - .excludeObjects(finalExcludeObjectsList); |
189 | | - |
190 | | - testRunner.run(conn); |
191 | | - } catch (SomeTestsFailedException e) { |
192 | | - returnCode[0] = this.failureExitCode; |
193 | 163 | } catch (SQLException e) { |
194 | | - System.out.println(e.getMessage()); |
195 | | - returnCode[0] = Cli.DEFAULT_ERROR_CODE; |
196 | | - executorService.shutdownNow(); |
| 164 | + if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) { |
| 165 | + throw new DatabaseConnectionFailed(e); |
| 166 | + } else { |
| 167 | + throw e; |
| 168 | + } |
197 | 169 | } |
198 | | - }); |
199 | 170 |
|
200 | | - // Gather each reporter results on a separate thread. |
201 | | - getReporterManager().startReporterGatherers(executorService, dataSource, returnCode); |
| 171 | + // Output a message if --failureExitCode is set but database framework is not capable of |
| 172 | + String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getDatabaseVersion()); |
| 173 | + if (msg != null) { |
| 174 | + System.out.println(msg); |
| 175 | + } |
202 | 176 |
|
203 | | - executorService.shutdown(); |
204 | | - executorService.awaitTermination(60, TimeUnit.MINUTES); |
205 | | - return returnCode[0]; |
206 | | - } |
| 177 | + ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size()); |
| 178 | + |
| 179 | + // Run tests. |
| 180 | + executorService.submit(() -> { |
| 181 | + try (Connection conn = dataSource.getConnection()) { |
| 182 | + TestRunner testRunner = new TestRunner() |
| 183 | + .addPathList(testPaths) |
| 184 | + .addReporterList(reporterList) |
| 185 | + .sourceMappingOptions(sourceMappingOptions[0]) |
| 186 | + .testMappingOptions(testMappingOptions[0]) |
| 187 | + .colorConsole(this.colorConsole) |
| 188 | + .failOnErrors(true) |
| 189 | + .skipCompatibilityCheck(skipCompatibilityCheck) |
| 190 | + .includeObjects(finalIncludeObjectsList) |
| 191 | + .excludeObjects(finalExcludeObjectsList); |
| 192 | + |
| 193 | + testRunner.run(conn); |
| 194 | + } catch (SomeTestsFailedException e) { |
| 195 | + returnCode[0] = this.failureExitCode; |
| 196 | + } catch (SQLException e) { |
| 197 | + System.out.println(e.getMessage()); |
| 198 | + returnCode[0] = Cli.DEFAULT_ERROR_CODE; |
| 199 | + executorService.shutdownNow(); |
| 200 | + } |
| 201 | + }); |
207 | 202 |
|
| 203 | + // Gather each reporter results on a separate thread. |
| 204 | + getReporterManager().startReporterGatherers(executorService, dataSource, returnCode); |
208 | 205 |
|
| 206 | + executorService.shutdown(); |
| 207 | + executorService.awaitTermination(60, TimeUnit.MINUTES); |
| 208 | + return returnCode[0]; |
| 209 | + } |
| 210 | + catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed e ) { |
| 211 | + System.out.println(e.getMessage()); |
| 212 | + } catch (Exception e) { |
| 213 | + e.printStackTrace(); |
| 214 | + } |
| 215 | + return 1; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public String getCommand() { |
| 220 | + return "run"; |
| 221 | + } |
209 | 222 |
|
210 | 223 |
|
211 | 224 | /** Returns FileMapperOptions for the first item of a given param list in a baseDir |
|
0 commit comments