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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Outsource Reporter-related things into ReporterManager
  • Loading branch information
pesse committed Mar 13, 2018
commit c392e4262817a87921fe148f19be7fc4a1289ded
118 changes: 118 additions & 0 deletions src/main/java/org/utplsql/cli/ReporterManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.utplsql.cli;

import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.reporter.CoreReporters;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.reporters.ReporterOptionsAware;

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;

class ReporterManager {

private List<ReporterOptions> reporterOptionsList;

ReporterManager(List<String> reporterParams ) {
initReporterOptionsList(reporterParams);
}

private void initReporterOptionsList( List<String> reporterParams ) {
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(CoreReporters.UT_DOCUMENTATION_REPORTER.name()));
}
}


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

for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = reporterFactory.createReporter(ro.getReporterName());

if ( reporter instanceof ReporterOptionsAware)
((ReporterOptionsAware) reporter).setReporterOptions(ro);

reporter.init(conn, compatibilityProxy, reporterFactory);

ro.setReporterObj(reporter);
reporterList.add(reporter);
}

return reporterList;
}

/** Starts a separate thread for each Reporter to gather its results
*
* @param executorService
* @param ci
* @param returnCode
*/
public void startReporterGatherers(ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
{
// TODO: Implement Init-check
// Gather each reporter results on a separate thread.
for (ReporterOptions ro : reporterOptionsList) {
executorService.submit(() -> {
List<PrintStream> 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);
}

ro.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
} catch (SQLException | FileNotFoundException e) {
System.out.println(e.getMessage());
returnCode[0] = Cli.DEFAULT_ERROR_CODE;
executorService.shutdownNow();
} finally {
if (fileOutStream != null)
fileOutStream.close();
}
});
}
}

public List<ReporterOptions> getReporterOptionsList() {
return reporterOptionsList;
}
}
110 changes: 14 additions & 96 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@
import org.utplsql.api.Version;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.CoreReporters;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.exception.DatabaseConnectionFailed;
import org.utplsql.cli.reporters.ReporterOptionsAware;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -104,6 +99,7 @@ public class RunCommand {

private CompatibilityProxy compatibilityProxy;
private ReporterFactory reporterFactory;
private ReporterManager reporterManager;

public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
Expand All @@ -120,7 +116,6 @@ public int run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();

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

final File baseDir = new File("").getAbsoluteFile();
Expand Down Expand Up @@ -160,7 +155,7 @@ public int run() throws Exception {
compatibilityProxy = checkFrameworkCompatibility(conn);
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);

reporterList = initReporters(conn, reporterOptionsList);
reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);

} catch (SQLException e) {
if ( e.getErrorCode() == 1017 || e.getErrorCode() == 12514 ) {
Expand Down Expand Up @@ -204,76 +199,15 @@ public int run() throws Exception {
});

// Gather each reporter results on a separate thread.
startReporterGatherers(reporterOptionsList, executorService, ci, returnCode);
getReporterManager().startReporterGatherers(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());

if ( reporter instanceof ReporterOptionsAware )
((ReporterOptionsAware) reporter).setReporterOptions(ro);

reporter.init(conn, compatibilityProxy, reporterFactory);

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(() -> {
List<PrintStream> 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);
}

ro.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
} catch (SQLException | FileNotFoundException e) {
System.out.println(e.getMessage());
returnCode[0] = Cli.DEFAULT_ERROR_CODE;
executorService.shutdownNow();
} finally {
if (fileOutStream != null)
fileOutStream.close();
}
});
}
}

/** Returns FileMapperOptions for the first item of a given param list in a baseDir
*
Expand All @@ -292,33 +226,6 @@ private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathP
return null;
}

public List<ReporterOptions> getReporterOptionsList() {
List<ReporterOptions> 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(CoreReporters.UT_DOCUMENTATION_REPORTER.name()));
}

return reporterOptionsList;
}

/** Checks whether cli is compatible with the database framework
*
* @param conn Active Connection
Expand Down Expand Up @@ -396,4 +303,15 @@ public Version getDatabaseVersion() {

return null;
}

private ReporterManager getReporterManager() {
if ( reporterManager == null )
reporterManager = new ReporterManager(reporterParams);

return reporterManager;
}

public List<ReporterOptions> getReporterOptionsList() {
return getReporterManager().getReporterOptionsList();
}
}