diff --git a/core/src/main/java/de/jplag/reporting/FilePathUtil.java b/core/src/main/java/de/jplag/reporting/FilePathUtil.java index d2db74ad7..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 @@ -26,4 +27,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 joinZipPathSegments(String left, String right) { + String rightStripped = right; + while (rightStripped.startsWith(ZIP_PATH_SEPARATOR)) { + rightStripped = rightStripped.substring(1); + } + + String leftStripped = left; + while (leftStripped.endsWith(ZIP_PATH_SEPARATOR)) { + leftStripped = leftStripped.substring(0, leftStripped.length() - 1); + } + + 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 2354b254a..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 = 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 new file mode 100644 index 000000000..91dd56462 --- /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.joinZipPathSegments(LEFT, RIGHT)); + } + + @Test + void testJoinPathWithLeftSlashSuffix() { + assertEquals(JOINED, FilePathUtil.joinZipPathSegments(LEFT + "/", RIGHT)); + } + + @Test + void testJoinPathWithRightSlashSuffix() { + assertEquals(JOINED, FilePathUtil.joinZipPathSegments(LEFT, "/" + RIGHT)); + } +} \ No newline at end of file