Skip to content

Commit

Permalink
Development: Fix running server tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Strohgelaender committed Oct 8, 2023
1 parent d32f834 commit 9bad662
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends ZipEntry> zipFileEntries = zip.entries();
new File(newPath).mkdir();
Enumeration<? extends ZipEntry> 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);

Check warning on line 59 in src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java

View check run for this annotation

Teamscale / teamscale-findings

src/test/java/de/tum/in/www1/artemis/util/ZipFileTestUtilService.java#L59

[New] This method is slightly nested [0]. Consider extracting helper methods or reducing the nesting by using early breaks or returns. [0] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=chore%2Fserver-tests-windows%3AHEAD&id=6403FEC79E96F4A5AAEF1F60B985D2F1
}
}
}
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());
}
}
}
}
Expand Down

0 comments on commit 9bad662

Please sign in to comment.