From 97aec31f39ab6d8a6ff31e2a55903ca8e5403c81 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Fri, 16 Feb 2024 13:58:50 +0100 Subject: [PATCH 1/2] Fixed a bug in the creation of the result due to missing slashes. --- .../java/de/jplag/reporting/FilePathUtil.java | 19 ++++++++++++++ .../reportobject/ReportObjectFactory.java | 2 +- .../de/jplag/reporting/FilePathUtilTest.java | 26 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/de/jplag/reporting/FilePathUtilTest.java diff --git a/core/src/main/java/de/jplag/reporting/FilePathUtil.java b/core/src/main/java/de/jplag/reporting/FilePathUtil.java index d2db74ad7..3c8724bc8 100644 --- a/core/src/main/java/de/jplag/reporting/FilePathUtil.java +++ b/core/src/main/java/de/jplag/reporting/FilePathUtil.java @@ -26,4 +26,23 @@ public static String getRelativeSubmissionPath(File file, Submission submission, return Path.of(submissionToIdFunction.apply(submission), submission.getRoot().toPath().relativize(file.toPath()).toString()).toString(); } + /** + * Joins logical paths using a slash. This method ensures, that no duplicate slashes are created in between. + * @param left The left path segment + * @param right The right path segment + * @return The joined paths + */ + public static String joinPathSegments(String left, String right) { + String rightStripped = right; + while (rightStripped.startsWith("/")) { + rightStripped = rightStripped.substring(1); + } + + String leftStripped = left; + while (leftStripped.endsWith("/")) { + leftStripped = leftStripped.substring(0, leftStripped.length() - 1); + } + + return leftStripped + "/" + rightStripped; + } } 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 2354b254a..f18049707 100644 --- a/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java +++ b/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java @@ -112,7 +112,7 @@ private void copySubmissionFilesToReport(JPlagResult result) { if (relativeFilePath.isEmpty()) { relativeFilePath = file.getName(); } - String zipPath = submissionRootPath + relativeFilePath; + String zipPath = FilePathUtil.joinPathSegments(submissionRootPath, relativeFilePath); File fileToCopy = getFileToCopy(language, file); this.resultWriter.addFileContentEntry(zipPath, fileToCopy); diff --git a/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java b/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java new file mode 100644 index 000000000..97e2b276f --- /dev/null +++ b/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java @@ -0,0 +1,26 @@ +package de.jplag.reporting; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class FilePathUtilTest { + private static final String JOINED = "left/right"; + private static final String LEFT = "left"; + private static final String RIGHT = "right"; + + @Test + void testJoinPath() { + assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, RIGHT)); + } + + @Test + void testJoinPathWithLeftSlashSuffix() { + assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT + "/", RIGHT)); + } + + @Test + void testJoinPathWithRightSlashSuffix() { + assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, "/" + RIGHT)); + } +} \ No newline at end of file From 3dad24e97774a220914f87f28649db8b34caa4a6 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 20 Feb 2024 09:57:37 +0100 Subject: [PATCH 2/2] Improved naming of joinPathSegments --- core/src/main/java/de/jplag/reporting/FilePathUtil.java | 9 +++++---- .../reporting/reportobject/ReportObjectFactory.java | 2 +- .../test/java/de/jplag/reporting/FilePathUtilTest.java | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/de/jplag/reporting/FilePathUtil.java b/core/src/main/java/de/jplag/reporting/FilePathUtil.java index 3c8724bc8..51aefdd36 100644 --- a/core/src/main/java/de/jplag/reporting/FilePathUtil.java +++ b/core/src/main/java/de/jplag/reporting/FilePathUtil.java @@ -7,6 +7,7 @@ import de.jplag.Submission; public final class FilePathUtil { + private static final String ZIP_PATH_SEPARATOR = "/"; // Paths in zip files are always separated by a slash private FilePathUtil() { // private constructor to prevent instantiation @@ -32,17 +33,17 @@ public static String getRelativeSubmissionPath(File file, Submission submission, * @param right The right path segment * @return The joined paths */ - public static String joinPathSegments(String left, String right) { + public static String joinZipPathSegments(String left, String right) { String rightStripped = right; - while (rightStripped.startsWith("/")) { + while (rightStripped.startsWith(ZIP_PATH_SEPARATOR)) { rightStripped = rightStripped.substring(1); } String leftStripped = left; - while (leftStripped.endsWith("/")) { + while (leftStripped.endsWith(ZIP_PATH_SEPARATOR)) { leftStripped = leftStripped.substring(0, leftStripped.length() - 1); } - return leftStripped + "/" + rightStripped; + return leftStripped + ZIP_PATH_SEPARATOR + rightStripped; } } 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 f18049707..d569a5252 100644 --- a/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java +++ b/core/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java @@ -112,7 +112,7 @@ private void copySubmissionFilesToReport(JPlagResult result) { if (relativeFilePath.isEmpty()) { relativeFilePath = file.getName(); } - String zipPath = FilePathUtil.joinPathSegments(submissionRootPath, relativeFilePath); + String zipPath = FilePathUtil.joinZipPathSegments(submissionRootPath, relativeFilePath); File fileToCopy = getFileToCopy(language, file); this.resultWriter.addFileContentEntry(zipPath, fileToCopy); diff --git a/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java b/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java index 97e2b276f..91dd56462 100644 --- a/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java +++ b/core/src/test/java/de/jplag/reporting/FilePathUtilTest.java @@ -11,16 +11,16 @@ class FilePathUtilTest { @Test void testJoinPath() { - assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, RIGHT)); + assertEquals(JOINED, FilePathUtil.joinZipPathSegments(LEFT, RIGHT)); } @Test void testJoinPathWithLeftSlashSuffix() { - assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT + "/", RIGHT)); + assertEquals(JOINED, FilePathUtil.joinZipPathSegments(LEFT + "/", RIGHT)); } @Test void testJoinPathWithRightSlashSuffix() { - assertEquals(JOINED, FilePathUtil.joinPathSegments(LEFT, "/" + RIGHT)); + assertEquals(JOINED, FilePathUtil.joinZipPathSegments(LEFT, "/" + RIGHT)); } } \ No newline at end of file