From 9bad66268fb23c8a1d702e797426eb7cb000d12a Mon Sep 17 00:00:00 2001 From: Lucas Welscher Date: Sun, 8 Oct 2023 15:51:37 +0200 Subject: [PATCH] `Development`: Fix running server tests on windows --- .../www1/artemis/service/FileServiceTest.java | 9 ++- .../artemis/util/ZipFileTestUtilService.java | 62 +++++++++---------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/test/java/de/tum/in/www1/artemis/service/FileServiceTest.java b/src/test/java/de/tum/in/www1/artemis/service/FileServiceTest.java index 7f441d5d2778..9e15e8e33a2d 100644 --- a/src/test/java/de/tum/in/www1/artemis/service/FileServiceTest.java +++ b/src/test/java/de/tum/in/www1/artemis/service/FileServiceTest.java @@ -46,16 +46,21 @@ class FileServiceTest extends AbstractSpringIntegrationIndependentTest { private static final URI VALID_BACKGROUND_PATH = URI.create("/api/uploads/images/drag-and-drop/backgrounds/1/BackgroundFile.jpg"); - private static final URI VALID_INTENDED_BACKGROUND_PATH = URI.create("/api/" + FilePathService.getDragAndDropBackgroundFilePath() + "/"); + private static final URI VALID_INTENDED_BACKGROUND_PATH = createURIWithPath("/api/", FilePathService.getDragAndDropBackgroundFilePath()); private static final URI INVALID_BACKGROUND_PATH = URI.create("/api/uploads/images/drag-and-drop/backgrounds/1/../../../exam-users/signatures/some-file.png"); private static final URI VALID_DRAGITEM_PATH = URI.create("/api/uploads/images/drag-and-drop/drag-items/1/PictureFile.jpg"); - private static final URI VALID_INTENDED_DRAGITEM_PATH = URI.create("/api/" + FilePathService.getDragItemFilePath() + "/"); + private static final URI VALID_INTENDED_DRAGITEM_PATH = createURIWithPath("/api/", FilePathService.getDragItemFilePath()); private static final URI INVALID_DRAGITEM_PATH = URI.create("/api/uploads/images/drag-and-drop/drag-items/1/../../../exam-users/signatures/some-file.png"); + private static URI createURIWithPath(String prefix, Path path) { + String replacementForWindows = path.toString().replace('\\', '/'); + return URI.create(prefix + replacementForWindows + '/'); + } + @AfterEach void cleanup() throws IOException { Files.deleteIfExists(javaPath); diff --git a/src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java b/src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java index 6b7b584ce941..57834d0a1454 100644 --- a/src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java +++ b/src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java @@ -28,45 +28,43 @@ public void extractZipFileRecursively(String zipFile) throws IOException { int BUFFER = 2048; File file = new File(zipFile); - ZipFile zip = new ZipFile(file); - String newPath = zipFile.substring(0, zipFile.length() - 4); + try (ZipFile zip = new ZipFile(file)) { + String newPath = zipFile.substring(0, zipFile.length() - 4); - new File(newPath).mkdir(); - Enumeration zipFileEntries = zip.entries(); + new File(newPath).mkdir(); + Enumeration zipFileEntries = zip.entries(); - // Process each entry - while (zipFileEntries.hasMoreElements()) { - // grab a zip file entry - ZipEntry entry = zipFileEntries.nextElement(); - String currentEntry = entry.getName(); - File destFile = new File(newPath, currentEntry); - File destinationParent = destFile.getParentFile(); + // Process each entry + while (zipFileEntries.hasMoreElements()) { + // grab a zip file entry + ZipEntry entry = zipFileEntries.nextElement(); + String currentEntry = entry.getName(); + File destFile = new File(newPath, currentEntry); + File destinationParent = destFile.getParentFile(); - // create the parent directory structure if needed - destinationParent.mkdirs(); + // create the parent directory structure if needed + destinationParent.mkdirs(); - if (!entry.isDirectory()) { - BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); - int currentByte; - // establish buffer for writing file - byte[] data = new byte[BUFFER]; + if (!entry.isDirectory()) { + int currentByte; + // establish buffer for writing file + byte[] data = new byte[BUFFER]; + try (BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); + FileOutputStream fos = new FileOutputStream(destFile); + BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) { - // write the current file to disk - FileOutputStream fos = new FileOutputStream(destFile); - BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); - - // read and write until last byte is encountered - while ((currentByte = is.read(data, 0, BUFFER)) != -1) { - dest.write(data, 0, currentByte); + // write the current file to disk + // read and write until last byte is encountered + while ((currentByte = is.read(data, 0, BUFFER)) != -1) { + dest.write(data, 0, currentByte); + } + } } - dest.flush(); - dest.close(); - is.close(); - } - if (currentEntry.endsWith(".zip")) { - // found a zip file, try to open - extractZipFileRecursively(destFile.getAbsolutePath()); + if (currentEntry.endsWith(".zip")) { + // found a zip file, try to open + extractZipFileRecursively(destFile.getAbsolutePath()); + } } } }