22
33import com .beust .jcommander .Parameter ;
44import com .beust .jcommander .Parameters ;
5+ import io .github .utplsql .api .CustomTypes ;
56import io .github .utplsql .api .OutputBuffer ;
6- import io .github .utplsql .api .OutputBufferLines ;
77import 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 ;
1214import java .sql .Connection ;
1315import java .sql .SQLException ;
16+ import java .util .ArrayList ;
1417import java .util .List ;
1518import java .util .concurrent .ExecutorService ;
1619import 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}
0 commit comments