Skip to content

Commit

Permalink
Merge pull request #76 from mkacct/misc
Browse files Browse the repository at this point in the history
Add equals() to classes and update GuiSystemTest
  • Loading branch information
rhit-shirakrk authored May 14, 2024
2 parents 524c5cc + 1cacea7 commit 9cb5073
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/main/java/datasource/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,17 @@ public Configuration applyChanges(Map<String, Object> changes) {
Map<String, Object> getData() {
return Collections.unmodifiableMap(this.data);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {return true;}
if (!(obj instanceof Configuration)) {return false;}
Configuration other = (Configuration)obj;
return this.data.equals(other.data);
}

@Override
public int hashCode() {
return this.data.hashCode();
}
}
49 changes: 48 additions & 1 deletion src/main/java/datasource/configspec/ConfigSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.Collections;
import java.util.List;

import general.CmpUtil;

/**
* The configuration specification, for generating GUI settings.
* The configuration specification, for generating GUI settings. Immutable.
*/
public final class ConfigSpec {
private final List<Section> sections;
Expand All @@ -19,6 +21,19 @@ public List<Section> getSections() {
return Collections.unmodifiableList(this.sections);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null || !(obj instanceof ConfigSpec)) {return false;}
ConfigSpec other = (ConfigSpec)obj;
return CmpUtil.areEqual(this.sections, other.sections);
}

@Override
public int hashCode() {
return this.sections.hashCode();
}

public static final class Section {
private static final String DEFAULT_ENTITY_TYPE = "check";

Expand Down Expand Up @@ -56,6 +71,22 @@ public List<Setting> getSettings() {
if (this.settings == null) {return List.of();}
return Collections.unmodifiableList(this.settings);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null || !(obj instanceof Section)) {return false;}
Section other = (Section)obj;
return CmpUtil.areEqual(this.title, other.title)
&& CmpUtil.areEqual(this.checkName, other.checkName)
&& CmpUtil.areEqual(this.entityType, other.entityType)
&& CmpUtil.areEqual(this.settings, other.settings);
}

@Override
public int hashCode() {
return this.title.hashCode();
}
}

public static final class Setting {
Expand Down Expand Up @@ -94,6 +125,22 @@ private void validateCanHaveOptions() {
if (this.type != Type.STRING) {throw new IllegalStateException("Only String type can have options");}
}

@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null || !(obj instanceof Setting)) {return false;}
Setting other = (Setting)obj;
return CmpUtil.areEqual(this.name, other.name)
&& CmpUtil.areEqual(this.type, other.type)
&& CmpUtil.areEqual(this.desc, other.desc)
&& CmpUtil.areEqual(this.options, other.options);
}

@Override
public int hashCode() {
return this.name.hashCode();
}

public static enum Type {
BOOLEAN,
INT,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/domain/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String toStringWithoutLevel() {
@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null || this.getClass() != obj.getClass()) {return false;}
if (obj == null || !(obj instanceof Message)) {return false;}
Message other = (Message)obj;
return (this.level == other.level) && this.text.equals(other.text) && this.classFullNames.equals(other.classFullNames);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/domain/javadata/VariableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public VariableData(String name, String typeFullName, String signature) {
@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null || this.getClass() != obj.getClass()) {return false;}
if (obj == null || !(obj instanceof VariableData)) {return false;}
VariableData other = (VariableData)obj;
return this.name.equals(other.name) && this.typeFullName.equals(other.typeFullName);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/general/CmpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package general;

public final class CmpUtil {
/**
* Checks equality in a null-safe manner. Good for use in equals() methods.
*/
public static <T> boolean areEqual(T a, T b) {
if (a == null) {return b == null;}
return a.equals(b);
}
}
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 9cb5073

Please sign in to comment.