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;
13 import net.pterodactylus.rhynodge.State;
15 import com.fasterxml.jackson.annotation.JsonProperty;
16 import com.google.common.base.Optional;
17 import com.google.common.io.Files;
18 import org.hamcrest.Matchers;
19 import org.junit.BeforeClass;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.TemporaryFolder;
25 * Unit test for {@link StateManager}.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class StateManagerTest {
32 public TemporaryFolder tempFolder = new TemporaryFolder();
33 private final File statePath;
34 private final StateManager stateManager;
37 public static void deactivateLogging() {
38 getLogger(StateManager.class).setLevel(OFF);
41 public StateManagerTest() throws IOException {
43 statePath = tempFolder.newFolder();
44 stateManager = new StateManager(statePath.getPath());
48 public void successStateCanBeRestored() {
49 TestState testState = new TestState();
50 stateManager.saveState("test", testState);
51 Optional<State> restoredState = stateManager.loadLastState("test");
52 assertThat(restoredState.get(), Matchers.<State>is(testState));
53 restoredState = stateManager.loadLastSuccessfulState("test");
54 assertThat(restoredState.get(), Matchers.<State>is(testState));
58 public void failStateIsNotSavedAsSuccessfulState() {
59 TestState testState = new TestState(false);
60 stateManager.saveState("test", testState);
61 Optional<State> restoredState = stateManager.loadLastState("test");
62 assertThat(restoredState.get(), Matchers.<State>is(testState));
63 restoredState = stateManager.loadLastSuccessfulState("test");
64 assertThat(restoredState.isPresent(), is(false));
68 public void invalidJsonFileCanNotBeLoaded() throws IOException {
69 Files.write("not json", new File(statePath, "test.last.json"), UTF_8);
70 Optional<State> restoredState = stateManager.loadLastState("test");
71 assertThat(restoredState.isPresent(), is(false));
75 public void jsonWithInvalidFieldsCanNotBeLoaded() throws IOException {
76 Files.write("{\"not\":\"json\"}", new File(statePath, "test.last.json"), UTF_8);
77 Optional<State> restoredState = stateManager.loadLastState("test");
78 assertThat(restoredState.isPresent(), is(false));
82 public void unmappableStateCanBeSavedButNotLoaded() throws IOException {
83 InvalidState invalidState = new InvalidState();
84 stateManager.saveState("test", invalidState);
85 assertThat(new File(statePath, "test.last.json").exists(), is(false));
88 public static class TestState extends AbstractState {
94 public TestState(boolean success) {
99 public boolean equals(Object object) {
100 if (!(object instanceof TestState)) {
103 TestState testState = (TestState) object;
104 return equal(exception(), testState.exception())
105 && (failCount() == testState.failCount())
106 && (time() == testState.time());
111 public static class InvalidState extends AbstractState {
114 private final Object someObject = new Object();