|
| 1 | +package io.github.utplsql.cli; |
| 2 | + |
| 3 | +import com.beust.jcommander.Parameter; |
| 4 | +import com.beust.jcommander.Parameters; |
| 5 | +import com.sun.deploy.util.StringUtils; |
| 6 | +import io.github.utplsql.api.OutputBuffer; |
| 7 | +import io.github.utplsql.api.OutputBufferLines; |
| 8 | +import io.github.utplsql.api.TestRunner; |
| 9 | +import io.github.utplsql.api.utPLSQL; |
| 10 | +import io.github.utplsql.api.types.BaseReporter; |
| 11 | +import io.github.utplsql.api.types.DocumentationReporter; |
| 12 | + |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.SQLException; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.ExecutorService; |
| 17 | +import java.util.concurrent.Executors; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by vinicius.moreira on 19/04/2017. |
| 22 | + */ |
| 23 | +@Parameters(separators = "=", commandDescription = "run tests") |
| 24 | +public class RunCommand { |
| 25 | + |
| 26 | + @Parameter( |
| 27 | + required = true, converter = ConnectionStringConverter.class, |
| 28 | + description = "user/pass@[[host][:port]/]sid") |
| 29 | + private List<ConnectionInfo> connectionInfoList; |
| 30 | + |
| 31 | + @Parameter( |
| 32 | + names = {"-p", "--path"}, |
| 33 | + description = "run suites/tests by path, format: \n" + |
| 34 | + "schema or schema:[suite ...][.test] or schema[.suite ...][.test]") |
| 35 | + private List<String> testPaths; |
| 36 | + |
| 37 | + public ConnectionInfo getConnectionInfo() { |
| 38 | + return connectionInfoList.get(0); |
| 39 | + } |
| 40 | + |
| 41 | + public String getTestPaths() { |
| 42 | + if (testPaths != null && testPaths.size() > 1) |
| 43 | + throw new RuntimeException("Multiple test paths not supported yet."); |
| 44 | + |
| 45 | + return (testPaths == null) ? null : StringUtils.join(testPaths, ","); |
| 46 | + } |
| 47 | + |
| 48 | + public void run() throws Exception { |
| 49 | + ConnectionInfo ci = getConnectionInfo(); |
| 50 | + System.out.println("Running Tests For: " + ci.toString()); |
| 51 | + |
| 52 | + utPLSQL.init(ci.getConnectionUrl(), ci.getUser(), ci.getPassword()); |
| 53 | + |
| 54 | + String tempTestPaths = getTestPaths(); |
| 55 | + if (tempTestPaths == null) tempTestPaths = ci.getUser(); |
| 56 | + |
| 57 | + final BaseReporter reporter = createDocumentationReporter(); |
| 58 | + final String testPaths = tempTestPaths; |
| 59 | + |
| 60 | + ExecutorService executorService = Executors.newFixedThreadPool(2); |
| 61 | + |
| 62 | + executorService.submit(() -> { |
| 63 | + Connection conn = null; |
| 64 | + try { |
| 65 | + conn = utPLSQL.getConnection(); |
| 66 | + new TestRunner().run(conn, testPaths, reporter); |
| 67 | + } catch (SQLException e) { |
| 68 | + // TODO |
| 69 | + e.printStackTrace(); |
| 70 | + } finally { |
| 71 | + if (conn != null) |
| 72 | + try { conn.close(); } catch (SQLException ignored) {} |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + executorService.submit(() -> { |
| 77 | + Connection conn = null; |
| 78 | + try { |
| 79 | + conn = utPLSQL.getConnection(); |
| 80 | + OutputBufferLines outputLines; |
| 81 | + do { |
| 82 | + outputLines = new OutputBuffer(reporter.getReporterId()) |
| 83 | + .fetchAvailable(conn); |
| 84 | + |
| 85 | + Thread.sleep(500); |
| 86 | + |
| 87 | + if (outputLines.getLines().size() > 0) |
| 88 | + System.out.println(outputLines.toString()); |
| 89 | + } while (!outputLines.isFinished()); |
| 90 | + } catch (SQLException e) { |
| 91 | + // TODO |
| 92 | + e.printStackTrace(); |
| 93 | + } catch (InterruptedException ignored) { |
| 94 | + // ignored |
| 95 | + } finally { |
| 96 | + if (conn != null) |
| 97 | + try { conn.close(); } catch (SQLException ignored) {} |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + executorService.shutdown(); |
| 102 | + executorService.awaitTermination(60, TimeUnit.MINUTES); |
| 103 | + } |
| 104 | + |
| 105 | + private BaseReporter createDocumentationReporter() throws SQLException { |
| 106 | + Connection conn = null; |
| 107 | + try { |
| 108 | + conn = utPLSQL.getConnection(); |
| 109 | + BaseReporter reporter = new DocumentationReporter(); |
| 110 | + reporter.setReporterId(utPLSQL.newSysGuid(conn)); |
| 111 | + return reporter; |
| 112 | + } finally { |
| 113 | + if (conn != null) |
| 114 | + try { conn.close(); } catch (SQLException ignored) {} |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments