Skip to content

Commit

Permalink
Merge branch 'develop' into feature/internal-web-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss authored Dec 6, 2023
2 parents a207694 + 1c9a906 commit dc58ad6
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 94 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/de/jplag/options/JPlagOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public record JPlagOptions(Language language, Integer minimumTokenMatch, Set<Fil
boolean debugParser, MergingOptions mergingOptions) {

public static final double DEFAULT_SIMILARITY_THRESHOLD = 0;
public static final int DEFAULT_SHOWN_COMPARISONS = 100;
public static final int DEFAULT_SHOWN_COMPARISONS = 500;
public static final int SHOW_ALL_COMPARISONS = 0;
public static final SimilarityMetric DEFAULT_SIMILARITY_METRIC = SimilarityMetric.AVG;
public static final Charset CHARSET = StandardCharsets.UTF_8;
Expand Down
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 File 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 @@ -161,7 +168,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 @@ -196,8 +203,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 @@ -212,7 +223,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 {
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
6 changes: 6 additions & 0 deletions endtoend-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.endtoend.model.DataSet;

public class UnzipManager {
private final Map<DataSet, File> unzippedFiles;
private static UnzipManager instance;
private final Logger logger = Logger.getLogger("Unzip Manager");
private final Logger logger = LoggerFactory.getLogger(UnzipManager.class);

private static synchronized UnzipManager getInstance() {
if (instance == null) {
Expand Down Expand Up @@ -46,7 +47,7 @@ private File unzipOrCacheInternal(DataSet dataSet, File zip) throws IOException
} else {
target = Files.createTempDirectory(zip.getName()).toFile();
if (!(target.setReadable(true, true) && target.setWritable(true, true) && target.setExecutable(true, true))) {
logger.warning("Could not set permissions for temp directory (" + target.getAbsolutePath() + ").");
logger.warn("Could not set permissions for temp directory ({}).", target.getAbsolutePath());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.jplag.endtoend.architecture;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;

@AnalyzeClasses(packages = "de.jplag")
public class JPlagArchitectureTest {
@ArchTest
public static final ArchRule enforceCorrectLogger = noClasses().should().accessClassesThat()
.haveNameMatching(java.util.logging.Logger.class.getName());

@ArchTest
public static final ArchRule enforceNameOfLogger = fields().that().haveRawType(org.slf4j.Logger.class).should().haveName("logger");
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.Language;
import de.jplag.ParsingException;
Expand All @@ -40,7 +40,7 @@
public abstract class LanguageModuleTest {
private static final Path DEFAULT_TEST_CODE_PATH_BASE = Path.of("src", "test", "resources", "de", "jplag");

private final Logger LOG = Logger.getLogger(this.getClass().getName());
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final TestDataCollector collector;
private final Language language;
Expand Down Expand Up @@ -253,7 +253,7 @@ final void collectTestData() {

private List<Token> parseTokens(TestData source) throws ParsingException, IOException {
List<Token> tokens = source.parseTokens(this.language);
LOG.log(Level.INFO, TokenPrinter.printTokens(tokens));
logger.info(TokenPrinter.printTokens(tokens));
return tokens;
}

Expand Down
8 changes: 4 additions & 4 deletions report-viewer/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"*.ts": ["npx prettier --write", "npx eslint"],
"*.vue": ["npx prettier --write", "npx eslint"],
"*.js": ["npx prettier --write", "npx eslint"],
"*.html": ["npx prettier --write", "npx eslint"],
"*.ts": ["npx prettier --write", "npx eslint --max-warnings 0"],
"*.vue": ["npx prettier --write", "npx eslint --max-warnings 0"],
"*.js": ["npx prettier --write", "npx eslint --max-warnings 0"],
"*.html": ["npx prettier --write", "npx eslint --max-warnings 0"],
"*.css": ["npx prettier --write"]
}
Loading

0 comments on commit dc58ad6

Please sign in to comment.