Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readme in result zip file #1361

Merged
merged 8 commits into from
Dec 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
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
* a function that associates a submission to its id.
*/
public class ComparisonReportWriter {

private final FileWriter fileWriter;
private final JsonWriter fileWriter;
private final Function<Submission, String> submissionToIdFunction;
private final Map<String, Map<String, String>> submissionIdToComparisonFileName = new ConcurrentHashMap<>();
private final Map<String, AtomicInteger> fileNameCollisions = new ConcurrentHashMap<>();

public ComparisonReportWriter(Function<Submission, String> submissionToIdFunction, FileWriter fileWriter) {
public ComparisonReportWriter(Function<Submission, String> submissionToIdFunction, JsonWriter fileWriter) {
this.submissionToIdFunction = submissionToIdFunction;
this.fileWriter = fileWriter;
}
Expand Down Expand Up @@ -59,7 +60,7 @@ private void writeComparisons(String path, List<JPlagComparison> 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);
});
}

Expand Down
13 changes: 0 additions & 13 deletions core/src/main/java/de/jplag/reporting/jsonfactory/DummyWriter.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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<Submission> getSubmissions(List<JPlagComparison> comparisons) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Kr0nox marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.jplag.reporting.reportobject.writer;

/**
* Responsible for writing a specific file type to the disk.
* @param <T> Object that the FileWriter writes.
*/
public interface FileWriter<T> {

/**
* 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);
}
Original file line number Diff line number Diff line change
@@ -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<Object> {
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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<String> {

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading