Skip to content

Commit c2f6a32

Browse files
authored
Merge pull request #3 from viniciusam/develop
Lib update, support for multiple reporters and api changes
2 parents 28c87e9 + 07275ca commit c2f6a32

File tree

6 files changed

+267
-83
lines changed

6 files changed

+267
-83
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>com.beust</groupId>
2222
<artifactId>jcommander</artifactId>
23-
<version>1.60</version>
23+
<version>1.69</version>
2424
<scope>compile</scope>
2525
</dependency>
2626
<dependency>

src/main/java/io/github/utplsql/cli/ConnectionInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.beust.jcommander.ParameterException;
44

5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
58
import java.util.regex.Matcher;
69
import java.util.regex.Pattern;
710

@@ -32,6 +35,10 @@ public class ConnectionInfo {
3235
public ConnectionInfo() {
3336
}
3437

38+
public Connection getConnection() throws SQLException {
39+
return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword());
40+
}
41+
3542
public ConnectionInfo parseConnectionString(String connectionString) throws ParameterException {
3643
Pattern p = Pattern.compile(CONNSTR_PATTERN);
3744
Matcher m = p.matcher(connectionString);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.github.utplsql.cli;
2+
3+
import io.github.utplsql.api.reporter.Reporter;
4+
5+
/**
6+
* Created by Vinicius on 20/05/2017.
7+
*/
8+
public class ReporterOptions {
9+
10+
private String reporterName;
11+
private String outputFileName;
12+
private boolean outputToScreen;
13+
private boolean forceOutputToScreen;
14+
15+
private Reporter reporterObj = null;
16+
17+
public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) {
18+
setReporterName(reporterName);
19+
setOutputFileName(outputFileName);
20+
this.outputToScreen = outputToScreen;
21+
this.forceOutputToScreen = false;
22+
}
23+
24+
public ReporterOptions(String reporterName) {
25+
this(reporterName, null, true);
26+
}
27+
28+
public Reporter getReporterObj() {
29+
return reporterObj;
30+
}
31+
32+
public void setReporterObj(Reporter reporterObj) {
33+
this.reporterObj = reporterObj;
34+
}
35+
36+
public String getReporterName() {
37+
return reporterName.toUpperCase();
38+
}
39+
40+
public void setReporterName(String reporterName) {
41+
this.reporterName = reporterName;
42+
}
43+
44+
public String getOutputFileName() {
45+
return outputFileName;
46+
}
47+
48+
public void setOutputFileName(String outputFileName) {
49+
this.outputFileName = outputFileName;
50+
this.outputToScreen = false;
51+
}
52+
53+
public boolean outputToFile() {
54+
return outputFileName != null && !outputFileName.isEmpty();
55+
}
56+
57+
public boolean outputToScreen() {
58+
return outputToScreen || forceOutputToScreen;
59+
}
60+
61+
public void forceOutputToScreen(boolean outputToScreen) {
62+
this.forceOutputToScreen = outputToScreen;
63+
}
64+
65+
}

src/main/java/io/github/utplsql/cli/RunCommand.java

Lines changed: 95 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import com.beust.jcommander.Parameter;
44
import com.beust.jcommander.Parameters;
5+
import io.github.utplsql.api.CustomTypes;
56
import io.github.utplsql.api.OutputBuffer;
6-
import io.github.utplsql.api.OutputBufferLines;
77
import io.github.utplsql.api.TestRunner;
8-
import io.github.utplsql.api.types.BaseReporter;
9-
import io.github.utplsql.api.types.DocumentationReporter;
10-
import io.github.utplsql.api.utPLSQL;
8+
import io.github.utplsql.api.reporter.Reporter;
9+
import io.github.utplsql.api.reporter.ReporterFactory;
1110

11+
import java.io.FileNotFoundException;
12+
import java.io.FileOutputStream;
13+
import java.io.PrintStream;
1214
import java.sql.Connection;
1315
import java.sql.SQLException;
16+
import java.util.ArrayList;
1417
import java.util.List;
1518
import java.util.concurrent.ExecutorService;
1619
import java.util.concurrent.Executors;
@@ -24,98 +27,124 @@ public class RunCommand {
2427

2528
@Parameter(
2629
required = true, converter = ConnectionStringConverter.class,
30+
arity = 1,
2731
description = "user/pass@[[host][:port]/]db")
28-
private List<ConnectionInfo> connectionInfoList;
32+
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
2933

3034
@Parameter(
3135
names = {"-p", "--path"},
3236
description = "run suites/tests by path, format: \n" +
33-
"schema or schema:[suite ...][.test] or schema[.suite ...][.test]")
34-
private List<String> testPaths;
37+
"-p schema or schema:[suite ...][.test] or schema[.suite ...][.test]")
38+
private List<String> testPaths = new ArrayList<>();
39+
40+
@Parameter(
41+
names = {"-f", "--format"},
42+
variableArity = true,
43+
description = "output reporter format: \n" +
44+
"-f reporter_name [output_file] [console_output]")
45+
private List<String> reporterParams = new ArrayList<>();
3546

3647
public ConnectionInfo getConnectionInfo() {
3748
return connectionInfoList.get(0);
3849
}
3950

40-
public String getTestPaths() {
41-
// if (testPaths != null && testPaths.size() > 1)
42-
// throw new RuntimeException("Multiple test paths not supported yet.");
51+
public List<String> getTestPaths() {
52+
return testPaths;
53+
}
54+
55+
public List<ReporterOptions> getReporterOptionsList() {
56+
List<ReporterOptions> reporterOptionsList = new ArrayList<>();
57+
ReporterOptions reporterOptions = null;
58+
59+
for (String p : reporterParams) {
60+
if (reporterOptions == null || !p.startsWith("-")) {
61+
reporterOptions = new ReporterOptions(p);
62+
reporterOptionsList.add(reporterOptions);
63+
}
64+
else
65+
if (p.startsWith("-o=")) {
66+
reporterOptions.setOutputFileName(p.substring(3));
67+
}
68+
else
69+
if (p.equals("-s")) {
70+
reporterOptions.forceOutputToScreen(true);
71+
}
72+
}
73+
74+
// If no reporter parameters were passed, use default reporter.
75+
if (reporterOptionsList.isEmpty()) {
76+
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
77+
}
4378

44-
return (testPaths == null) ? null : String.join(",", testPaths);
79+
return reporterOptionsList;
4580
}
4681

4782
public void run() throws Exception {
48-
ConnectionInfo ci = getConnectionInfo();
83+
final ConnectionInfo ci = getConnectionInfo();
4984
System.out.println("Running Tests For: " + ci.toString());
5085

51-
utPLSQL.init(ci.getConnectionUrl(), ci.getUser(), ci.getPassword());
86+
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
87+
final List<Reporter> reporterList = new ArrayList<>();
88+
final List<String> testPaths = getTestPaths();
5289

53-
String tempTestPaths = getTestPaths();
54-
if (tempTestPaths == null) tempTestPaths = ci.getUser();
90+
if (testPaths.isEmpty()) testPaths.add(ci.getUser());
5591

56-
final BaseReporter reporter = createDocumentationReporter();
57-
final String testPaths = tempTestPaths;
92+
// Do the reporters initialization, so we can use the id to run and gather results.
93+
try (Connection conn = ci.getConnection()) {
94+
for (ReporterOptions ro : reporterOptionsList) {
95+
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
96+
reporter.init(conn);
97+
ro.setReporterObj(reporter);
98+
reporterList.add(reporter);
99+
}
100+
} catch (SQLException e) {
101+
// TODO
102+
e.printStackTrace();
103+
}
58104

59-
ExecutorService executorService = Executors.newFixedThreadPool(2);
105+
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());
60106

61107
executorService.submit(() -> {
62-
Connection conn = null;
63-
try {
64-
conn = utPLSQL.getConnection();
65-
new TestRunner().run(conn, testPaths, reporter);
66-
67-
OutputBufferLines outputLines = new OutputBuffer(reporter.getReporterId())
68-
.fetchAll(conn);
69-
70-
if (outputLines.getLines().size() > 0)
71-
System.out.println(outputLines.toString());
108+
try (Connection conn = ci.getConnection()){
109+
new TestRunner()
110+
.addPathList(testPaths)
111+
.addReporterList(reporterList)
112+
.run(conn);
72113
} catch (SQLException e) {
73114
// TODO
74115
e.printStackTrace();
75-
} finally {
76-
if (conn != null)
77-
try { conn.close(); } catch (SQLException ignored) {}
78116
}
79117
});
80118

81-
// executorService.submit(() -> {
82-
// Connection conn = null;
83-
// try {
84-
// conn = utPLSQL.getConnection();
85-
// OutputBufferLines outputLines;
86-
// do {
87-
// outputLines = new OutputBuffer(reporter.getReporterId())
88-
// .fetchAvailable(conn);
89-
//
90-
// Thread.sleep(500);
91-
//
92-
// if (outputLines.getLines().size() > 0)
93-
// System.out.println(outputLines.toString());
94-
// } while (!outputLines.isFinished());
95-
// } catch (SQLException | InterruptedException e) {
96-
// // TODO
97-
// e.printStackTrace();
98-
// } finally {
99-
// if (conn != null)
100-
// try { conn.close(); } catch (SQLException ignored) {}
101-
// }
102-
// });
119+
120+
for (ReporterOptions ro : reporterOptionsList) {
121+
executorService.submit(() -> {
122+
List<PrintStream> printStreams = new ArrayList<>();
123+
PrintStream fileOutStream = null;
124+
125+
try (Connection conn = ci.getConnection()) {
126+
if (ro.outputToScreen()) {
127+
printStreams.add(System.out);
128+
}
129+
130+
if (ro.outputToFile()) {
131+
fileOutStream = new PrintStream(new FileOutputStream(ro.getOutputFileName()));
132+
printStreams.add(fileOutStream);
133+
}
134+
135+
new OutputBuffer(ro.getReporterObj()).printAvailable(conn, printStreams);
136+
} catch (SQLException | FileNotFoundException e) {
137+
// TODO
138+
e.printStackTrace();
139+
} finally {
140+
if (fileOutStream != null)
141+
fileOutStream.close();
142+
}
143+
});
144+
}
103145

104146
executorService.shutdown();
105147
executorService.awaitTermination(60, TimeUnit.MINUTES);
106148
}
107149

108-
private BaseReporter createDocumentationReporter() throws SQLException {
109-
Connection conn = null;
110-
try {
111-
conn = utPLSQL.getConnection();
112-
BaseReporter reporter = new DocumentationReporter();
113-
reporter.setReporterId(utPLSQL.newSysGuid(conn));
114-
return reporter;
115-
} finally {
116-
if (conn != null)
117-
try { conn.close(); } catch (SQLException ignored) {}
118-
}
119-
}
120-
121150
}

src/test/java/io/github/utplsql/cli/CliTest.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)