Skip to content

Commit

Permalink
Removed superfluous interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Apr 2, 2024
1 parent f8bb719 commit a9e6ef5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 66 deletions.
17 changes: 6 additions & 11 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ public final class CLI {

private static final String DEFAULT_FILE_ENDING = ".zip";

private final JPlagRunner runner;
private final CliInputHandler inputHandler;
private final OutputFileGenerator outputFileGenerator;

/**
* Creates a cli.
* @param runner The way to run JPlag
* @param args The command line arguments
*/
public CLI(JPlagRunner runner, OutputFileGenerator outputFileGenerator, String[] args) {
this.runner = runner;
this.outputFileGenerator = outputFileGenerator;
public CLI(String[] args) {
this.inputHandler = new CliInputHandler(args);
}

Expand Down Expand Up @@ -92,11 +87,11 @@ public boolean executeCliAndHandleErrors() {
public File runJPlag() throws ExitException, FileNotFoundException {
JPlagOptionsBuilder optionsBuilder = new JPlagOptionsBuilder(this.inputHandler);
JPlagOptions options = optionsBuilder.buildOptions();
JPlagResult result = this.runner.runJPlag(options);
JPlagResult result = JPlagRunner.runJPlag(options);

File target = new File(getResultFilePath());
this.outputFileGenerator.generateJPlagResultZip(result, target);
this.outputFileGenerator.generateCsvOutput(result, new File(getResultFileBaseName()), this.inputHandler.getCliOptions());
OutputFileGenerator.generateJPlagResultZip(result, target);
OutputFileGenerator.generateCsvOutput(result, new File(getResultFileBaseName()), this.inputHandler.getCliOptions());

return target;
}
Expand All @@ -107,7 +102,7 @@ public File runJPlag() throws ExitException, FileNotFoundException {
* @throws IOException If something went wrong with the internal server
*/
public void runViewer(File zipFile) throws IOException {
this.runner.runInternalServer(zipFile, this.inputHandler.getCliOptions().advanced.port);
JPlagRunner.runInternalServer(zipFile, this.inputHandler.getCliOptions().advanced.port);
}

private void finalizeLogger() {
Expand All @@ -132,7 +127,7 @@ private String getResultFileBaseName() {
}

public static void main(String[] args) {
CLI cli = new CLI(JPlagRunner.DEFAULT_JPLAG_RUNNER, OutputFileGenerator.DEFAULT_OUTPUT_FILE_GENERATOR, args);
CLI cli = new CLI(args);
if (cli.executeCliAndHandleErrors()) {
System.exit(1);
}
Expand Down
48 changes: 21 additions & 27 deletions cli/src/main/java/de/jplag/cli/JPlagRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,40 @@
import de.jplag.options.JPlagOptions;

/**
* Wraps the execution of the JPlag elements, so dummy implementations can be used for unit tests.
* Wraps the execution of the JPlag components
*/
public interface JPlagRunner {
Logger logger = LoggerFactory.getLogger(JPlagRunner.class);
public final class JPlagRunner {
private static final Logger logger = LoggerFactory.getLogger(JPlagRunner.class);

/**
* The default JPlag runner. Simply passes the calls to the appropriate JPlag elements
*/
JPlagRunner DEFAULT_JPLAG_RUNNER = new JPlagRunner() {
@Override
public JPlagResult runJPlag(JPlagOptions options) throws ExitException {
return JPlag.run(options);
}

@Override
public void runInternalServer(File zipFile, int port) throws IOException {
ReportViewer reportViewer = new ReportViewer(zipFile, port);
int actualPort = reportViewer.start();
logger.info("ReportViewer started on port http://localhost:{}", actualPort);
Desktop.getDesktop().browse(URI.create("http://localhost:" + actualPort + "/"));

System.out.println("Press Enter key to exit...");
System.in.read();
reportViewer.stop();
}
};
private JPlagRunner() {
}

/**
* Executes JPlag
*
* @param options The options to pass to JPlag
* @return The result returned by JPlag
* @throws ExitException If JPlag throws an error
*/
JPlagResult runJPlag(JPlagOptions options) throws ExitException;
public static JPlagResult runJPlag(JPlagOptions options) throws ExitException {
return JPlag.run(options);
}

/**
* Runs the internal server. Blocks until the server has stopped.
*
* @param zipFile The zip file to pass to the server. May be null.
* @param port The port to open the server on
* @param port The port to open the server on
* @throws IOException If the internal server throws an exception
*/
void runInternalServer(File zipFile, int port) throws IOException;
public static void runInternalServer(File zipFile, int port) throws IOException {
ReportViewer reportViewer = new ReportViewer(zipFile, port);
int actualPort = reportViewer.start();
logger.info("ReportViewer started on port http://localhost:{}", actualPort);
Desktop.getDesktop().browse(URI.create("http://localhost:" + actualPort + "/"));

System.out.println("Press Enter key to exit...");
System.in.read();
reportViewer.stop();
}
}
56 changes: 28 additions & 28 deletions cli/src/main/java/de/jplag/cli/OutputFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@
import de.jplag.csv.comparisons.CsvComparisonOutput;
import de.jplag.reporting.reportobject.ReportObjectFactory;

public interface OutputFileGenerator {
Logger logger = LoggerFactory.getLogger(OutputFileGenerator.class);
/**
* Manages the creation of output files
*/
public final class OutputFileGenerator {
private static final Logger logger = LoggerFactory.getLogger(OutputFileGenerator.class);

OutputFileGenerator DEFAULT_OUTPUT_FILE_GENERATOR = new OutputFileGenerator() {
@Override
public void generateCsvOutput(JPlagResult result, File outputRoot, CliOptions options) {
if (options.advanced.csvExport) {
try {
CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), false, outputRoot, "results");
CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), true, outputRoot, "results-anonymous");
} catch (IOException e) {
logger.warn("Could not write csv results", e);
}
}
}

@Override
public void generateJPlagResultZip(JPlagResult result, File outputFile) throws FileNotFoundException {
ReportObjectFactory reportObjectFactory = new ReportObjectFactory(outputFile);
reportObjectFactory.createAndSaveReport(result);
logger.info("Successfully written the result: {}", outputFile.getPath());
logger.info("View the result using --mode or at: https://jplag.github.io/JPlag/");
}
};
private OutputFileGenerator() {
}

/**
* Exports the given result as CSVs, if the csvExport is activated in the options. Both a full and an anonymized version
* will be written.
* @param result The result to export
*
* @param result The result to export
* @param outputRoot The root folder for the output
* @param options The cli options
* @param options The cli options
*/
void generateCsvOutput(JPlagResult result, File outputRoot, CliOptions options);
public static void generateCsvOutput(JPlagResult result, File outputRoot, CliOptions options) {
if (options.advanced.csvExport) {
try {
CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), false, outputRoot, "results");
CsvComparisonOutput.writeCsvResults(result.getAllComparisons(), true, outputRoot, "results-anonymous");
} catch (IOException e) {
logger.warn("Could not write csv results", e);
}
}
}

/**
* Generates the JPLag result zip
* @param result The JPlag result
*
* @param result The JPlag result
* @param outputFile The output file
* @throws FileNotFoundException If the file cannot be written
*/
void generateJPlagResultZip(JPlagResult result, File outputFile) throws FileNotFoundException;
public static void generateJPlagResultZip(JPlagResult result, File outputFile) throws FileNotFoundException {
ReportObjectFactory reportObjectFactory = new ReportObjectFactory(outputFile);
reportObjectFactory.createAndSaveReport(result);
logger.info("Successfully written the result: {}", outputFile.getPath());
logger.info("View the result using --mode or at: https://jplag.github.io/JPlag/");
}
}

0 comments on commit a9e6ef5

Please sign in to comment.