1 package net.pterodactylus.rhynodge.states;
3 import static com.google.common.base.Charsets.UTF_8;
4 import static com.google.common.base.Objects.equal;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.is;
9 import java.io.IOException;
11 import net.pterodactylus.rhynodge.State;
13 import com.fasterxml.jackson.annotation.JsonProperty;
14 import com.google.common.base.Optional;
15 import com.google.common.io.Files;
16 import org.hamcrest.Matchers;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
22 * Unit test for {@link StateManager}.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 public class StateManagerTest {
29 public TemporaryFolder tempFolder = new TemporaryFolder();
30 private final File statePath;
31 private final StateManager stateManager;
33 public StateManagerTest() throws IOException {
35 statePath = tempFolder.newFolder();
36 stateManager = new StateManager(statePath.getPath());
40 public void successStateCanBeRestored() {
41 TestState testState = new TestState();
42 stateManager.saveState("test", testState);
43 Optional<State> restoredState = stateManager.loadLastState("test");
44 assertThat(restoredState.get(), Matchers.<State>is(testState));
45 restoredState = stateManager.loadLastSuccessfulState("test");
46 assertThat(restoredState.get(), Matchers.<State>is(testState));
50 public void failStateIsNotSavedAsSuccessfulState() {
51 TestState testState = new TestState(false);
52 stateManager.saveState("test", testState);
53 Optional<State> restoredState = stateManager.loadLastState("test");
54 assertThat(restoredState.get(), Matchers.<State>is(testState));
55 restoredState = stateManager.loadLastSuccessfulState("test");
56 assertThat(restoredState.isPresent(), is(false));
60 public void invalidJsonFileCanNotBeLoaded() throws IOException {
61 Files.write("not json", new File(statePath, "test.last.json"), UTF_8);
62 Optional<State> restoredState = stateManager.loadLastState("test");
63 assertThat(restoredState.isPresent(), is(false));
67 public void jsonWithInvalidFieldsCanNotBeLoaded() throws IOException {
68 Files.write("{\"not\":\"json\"}", new File(statePath, "test.last.json"), UTF_8);
69 Optional<State> restoredState = stateManager.loadLastState("test");
70 assertThat(restoredState.isPresent(), is(false));
74 public void unmappableStateCanBeSavedButNotLoaded() throws IOException {
75 InvalidState invalidState = new InvalidState();
76 stateManager.saveState("test", invalidState);
77 assertThat(new File(statePath, "test.last.json").exists(), is(false));
80 public static class TestState extends AbstractState {
86 public TestState(boolean success) {
91 public boolean equals(Object object) {
92 if (!(object instanceof TestState)) {
95 TestState testState = (TestState) object;
96 return equal(exception(), testState.exception())
97 && (failCount() == testState.failCount())
98 && (time() == testState.time());
103 public static class InvalidState extends AbstractState {
106 private final Object someObject = new Object();