Add settingsprovider test

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-26 21:09:53 +02:00
parent 057c1e0074
commit c53d5716c3
3 changed files with 91 additions and 24 deletions

View File

@ -19,7 +19,7 @@ public class SettingsProvider implements SettingsProviderI {
private EventBus eventBus; private EventBus eventBus;
private String SETTINGS_PATH = private String settingsPath =
(System.getProperty("os.name").startsWith("Windows")) (System.getProperty("os.name").startsWith("Windows"))
? System.getProperty("user.home") + "\\AppData\\Roaming\\/BNNsettings.dat" ? System.getProperty("user.home") + "\\AppData\\Roaming\\/BNNsettings.dat"
: System.getProperty("user.home") + System.getProperty("file.separator") + ".BNNsettings.dat"; : System.getProperty("user.home") + System.getProperty("file.separator") + ".BNNsettings.dat";
@ -28,6 +28,12 @@ public class SettingsProvider implements SettingsProviderI {
Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light");
// Only for testing purposes
protected void setSettingsPath(String settingsPath) {
this.settingsPath = settingsPath;
}
public SettingsProvider(EventBus eB) { public SettingsProvider(EventBus eB) {
setEventBus(eB); setEventBus(eB);
Model.setSettingsProvider(this); Model.setSettingsProvider(this);
@ -41,7 +47,7 @@ public class SettingsProvider implements SettingsProviderI {
@Override @Override
public void loadSettings() { public void loadSettings() {
List<String> settings = new ArrayList<>(); List<String> settings = new ArrayList<>();
try (Scanner sc = new Scanner(new File(SETTINGS_PATH))) { try (Scanner sc = new Scanner(new File(settingsPath))) {
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
var nextLine = sc.nextLine().trim(); var nextLine = sc.nextLine().trim();
@ -69,7 +75,7 @@ public class SettingsProvider implements SettingsProviderI {
@Override @Override
public void saveSettings() { 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("- Settings:");
writer.println("Programming Language = " + Model.getLanguage().getName()); writer.println("Programming Language = " + Model.getLanguage().getName());
writer.println("Theme = " + Model.getTheme()); writer.println("Theme = " + Model.getTheme());

View File

@ -2,8 +2,14 @@ package app.settings;
public interface SettingsProviderI { public interface SettingsProviderI {
/**
* Load settings from disk, and fire events to update the program state
*/
void loadSettings(); void loadSettings();
/**
* Save the state from {@link app.model.Model Model} to disk.
*/
void saveSettings(); void saveSettings();
} }

View File

@ -1,53 +1,108 @@
package app.settings; 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.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.nio.file.StandardOpenOption;
import java.util.List;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.mockito.InjectMocks; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.Mock;
import app.model.Model;
import app.model.languages.Java;
import app.model.languages.Markdown;
@ExtendWith(MockitoExtension.class)
public class SettingsProviderTest { public class SettingsProviderTest {
@TempDir @TempDir
File tmp; File tmp;
@Mock private EventBus eventBus = new EventBus();
private String SETTINGS_PATH = Paths.get(tmp.toPath().toString(), "BNNsetting.dat").toString();
private SettingsProvider sp = new SettingsProvider(eventBus);
@Mock @BeforeEach
private List<String> legalSettings = private void initializeSettingsPath() {
Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); sp.setSettingsPath(Paths.get(tmp.toPath().toString(), "BNNsettings.dat").toString());
}
private EventBus eventBus;
@InjectMocks
private SettingsProvider sp;
@Test @Test
@DisplayName("Test load settings") @DisplayName("Test loadSettings with pre-existing settings file")
public void testLoadSettings() throws IOException { public void testLoadSettings() throws IOException {
File f = new File(tmp, "test.txt"); File f = new File(tmp, "BNNsettings.dat");
f.createNewFile(); 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 @Test
@DisplayName("Test save settings") @DisplayName("Test save settings")
public void testSaveSettings() { 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");
}
} }
} }