72831fc15585ae68c2b8ff88033288c340aa82ad
[Sone.git] / src / test / java / net / pterodactylus / sone / core / PreferencesLoaderTest.java
1 package net.pterodactylus.sone.core;
2
3 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.not;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import net.pterodactylus.sone.test.TestValue;
11 import net.pterodactylus.util.config.Configuration;
12
13 import com.google.common.eventbus.EventBus;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 /**
18  * Unit test for {@link PreferencesLoader}.
19  */
20 public class PreferencesLoaderTest {
21
22         private final EventBus eventBus = mock(EventBus.class);
23         private final Preferences preferences = new Preferences(eventBus);
24         private final Configuration configuration = mock(Configuration.class);
25         private final PreferencesLoader preferencesLoader =
26                         new PreferencesLoader(preferences);
27
28         @Before
29         public void setupConfiguration() {
30                 setupIntValue("InsertionDelay", 15);
31                 setupIntValue("PostsPerPage", 25);
32                 setupIntValue("ImagesPerPage", 12);
33                 setupIntValue("CharactersPerPost", 150);
34                 setupIntValue("PostCutOffLength", 300);
35                 setupBooleanValue("RequireFullAccess", true);
36                 setupBooleanValue("ActivateFcpInterface", true);
37                 setupIntValue("FcpFullAccessRequired", 1);
38         }
39
40         private void setupIntValue(String optionName, int value) {
41                 when(configuration.getIntValue("Option/" + optionName)).thenReturn(
42                                 TestValue.from(value));
43         }
44
45         private void setupBooleanValue(String optionName, boolean value) {
46                 when(configuration.getBooleanValue(
47                                 "Option/" + optionName)).thenReturn(
48                                 TestValue.from(value));
49         }
50
51         @Test
52         public void configurationIsLoadedCorrectly() {
53                 setupConfiguration();
54                 preferencesLoader.loadFrom(configuration);
55                 assertThat(preferences.getInsertionDelay(), is(15));
56                 assertThat(preferences.getPostsPerPage(), is(25));
57                 assertThat(preferences.getImagesPerPage(), is(12));
58                 assertThat(preferences.getCharactersPerPost(), is(150));
59                 assertThat(preferences.getPostCutOffLength(), is(300));
60                 assertThat(preferences.getRequireFullAccess(), is(true));
61                 assertThat(preferences.getFcpInterfaceActive(), is(true));
62                 assertThat(preferences.getFcpFullAccessRequired(), is(WRITING));
63         }
64
65         @Test
66         public void configurationIsLoadedCorrectlyWithCutOffLengthMinusOne() {
67             setupConfiguration();
68                 setupIntValue("PostCutOffLength", -1);
69                 preferencesLoader.loadFrom(configuration);
70                 assertThat(preferences.getPostCutOffLength(), not(is(-1)));
71         }
72
73 }