Skip to content

Commit

Permalink
Merge pull request #1568 from jplag/bug/submissionFilePathsMissingSlash
Browse files Browse the repository at this point in the history
Fixed a bug in the creation of the result due to missing slashes.
  • Loading branch information
tsaglam authored Feb 20, 2024
2 parents 44a6a8d + 3dad24e commit eaea554
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
20 changes: 20 additions & 0 deletions core/src/main/java/de/jplag/reporting/FilePathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/de/jplag/reporting/FilePathUtilTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit eaea554

Please sign in to comment.