1 package net.pterodactylus.sone.core;
3 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.instanceOf;
6 import static org.hamcrest.Matchers.is;
7 import static org.mockito.ArgumentCaptor.forClass;
8 import static org.mockito.Matchers.any;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.verifyNoMoreInteractions;
12 import static org.mockito.Mockito.when;
14 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
15 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
16 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
17 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
18 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
19 import net.pterodactylus.sone.utils.Option;
21 import com.google.common.eventbus.EventBus;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
27 * Unit test for {@link Preferences}.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class PreferencesTest {
33 private static final int INTEGER_VALUE = 1;
34 private static final String STRING_VALUE = "string-value";
35 private final Options options = mock(Options.class);
36 private final EventBus eventBus = mock(EventBus.class);
37 private final Preferences preferences = new Preferences(eventBus, options);
38 private final Option<Integer> integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock();
39 private final Option<Boolean> booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock();
40 private final Option<String> stringOption = when(mock(Option.class).get()).thenReturn(STRING_VALUE).getMock();
43 public void setupOptions() {
44 when(integerOption.validate(INTEGER_VALUE)).thenReturn(true);
45 when(options.getIntegerOption("InsertionDelay")).thenReturn(integerOption);
46 when(options.getIntegerOption("PostsPerPage")).thenReturn(integerOption);
47 when(options.getIntegerOption("ImagesPerPage")).thenReturn(integerOption);
48 when(options.getIntegerOption("CharactersPerPost")).thenReturn(integerOption);
49 when(options.getIntegerOption("PostCutOffLength")).thenReturn(integerOption);
50 when(options.getBooleanOption("RequireFullAccess")).thenReturn(booleanOption);
51 when(options.getIntegerOption("PositiveTrust")).thenReturn(integerOption);
52 when(options.getIntegerOption("NegativeTrust")).thenReturn(integerOption);
53 when(options.getStringOption("TrustComment")).thenReturn(stringOption);
54 when(options.getBooleanOption("ActivateFcpInterface")).thenReturn(booleanOption);
55 when(options.getIntegerOption("FcpFullAccessRequired")).thenReturn(integerOption);
59 public void testGettingInsertionDelay() {
60 assertThat(preferences.getInsertionDelay(), is(INTEGER_VALUE));
61 verify(integerOption).get();
65 public void validationOfInsertionDelayIsForwardedToOptions() {
66 preferences.validateInsertionDelay(INTEGER_VALUE);
67 verify(integerOption).validate(INTEGER_VALUE);
71 public void settingInsertionDelayIsForwardedToOptions() {
72 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
73 verify(integerOption).set(INTEGER_VALUE);
77 public void settingInsertionDelayIsForwardedToEventBus() {
78 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
79 verify(eventBus).post(any(InsertionDelayChangedEvent.class));
83 public void testGettingPostsPerPage() {
84 assertThat(preferences.getPostsPerPage(), is(INTEGER_VALUE));
85 verify(integerOption).get();
89 public void validationOfPostsPerPageIsForwardedToOptions() {
90 preferences.validatePostsPerPage(INTEGER_VALUE);
91 verify(integerOption).validate(INTEGER_VALUE);
95 public void settingPostsPerPageIsForwardedToOptions() {
96 assertThat(preferences.setPostsPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
97 verify(integerOption).set(INTEGER_VALUE);
101 public void testGettingImagesPerPage() {
102 assertThat(preferences.getImagesPerPage(), is(INTEGER_VALUE));
103 verify(integerOption).get();
107 public void validationOfImagesPerPageIsForwardedToOptions() {
108 preferences.validateImagesPerPage(INTEGER_VALUE);
109 verify(integerOption).validate(INTEGER_VALUE);
113 public void settingImagesPerPageIsForwardedToOptions() {
114 assertThat(preferences.setImagesPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
115 verify(integerOption).set(INTEGER_VALUE);
119 public void testGettingCharactersPerPost() {
120 assertThat(preferences.getCharactersPerPost(), is(INTEGER_VALUE));
121 verify(integerOption).get();
125 public void validationOfCharactersPerPostIsForwardedToOptions() {
126 preferences.validateCharactersPerPost(INTEGER_VALUE);
127 verify(integerOption).validate(INTEGER_VALUE);
131 public void settingCharactersPerPostIsForwardedToOptions() {
132 assertThat(preferences.setCharactersPerPost(INTEGER_VALUE), instanceOf(Preferences.class));
133 verify(integerOption).set(INTEGER_VALUE);
137 public void testGettingPostCutOffLength() {
138 assertThat(preferences.getPostCutOffLength(), is(INTEGER_VALUE));
139 verify(integerOption).get();
143 public void validationOfPostCutOffLengthIsForwardedToOptions() {
144 preferences.validatePostCutOffLength(INTEGER_VALUE);
145 verify(integerOption).validate(INTEGER_VALUE);
149 public void settingPostCutOffLengthIsForwardedToOptions() {
150 assertThat(preferences.setPostCutOffLength(INTEGER_VALUE), instanceOf(Preferences.class));
151 verify(integerOption).set(INTEGER_VALUE);
155 public void testGettingRequireFullAccess() {
156 assertThat(preferences.isRequireFullAccess(), is(true));
157 verify(booleanOption).get();
161 public void settingRequireFullAccessIsForwardedToOption() {
162 preferences.setRequireFullAccess(true);
163 verify(booleanOption).set(true);
167 public void testGettingPositiveTrust() {
168 assertThat(preferences.getPositiveTrust(), is(INTEGER_VALUE));
169 verify(integerOption).get();
173 public void validationOfPositiveTrustIsForwardedToOptions() {
174 preferences.validatePositiveTrust(INTEGER_VALUE);
175 verify(integerOption).validate(INTEGER_VALUE);
179 public void settingPositiveTrustIsForwardedToOptions() {
180 assertThat(preferences.setPositiveTrust(INTEGER_VALUE), instanceOf(Preferences.class));
181 verify(integerOption).set(INTEGER_VALUE);
185 public void testGettingNegativeTrust() {
186 assertThat(preferences.getNegativeTrust(), is(INTEGER_VALUE));
187 verify(integerOption).get();
191 public void validationOfNegativeTrustIsForwardedToOptions() {
192 preferences.validateNegativeTrust(INTEGER_VALUE);
193 verify(integerOption).validate(INTEGER_VALUE);
197 public void settingNegativeTrustIsForwardedToOptions() {
198 assertThat(preferences.setNegativeTrust(INTEGER_VALUE), instanceOf(Preferences.class));
199 verify(integerOption).set(INTEGER_VALUE);
203 public void gettingTrustCommentIsForwardedToOption() {
204 assertThat(preferences.getTrustComment(), is(STRING_VALUE));
205 verify(stringOption).get();
209 public void settingTrustCommentIsForwardedToOption() {
210 preferences.setTrustComment(STRING_VALUE);
211 verify(stringOption).set(STRING_VALUE);
215 public void gettingFcpInterfaceActiveIsForwardedToOption() {
216 assertThat(preferences.isFcpInterfaceActive(), is(true));
217 verify(booleanOption).get();
221 public void settingFcpInterfaceActiveIsForwardedToEventBus() {
222 preferences.setFcpInterfaceActive(true);
223 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
224 verifyNoMoreInteractions(eventBus);
228 public void settingFcpInterfaceInactiveIsForwardedToEventBus() {
229 preferences.setFcpInterfaceActive(false);
230 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
231 verifyNoMoreInteractions(eventBus);
235 public void settingFcpInterfaceActiveIsForwardedToOption() {
236 preferences.setFcpInterfaceActive(true);
237 verify(booleanOption).set(true);
241 public void gettingFcpFullAccessRequired() {
242 assertThat(preferences.getFcpFullAccessRequired(), is(FullAccessRequired.values()[INTEGER_VALUE]));
243 verify(integerOption).get();
247 public void settingFcpFullAccessRequiredIsForwardedToOption() {
248 preferences.setFcpFullAccessRequired(ALWAYS);
249 verify(integerOption).set(ALWAYS.ordinal());
253 public void settingFcpFullAccessRequiredToNullIsForwardedToOption() {
254 preferences.setFcpFullAccessRequired(null);
255 verify(integerOption).set(null);
259 public void settingFcpFullAccessRequiredIsForwardedToEventBus() {
260 preferences.setFcpFullAccessRequired(ALWAYS);
261 verify(integerOption).set(2);
262 ArgumentCaptor<FullAccessRequiredChanged> fullAccessRequiredChangedCaptor = forClass(FullAccessRequiredChanged.class);
263 verify(eventBus).post(fullAccessRequiredChangedCaptor.capture());
264 assertThat(fullAccessRequiredChangedCaptor.getValue().getFullAccessRequired(), is(ALWAYS));