diff --git a/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java b/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java index 242d09138..8ebb4c555 100644 --- a/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java +++ b/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java @@ -16,6 +16,7 @@ import de.jplag.reporting.FilePathUtil; import de.jplag.reporting.reportobject.model.ComparisonReport; import de.jplag.reporting.reportobject.model.Match; +import de.jplag.reporting.reportobject.writer.JsonWriter; /** * Writes {@link ComparisonReport}s of given {@link JPlagResult} to the disk under the specified path. Instantiated with @@ -23,12 +24,12 @@ */ public class ComparisonReportWriter { - private final FileWriter fileWriter; + private final JsonWriter fileWriter; private final Function submissionToIdFunction; private final Map> submissionIdToComparisonFileName = new ConcurrentHashMap<>(); private final Map fileNameCollisions = new ConcurrentHashMap<>(); - public ComparisonReportWriter(Function submissionToIdFunction, FileWriter fileWriter) { + public ComparisonReportWriter(Function submissionToIdFunction, JsonWriter fileWriter) { this.submissionToIdFunction = submissionToIdFunction; this.fileWriter = fileWriter; } @@ -59,7 +60,7 @@ private void writeComparisons(String path, List comparisons) { var comparisonReport = new ComparisonReport(firstSubmissionId, secondSubmissionId, Map.of(SimilarityMetric.AVG.name(), comparison.similarity(), SimilarityMetric.MAX.name(), comparison.maximalSimilarity()), convertMatchesToReportMatches(comparison)); - fileWriter.saveAsJSON(comparisonReport, path, fileName); + fileWriter.writeFile(comparisonReport, path, fileName); }); } diff --git a/core/src/main/java/de/jplag/reporting/jsonfactory/DummyWriter.java b/core/src/main/java/de/jplag/reporting/jsonfactory/DummyWriter.java deleted file mode 100644 index 7f8841fc9..000000000 --- a/core/src/main/java/de/jplag/reporting/jsonfactory/DummyWriter.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.jplag.reporting.jsonfactory; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DummyWriter implements FileWriter { - private static final Logger logger = LoggerFactory.getLogger(DummyWriter.class); - - @Override - public void saveAsJSON(Object fileToSave, String folderPath, String fileName) { - logger.info("DummyWriter writes object " + fileToSave + " to path " + folderPath + " with name " + fileName + " as JSON."); - } -} diff --git a/core/src/main/java/de/jplag/reporting/jsonfactory/FileWriter.java b/core/src/main/java/de/jplag/reporting/jsonfactory/FileWriter.java deleted file mode 100644 index e0198684b..000000000 --- a/core/src/main/java/de/jplag/reporting/jsonfactory/FileWriter.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.jplag.reporting.jsonfactory; - -public interface FileWriter { - void saveAsJSON(Object fileToSave, String folderPath, String fileName); -} diff --git a/core/src/main/java/de/jplag/reporting/jsonfactory/ToDiskWriter.java b/core/src/main/java/de/jplag/reporting/jsonfactory/ToDiskWriter.java deleted file mode 100644 index 48722e6b1..000000000 --- a/core/src/main/java/de/jplag/reporting/jsonfactory/ToDiskWriter.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.jplag.reporting.jsonfactory; - -import java.io.IOException; -import java.nio.file.Path; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class ToDiskWriter implements FileWriter { - private static final Logger logger = LoggerFactory.getLogger(ToDiskWriter.class); - - private static final ObjectMapper objectMapper = new ObjectMapper(); - - /** - * Saves the provided object to the provided path under the provided name - * @param fileToSave The object to save - * @param folderPath The path to save the object to - * @param fileName The name to save the object under - */ - @Override - public void saveAsJSON(Object fileToSave, String folderPath, String fileName) { - try { - objectMapper.writeValue(Path.of(folderPath, fileName).toFile(), fileToSave); - } catch (IOException e) { - logger.error("Failed to save json file " + fileName + ": " + e.getMessage(), e); - } - } -} diff --git a/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java b/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java index 0f8f142da..342338fe7 100644 --- a/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java +++ b/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java @@ -30,12 +30,13 @@ import de.jplag.Submission; import de.jplag.reporting.FilePathUtil; import de.jplag.reporting.jsonfactory.ComparisonReportWriter; -import de.jplag.reporting.jsonfactory.ToDiskWriter; import de.jplag.reporting.reportobject.mapper.ClusteringResultMapper; import de.jplag.reporting.reportobject.mapper.MetricMapper; import de.jplag.reporting.reportobject.model.OverviewReport; import de.jplag.reporting.reportobject.model.SubmissionFileIndex; import de.jplag.reporting.reportobject.model.Version; +import de.jplag.reporting.reportobject.writer.JsonWriter; +import de.jplag.reporting.reportobject.writer.TextWriter; /** * Factory class, responsible for converting a JPlagResult object to Overview and Comparison DTO classes and writing it @@ -46,8 +47,13 @@ public class ReportObjectFactory { private static final Logger logger = LoggerFactory.getLogger(ReportObjectFactory.class); - private static final ToDiskWriter fileWriter = new ToDiskWriter(); + private static final JsonWriter jsonFileWriter = new JsonWriter(); public static final String OVERVIEW_FILE_NAME = "overview.json"; + + public static final String README_FILE_NAME = "README.txt"; + private static final String[] README_CONTENT = new String[] {"This is a software plagiarism report generated by JPlag.", + "To view the report go to https://jplag.github.io/JPlag/ and drag the generated zip file onto the page."}; + public static final String SUBMISSIONS_FOLDER = "files"; public static final String SUBMISSION_FILE_INDEX_FILE_NAME = "submissionFileIndex.json"; public static final Version REPORT_VIEWER_VERSION = JPlag.JPLAG_VERSION; @@ -73,6 +79,7 @@ public void createAndSaveReport(JPlagResult result, String path) { writeComparisons(result, path); writeOverview(result, path); writeSubmissionIndexFile(result, path); + writeReadMeFile(path); logger.info("Zipping report files..."); zipAndDelete(path); @@ -159,7 +166,7 @@ private File getFileToCopy(Language language, File file) { } private void writeComparisons(JPlagResult result, String path) { - ComparisonReportWriter comparisonReportWriter = new ComparisonReportWriter(submissionToIdFunction, fileWriter); + ComparisonReportWriter comparisonReportWriter = new ComparisonReportWriter(submissionToIdFunction, jsonFileWriter); submissionNameToNameToComparisonFileName = comparisonReportWriter.writeComparisonReports(result, path); } @@ -194,8 +201,12 @@ private void writeOverview(JPlagResult result, String path) { clusteringResultMapper.map(result), // clusters totalComparisons); // totalComparisons - fileWriter.saveAsJSON(overviewReport, path, OVERVIEW_FILE_NAME); + jsonFileWriter.writeFile(overviewReport, path, OVERVIEW_FILE_NAME); + + } + private void writeReadMeFile(String path) { + new TextWriter().writeFile(String.join(System.lineSeparator(), README_CONTENT), path, README_FILE_NAME); } private void writeSubmissionIndexFile(JPlagResult result, String path) { @@ -210,7 +221,7 @@ private void writeSubmissionIndexFile(JPlagResult result, String path) { } fileIndex.fileIndexes().put(submissionNameToIdMap.get(submission.getName()), filePaths); } - fileWriter.saveAsJSON(fileIndex, path, SUBMISSION_FILE_INDEX_FILE_NAME); + jsonFileWriter.writeFile(fileIndex, path, SUBMISSION_FILE_INDEX_FILE_NAME); } private Set getSubmissions(List comparisons) { diff --git a/core/src/main/java/de/jplag/reporting/reportobject/writer/DummyWriter.java b/core/src/main/java/de/jplag/reporting/reportobject/writer/DummyWriter.java new file mode 100644 index 000000000..c344802dc --- /dev/null +++ b/core/src/main/java/de/jplag/reporting/reportobject/writer/DummyWriter.java @@ -0,0 +1,17 @@ +package de.jplag.reporting.reportobject.writer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This writer is used as a mock for testing purposes only. + */ +public class DummyWriter extends JsonWriter { + private static final Logger logger = LoggerFactory.getLogger(DummyWriter.class); + private static final String MESSAGE = "DummyWriter writes object {} to path {} with name {} as JSON."; + + @Override + public void writeFile(Object fileToSave, String folderPath, String fileName) { + logger.info(MESSAGE, fileToSave, folderPath, fileName); + } +} diff --git a/core/src/main/java/de/jplag/reporting/reportobject/writer/FileWriter.java b/core/src/main/java/de/jplag/reporting/reportobject/writer/FileWriter.java new file mode 100644 index 000000000..ec8888a49 --- /dev/null +++ b/core/src/main/java/de/jplag/reporting/reportobject/writer/FileWriter.java @@ -0,0 +1,16 @@ +package de.jplag.reporting.reportobject.writer; + +/** + * Responsible for writing a specific file type to the disk. + * @param Object that the FileWriter writes. + */ +public interface FileWriter { + + /** + * Saves the provided object to the provided path under the provided name + * @param fileContent The object to save + * @param folderPath The path to save the object to + * @param fileName The name to save the object under + */ + void writeFile(T fileContent, String folderPath, String fileName); +} diff --git a/core/src/main/java/de/jplag/reporting/reportobject/writer/JsonWriter.java b/core/src/main/java/de/jplag/reporting/reportobject/writer/JsonWriter.java new file mode 100644 index 000000000..723718cf8 --- /dev/null +++ b/core/src/main/java/de/jplag/reporting/reportobject/writer/JsonWriter.java @@ -0,0 +1,29 @@ +package de.jplag.reporting.reportobject.writer; + +import java.io.IOException; +import java.nio.file.Path; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Writes an object with {@link com.fasterxml.jackson.annotation.JsonProperty}s to the disk. + */ +public class JsonWriter implements FileWriter { + private static final Logger logger = LoggerFactory.getLogger(JsonWriter.class); + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final String WRITE_ERROR = "Failed to write JSON file {}"; + + @Override + public void writeFile(Object fileToSave, String folderPath, String fileName) { + Path path = Path.of(folderPath, fileName); + try { + objectMapper.writeValue(path.toFile(), fileToSave); + } catch (IOException e) { + logger.error(WRITE_ERROR, e, path); + } + } + +} diff --git a/core/src/main/java/de/jplag/reporting/reportobject/writer/TextWriter.java b/core/src/main/java/de/jplag/reporting/reportobject/writer/TextWriter.java new file mode 100644 index 000000000..d4b3601d4 --- /dev/null +++ b/core/src/main/java/de/jplag/reporting/reportobject/writer/TextWriter.java @@ -0,0 +1,27 @@ +package de.jplag.reporting.reportobject.writer; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Path; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Writes plain text to a file. + */ +public class TextWriter implements FileWriter { + + private static final Logger logger = LoggerFactory.getLogger(TextWriter.class); + private static final String WRITE_ERROR = "Failed to write text file {}"; + + @Override + public void writeFile(String fileContent, String folderPath, String fileName) { + String path = Path.of(folderPath, fileName).toString(); + try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(path))) { + writer.write(fileContent); + } catch (IOException e) { + logger.error(WRITE_ERROR, e, path); + } + } +} diff --git a/core/src/test/java/de/jplag/reporting/reportobject/mapper/ComparisonReportWriterTest.java b/core/src/test/java/de/jplag/reporting/reportobject/mapper/ComparisonReportWriterTest.java index ffde264e9..02af18f52 100644 --- a/core/src/test/java/de/jplag/reporting/reportobject/mapper/ComparisonReportWriterTest.java +++ b/core/src/test/java/de/jplag/reporting/reportobject/mapper/ComparisonReportWriterTest.java @@ -10,11 +10,11 @@ import de.jplag.TestBase; import de.jplag.exceptions.ExitException; import de.jplag.reporting.jsonfactory.ComparisonReportWriter; -import de.jplag.reporting.jsonfactory.DummyWriter; -import de.jplag.reporting.jsonfactory.FileWriter; +import de.jplag.reporting.reportobject.writer.DummyWriter; +import de.jplag.reporting.reportobject.writer.JsonWriter; public class ComparisonReportWriterTest extends TestBase { - private final FileWriter fileWriter = new DummyWriter(); + private final JsonWriter fileWriter = new DummyWriter(); @Test public void firsLevelOfLookupMapComplete() throws ExitException {