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.verify;
15 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
16 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
17 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
18 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
19 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
21 import com.google.common.eventbus.EventBus;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
26 * Unit test for {@link Preferences}.
28 public class PreferencesTest {
30 private final EventBus eventBus = mock(EventBus.class);
31 private final Preferences preferences = new Preferences(eventBus);
34 public void preferencesRetainInsertionDelay() {
35 preferences.setInsertionDelay(15);
36 assertThat(preferences.getInsertionDelay(), is(15));
40 public void preferencesSendsEventOnSettingInsertionDelay() {
41 preferences.setInsertionDelay(15);
42 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
43 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
44 assertThat(eventsCaptor.getAllValues(), hasItem(new InsertionDelayChangedEvent(15)));
47 @Test(expected = IllegalArgumentException.class)
48 public void invalidInsertionDelayIsRejected() {
49 preferences.setInsertionDelay(-15);
53 public void preferencesReturnDefaultValueWhenInsertionDelayIsSetToNull() {
54 preferences.setInsertionDelay(null);
55 assertThat(preferences.getInsertionDelay(), is(60));
59 public void preferencesStartWithInsertionDelayDefaultValue() {
60 assertThat(preferences.getInsertionDelay(), is(60));
64 public void preferencesRetainPostsPerPage() {
65 preferences.setPostsPerPage(15);
66 assertThat(preferences.getPostsPerPage(), is(15));
69 @Test(expected = IllegalArgumentException.class)
70 public void invalidPostsPerPageIsRejected() {
71 preferences.setPostsPerPage(-15);
75 public void preferencesReturnDefaultValueWhenPostsPerPageIsSetToNull() {
76 preferences.setPostsPerPage(null);
77 assertThat(preferences.getPostsPerPage(), is(10));
81 public void preferencesStartWithPostsPerPageDefaultValue() {
82 assertThat(preferences.getPostsPerPage(), is(10));
86 public void preferencesRetainImagesPerPage() {
87 preferences.setImagesPerPage(15);
88 assertThat(preferences.getImagesPerPage(), is(15));
91 @Test(expected = IllegalArgumentException.class)
92 public void invalidImagesPerPageIsRejected() {
93 preferences.setImagesPerPage(-15);
97 public void preferencesReturnDefaultValueWhenImagesPerPageIsSetToNull() {
98 preferences.setImagesPerPage(null);
99 assertThat(preferences.getImagesPerPage(), is(9));
103 public void preferencesStartWithImagesPerPageDefaultValue() {
104 assertThat(preferences.getImagesPerPage(), is(9));
108 public void preferencesRetainCharactersPerPost() {
109 preferences.setCharactersPerPost(150);
110 assertThat(preferences.getCharactersPerPost(), is(150));
113 @Test(expected = IllegalArgumentException.class)
114 public void invalidCharactersPerPostIsRejected() {
115 preferences.setCharactersPerPost(-15);
119 public void preferencesReturnDefaultValueWhenCharactersPerPostIsSetToNull() {
120 preferences.setCharactersPerPost(null);
121 assertThat(preferences.getCharactersPerPost(), is(400));
125 public void preferencesStartWithCharactersPerPostDefaultValue() {
126 assertThat(preferences.getCharactersPerPost(), is(400));
130 public void preferencesRetainPostCutOffLength() {
131 preferences.setPostCutOffLength(150);
132 assertThat(preferences.getPostCutOffLength(), is(150));
135 @Test(expected = IllegalArgumentException.class)
136 public void invalidPostCutOffLengthIsRejected() {
137 preferences.setPostCutOffLength(-15);
140 @Test(expected = IllegalArgumentException.class)
141 public void cutOffLengthOfMinusOneIsNotAllowed() {
142 preferences.setPostCutOffLength(-1);
146 public void preferencesReturnDefaultValueWhenPostCutOffLengthIsSetToNull() {
147 preferences.setPostCutOffLength(null);
148 assertThat(preferences.getPostCutOffLength(), is(200));
152 public void preferencesStartWithPostCutOffLengthDefaultValue() {
153 assertThat(preferences.getPostCutOffLength(), is(200));
157 public void preferencesRetainRequireFullAccessOfTrue() {
158 preferences.setRequireFullAccess(true);
159 assertThat(preferences.isRequireFullAccess(), is(true));
163 public void preferencesRetainRequireFullAccessOfFalse() {
164 preferences.setRequireFullAccess(false);
165 assertThat(preferences.isRequireFullAccess(), is(false));
169 public void preferencesReturnDefaultValueWhenRequireFullAccessIsSetToNull() {
170 preferences.setRequireFullAccess(null);
171 assertThat(preferences.isRequireFullAccess(), is(false));
175 public void preferencesStartWithRequireFullAccessDefaultValue() {
176 assertThat(preferences.isRequireFullAccess(), is(false));
180 public void preferencesRetainPositiveTrust() {
181 preferences.setPositiveTrust(15);
182 assertThat(preferences.getPositiveTrust(), is(15));
185 @Test(expected = IllegalArgumentException.class)
186 public void invalidPositiveTrustIsRejected() {
187 preferences.setPositiveTrust(-15);
191 public void preferencesReturnDefaultValueWhenPositiveTrustIsSetToNull() {
192 preferences.setPositiveTrust(null);
193 assertThat(preferences.getPositiveTrust(), is(75));
197 public void preferencesStartWithPositiveTrustDefaultValue() {
198 assertThat(preferences.getPositiveTrust(), is(75));
202 public void preferencesRetainNegativeTrust() {
203 preferences.setNegativeTrust(-15);
204 assertThat(preferences.getNegativeTrust(), is(-15));
207 @Test(expected = IllegalArgumentException.class)
208 public void invalidNegativeTrustIsRejected() {
209 preferences.setNegativeTrust(150);
213 public void preferencesReturnDefaultValueWhenNegativeTrustIsSetToNull() {
214 preferences.setNegativeTrust(null);
215 assertThat(preferences.getNegativeTrust(), is(-25));
219 public void preferencesStartWithNegativeTrustDefaultValue() {
220 assertThat(preferences.getNegativeTrust(), is(-25));
224 public void preferencesRetainTrustComment() {
225 preferences.setTrustComment("Trust");
226 assertThat(preferences.getTrustComment(), is("Trust"));
230 public void preferencesReturnDefaultValueWhenTrustCommentIsSetToNull() {
231 preferences.setTrustComment(null);
232 assertThat(preferences.getTrustComment(),
233 is("Set from Sone Web Interface"));
237 public void preferencesStartWithTrustCommentDefaultValue() {
238 assertThat(preferences.getTrustComment(),
239 is("Set from Sone Web Interface"));
243 public void preferencesRetainFcpInterfaceActiveOfTrue() {
244 preferences.setFcpInterfaceActive(true);
245 assertThat(preferences.isFcpInterfaceActive(), is(true));
246 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
250 public void preferencesRetainFcpInterfaceActiveOfFalse() {
251 preferences.setFcpInterfaceActive(false);
252 assertThat(preferences.isFcpInterfaceActive(), is(false));
253 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
257 public void preferencesReturnDefaultValueWhenFcpInterfaceActiveIsSetToNull() {
258 preferences.setFcpInterfaceActive(null);
259 assertThat(preferences.isFcpInterfaceActive(), is(false));
260 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
264 public void preferencesStartWithFcpInterfaceActiveDefaultValue() {
265 assertThat(preferences.isFcpInterfaceActive(), is(false));
269 public void preferencesRetainFcpFullAccessRequiredOfNo() {
270 preferences.setFcpFullAccessRequired(NO);
271 assertThat(preferences.getFcpFullAccessRequired(), is(NO));
272 verifyFullAccessRequiredChangedEvent(NO);
275 private void verifyFullAccessRequiredChangedEvent(
276 FullAccessRequired fullAccessRequired) {
277 ArgumentCaptor<FullAccessRequiredChanged> fullAccessRequiredCaptor =
278 forClass(FullAccessRequiredChanged.class);
279 verify(eventBus).post(fullAccessRequiredCaptor.capture());
281 fullAccessRequiredCaptor.getValue().getFullAccessRequired(),
282 is(fullAccessRequired));
286 public void preferencesRetainFcpFullAccessRequiredOfWriting() {
287 preferences.setFcpFullAccessRequired(WRITING);
288 assertThat(preferences.getFcpFullAccessRequired(), is(WRITING));
289 verifyFullAccessRequiredChangedEvent(WRITING);
293 public void preferencesRetainFcpFullAccessRequiredOfAlways() {
294 preferences.setFcpFullAccessRequired(ALWAYS);
295 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
296 verifyFullAccessRequiredChangedEvent(ALWAYS);
300 public void preferencesReturnDefaultValueWhenFcpFullAccessRequiredIsSetToNull() {
301 preferences.setFcpFullAccessRequired(null);
302 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
303 verifyFullAccessRequiredChangedEvent(ALWAYS);
307 public void preferencesStartWithFcpFullAccessRequiredDefaultValue() {
308 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
312 public void settingInsertionDelayToValidValueSendsChangeEvent() {
313 preferences.setInsertionDelay(30);
314 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
315 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
316 assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30)));