Use Java 1.8, and its Optional.
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / states / StateManagerTest.java
1 package net.pterodactylus.rhynodge.states;
2
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;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.Optional;
13
14 import net.pterodactylus.rhynodge.State;
15
16 import com.fasterxml.jackson.annotation.JsonProperty;
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;
23
24 /**
25  * Unit test for {@link StateManager}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class StateManagerTest {
30
31         @Rule
32         public TemporaryFolder tempFolder = new TemporaryFolder();
33         private final File statePath;
34         private final StateManager stateManager;
35
36         @BeforeClass
37         public static void deactivateLogging() {
38                 getLogger(StateManager.class).setLevel(OFF);
39         }
40
41         public StateManagerTest() throws IOException {
42                 tempFolder.create();
43                 statePath = tempFolder.newFolder();
44                 stateManager = new StateManager(statePath.getPath());
45         }
46
47         @Test
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));
55         }
56
57         @Test
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));
65         }
66
67         @Test
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));
72         }
73
74         @Test
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));
79         }
80
81         @Test
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));
86         }
87
88         public static class TestState extends AbstractState {
89
90                 public TestState() {
91                         this(true);
92                 }
93
94                 public TestState(boolean success) {
95                         super(success);
96                 }
97
98                 @Override
99                 public boolean equals(Object object) {
100                         if (!(object instanceof TestState)) {
101                                 return false;
102                         }
103                         TestState testState = (TestState) object;
104                         return equal(exception(), testState.exception())
105                                         && (failCount() == testState.failCount())
106                                         && (time() == testState.time());
107                 }
108
109         }
110
111         public static class InvalidState extends AbstractState {
112
113                 @JsonProperty
114                 private final Object someObject = new Object();
115
116         }
117
118 }