From da83d108b2df26a3996ea34f093214522c6873ca Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 25 Apr 2021 00:03:48 +0200 Subject: [PATCH] Add FileTreeOperationTest --- .../app/controllers/FiletreeController.java | 11 ++- .../java/app/service/FiletreeOperations.java | 15 ++-- .../app/service/FileTreeOperationsTest.java | 68 +++++++++++++++++++ .../app/service/LanguageOperationsTest.java | 4 +- 4 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/test/java/app/service/FileTreeOperationsTest.java diff --git a/src/main/java/app/controllers/FiletreeController.java b/src/main/java/app/controllers/FiletreeController.java index 1cd6f6d..2f1ce65 100644 --- a/src/main/java/app/controllers/FiletreeController.java +++ b/src/main/java/app/controllers/FiletreeController.java @@ -7,6 +7,7 @@ import javafx.scene.control.TreeView; import javafx.scene.input.MouseEvent; import java.io.File; +import java.io.FileNotFoundException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -83,10 +84,14 @@ public class FiletreeController implements Initializable, Controller { if (event.getClickCount() == 2) { TreeItem item = filetree.getSelectionModel().getSelectedItem(); - Path path = FiletreeOperations.getPathOfTreeItem(item); + try { + Path path = FiletreeOperations.getPathOfTreeItem(item); - if (!Files.isDirectory(path)) { - this.eventBus.post(new OpenFileEvent(Optional.ofNullable(path))); + if (!Files.isDirectory(path)) { + this.eventBus.post(new OpenFileEvent(Optional.ofNullable(path))); + } + } catch (FileNotFoundException e) { + System.err.println("[ERROR]: Could not find filepath from filetree"); } } } diff --git a/src/main/java/app/service/FiletreeOperations.java b/src/main/java/app/service/FiletreeOperations.java index e3c6119..832d76b 100644 --- a/src/main/java/app/service/FiletreeOperations.java +++ b/src/main/java/app/service/FiletreeOperations.java @@ -1,6 +1,7 @@ package app.service; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -19,7 +20,6 @@ public class FiletreeOperations { private static int iconSize = 20; - // FIXME: File specific icons not working properly // TODO: Clean up code that is not in use // TODO: Error check for recursiveness, and files without icons @@ -111,11 +111,14 @@ public class FiletreeOperations { return icon; } - public static Path getPathOfTreeItem(TreeItem item) { - final String rootFolderName = + public static Path getPathOfTreeItem(TreeItem item) throws FileNotFoundException { + Path projectPath = Model .getProjectPath() - .orElseThrow() + .orElseThrow(() -> new IllegalStateException()); + + final String rootFolderName = + projectPath .getFileName() .toString(); @@ -123,9 +126,11 @@ public class FiletreeOperations { while (!rootFolderName.equals(item.getValue())) { path = File.separator + item.getValue() + path; item = item.getParent(); + if (item == null) + throw new FileNotFoundException(); } - path = Model.getProjectPath().orElseThrow().toString() + path; + path = projectPath.toString() + path; Path pathToString = Paths.get(path); diff --git a/src/test/java/app/service/FileTreeOperationsTest.java b/src/test/java/app/service/FileTreeOperationsTest.java new file mode 100644 index 0000000..7862588 --- /dev/null +++ b/src/test/java/app/service/FileTreeOperationsTest.java @@ -0,0 +1,68 @@ +package app.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.FileNotFoundException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import app.model.Model; +import javafx.scene.control.TreeItem; + +public class FileTreeOperationsTest { + + @Test + @DisplayName("Test generateTree") + public void testGenerateTree() { + + } + + private void generateTreeItemsFromPathsAndChild(Path root, Path filePath, TreeItem file) { + Path traversalPath = filePath; + TreeItem child = file; + while (!traversalPath.equals(root)) { + System.out.println(traversalPath); + traversalPath = traversalPath.getParent(); + TreeItem parent = new TreeItem<>(traversalPath.getFileName().toString()); + parent.getChildren().add(child); + child = parent; + } + } + + @Test + @DisplayName("Test getPathOftreeItem") + public void testGetPathOfTreeItem() { + + try (MockedStatic model = Mockito.mockStatic(Model.class)) { + TreeItem treeItem = new TreeItem<>("file.txt"); + + model.when(Model::getProjectPath).thenReturn(Optional.empty()); + assertThrows(IllegalStateException.class, () -> FiletreeOperations.getPathOfTreeItem(treeItem)); + + Path root = Paths.get("/tmp"); + Path filePath = Paths.get("/tmp/testfolder/folder1/folder2/file.txt"); + generateTreeItemsFromPathsAndChild(root, filePath, treeItem); + + model.when(Model::getProjectPath).thenReturn(Optional.of(root)); + + Path pathOfTreeItem = FiletreeOperations.getPathOfTreeItem(treeItem); + assertEquals(filePath, pathOfTreeItem); + + Path illegalRoot = Paths.get("/var"); + model.when(Model::getProjectPath).thenReturn(Optional.of(illegalRoot)); + assertThrows(FileNotFoundException.class, () -> FiletreeOperations.getPathOfTreeItem(treeItem)); + + } catch (FileNotFoundException e) { + fail("Paths did not match!"); + } + } + +} diff --git a/src/test/java/app/service/LanguageOperationsTest.java b/src/test/java/app/service/LanguageOperationsTest.java index 4261383..237ef4c 100644 --- a/src/test/java/app/service/LanguageOperationsTest.java +++ b/src/test/java/app/service/LanguageOperationsTest.java @@ -141,8 +141,8 @@ class TestClass { .create(); @Test - @DisplayName("") - public void testSyntaxHighlighting() { + @DisplayName("test syntaxHighlight") + public void testSyntaxHighlight() { StyleSpans> highlightData = LanguageOperations.syntaxHighlight(javaCode, new Java());