From 909e241c5b3200d8867b0d2ad63d8c82d1a715ea Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 26 Apr 2021 16:52:12 +0200 Subject: [PATCH] Flush --- src/main/java/app/MainController.java | 4 +- src/main/java/app/MainLauncher.java | 8 + .../app/controllers/EditorControllerTest.java | 11 +- .../java/app/service/FileOperationsTest.java | 168 ++++++++++++++++++ .../app/settings/SettingsProviderTest.java | 18 +- 5 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 src/test/java/app/service/FileOperationsTest.java diff --git a/src/main/java/app/MainController.java b/src/main/java/app/MainController.java index 06d4bc4..f231514 100644 --- a/src/main/java/app/MainController.java +++ b/src/main/java/app/MainController.java @@ -69,7 +69,9 @@ public class MainController implements Initializable { return hostServices; } - // TODO: Document + /** + * @return All subcontrollers of this controller + */ public List getInnerControllers() { return List.of(editorController, filetreeController, modelineController, menubarController); } diff --git a/src/main/java/app/MainLauncher.java b/src/main/java/app/MainLauncher.java index 14ef3ce..882f3c8 100644 --- a/src/main/java/app/MainLauncher.java +++ b/src/main/java/app/MainLauncher.java @@ -1,6 +1,14 @@ package app; +/** + * A launcher class to point towards as the start point for a packaged JAR + */ public class MainLauncher { + /** + * The root function of the call stack + * + * @param args Commandline arguments + */ public static void main(String[] args) { Main.main(args); } diff --git a/src/test/java/app/controllers/EditorControllerTest.java b/src/test/java/app/controllers/EditorControllerTest.java index a8d14d1..63ea51c 100644 --- a/src/test/java/app/controllers/EditorControllerTest.java +++ b/src/test/java/app/controllers/EditorControllerTest.java @@ -2,6 +2,7 @@ package app.controllers; import java.io.File; import java.io.IOException; +import java.nio.file.Paths; import java.util.Optional; import org.fxmisc.richtext.CodeArea; @@ -100,11 +101,15 @@ public class EditorControllerTest extends FxTestTemplate { @Test @DisplayName("Test handling of OpenFileEvent with a file that doesn't exist") public void testOpenFileEventWithUnrealFile() throws IOException { + try (MockedStatic mocked = mockStatic(FileOperations.class)) { + mocked.when(() -> FileOperations.readFile(any())) + .thenReturn(null); - String brokenFilePath = "/doesNotExist.txt"; - eventBus.post(new OpenFileEvent(Optional.ofNullable(new File(brokenFilePath).toPath()))); + String brokenFilePath = "/doesNotExist.txt"; + eventBus.post(new OpenFileEvent(Optional.ofNullable(Paths.get(brokenFilePath)))); - verify(editor, never()).clear(); + verify(editor, never()).clear(); + } } @Test diff --git a/src/test/java/app/service/FileOperationsTest.java b/src/test/java/app/service/FileOperationsTest.java new file mode 100644 index 0000000..8bbe2a4 --- /dev/null +++ b/src/test/java/app/service/FileOperationsTest.java @@ -0,0 +1,168 @@ +package app.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import com.google.common.io.Files; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.MockedStatic; + +import app.model.Model; +import javafx.stage.Stage; + +public class FileOperationsTest { + + @TempDir + File tmp; + + @Test + @DisplayName("Test openFileWithDialog") + public void testOpenFileWithDialog() { + try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { + Stage stage = mock(Stage.class); + + mocked.when(() -> DialogBoxes.showopenFileWithDialog(any())) + .thenReturn(null); + assertThrows(FileNotFoundException.class, () -> FileOperations.openFileWithDialog(stage)); + + File file = mock(File.class); + mocked.when(() -> DialogBoxes.showopenFileWithDialog(any())) + .thenReturn(file); + try { + assertEquals(file, FileOperations.openFileWithDialog(stage)); + } catch (FileNotFoundException e) { + fail("Chosen file was null when it was expected to be mock file"); + } + } + } + + + @Test + @DisplayName("Test openFolderWithDialog") + public void testOpenFolderWithDialog() { + try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { + Stage stage = mock(Stage.class); + + mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) + .thenReturn(null); + assertThrows(FileNotFoundException.class, () -> FileOperations.openFolderWithDialog(stage)); + + File file = mock(File.class); + mocked.when(() -> DialogBoxes.showOpenFolderWithDialog(any())) + .thenReturn(file); + try { + assertEquals(file, FileOperations.openFolderWithDialog(stage)); + } catch (FileNotFoundException e) { + fail("Chosen file was null when it was expected to be mock file"); + } + + } + } + + private File createTemporaryFile() throws IOException { + File f = new File(tmp, "test.txt"); + f.createNewFile(); + return f; + } + + @Test + @DisplayName("Test saveFile") + public void testSaveFile() { + String content = "test\ncontent\nfor\nyou"; + File f; + + try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { + // mocked.when(() -> DialogBoxes.showErrorMessage(anyString())); + + f = createTemporaryFile(); + assertTrue(FileOperations.saveFile(f.toPath(), content)); + + List read = Files.readLines(f, StandardCharsets.UTF_8); + String value = String.join("\n", read); + assertEquals(content, value); + + Path wrongPath = Paths.get("wrongPath.txt"); + assertFalse(FileOperations.saveFile(wrongPath, content)); + + } catch (IOException e) { + fail("Unexpected temporary file failure"); + } + } + + @Test + @DisplayName("Test saveFileWithDialog") + public void testSaveFileWithDialog() { + String content = "test\ncontent\nfor\nyou"; + File f; + + try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { + Stage stage = mock(Stage.class); + + mocked.when(() -> DialogBoxes.showSaveFileWithDialog(any())) + .thenReturn(false); + assertFalse(FileOperations.saveFileWithDialog(stage, content)); + + mocked.when(() -> DialogBoxes.showSaveFileWithDialog(any())) + .thenReturn(null); + assertFalse(FileOperations.saveFileWithDialog(stage, content)); + + f = createTemporaryFile(); + mocked.when(() -> DialogBoxes.showSaveFileWithDialog(any())) + .thenReturn(f); + assertTrue(FileOperations.saveFileWithDialog(stage, content)); + assertEquals(Model.getActiveFilePath(), f.toPath()); + + File wrongFile = new File("Does not exist"); + mocked.when(() -> DialogBoxes.showSaveFileWithDialog(any())) + .thenReturn(wrongFile); + assertFalse(FileOperations.saveFileWithDialog(stage, content)); + } catch (IOException e) { + fail("Unexpected IOexception when creating temporary file"); + } + } + + @Test + @DisplayName("Test readFile") + public void testReadFile() { + File f; + + try (MockedStatic mocked = mockStatic(DialogBoxes.class)) { + // mocked.when(() -> DialogBoxes.showErrorMessage(anyString())); + + assertEquals("", FileOperations.readFile(null)); + + String content = "test\ncontent\nfor\nyou"; + f = createTemporaryFile(); + + Files.write(content.getBytes(), f); + + assertEquals(content, FileOperations.readFile(f.toPath())); + + Path wrongPath = Paths.get("wrongPath.txt"); + assertThrows(FileNotFoundException.class, () -> FileOperations.readFile(wrongPath)); + + + } catch (IOException e) { + fail("Unexpected temporary file failure"); + } + } + +} diff --git a/src/test/java/app/settings/SettingsProviderTest.java b/src/test/java/app/settings/SettingsProviderTest.java index 84ea332..f5bad89 100644 --- a/src/test/java/app/settings/SettingsProviderTest.java +++ b/src/test/java/app/settings/SettingsProviderTest.java @@ -5,10 +5,26 @@ import org.junit.jupiter.api.Test; public class SettingsProviderTest { + @Mock + private static final String SETTINGS_PATH = + (System.getProperty("os.name").startsWith("Windows")) + ? System.getProperty("user.home") + "\\AppData\\Roaming\\/BNNsettings.dat" + : System.getProperty("user.home") + System.getProperty("file.separator") + ".BNNsettings.dat"; + + @Mock + private static List legalSettings = + Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); + + private static EventBus eventBus; + + + @InjectMocks + private SettingsProvider sp; + @Test @DisplayName("Test load settings") public void testLoadSettings() { - + sp.when() }