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.apache.log4j.Level.OFF;
6 import static org.apache.log4j.Logger.getLogger;
7 import static org.hamcrest.MatcherAssert.assertThat;
8 import static org.hamcrest.Matchers.is;
11 import java.io.IOException;
12 import java.util.Optional;
14 import net.pterodactylus.rhynodge.State;
15 import net.pterodactylus.rhynodge.states.StateManager.Directory;
17 import com.fasterxml.jackson.annotation.JsonProperty;
18 import com.google.common.io.Files;
19 import org.hamcrest.Matchers;
20 import org.junit.BeforeClass;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.TemporaryFolder;
26 * Unit test for {@link StateManager}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class StateManagerTest {
33 public TemporaryFolder tempFolder = new TemporaryFolder();
34 private final File statePath;
35 private final StateManager stateManager;
38 public static void deactivateLogging() {
39 getLogger(StateManager.class).setLevel(OFF);
42 public StateManagerTest() throws IOException {
44 statePath = tempFolder.newFolder();
45 stateManager = new StateManager(Directory.of(statePath.getPath()));
49 public void successStateCanBeRestored() {
50 TestState testState = new TestState();
51 stateManager.saveState("test", testState);
52 Optional<State> restoredState = stateManager.loadLastState("test");
53 assertThat(restoredState.get(), Matchers.<State>is(testState));
54 restoredState = stateManager.loadLastSuccessfulState("test");
55 assertThat(restoredState.get(), Matchers.<State>is(testState));
59 public void failStateIsNotSavedAsSuccessfulState() {
60 TestState testState = new TestState(false);
61 stateManager.saveState("test", testState);
62 Optional<State> restoredState = stateManager.loadLastState("test");
63 assertThat(restoredState.get(), Matchers.<State>is(testState));
64 restoredState = stateManager.loadLastSuccessfulState("test");
65 assertThat(restoredState.isPresent(), is(false));
69 public void invalidJsonFileCanNotBeLoaded() throws IOException {
70 Files.write("not json", new File(statePath, "test.last.json"), UTF_8);
71 Optional<State> restoredState = stateManager.loadLastState("test");
72 assertThat(restoredState.isPresent(), is(false));
76 public void jsonWithInvalidFieldsCanNotBeLoaded() throws IOException {
77 Files.write("{\"not\":\"json\"}", new File(statePath, "test.last.json"), UTF_8);
78 Optional<State> restoredState = stateManager.loadLastState("test");
79 assertThat(restoredState.isPresent(), is(false));
83 public void unmappableStateCanBeSavedButNotLoaded() throws IOException {
84 InvalidState invalidState = new InvalidState();
85 stateManager.saveState("test", invalidState);
86 assertThat(new File(statePath, "test.last.json").exists(), is(false));
89 public static class TestState extends AbstractState {
95 public TestState(boolean success) {
100 public boolean equals(Object object) {
101 if (!(object instanceof TestState)) {
104 TestState testState = (TestState) object;
105 return equal(exception(), testState.exception())
106 && (failCount() == testState.failCount())
107 && (time() == testState.time());
112 public static class InvalidState extends AbstractState {
115 private final Object someObject = new Object();