Skip to content

Commit

Permalink
Improve GuiSystemTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mkacct committed May 14, 2024
1 parent c63dc86 commit 1cacea7
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/test/java/gui/GuiSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -18,6 +17,7 @@

import datasource.ConfigRW;
import datasource.Configuration;
import datasource.configspec.ConfigSpec;
import datasource.configspec.ConfigSpecLoader;
import datasource.configspec.JsonFileConfigSpecLoader;
import domain.Check;
Expand All @@ -32,6 +32,7 @@ public class GuiSystemTest {
private static final String TEST_RESOURCES_DIR_PATH = "src/test/resources/system-test";
private static final String CLASS_DIR_PATH = TEST_RESOURCES_DIR_PATH + "/classes";
private static final String TEST_CONFIG_SPEC_RES_PATH = "/test-config-spec.json";
private static final ConfigSpec TEST_CONFIG_SPEC = (new JsonFileConfigSpecLoader(TEST_CONFIG_SPEC_RES_PATH)).loadConfigSpec();

private static final Check[] CHECKS = new Check[] {
new NamingConventionsCheck(),
Expand Down Expand Up @@ -107,6 +108,7 @@ public Configuration loadConfig() throws IOException {
public void testDefaultInitialState() {
App app = new App(CHECKS, createTestConfigSpecLoader(), null);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertNull(app.retrieveConfigLoadEx());
assertFalse(app.canRunNow());
assertEquals("", app.getTargetPath());
Expand All @@ -118,6 +120,7 @@ public void testWithConfigInitialState() {
FakeConfigRW configRW = createFakeConfigRW(CONFIG);
App app = new App(CHECKS, createTestConfigSpecLoader(), configRW);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertNull(app.retrieveConfigLoadEx());
assertTrue(app.canRunNow());
assertEquals(CLASS_DIR_PATH, app.getTargetPath());
Expand All @@ -130,6 +133,7 @@ public void testWithNonexistentConfigInitialState() {
FakeConfigRW configRW = createNonexistentConfigRW();
App app = new App(CHECKS, createTestConfigSpecLoader(), configRW);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertNull(app.retrieveConfigLoadEx());
assertFalse(configRW.didTryToLoad());
assertFalse(app.canRunNow());
Expand All @@ -142,6 +146,7 @@ public void testWithBadConfigInitialState() {
FakeConfigRW configRW = createBadConfigRW();
App app = new App(CHECKS, createTestConfigSpecLoader(), configRW);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertInstanceOf(IOException.class, app.retrieveConfigLoadEx());
assertFalse(app.canRunNow());
assertEquals("", app.getTargetPath());
Expand All @@ -154,6 +159,7 @@ public void testRunInBadState() {
ReloadCounter reloadCounter = new ReloadCounter();
app.addReloader(reloadCounter);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertFalse(app.canRunNow());
assertThrows(IllegalStateException.class, () -> {
app.runChecks();
Expand All @@ -169,6 +175,7 @@ public void testRunWithBadPath() {
app.setTargetPath("this is not a real directory");
assertEquals(1, reloadCounter.getReloadCount());

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertTrue(app.canRunNow());
assertThrows(IOException.class, () -> {
app.runChecks();
Expand All @@ -187,6 +194,7 @@ public void testRunDefault() throws IOException {
app.setTargetPath(CLASS_DIR_PATH);
assertEquals(1, reloadCounter.getReloadCount());

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertTrue(app.canRunNow());
app.runChecks();
assertEquals(2, reloadCounter.getReloadCount());
Expand Down Expand Up @@ -222,6 +230,7 @@ public void testRunWithConfig() throws IOException {
ReloadCounter reloadCounter = new ReloadCounter();
app.addReloader(reloadCounter);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertTrue(app.canRunNow());
app.runChecks();
assertEquals(1, reloadCounter.getReloadCount());
Expand Down Expand Up @@ -258,20 +267,50 @@ public void testSaveTargetPathToConfig() {
ReloadCounter reloadCounter = new ReloadCounter();
app.addReloader(reloadCounter);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertTrue(app.canRunNow());
assertEquals(CLASS_DIR_PATH, app.getTargetPath());
assertNull(configRW.retrieveLastSavedConfig());
app.setTargetPath("asdf");
Configuration savedConfig = configRW.retrieveLastSavedConfig();
assertNotNull(savedConfig);
assertEquals("asdf", savedConfig.getString("guiAppTargetPath"));
assertEquals(CONFIG.applyChanges(Map.of("guiAppTargetPath", "asdf")), savedConfig);
assertEquals("asdf", app.getTargetPath());

assertNoResults(app);
assertTrue(app.canRunNow());
assertNull(configRW.retrieveLastSavedConfig());
}

@Test
public void testGetConfig() {
FakeConfigRW configRW = createFakeConfigRW(CONFIG);
App app = new App(CHECKS, createTestConfigSpecLoader(), configRW);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
assertEquals(CONFIG, app.getConfig());
assertNull(configRW.retrieveLastSavedConfig());
}

@Test
public void testUpdateConfig() {
FakeConfigRW configRW = createFakeConfigRW(CONFIG);
App app = new App(CHECKS, createTestConfigSpecLoader(), configRW);
ReloadCounter reloadCounter = new ReloadCounter();
app.addReloader(reloadCounter);

assertEquals(TEST_CONFIG_SPEC, app.configSpec);
Map<String, Object> changes = Map.of(
"convConstant", "snake_case",
"foo", 43
);
Configuration expectedConfig = CONFIG.applyChanges(changes);
app.updateConfig(changes);
assertEquals(1, reloadCounter.getReloadCount());
assertEquals(expectedConfig, app.getConfig());
Configuration savedConfig = configRW.retrieveLastSavedConfig();
assertEquals(expectedConfig, savedConfig);
}

private static void assertNoResults(App app) {
assertFalse(app.hasResults());
assertEquals(0, app.getNumChecksRun());
Expand Down

0 comments on commit 1cacea7

Please sign in to comment.