diff --git a/pom.xml b/pom.xml index 72f3b24..f0f5b41 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ com.beust jcommander - 1.60 + 1.69 compile diff --git a/src/main/java/io/github/utplsql/cli/ConnectionInfo.java b/src/main/java/io/github/utplsql/cli/ConnectionInfo.java index ce540e3..4523537 100644 --- a/src/main/java/io/github/utplsql/cli/ConnectionInfo.java +++ b/src/main/java/io/github/utplsql/cli/ConnectionInfo.java @@ -2,6 +2,9 @@ import com.beust.jcommander.ParameterException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,6 +35,10 @@ public class ConnectionInfo { public ConnectionInfo() { } + public Connection getConnection() throws SQLException { + return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword()); + } + public ConnectionInfo parseConnectionString(String connectionString) throws ParameterException { Pattern p = Pattern.compile(CONNSTR_PATTERN); Matcher m = p.matcher(connectionString); diff --git a/src/main/java/io/github/utplsql/cli/ReporterOptions.java b/src/main/java/io/github/utplsql/cli/ReporterOptions.java new file mode 100644 index 0000000..61eb930 --- /dev/null +++ b/src/main/java/io/github/utplsql/cli/ReporterOptions.java @@ -0,0 +1,65 @@ +package io.github.utplsql.cli; + +import io.github.utplsql.api.reporter.Reporter; + +/** + * Created by Vinicius on 20/05/2017. + */ +public class ReporterOptions { + + private String reporterName; + private String outputFileName; + private boolean outputToScreen; + private boolean forceOutputToScreen; + + private Reporter reporterObj = null; + + public ReporterOptions(String reporterName, String outputFileName, boolean outputToScreen) { + setReporterName(reporterName); + setOutputFileName(outputFileName); + this.outputToScreen = outputToScreen; + this.forceOutputToScreen = false; + } + + public ReporterOptions(String reporterName) { + this(reporterName, null, true); + } + + public Reporter getReporterObj() { + return reporterObj; + } + + public void setReporterObj(Reporter reporterObj) { + this.reporterObj = reporterObj; + } + + public String getReporterName() { + return reporterName.toUpperCase(); + } + + public void setReporterName(String reporterName) { + this.reporterName = reporterName; + } + + public String getOutputFileName() { + return outputFileName; + } + + public void setOutputFileName(String outputFileName) { + this.outputFileName = outputFileName; + this.outputToScreen = false; + } + + public boolean outputToFile() { + return outputFileName != null && !outputFileName.isEmpty(); + } + + public boolean outputToScreen() { + return outputToScreen || forceOutputToScreen; + } + + public void forceOutputToScreen(boolean outputToScreen) { + this.forceOutputToScreen = outputToScreen; + } + +} diff --git a/src/main/java/io/github/utplsql/cli/RunCommand.java b/src/main/java/io/github/utplsql/cli/RunCommand.java index 8777e2d..39e2299 100644 --- a/src/main/java/io/github/utplsql/cli/RunCommand.java +++ b/src/main/java/io/github/utplsql/cli/RunCommand.java @@ -2,15 +2,18 @@ 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.OutputBufferLines; import io.github.utplsql.api.TestRunner; -import io.github.utplsql.api.types.BaseReporter; -import io.github.utplsql.api.types.DocumentationReporter; -import io.github.utplsql.api.utPLSQL; +import io.github.utplsql.api.reporter.Reporter; +import io.github.utplsql.api.reporter.ReporterFactory; +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; @@ -24,98 +27,124 @@ public class RunCommand { @Parameter( required = true, converter = ConnectionStringConverter.class, + arity = 1, description = "user/pass@[[host][:port]/]db") - private List connectionInfoList; + private List connectionInfoList = new ArrayList<>(); @Parameter( names = {"-p", "--path"}, description = "run suites/tests by path, format: \n" + - "schema or schema:[suite ...][.test] or schema[.suite ...][.test]") - private List testPaths; + "-p schema or schema:[suite ...][.test] or schema[.suite ...][.test]") + private List testPaths = new ArrayList<>(); + + @Parameter( + names = {"-f", "--format"}, + variableArity = true, + description = "output reporter format: \n" + + "-f reporter_name [output_file] [console_output]") + private List reporterParams = new ArrayList<>(); 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."); + public List getTestPaths() { + return testPaths; + } + + public List getReporterOptionsList() { + List 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 (testPaths == null) ? null : String.join(",", testPaths); + return reporterOptionsList; } public void run() throws Exception { - ConnectionInfo ci = getConnectionInfo(); + final ConnectionInfo ci = getConnectionInfo(); System.out.println("Running Tests For: " + ci.toString()); - utPLSQL.init(ci.getConnectionUrl(), ci.getUser(), ci.getPassword()); + final List reporterOptionsList = getReporterOptionsList(); + final List reporterList = new ArrayList<>(); + final List testPaths = getTestPaths(); - String tempTestPaths = getTestPaths(); - if (tempTestPaths == null) tempTestPaths = ci.getUser(); + if (testPaths.isEmpty()) testPaths.add(ci.getUser()); - final BaseReporter reporter = createDocumentationReporter(); - final String testPaths = tempTestPaths; + // 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(2); + ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size()); executorService.submit(() -> { - Connection conn = null; - try { - conn = utPLSQL.getConnection(); - new TestRunner().run(conn, testPaths, reporter); - - OutputBufferLines outputLines = new OutputBuffer(reporter.getReporterId()) - .fetchAll(conn); - - if (outputLines.getLines().size() > 0) - System.out.println(outputLines.toString()); + try (Connection conn = ci.getConnection()){ + new TestRunner() + .addPathList(testPaths) + .addReporterList(reporterList) + .run(conn); } catch (SQLException e) { // TODO e.printStackTrace(); - } finally { - if (conn != null) - try { conn.close(); } catch (SQLException ignored) {} } }); -// executorService.submit(() -> { -// Connection conn = null; -// try { -// conn = utPLSQL.getConnection(); -// OutputBufferLines outputLines; -// do { -// outputLines = new OutputBuffer(reporter.getReporterId()) -// .fetchAvailable(conn); -// -// Thread.sleep(500); -// -// if (outputLines.getLines().size() > 0) -// System.out.println(outputLines.toString()); -// } while (!outputLines.isFinished()); -// } catch (SQLException | InterruptedException e) { -// // TODO -// e.printStackTrace(); -// } finally { -// if (conn != null) -// try { conn.close(); } catch (SQLException ignored) {} -// } -// }); + + for (ReporterOptions ro : reporterOptionsList) { + executorService.submit(() -> { + List 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); } - private BaseReporter createDocumentationReporter() throws SQLException { - Connection conn = null; - try { - conn = utPLSQL.getConnection(); - BaseReporter reporter = new DocumentationReporter(); - reporter.setReporterId(utPLSQL.newSysGuid(conn)); - return reporter; - } finally { - if (conn != null) - try { conn.close(); } catch (SQLException ignored) {} - } - } - } diff --git a/src/test/java/io/github/utplsql/cli/CliTest.java b/src/test/java/io/github/utplsql/cli/CliTest.java deleted file mode 100644 index b96e63f..0000000 --- a/src/test/java/io/github/utplsql/cli/CliTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.github.utplsql.cli; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Unit test for simple Cli. - */ -public class CliTest { - - @Test - public void dummyTest() { - Assert.assertTrue(true); - } - -} diff --git a/src/test/java/io/github/utplsql/cli/RunCommandTest.java b/src/test/java/io/github/utplsql/cli/RunCommandTest.java new file mode 100644 index 0000000..9184234 --- /dev/null +++ b/src/test/java/io/github/utplsql/cli/RunCommandTest.java @@ -0,0 +1,99 @@ +package io.github.utplsql.cli; + +import com.beust.jcommander.JCommander; +import io.github.utplsql.api.CustomTypes; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * Unit test for run command. + */ +public class RunCommandTest { + + private RunCommand createCommand(String... args) { + RunCommand runCmd = new RunCommand(); + + JCommander.newBuilder() + .addObject(runCmd) + .args(args) + .build(); + + return runCmd; + } + + @Test + public void reporterOptions_Default() { + RunCommand runCmd = createCommand("run", "app/app"); + + List reporterOptionsList = runCmd.getReporterOptionsList(); + + ReporterOptions reporterOptions1 = reporterOptionsList.get(0); + Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + Assert.assertNull(reporterOptions1.getOutputFileName()); + Assert.assertFalse(reporterOptions1.outputToFile()); + Assert.assertTrue(reporterOptions1.outputToScreen()); + } + + @Test + public void reporterOptions_OneReporter() { + RunCommand runCmd = createCommand("run", "app/app", "-f=ut_documentation_reporter", "-o=output.txt"); + + List reporterOptionsList = runCmd.getReporterOptionsList(); + + ReporterOptions reporterOptions1 = reporterOptionsList.get(0); + Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + Assert.assertTrue(reporterOptions1.outputToFile()); + Assert.assertFalse(reporterOptions1.outputToScreen()); + } + + @Test + public void reporterOptions_OneReporterForceScreen() { + RunCommand runCmd = createCommand("run", "app/app", "-f=ut_documentation_reporter", "-o=output.txt", "-s"); + + List reporterOptionsList = runCmd.getReporterOptionsList(); + + ReporterOptions reporterOptions1 = reporterOptionsList.get(0); + Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + Assert.assertTrue(reporterOptions1.outputToFile()); + Assert.assertTrue(reporterOptions1.outputToScreen()); + } + + @Test + public void reporterOptions_OneReporterForceScreenInverse() { + RunCommand runCmd = createCommand("run", "app/app", "-f=ut_documentation_reporter", "-s", "-o=output.txt"); + + List reporterOptionsList = runCmd.getReporterOptionsList(); + + ReporterOptions reporterOptions1 = reporterOptionsList.get(0); + Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + Assert.assertTrue(reporterOptions1.outputToFile()); + Assert.assertTrue(reporterOptions1.outputToScreen()); + } + + @Test + public void reporterOptions_TwoReporters() { + RunCommand runCmd = createCommand("run", "app/app", + "-f=ut_documentation_reporter", + "-f=ut_coverage_html_reporter", "-o=coverage.html", "-s"); + + List reporterOptionsList = runCmd.getReporterOptionsList(); + + ReporterOptions reporterOptions1 = reporterOptionsList.get(0); + 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, reporterOptions2.getReporterName()); + Assert.assertEquals(reporterOptions2.getOutputFileName(), "coverage.html"); + Assert.assertTrue(reporterOptions2.outputToFile()); + Assert.assertTrue(reporterOptions2.outputToScreen()); + } + +}