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

Skip to content
Prev Previous commit
Next Next commit
Refactoring aiming for single-responsibility and IOSP
Not yet there, but a bit cleaner. Also added run-Test
  • Loading branch information
pesse authored and Samuel Nitsche committed Nov 10, 2017
commit 7617e789f66b8dbf981e0a6bc3b774ce5f0d2782
81 changes: 59 additions & 22 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,27 @@ public List<String> getTestPaths() {
public int run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();


final List<Reporter> reporterList;
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
final List<String> testPaths = getTestPaths();
final List<Reporter> reporterList = new ArrayList<>();

final File baseDir = new File("").getAbsoluteFile();
final FileMapperOptions[] sourceMappingOptions = {null};
final FileMapperOptions[] testMappingOptions = {null};

final int[] returnCode = {0};

if (!this.sourcePathParams.isEmpty()) {
String sourcePath = this.sourcePathParams.get(0);
List<String> sourceFiles = new FileWalker().getFileList(baseDir, sourcePath);
sourceMappingOptions[0] = getMapperOptions(this.sourcePathParams, sourceFiles);
}

if (!this.testPathParams.isEmpty()) {
String testPath = this.testPathParams.get(0);
List<String> testFiles = new FileWalker().getFileList(baseDir, testPath);
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
}
sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir);
testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir);

// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = ci.getConnection()) {

// First of all do a compatibility check and fail-fast
checkFrameworkCompatibility(conn);

for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}
reporterList = initReporters(conn, reporterOptionsList);

} catch (SQLException e) {
System.out.println(e.getMessage());
return Cli.DEFAULT_ERROR_CODE;
Expand Down Expand Up @@ -154,6 +140,44 @@ public int run() throws Exception {
}
});

// Gather each reporter results on a separate thread.
startReporterGatherers(reporterOptionsList, executorService, ci, returnCode);

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
return returnCode[0];
}

/** Initializes the reporters so we can use the id to gather results
*
* @param conn Active Connection
* @param reporterOptionsList
* @return List of Reporters
* @throws SQLException
*/
private List<Reporter> initReporters( Connection conn, List<ReporterOptions> reporterOptionsList ) throws SQLException
{
final List<Reporter> reporterList = new ArrayList<>();

for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}

return reporterList;
}

/** Starts a separate thread for each Reporter to gather its results
*
* @param reporterOptionsList
* @param executorService
* @param ci
* @param returnCode
*/
private void startReporterGatherers(List<ReporterOptions> reporterOptionsList, ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
{
// Gather each reporter results on a separate thread.
for (ReporterOptions ro : reporterOptionsList) {
executorService.submit(() -> {
Expand Down Expand Up @@ -181,10 +205,23 @@ public int run() throws Exception {
}
});
}
}

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
return returnCode[0];
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
*
* @param pathParams
* @param baseDir
* @return FileMapperOptions or null
*/
private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathParams, File baseDir )
{
if (!pathParams.isEmpty()) {
String sourcePath = pathParams.get(0);
List<String> files = new FileWalker().getFileList(baseDir, sourcePath);
return getMapperOptions(pathParams, files);
}

return null;
}

public List<ReporterOptions> getReporterOptionsList() {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
*/
public class RunCommandTest {

private static String sUrl;
private static String sUser;
private static String sPass;

static {
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
}

private RunCommand createRunCommand(String... args) {
RunCommand runCmd = new RunCommand();

Expand Down Expand Up @@ -96,4 +106,17 @@ public void reporterOptions_TwoReporters() {
Assert.assertTrue(reporterOptions2.outputToScreen());
}

@Test
public void run_Default() {
RunCommand runCmd = createRunCommand(sUser + "/" + sPass + "@" + sUrl);

try {
int result = runCmd.run();
Assert.assertEquals(0, result);
}
catch ( Exception e ) {
Assert.fail(e.getMessage());
}
}

}