From c53d5716c31d84565ff7be425dac8620cb3fadd8 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 26 Apr 2021 21:09:53 +0200 Subject: [PATCH] Add settingsprovider test --- .../java/app/settings/SettingsProvider.java | 12 ++- .../java/app/settings/SettingsProviderI.java | 10 +- .../app/settings/SettingsProviderTest.java | 93 +++++++++++++++---- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/main/java/app/settings/SettingsProvider.java b/src/main/java/app/settings/SettingsProvider.java index 35cc92f..6ed5a87 100644 --- a/src/main/java/app/settings/SettingsProvider.java +++ b/src/main/java/app/settings/SettingsProvider.java @@ -19,13 +19,19 @@ public class SettingsProvider implements SettingsProviderI { private EventBus eventBus; - private String SETTINGS_PATH = + private String settingsPath = (System.getProperty("os.name").startsWith("Windows")) ? System.getProperty("user.home") + "\\AppData\\Roaming\\/BNNsettings.dat" : System.getProperty("user.home") + System.getProperty("file.separator") + ".BNNsettings.dat"; private List legalSettings = Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); + + + // Only for testing purposes + protected void setSettingsPath(String settingsPath) { + this.settingsPath = settingsPath; + } public SettingsProvider(EventBus eB) { @@ -41,7 +47,7 @@ public class SettingsProvider implements SettingsProviderI { @Override public void loadSettings() { List settings = new ArrayList<>(); - try (Scanner sc = new Scanner(new File(SETTINGS_PATH))) { + try (Scanner sc = new Scanner(new File(settingsPath))) { while (sc.hasNextLine()) { var nextLine = sc.nextLine().trim(); @@ -69,7 +75,7 @@ public class SettingsProvider implements SettingsProviderI { @Override public void saveSettings() { - try (PrintWriter writer = new PrintWriter(new File(SETTINGS_PATH))) { + try (PrintWriter writer = new PrintWriter(new File(settingsPath))) { writer.println("- Settings:"); writer.println("Programming Language = " + Model.getLanguage().getName()); writer.println("Theme = " + Model.getTheme()); diff --git a/src/main/java/app/settings/SettingsProviderI.java b/src/main/java/app/settings/SettingsProviderI.java index 8bed651..e8e3ba6 100644 --- a/src/main/java/app/settings/SettingsProviderI.java +++ b/src/main/java/app/settings/SettingsProviderI.java @@ -2,8 +2,14 @@ package app.settings; public interface SettingsProviderI { - void loadSettings(); + /** + * Load settings from disk, and fire events to update the program state + */ + void loadSettings(); - void saveSettings(); + /** + * Save the state from {@link app.model.Model Model} to disk. + */ + void saveSettings(); } diff --git a/src/test/java/app/settings/SettingsProviderTest.java b/src/test/java/app/settings/SettingsProviderTest.java index 13f30e9..4346020 100644 --- a/src/test/java/app/settings/SettingsProviderTest.java +++ b/src/test/java/app/settings/SettingsProviderTest.java @@ -1,53 +1,108 @@ package app.settings; - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; +import java.nio.file.StandardOpenOption; import com.google.common.eventbus.EventBus; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; -import org.mockito.InjectMocks; -import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import app.model.Model; +import app.model.languages.Java; +import app.model.languages.Markdown; + +@ExtendWith(MockitoExtension.class) public class SettingsProviderTest { @TempDir File tmp; - @Mock - private String SETTINGS_PATH = Paths.get(tmp.toPath().toString(), "BNNsetting.dat").toString(); - - - @Mock - private List legalSettings = - Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); - - private EventBus eventBus; + private EventBus eventBus = new EventBus(); + private SettingsProvider sp = new SettingsProvider(eventBus); - @InjectMocks - private SettingsProvider sp; + @BeforeEach + private void initializeSettingsPath() { + sp.setSettingsPath(Paths.get(tmp.toPath().toString(), "BNNsettings.dat").toString()); + } @Test - @DisplayName("Test load settings") + @DisplayName("Test loadSettings with pre-existing settings file") public void testLoadSettings() throws IOException { - File f = new File(tmp, "test.txt"); + File f = new File(tmp, "BNNsettings.dat"); f.createNewFile(); + Files.writeString( + f.toPath(), + "- Settings:\n" + + "Programming Language = Markdown\n" + + "Theme = Solarized Light", + StandardOpenOption.WRITE + ); + + sp.loadSettings(); + assertTrue(Model.getLanguage() instanceof Markdown); + assertEquals("Solarized Light", Model.getTheme()); + } + + @Test + @DisplayName("Test loadSettings without pre-existing settings file") + public void testLoadSettingsWithoutFile() throws IOException { + + sp.loadSettings(); + assertTrue(Model.getLanguage() instanceof Java); + assertEquals("Monokai", Model.getTheme()); + } + + @Test + @DisplayName("Test loadSettings with broken settings file") + public void testLoadSettingsWithErrorFile() throws IOException { + File f = new File(tmp, "BNNsettings.dat"); + f.createNewFile(); + + Files.writeString( + f.toPath(), + "- Settings:\n" + + "Programming Language = Nonexisting Language\n" + + "Theme = Solarized Light", + StandardOpenOption.WRITE + ); + + sp.loadSettings(); + assertTrue(Model.getLanguage() instanceof Java); + assertEquals("Monokai", Model.getTheme()); } @Test @DisplayName("Test save settings") public void testSaveSettings() { - + Model.setLanguage(new Markdown()); + Model.setTheme("Solarized Light"); + + sp.saveSettings(); + try { + assertEquals( + "- Settings:\n" + + "Programming Language = Markdown\n" + + "Theme = Solarized Light\n", + Files.readString(Paths.get(tmp.toString(), "BNNsettings.dat")) + ); + } catch (IOException e) { + fail("Couldn't read settings file"); + } } }