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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.utplsql</groupId>
<artifactId>cli</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cli</name>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/utplsql/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void main(String[] args) {
LocaleInitializer.initLocale();

JCommander jc = new JCommander();
jc.setProgramName("utplsql");
// jc.addCommand(HELP_CMD, new HelpCommand());
RunCommand runCmd = new RunCommand();
jc.addCommand(RUN_CMD, runCmd);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/utplsql/cli/ReporterFactoryProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.utplsql.cli;

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

/** A simple class to provide a ReporterFactory for the RunCommand
*
* @author pesse
*/
public class ReporterFactoryProvider {

public static ReporterFactory createReporterFactory(CompatibilityProxy proxy ) {
ReporterFactory reporterFactory = ReporterFactory.createDefault(proxy);
reporterFactory.registerReporterFactoryMethod(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), LocalAssetsCoverageHTMLReporter::new, "Will copy all necessary assets to a folder named after the Output-File");

return reporterFactory;
}
}
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;
}
}
119 changes: 20 additions & 99 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.utplsql.api.*;
import org.utplsql.api.FileMapperOptions;
import org.utplsql.api.KeyValuePair;
import org.utplsql.api.TestRunner;
import org.utplsql.api.Version;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.CoverageHTMLReporter;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.exception.DatabaseConnectionFailed;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -100,6 +98,8 @@ public class RunCommand {


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

public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
Expand All @@ -116,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 @@ -154,8 +153,9 @@ public int run() throws Exception {

// First of all do a compatibility check and fail-fast
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 @@ -199,78 +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());

// Quick-hack for CoverageHTML Reporter
if ( reporter instanceof CoverageHTMLReporter && ro.outputToFile() ) {
((CoverageHTMLReporter)reporter).setAssetsPath(ro.getOutputFileName()+"_assets/");
CoverageHTMLReporter.writeReportAssetsTo(Paths.get(ro.getOutputFileName()+"_assets/"));
}

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

new OutputBuffer(ro.getReporterObj()).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 @@ -289,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(CustomTypes.UT_DOCUMENTATION_REPORTER));
}

return reporterOptionsList;
}

/** Checks whether cli is compatible with the database framework
*
* @param conn Active Connection
Expand Down Expand Up @@ -393,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();
}
}
Loading