Add FileTreeOperationTest

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-25 00:03:48 +02:00
parent 3b5600eba8
commit da83d108b2
4 changed files with 88 additions and 10 deletions

View File

@ -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<String> 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");
}
}
}

View File

@ -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<String> item) {
final String rootFolderName =
public static Path getPathOfTreeItem(TreeItem<String> 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);

View 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!");
}
}
}

View File

@ -141,8 +141,8 @@ class TestClass {
.create();
@Test
@DisplayName("")
public void testSyntaxHighlighting() {
@DisplayName("test syntaxHighlight")
public void testSyntaxHighlight() {
StyleSpans<Collection<String>> highlightData =
LanguageOperations.syntaxHighlight(javaCode, new Java());