Add FileTreeOperationTest
This commit is contained in:
parent
3b5600eba8
commit
da83d108b2
@ -7,6 +7,7 @@ import javafx.scene.control.TreeView;
|
|||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.input.MouseEvent;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@ -83,10 +84,14 @@ public class FiletreeController implements Initializable, Controller {
|
|||||||
if (event.getClickCount() == 2) {
|
if (event.getClickCount() == 2) {
|
||||||
TreeItem<String> item = filetree.getSelectionModel().getSelectedItem();
|
TreeItem<String> item = filetree.getSelectionModel().getSelectedItem();
|
||||||
|
|
||||||
Path path = FiletreeOperations.getPathOfTreeItem(item);
|
try {
|
||||||
|
Path path = FiletreeOperations.getPathOfTreeItem(item);
|
||||||
|
|
||||||
if (!Files.isDirectory(path)) {
|
if (!Files.isDirectory(path)) {
|
||||||
this.eventBus.post(new OpenFileEvent(Optional.ofNullable(path)));
|
this.eventBus.post(new OpenFileEvent(Optional.ofNullable(path)));
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.err.println("[ERROR]: Could not find filepath from filetree");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package app.service;
|
package app.service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -19,7 +20,6 @@ public class FiletreeOperations {
|
|||||||
|
|
||||||
private static int iconSize = 20;
|
private static int iconSize = 20;
|
||||||
|
|
||||||
// FIXME: File specific icons not working properly
|
|
||||||
// TODO: Clean up code that is not in use
|
// TODO: Clean up code that is not in use
|
||||||
// TODO: Error check for recursiveness, and files without icons
|
// TODO: Error check for recursiveness, and files without icons
|
||||||
|
|
||||||
@ -111,11 +111,14 @@ public class FiletreeOperations {
|
|||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getPathOfTreeItem(TreeItem<String> item) {
|
public static Path getPathOfTreeItem(TreeItem<String> item) throws FileNotFoundException {
|
||||||
final String rootFolderName =
|
Path projectPath =
|
||||||
Model
|
Model
|
||||||
.getProjectPath()
|
.getProjectPath()
|
||||||
.orElseThrow()
|
.orElseThrow(() -> new IllegalStateException());
|
||||||
|
|
||||||
|
final String rootFolderName =
|
||||||
|
projectPath
|
||||||
.getFileName()
|
.getFileName()
|
||||||
.toString();
|
.toString();
|
||||||
|
|
||||||
@ -123,9 +126,11 @@ public class FiletreeOperations {
|
|||||||
while (!rootFolderName.equals(item.getValue())) {
|
while (!rootFolderName.equals(item.getValue())) {
|
||||||
path = File.separator + item.getValue() + path;
|
path = File.separator + item.getValue() + path;
|
||||||
item = item.getParent();
|
item = item.getParent();
|
||||||
|
if (item == null)
|
||||||
|
throw new FileNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
path = Model.getProjectPath().orElseThrow().toString() + path;
|
path = projectPath.toString() + path;
|
||||||
|
|
||||||
Path pathToString = Paths.get(path);
|
Path pathToString = Paths.get(path);
|
||||||
|
|
||||||
|
68
src/test/java/app/service/FileTreeOperationsTest.java
Normal file
68
src/test/java/app/service/FileTreeOperationsTest.java
Normal file
@ -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<String> file) {
|
||||||
|
Path traversalPath = filePath;
|
||||||
|
TreeItem<String> child = file;
|
||||||
|
while (!traversalPath.equals(root)) {
|
||||||
|
System.out.println(traversalPath);
|
||||||
|
traversalPath = traversalPath.getParent();
|
||||||
|
TreeItem<String> parent = new TreeItem<>(traversalPath.getFileName().toString());
|
||||||
|
parent.getChildren().add(child);
|
||||||
|
child = parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test getPathOftreeItem")
|
||||||
|
public void testGetPathOfTreeItem() {
|
||||||
|
|
||||||
|
try (MockedStatic<Model> model = Mockito.mockStatic(Model.class)) {
|
||||||
|
TreeItem<String> 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -141,8 +141,8 @@ class TestClass {
|
|||||||
.create();
|
.create();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("")
|
@DisplayName("test syntaxHighlight")
|
||||||
public void testSyntaxHighlighting() {
|
public void testSyntaxHighlight() {
|
||||||
StyleSpans<Collection<String>> highlightData =
|
StyleSpans<Collection<String>> highlightData =
|
||||||
LanguageOperations.syntaxHighlight(javaCode, new Java());
|
LanguageOperations.syntaxHighlight(javaCode, new Java());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user