-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUtPLSQLMojo.java
349 lines (273 loc) · 12 KB
/
UtPLSQLMojo.java
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package org.utplsql.maven.plugin;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.utplsql.api.DBHelper;
import org.utplsql.api.FileMapperOptions;
import org.utplsql.api.JavaApiVersionInfo;
import org.utplsql.api.KeyValuePair;
import org.utplsql.api.TestRunner;
import org.utplsql.api.Version;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.CoreReporters;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.maven.plugin.helper.PluginDefault;
import org.utplsql.maven.plugin.helper.SQLScannerHelper;
import org.utplsql.maven.plugin.model.ReporterParameter;
import org.utplsql.maven.plugin.reporter.ReporterWriter;
/**
* This class expose the {@link TestRunner} interface to Maven.
*
* @author Alberto Hernández
*/
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST)
public class UtPLSQLMojo extends AbstractMojo {
@Parameter(readonly = true, defaultValue = "${project}")
private MavenProject project;
@Parameter(property = "dbUrl")
protected String url;
@Parameter(property = "dbUser")
protected String user;
@Parameter(property = "dbPass")
protected String password;
@Parameter
protected String includeObject;
@Parameter
protected String excludeObject;
@Parameter(defaultValue = "false")
protected boolean skipCompatibilityCheck;
@Parameter
protected List<ReporterParameter> reporters = new ArrayList<>();
@Parameter
protected List<String> paths = new ArrayList<>();
// Sources Configuration
@Parameter
protected List<Resource> sources = new ArrayList<>();
@Parameter
private String sourcesOwner;
@Parameter
private String sourcesRegexExpression;
@Parameter
private Integer sourcesOwnerSubexpression;
@Parameter
private Integer sourcesNameSubexpression;
@Parameter
private Integer sourcesTypeSubexpression;
@Parameter
private List<CustomTypeMapping> sourcesCustomTypeMapping;
// Tests Configuration
@Parameter
protected List<Resource> tests = new ArrayList<>();
@Parameter
private String testsOwner;
@Parameter
private String testsRegexExpression;
@Parameter
private Integer testsOwnerSubexpression;
@Parameter
private Integer testsNameSubexpression;
@Parameter
private Integer testsTypeSubexpression;
@Parameter
private List<CustomTypeMapping> testsCustomTypeMapping;
@Parameter(defaultValue = "${project.build.directory}", readonly = true)
protected String targetDir;
@Parameter(defaultValue = "${maven.test.failure.ignore}")
protected boolean ignoreFailure;
// Color in the console, bases on Maven logging configuration.
private boolean colorConsole = MessageUtils.isColorEnabled();
private ReporterWriter reporterWriter;
/**
* Executes the plugin.
*/
@Override
public void execute() throws MojoExecutionException {
getLog().debug("Java Api Version = " + JavaApiVersionInfo.getVersion());
loadConfFromEnvironment();
Connection connection = null;
try {
FileMapperOptions sourceMappingOptions = buildSourcesOptions();
FileMapperOptions testMappingOptions = buildTestsOptions();
connection = DriverManager.getConnection(url, user, password);
Version utlVersion = DBHelper.getDatabaseFrameworkVersion(connection);
getLog().info("utPLSQL Version = " + utlVersion);
List<Reporter> reporterList = initReporters(connection, utlVersion, ReporterFactory.createEmpty());
logParameters(sourceMappingOptions, testMappingOptions, reporterList);
TestRunner runner = new TestRunner()
.addPathList(paths)
.addReporterList(reporterList)
.sourceMappingOptions(sourceMappingOptions)
.testMappingOptions(testMappingOptions)
.skipCompatibilityCheck(skipCompatibilityCheck)
.colorConsole(colorConsole)
.failOnErrors(!ignoreFailure);
if (StringUtils.isNotBlank(excludeObject)) {
runner.excludeObject(excludeObject);
}
if (StringUtils.isNotBlank(includeObject)) {
runner.includeObject(includeObject);
}
runner.run(connection);
} catch (SomeTestsFailedException e) {
if (!this.ignoreFailure) {
throw new MojoExecutionException(e.getMessage(), e);
}
} catch (SQLException e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
try {
if (null != connection) {
reporterWriter.writeReporters(connection);
}
} catch (Exception e) {
getLog().error(e.getMessage(), e);
}
}
}
private void loadConfFromEnvironment() {
if (StringUtils.isEmpty(url)) {
url = System.getProperty("dbUrl");
}
if (StringUtils.isEmpty(user)) {
user = System.getProperty("dbUser");
}
if (StringUtils.isEmpty(password)) {
password = System.getProperty("dbPass");
}
}
private FileMapperOptions buildSourcesOptions() throws MojoExecutionException {
try {
if (sources.isEmpty()) {
File defaultSourceDirectory = new File(project.getBasedir(), PluginDefault.SOURCE_DIRECTORY);
if (defaultSourceDirectory.exists()) {
sources.add(PluginDefault.buildDefaultSource());
} else {
return new FileMapperOptions(new ArrayList<String>());
}
}
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(), sources,
PluginDefault.SOURCE_DIRECTORY, PluginDefault.SOURCE_FILE_PATTERN);
FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts);
if (StringUtils.isNotEmpty(sourcesOwner)) {
fileMapperOptions.setObjectOwner(sourcesOwner);
}
if (StringUtils.isNotEmpty(sourcesRegexExpression)) {
fileMapperOptions.setRegexPattern(sourcesRegexExpression);
}
if (sourcesOwnerSubexpression != null) {
fileMapperOptions.setOwnerSubExpression(sourcesOwnerSubexpression);
}
if (sourcesNameSubexpression != null) {
fileMapperOptions.setNameSubExpression(sourcesNameSubexpression);
}
if (sourcesTypeSubexpression != null) {
fileMapperOptions.setTypeSubExpression(sourcesTypeSubexpression);
}
if (sourcesCustomTypeMapping != null && !sourcesCustomTypeMapping.isEmpty()) {
fileMapperOptions.setTypeMappings(new ArrayList<KeyValuePair>());
for (CustomTypeMapping typeMapping : sourcesCustomTypeMapping) {
fileMapperOptions.getTypeMappings()
.add(new KeyValuePair(typeMapping.getCustomMapping(), typeMapping.getType()));
}
}
return fileMapperOptions;
} catch (Exception e) {
throw new MojoExecutionException("Invalid <SOURCES> in your pom.xml", e);
}
}
private FileMapperOptions buildTestsOptions() throws MojoExecutionException {
try {
if (tests.isEmpty()) {
File defaultTestDirectory = new File(project.getBasedir(), PluginDefault.TEST_DIRECTORY);
if (defaultTestDirectory.exists()) {
tests.add(PluginDefault.buildDefaultTest());
} else {
return new FileMapperOptions(new ArrayList<String>());
}
}
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(), tests, PluginDefault.TEST_DIRECTORY,
PluginDefault.TEST_FILE_PATTERN);
FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts);
if (StringUtils.isNotEmpty(testsOwner)) {
fileMapperOptions.setObjectOwner(testsOwner);
}
if (StringUtils.isNotEmpty(testsRegexExpression)) {
fileMapperOptions.setRegexPattern(testsRegexExpression);
}
if (testsOwnerSubexpression != null) {
fileMapperOptions.setOwnerSubExpression(testsOwnerSubexpression);
}
if (testsNameSubexpression != null) {
fileMapperOptions.setNameSubExpression(testsNameSubexpression);
}
if (testsTypeSubexpression != null) {
fileMapperOptions.setTypeSubExpression(testsTypeSubexpression);
}
if (testsCustomTypeMapping != null && !testsCustomTypeMapping.isEmpty()) {
fileMapperOptions.setTypeMappings(new ArrayList<KeyValuePair>());
for (CustomTypeMapping typeMapping : testsCustomTypeMapping) {
fileMapperOptions.getTypeMappings()
.add(new KeyValuePair(typeMapping.getCustomMapping(), typeMapping.getType()));
}
}
return fileMapperOptions;
} catch (Exception e) {
throw new MojoExecutionException("Invalid <TESTS> in your pom.xml: " + e.getMessage());
}
}
private List<Reporter> initReporters(Connection connection, Version utlVersion, ReporterFactory reporterFactory)
throws SQLException {
List<Reporter> reporterList = new ArrayList<>();
reporterWriter = new ReporterWriter(targetDir, utlVersion);
if (reporters.isEmpty()) {
ReporterParameter reporterParameter = new ReporterParameter();
reporterParameter.setConsoleOutput(true);
reporterParameter.setName(CoreReporters.UT_DOCUMENTATION_REPORTER.name());
reporters.add(reporterParameter);
}
for (ReporterParameter reporterParameter : reporters) {
Reporter reporter = reporterFactory.createReporter(reporterParameter.getName());
reporter.init(connection);
reporterList.add(reporter);
// Turns the console output on by default if both file and console output are
// empty.
if (!reporterParameter.isFileOutput() && null == reporterParameter.getConsoleOutput()) {
reporterParameter.setConsoleOutput(true);
}
// Only added the reporter if at least one of the output is required
if (StringUtils.isNotBlank(reporterParameter.getFileOutput()) || reporterParameter.isConsoleOutput()) {
reporterWriter.addReporter(reporterParameter, reporter);
}
}
return reporterList;
}
private void logParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions,
List<Reporter> reporterList) {
Log log = getLog();
log.info("Invoking TestRunner with: " + targetDir);
if (!log.isDebugEnabled()) {
return;
}
log.debug("Invoking TestRunner with: ");
log.debug("reporters=");
reporterList.forEach((Reporter r) -> log.debug(r.getTypeName()));
log.debug("sources=");
sourceMappingOptions.getFilePaths().forEach(log::debug);
log.debug("tests=");
testMappingOptions.getFilePaths().forEach(log::debug);
}
}