Repository navigation
Expand file tree
/
Copy pathRunCommand.java
More file actions
179 lines (148 loc) · 6.41 KB
/
Copy pathRunCommand.java
File metadata and controls
179 lines (148 loc) · 6.41 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
package io.github.utplsql.cli;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import io.github.utplsql.api.CustomTypes;
import io.github.utplsql.api.OutputBuffer;
import io.github.utplsql.api.TestRunner;
import io.github.utplsql.api.reporter.Reporter;
import io.github.utplsql.api.reporter.ReporterFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Created by vinicius.moreira on 19/04/2017.
*/
@Parameters(separators = "=", commandDescription = "run tests")
public class RunCommand {
@Parameter(
required = true, converter = ConnectionStringConverter.class,
arity = 1,
description = "user/pass@[[host][:port]/]db")
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
@Parameter(
names = {"-p", "--path"},
description = "run suites/tests by path, format: \n" +
"-p=[schema|schema:[suite ...][.test]|schema[.suite ...][.test]")
private List<String> testPaths = new ArrayList<>();
@Parameter(
names = {"-f", "--format"},
variableArity = true,
description = "output reporter format: \n" +
"enables specified format reporting to specified output file (-o) and to screen (-s)\n" +
"-f=reporter_name [-o=output_file [-s]]")
private List<String> reporterParams = new ArrayList<>();
@Parameter(
names = {"-c", "--color"},
description = "enables printing of test results in colors as defined by ANSICONSOLE standards")
private boolean colorConsole = false;
@Parameter(names = {"-source_path"}, description = "path to project source files")
private String sourcePath;
@Parameter(names = {"-test_path"}, description = "path to project test files")
private String testPath;
public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
}
public List<String> getTestPaths() {
return testPaths;
}
public List<ReporterOptions> getReporterOptionsList() {
List<ReporterOptions> reporterOptionsList = new ArrayList<>();
ReporterOptions reporterOptions = null;
for (String p : reporterParams) {
if (reporterOptions == null || !p.startsWith("-")) {
reporterOptions = new ReporterOptions(p);
reporterOptionsList.add(reporterOptions);
}
else
if (p.startsWith("-o=")) {
reporterOptions.setOutputFileName(p.substring(3));
}
else
if (p.equals("-s")) {
reporterOptions.forceOutputToScreen(true);
}
}
// If no reporter parameters were passed, use default reporter.
if (reporterOptionsList.isEmpty()) {
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
}
return reporterOptionsList;
}
public void run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
final List<String> testPaths = getTestPaths();
final List<Reporter> reporterList = new ArrayList<>();
final File baseDir = new File("").getAbsoluteFile();
List<String> sourceFilesTmp = null;
List<String> testFilesTmp = null;
if (this.sourcePath != null)
sourceFilesTmp = new FileWalker().getFileList(baseDir, this.sourcePath);
if (this.testPath != null)
testFilesTmp = new FileWalker().getFileList(baseDir, this.testPath);
final List<String> sourceFiles = sourceFilesTmp;
final List<String> testFiles = testFilesTmp;
if (testPaths.isEmpty()) testPaths.add(ci.getUser());
// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = ci.getConnection()) {
for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}
} catch (SQLException e) {
// TODO
e.printStackTrace();
}
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());
// Run tests.
executorService.submit(() -> {
try (Connection conn = ci.getConnection()){
new TestRunner()
.addPathList(testPaths)
.addReporterList(reporterList)
.withSourceFiles(sourceFiles)
.withTestFiles(testFiles)
.colorConsole(colorConsole)
.run(conn);
} catch (SQLException e) {
// TODO
e.printStackTrace();
}
});
// Gather each reporter results on a separate thread.
for (ReporterOptions ro : reporterOptionsList) {
executorService.submit(() -> {
List<PrintStream> printStreams = new ArrayList<>();
PrintStream fileOutStream = null;
try (Connection conn = ci.getConnection()) {
if (ro.outputToScreen()) {
printStreams.add(System.out);
}
if (ro.outputToFile()) {
fileOutStream = new PrintStream(new FileOutputStream(ro.getOutputFileName()));
printStreams.add(fileOutStream);
}
new OutputBuffer(ro.getReporterObj()).printAvailable(conn, printStreams);
} catch (SQLException | FileNotFoundException e) {
// TODO
e.printStackTrace();
} finally {
if (fileOutStream != null)
fileOutStream.close();
}
});
}
executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
}
}