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 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";
@ -28,6 +28,12 @@ public class SettingsProvider implements SettingsProviderI {
Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light");
// Only for testing purposes
protected void setSettingsPath(String settingsPath) {
this.settingsPath = settingsPath;
}
public SettingsProvider(EventBus eB) {
setEventBus(eB);
Model.setSettingsProvider(this);
@ -41,7 +47,7 @@ public class SettingsProvider implements SettingsProviderI {
@Override
public void loadSettings() {
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()) {
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());

View File

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

View File

@ -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();
private EventBus eventBus = new EventBus();
private SettingsProvider sp = new SettingsProvider(eventBus);
@Mock
private List<String> legalSettings =
Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light");
private EventBus 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");
}
}
}