33import com .beust .jcommander .Parameter ;
44import com .beust .jcommander .Parameters ;
55import org .utplsql .api .*;
6+ import org .utplsql .api .exception .DatabaseNotCompatibleException ;
67import org .utplsql .api .exception .SomeTestsFailedException ;
78import org .utplsql .api .reporter .Reporter ;
89import org .utplsql .api .reporter .ReporterFactory ;
2122
2223/**
2324 * Created by vinicius.moreira on 19/04/2017.
25+ *
26+ * @author vinicious moreira
27+ * @author pesse
2428 */
2529@ Parameters (separators = "=" , commandDescription = "run tests" )
2630public class RunCommand {
@@ -70,6 +74,12 @@ public class RunCommand {
7074 "-name_subexpression=0] - path to project test files" )
7175 private List <String > testPathParams = new ArrayList <>();
7276
77+ @ Parameter (
78+ names = {"-scc" , "--skip-compatibility-check" },
79+ description = "Skips the check for compatibility with database framework. CLI expects the framework to be " +
80+ "most actual. Use this if you use CLI with a development version of utPLSQL-framework" )
81+ private boolean skipCompatibilityCheck = false ;
82+
7383 public ConnectionInfo getConnectionInfo () {
7484 return connectionInfoList .get (0 );
7585 }
@@ -81,36 +91,27 @@ public List<String> getTestPaths() {
8191 public int run () throws Exception {
8292 final ConnectionInfo ci = getConnectionInfo ();
8393
94+ final List <Reporter > reporterList ;
8495 final List <ReporterOptions > reporterOptionsList = getReporterOptionsList ();
8596 final List <String > testPaths = getTestPaths ();
86- final List <Reporter > reporterList = new ArrayList <>();
8797
8898 final File baseDir = new File ("" ).getAbsoluteFile ();
8999 final FileMapperOptions [] sourceMappingOptions = {null };
90100 final FileMapperOptions [] testMappingOptions = {null };
91101
92102 final int [] returnCode = {0 };
93103
94- if (!this .sourcePathParams .isEmpty ()) {
95- String sourcePath = this .sourcePathParams .get (0 );
96- List <String > sourceFiles = new FileWalker ().getFileList (baseDir , sourcePath );
97- sourceMappingOptions [0 ] = getMapperOptions (this .sourcePathParams , sourceFiles );
98- }
99-
100- if (!this .testPathParams .isEmpty ()) {
101- String testPath = this .testPathParams .get (0 );
102- List <String > testFiles = new FileWalker ().getFileList (baseDir , testPath );
103- testMappingOptions [0 ] = getMapperOptions (this .testPathParams , testFiles );
104- }
104+ sourceMappingOptions [0 ] = getFileMapperOptionsByParamListItem (this .sourcePathParams , baseDir );
105+ testMappingOptions [0 ] = getFileMapperOptionsByParamListItem (this .testPathParams , baseDir );
105106
106107 // Do the reporters initialization, so we can use the id to run and gather results.
107108 try (Connection conn = ci .getConnection ()) {
108- for ( ReporterOptions ro : reporterOptionsList ) {
109- Reporter reporter = ReporterFactory . createReporter ( ro . getReporterName ());
110- reporter . init (conn );
111- ro . setReporterObj ( reporter );
112- reporterList . add ( reporter );
113- }
109+
110+ // First of all do a compatibility check and fail-fast
111+ checkFrameworkCompatibility (conn );
112+
113+ reporterList = initReporters ( conn , reporterOptionsList );
114+
114115 } catch (SQLException e ) {
115116 System .out .println (e .getMessage ());
116117 return Cli .DEFAULT_ERROR_CODE ;
@@ -128,6 +129,7 @@ public int run() throws Exception {
128129 .testMappingOptions (testMappingOptions [0 ])
129130 .colorConsole (this .colorConsole )
130131 .failOnErrors (true )
132+ .skipCompatibilityCheck (skipCompatibilityCheck )
131133 .run (conn );
132134 } catch (SomeTestsFailedException e ) {
133135 returnCode [0 ] = this .failureExitCode ;
@@ -138,6 +140,44 @@ public int run() throws Exception {
138140 }
139141 });
140142
143+ // Gather each reporter results on a separate thread.
144+ startReporterGatherers (reporterOptionsList , executorService , ci , returnCode );
145+
146+ executorService .shutdown ();
147+ executorService .awaitTermination (60 , TimeUnit .MINUTES );
148+ return returnCode [0 ];
149+ }
150+
151+ /** Initializes the reporters so we can use the id to gather results
152+ *
153+ * @param conn Active Connection
154+ * @param reporterOptionsList
155+ * @return List of Reporters
156+ * @throws SQLException
157+ */
158+ private List <Reporter > initReporters ( Connection conn , List <ReporterOptions > reporterOptionsList ) throws SQLException
159+ {
160+ final List <Reporter > reporterList = new ArrayList <>();
161+
162+ for (ReporterOptions ro : reporterOptionsList ) {
163+ Reporter reporter = ReporterFactory .createReporter (ro .getReporterName ());
164+ reporter .init (conn );
165+ ro .setReporterObj (reporter );
166+ reporterList .add (reporter );
167+ }
168+
169+ return reporterList ;
170+ }
171+
172+ /** Starts a separate thread for each Reporter to gather its results
173+ *
174+ * @param reporterOptionsList
175+ * @param executorService
176+ * @param ci
177+ * @param returnCode
178+ */
179+ private void startReporterGatherers (List <ReporterOptions > reporterOptionsList , ExecutorService executorService , final ConnectionInfo ci , final int [] returnCode )
180+ {
141181 // Gather each reporter results on a separate thread.
142182 for (ReporterOptions ro : reporterOptionsList ) {
143183 executorService .submit (() -> {
@@ -165,10 +205,23 @@ public int run() throws Exception {
165205 }
166206 });
167207 }
208+ }
168209
169- executorService .shutdown ();
170- executorService .awaitTermination (60 , TimeUnit .MINUTES );
171- return returnCode [0 ];
210+ /** Returns FileMapperOptions for the first item of a given param list in a baseDir
211+ *
212+ * @param pathParams
213+ * @param baseDir
214+ * @return FileMapperOptions or null
215+ */
216+ private FileMapperOptions getFileMapperOptionsByParamListItem (List <String > pathParams , File baseDir )
217+ {
218+ if (!pathParams .isEmpty ()) {
219+ String sourcePath = pathParams .get (0 );
220+ List <String > files = new FileWalker ().getFileList (baseDir , sourcePath );
221+ return getMapperOptions (pathParams , files );
222+ }
223+
224+ return null ;
172225 }
173226
174227 public List <ReporterOptions > getReporterOptionsList () {
@@ -198,6 +251,28 @@ public List<ReporterOptions> getReporterOptionsList() {
198251 return reporterOptionsList ;
199252 }
200253
254+ /** Checks whether cli is compatible with the database framework
255+ *
256+ * @param conn Active Connection
257+ * @throws SQLException
258+ */
259+ private void checkFrameworkCompatibility (Connection conn ) throws SQLException {
260+
261+ if ( !skipCompatibilityCheck ) {
262+ try {
263+ DBHelper .failOnVersionCompatibilityCheckFailed (conn );
264+ } catch (DatabaseNotCompatibleException e ) {
265+ System .out .println (e .getMessage ());
266+
267+ throw e ;
268+ }
269+ }
270+ else {
271+ System .out .println ("Skipping Compatibility check with framework version, expecting the latest version " +
272+ "to be installed in database" );
273+ }
274+ }
275+
201276 public FileMapperOptions getMapperOptions (List <String > mappingParams , List <String > filePaths ) {
202277 FileMapperOptions mapperOptions = new FileMapperOptions (filePaths );
203278
0 commit comments