From: David ‘Bombe’ Roden Date: Mon, 17 Mar 2014 19:38:35 +0000 (+0100) Subject: Add unit test for state manager. X-Git-Tag: v2~258 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=80f8ecce60252f36558bce5893691df4d68d9892;p=rhynodge.git Add unit test for state manager. --- diff --git a/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java b/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java new file mode 100644 index 0000000..a7361f9 --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java @@ -0,0 +1,110 @@ +package net.pterodactylus.rhynodge.states; + +import static com.google.common.base.Charsets.UTF_8; +import static com.google.common.base.Objects.equal; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import java.io.File; +import java.io.IOException; + +import net.pterodactylus.rhynodge.State; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Optional; +import com.google.common.io.Files; +import org.hamcrest.Matchers; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Unit test for {@link StateManager}. + * + * @author David ‘Bombe’ Roden + */ +public class StateManagerTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + private final File statePath; + private final StateManager stateManager; + + public StateManagerTest() throws IOException { + tempFolder.create(); + statePath = tempFolder.newFolder(); + stateManager = new StateManager(statePath.getPath()); + } + + @Test + public void successStateCanBeRestored() { + TestState testState = new TestState(); + stateManager.saveState("test", testState); + Optional restoredState = stateManager.loadLastState("test"); + assertThat(restoredState.get(), Matchers.is(testState)); + restoredState = stateManager.loadLastSuccessfulState("test"); + assertThat(restoredState.get(), Matchers.is(testState)); + } + + @Test + public void failStateIsNotSavedAsSuccessfulState() { + TestState testState = new TestState(false); + stateManager.saveState("test", testState); + Optional restoredState = stateManager.loadLastState("test"); + assertThat(restoredState.get(), Matchers.is(testState)); + restoredState = stateManager.loadLastSuccessfulState("test"); + assertThat(restoredState.isPresent(), is(false)); + } + + @Test + public void invalidJsonFileCanNotBeLoaded() throws IOException { + Files.write("not json", new File(statePath, "test.last.json"), UTF_8); + Optional restoredState = stateManager.loadLastState("test"); + assertThat(restoredState.isPresent(), is(false)); + } + + @Test + public void jsonWithInvalidFieldsCanNotBeLoaded() throws IOException { + Files.write("{\"not\":\"json\"}", new File(statePath, "test.last.json"), UTF_8); + Optional restoredState = stateManager.loadLastState("test"); + assertThat(restoredState.isPresent(), is(false)); + } + + @Test + public void unmappableStateCanBeSavedButNotLoaded() throws IOException { + InvalidState invalidState = new InvalidState(); + stateManager.saveState("test", invalidState); + assertThat(new File(statePath, "test.last.json").exists(), is(false)); + } + + public static class TestState extends AbstractState { + + public TestState() { + this(true); + } + + public TestState(boolean success) { + super(success); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof TestState)) { + return false; + } + TestState testState = (TestState) object; + return equal(exception(), testState.exception()) + && (failCount() == testState.failCount()) + && (time() == testState.time()); + } + + } + + public static class InvalidState extends AbstractState { + + @JsonProperty + private final Object someObject = new Object(); + + } + +}