Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for multiple reporters
  • Loading branch information
viniciusam committed May 20, 2017
commit 4f1480288ebe5e55e6c87e8f7a2c775f0bd597ae
17 changes: 13 additions & 4 deletions src/main/java/io/github/utplsql/cli/ReporterOptions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.utplsql.cli;

import com.sun.istack.internal.NotNull;
import io.github.utplsql.api.types.BaseReporter;

/**
* Created by Vinicius on 20/05/2017.
Expand All @@ -12,6 +12,8 @@ public class ReporterOptions {
private boolean outputToScreen;
private boolean forceOutputToScreen;

private BaseReporter reporterObj = null;

public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) {
setReporterName(reporterName);
setOutputFileName(outputFileName);
Expand All @@ -23,13 +25,20 @@ public ReporterOptions(String reporterName) {
this(reporterName, null, true);
}

public BaseReporter getReporterObj() {
return reporterObj;
}

public void setReporterObj(BaseReporter reporterObj) {
this.reporterObj = reporterObj;
}

public String getReporterName() {
return reporterName;
return reporterName.toUpperCase();
}

@NotNull
public void setReporterName(String reporterName) {
this.reporterName = reporterName.toUpperCase();
this.reporterName = reporterName;
}

public String getOutputFileName() {
Expand Down
67 changes: 45 additions & 22 deletions src/main/java/io/github/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import io.github.utplsql.api.TestRunner;
import io.github.utplsql.api.types.BaseReporter;
import io.github.utplsql.api.types.CustomTypes;
import io.github.utplsql.api.types.DocumentationReporter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,11 +47,8 @@ public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
}

public String getTestPaths() {
// if (testPaths != null && testPaths.size() > 1)
// throw new RuntimeException("Multiple test paths not supported yet.");

return (testPaths == null) ? null : String.join(",", testPaths);
public List<String> getTestPaths() {
return testPaths;
}

public List<ReporterOptions> getReporterOptionsList() {
Expand All @@ -73,7 +72,7 @@ public List<ReporterOptions> getReporterOptionsList() {

// If no reporter parameters were passed, use default reporter.
if (reporterOptionsList.isEmpty()) {
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER.getName()));
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
}

return reporterOptionsList;
Expand All @@ -83,38 +82,62 @@ public void run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();
System.out.println("Running Tests For: " + ci.toString());

String tempTestPaths = getTestPaths();
if (tempTestPaths == null) tempTestPaths = ci.getUser();
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
final List<BaseReporter> reporterList = new ArrayList<>();
final List<String> testPaths = getTestPaths();

final BaseReporter reporter = new DocumentationReporter();
final String testPaths = tempTestPaths;
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()) {
reporter.init(conn);
for (ReporterOptions ro : reporterOptionsList) {
BaseReporter reporter = CustomTypes.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}
} catch (SQLException e) {
// TODO
e.printStackTrace();
}

ExecutorService executorService = Executors.newFixedThreadPool(2);
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());

executorService.submit(() -> {
try (Connection conn = ci.getConnection()){
new TestRunner().run(conn, testPaths, reporter);
new TestRunner().run(conn, testPaths, reporterList);
} catch (SQLException e) {
// TODO
e.printStackTrace();
}
});

executorService.submit(() -> {
try (Connection conn = ci.getConnection()){
new OutputBuffer(reporter).printAvailable(conn, System.out);
} catch (SQLException e) {
// TODO
e.printStackTrace();
}
});

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);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/io/github/utplsql/cli/RunCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void reporterOptions_Default() {
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
Assert.assertNull(reporterOptions1.getOutputFileName());
Assert.assertFalse(reporterOptions1.outputToFile());
Assert.assertTrue(reporterOptions1.outputToScreen());
Expand All @@ -43,7 +43,7 @@ public void reporterOptions_OneReporter() {
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
Assert.assertTrue(reporterOptions1.outputToFile());
Assert.assertFalse(reporterOptions1.outputToScreen());
Expand All @@ -56,7 +56,7 @@ public void reporterOptions_OneReporterForceScreen() {
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
Assert.assertTrue(reporterOptions1.outputToFile());
Assert.assertTrue(reporterOptions1.outputToScreen());
Expand All @@ -69,7 +69,7 @@ public void reporterOptions_OneReporterForceScreenInverse() {
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt");
Assert.assertTrue(reporterOptions1.outputToFile());
Assert.assertTrue(reporterOptions1.outputToScreen());
Expand All @@ -84,13 +84,13 @@ public void reporterOptions_TwoReporters() {
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

ReporterOptions reporterOptions1 = reporterOptionsList.get(0);
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), reporterOptions1.getReporterName());
Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName());
Assert.assertNull(reporterOptions1.getOutputFileName());
Assert.assertFalse(reporterOptions1.outputToFile());
Assert.assertTrue(reporterOptions1.outputToScreen());

ReporterOptions reporterOptions2 = reporterOptionsList.get(1);
Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER.getName(), reporterOptions2.getReporterName());
Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER, reporterOptions2.getReporterName());
Assert.assertEquals(reporterOptions2.getOutputFileName(), "coverage.html");
Assert.assertTrue(reporterOptions2.outputToFile());
Assert.assertTrue(reporterOptions2.outputToScreen());
Expand Down