1 package net.pterodactylus.sone.core;
3 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS;
4 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO;
5 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.hamcrest.Matchers.hasItem;
8 import static org.hamcrest.Matchers.is;
9 import static org.mockito.ArgumentCaptor.forClass;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.atLeastOnce;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.verify;
16 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
17 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
18 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
19 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
20 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
22 import com.google.common.eventbus.EventBus;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
27 * Unit test for {@link Preferences}.
29 public class PreferencesTest {
31 private final EventBus eventBus = mock(EventBus.class);
32 private final Preferences preferences = new Preferences(eventBus);
35 public void preferencesRetainInsertionDelay() {
36 preferences.setInsertionDelay(15);
37 assertThat(preferences.getInsertionDelay(), is(15));
41 public void preferencesSendsEventOnSettingInsertionDelay() {
42 preferences.setInsertionDelay(15);
43 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
44 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
45 assertThat(eventsCaptor.getAllValues(), hasItem(new InsertionDelayChangedEvent(15)));
48 @Test(expected = IllegalArgumentException.class)
49 public void invalidInsertionDelayIsRejected() {
50 preferences.setInsertionDelay(-15);
54 public void noEventIsSentWhenInvalidInsertionDelayIsSet() {
56 preferences.setInsertionDelay(-15);
57 } catch (IllegalArgumentException iae) {
60 verify(eventBus, never()).post(any());
64 public void preferencesReturnDefaultValueWhenInsertionDelayIsSetToNull() {
65 preferences.setInsertionDelay(null);
66 assertThat(preferences.getInsertionDelay(), is(60));
70 public void preferencesStartWithInsertionDelayDefaultValue() {
71 assertThat(preferences.getInsertionDelay(), is(60));
75 public void preferencesRetainPostsPerPage() {
76 preferences.setPostsPerPage(15);
77 assertThat(preferences.getPostsPerPage(), is(15));
80 @Test(expected = IllegalArgumentException.class)
81 public void invalidPostsPerPageIsRejected() {
82 preferences.setPostsPerPage(-15);
86 public void preferencesReturnDefaultValueWhenPostsPerPageIsSetToNull() {
87 preferences.setPostsPerPage(null);
88 assertThat(preferences.getPostsPerPage(), is(10));
92 public void preferencesStartWithPostsPerPageDefaultValue() {
93 assertThat(preferences.getPostsPerPage(), is(10));
97 public void preferencesRetainImagesPerPage() {
98 preferences.setImagesPerPage(15);
99 assertThat(preferences.getImagesPerPage(), is(15));
102 @Test(expected = IllegalArgumentException.class)
103 public void invalidImagesPerPageIsRejected() {
104 preferences.setImagesPerPage(-15);
108 public void preferencesReturnDefaultValueWhenImagesPerPageIsSetToNull() {
109 preferences.setImagesPerPage(null);
110 assertThat(preferences.getImagesPerPage(), is(9));
114 public void preferencesStartWithImagesPerPageDefaultValue() {
115 assertThat(preferences.getImagesPerPage(), is(9));
119 public void preferencesRetainCharactersPerPost() {
120 preferences.setCharactersPerPost(150);
121 assertThat(preferences.getCharactersPerPost(), is(150));
124 @Test(expected = IllegalArgumentException.class)
125 public void invalidCharactersPerPostIsRejected() {
126 preferences.setCharactersPerPost(-15);
130 public void preferencesReturnDefaultValueWhenCharactersPerPostIsSetToNull() {
131 preferences.setCharactersPerPost(null);
132 assertThat(preferences.getCharactersPerPost(), is(400));
136 public void preferencesStartWithCharactersPerPostDefaultValue() {
137 assertThat(preferences.getCharactersPerPost(), is(400));
141 public void preferencesRetainPostCutOffLength() {
142 preferences.setPostCutOffLength(150);
143 assertThat(preferences.getPostCutOffLength(), is(150));
146 @Test(expected = IllegalArgumentException.class)
147 public void invalidPostCutOffLengthIsRejected() {
148 preferences.setPostCutOffLength(-15);
151 @Test(expected = IllegalArgumentException.class)
152 public void cutOffLengthOfMinusOneIsNotAllowed() {
153 preferences.setPostCutOffLength(-1);
157 public void preferencesReturnDefaultValueWhenPostCutOffLengthIsSetToNull() {
158 preferences.setPostCutOffLength(null);
159 assertThat(preferences.getPostCutOffLength(), is(200));
163 public void preferencesStartWithPostCutOffLengthDefaultValue() {
164 assertThat(preferences.getPostCutOffLength(), is(200));
168 public void preferencesRetainRequireFullAccessOfTrue() {
169 preferences.setRequireFullAccess(true);
170 assertThat(preferences.isRequireFullAccess(), is(true));
174 public void preferencesRetainRequireFullAccessOfFalse() {
175 preferences.setRequireFullAccess(false);
176 assertThat(preferences.isRequireFullAccess(), is(false));
180 public void preferencesReturnDefaultValueWhenRequireFullAccessIsSetToNull() {
181 preferences.setRequireFullAccess(null);
182 assertThat(preferences.isRequireFullAccess(), is(false));
186 public void preferencesStartWithRequireFullAccessDefaultValue() {
187 assertThat(preferences.isRequireFullAccess(), is(false));
191 public void preferencesRetainPositiveTrust() {
192 preferences.setPositiveTrust(15);
193 assertThat(preferences.getPositiveTrust(), is(15));
196 @Test(expected = IllegalArgumentException.class)
197 public void invalidPositiveTrustIsRejected() {
198 preferences.setPositiveTrust(-15);
202 public void preferencesReturnDefaultValueWhenPositiveTrustIsSetToNull() {
203 preferences.setPositiveTrust(null);
204 assertThat(preferences.getPositiveTrust(), is(75));
208 public void preferencesStartWithPositiveTrustDefaultValue() {
209 assertThat(preferences.getPositiveTrust(), is(75));
213 public void preferencesRetainNegativeTrust() {
214 preferences.setNegativeTrust(-15);
215 assertThat(preferences.getNegativeTrust(), is(-15));
218 @Test(expected = IllegalArgumentException.class)
219 public void invalidNegativeTrustIsRejected() {
220 preferences.setNegativeTrust(150);
224 public void preferencesReturnDefaultValueWhenNegativeTrustIsSetToNull() {
225 preferences.setNegativeTrust(null);
226 assertThat(preferences.getNegativeTrust(), is(-25));
230 public void preferencesStartWithNegativeTrustDefaultValue() {
231 assertThat(preferences.getNegativeTrust(), is(-25));
235 public void preferencesRetainTrustComment() {
236 preferences.setTrustComment("Trust");
237 assertThat(preferences.getTrustComment(), is("Trust"));
241 public void preferencesReturnDefaultValueWhenTrustCommentIsSetToNull() {
242 preferences.setTrustComment(null);
243 assertThat(preferences.getTrustComment(),
244 is("Set from Sone Web Interface"));
248 public void preferencesStartWithTrustCommentDefaultValue() {
249 assertThat(preferences.getTrustComment(),
250 is("Set from Sone Web Interface"));
254 public void preferencesRetainFcpInterfaceActiveOfTrue() {
255 preferences.setFcpInterfaceActive(true);
256 assertThat(preferences.isFcpInterfaceActive(), is(true));
257 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
261 public void preferencesRetainFcpInterfaceActiveOfFalse() {
262 preferences.setFcpInterfaceActive(false);
263 assertThat(preferences.isFcpInterfaceActive(), is(false));
264 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
268 public void preferencesReturnDefaultValueWhenFcpInterfaceActiveIsSetToNull() {
269 preferences.setFcpInterfaceActive(null);
270 assertThat(preferences.isFcpInterfaceActive(), is(false));
271 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
275 public void preferencesStartWithFcpInterfaceActiveDefaultValue() {
276 assertThat(preferences.isFcpInterfaceActive(), is(false));
280 public void preferencesRetainFcpFullAccessRequiredOfNo() {
281 preferences.setFcpFullAccessRequired(NO);
282 assertThat(preferences.getFcpFullAccessRequired(), is(NO));
283 verifyFullAccessRequiredChangedEvent(NO);
286 private void verifyFullAccessRequiredChangedEvent(
287 FullAccessRequired fullAccessRequired) {
288 ArgumentCaptor<FullAccessRequiredChanged> fullAccessRequiredCaptor =
289 forClass(FullAccessRequiredChanged.class);
290 verify(eventBus).post(fullAccessRequiredCaptor.capture());
292 fullAccessRequiredCaptor.getValue().getFullAccessRequired(),
293 is(fullAccessRequired));
297 public void preferencesRetainFcpFullAccessRequiredOfWriting() {
298 preferences.setFcpFullAccessRequired(WRITING);
299 assertThat(preferences.getFcpFullAccessRequired(), is(WRITING));
300 verifyFullAccessRequiredChangedEvent(WRITING);
304 public void preferencesRetainFcpFullAccessRequiredOfAlways() {
305 preferences.setFcpFullAccessRequired(ALWAYS);
306 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
307 verifyFullAccessRequiredChangedEvent(ALWAYS);
311 public void preferencesReturnDefaultValueWhenFcpFullAccessRequiredIsSetToNull() {
312 preferences.setFcpFullAccessRequired(null);
313 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
314 verifyFullAccessRequiredChangedEvent(ALWAYS);
318 public void preferencesStartWithFcpFullAccessRequiredDefaultValue() {
319 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
323 public void settingInsertionDelayToValidValueSendsChangeEvent() {
324 preferences.setInsertionDelay(30);
325 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
326 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
327 assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30)));