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.Matchers.any;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.verifyNoMoreInteractions;
11 import static org.mockito.Mockito.when;
13 import net.pterodactylus.sone.core.Options.Option;
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;
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Before;
21 import org.junit.Test;
24 * Unit test for {@link Preferences}.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class PreferencesTest {
30 private static final int INTEGER_VALUE = 1;
31 private static final String STRING_VALUE = "string-value";
32 private final Options options = mock(Options.class);
33 private final EventBus eventBus = mock(EventBus.class);
34 private final Preferences preferences = new Preferences(eventBus, options);
35 private final Option<Integer> integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock();
36 private final Option<Boolean> booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock();
37 private final Option<String> stringOption = when(mock(Option.class).get()).thenReturn(STRING_VALUE).getMock();
40 public void setupOptions() {
41 when(integerOption.validate(INTEGER_VALUE)).thenReturn(true);
42 when(options.getIntegerOption("InsertionDelay")).thenReturn(integerOption);
43 when(options.getIntegerOption("PostsPerPage")).thenReturn(integerOption);
44 when(options.getIntegerOption("ImagesPerPage")).thenReturn(integerOption);
45 when(options.getIntegerOption("CharactersPerPost")).thenReturn(integerOption);
46 when(options.getIntegerOption("PostCutOffLength")).thenReturn(integerOption);
47 when(options.getBooleanOption("RequireFullAccess")).thenReturn(booleanOption);
48 when(options.getIntegerOption("PositiveTrust")).thenReturn(integerOption);
49 when(options.getIntegerOption("NegativeTrust")).thenReturn(integerOption);
50 when(options.getStringOption("TrustComment")).thenReturn(stringOption);
51 when(options.getBooleanOption("ActivateFcpInterface")).thenReturn(booleanOption);
52 when(options.getIntegerOption("FcpFullAccessRequired")).thenReturn(integerOption);
56 public void testGettingInsertionDelay() {
57 assertThat(preferences.getInsertionDelay(), is(INTEGER_VALUE));
58 verify(integerOption).get();
62 public void validationOfInsertionDelayIsForwardedToOptions() {
63 preferences.validateInsertionDelay(INTEGER_VALUE);
64 verify(integerOption).validate(INTEGER_VALUE);
68 public void settingInsertionDelayIsForwardedToOptions() {
69 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
70 verify(integerOption).set(INTEGER_VALUE);
74 public void settingInsertionDelayIsForwardedToEventBus() {
75 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
76 verify(eventBus).post(any(InsertionDelayChangedEvent.class));
80 public void testGettingPostsPerPage() {
81 assertThat(preferences.getPostsPerPage(), is(INTEGER_VALUE));
82 verify(integerOption).get();
86 public void validationOfPostsPerPageIsForwardedToOptions() {
87 preferences.validatePostsPerPage(INTEGER_VALUE);
88 verify(integerOption).validate(INTEGER_VALUE);
92 public void settingPostsPerPageIsForwardedToOptions() {
93 assertThat(preferences.setPostsPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
94 verify(integerOption).set(INTEGER_VALUE);
98 public void testGettingImagesPerPage() {
99 assertThat(preferences.getImagesPerPage(), is(INTEGER_VALUE));
100 verify(integerOption).get();
104 public void validationOfImagesPerPageIsForwardedToOptions() {
105 preferences.validateImagesPerPage(INTEGER_VALUE);
106 verify(integerOption).validate(INTEGER_VALUE);
110 public void settingImagesPerPageIsForwardedToOptions() {
111 assertThat(preferences.setImagesPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
112 verify(integerOption).set(INTEGER_VALUE);
116 public void testGettingCharactersPerPost() {
117 assertThat(preferences.getCharactersPerPost(), is(INTEGER_VALUE));
118 verify(integerOption).get();
122 public void validationOfCharactersPerPostIsForwardedToOptions() {
123 preferences.validateCharactersPerPost(INTEGER_VALUE);
124 verify(integerOption).validate(INTEGER_VALUE);
128 public void settingCharactersPerPostIsForwardedToOptions() {
129 assertThat(preferences.setCharactersPerPost(INTEGER_VALUE), instanceOf(Preferences.class));
130 verify(integerOption).set(INTEGER_VALUE);
134 public void testGettingPostCutOffLength() {
135 assertThat(preferences.getPostCutOffLength(), is(INTEGER_VALUE));
136 verify(integerOption).get();
140 public void validationOfPostCutOffLengthIsForwardedToOptions() {
141 preferences.validatePostCutOffLength(INTEGER_VALUE);
142 verify(integerOption).validate(INTEGER_VALUE);
146 public void settingPostCutOffLengthIsForwardedToOptions() {
147 assertThat(preferences.setPostCutOffLength(INTEGER_VALUE), instanceOf(Preferences.class));
148 verify(integerOption).set(INTEGER_VALUE);
152 public void testGettingRequireFullAccess() {
153 assertThat(preferences.isRequireFullAccess(), is(true));
154 verify(booleanOption).get();
158 public void settingRequireFullAccessIsForwardedToOption() {
159 preferences.setRequireFullAccess(true);
160 verify(booleanOption).set(true);
164 public void testGettingPositiveTrust() {
165 assertThat(preferences.getPositiveTrust(), is(INTEGER_VALUE));
166 verify(integerOption).get();
170 public void validationOfPositiveTrustIsForwardedToOptions() {
171 preferences.validatePositiveTrust(INTEGER_VALUE);
172 verify(integerOption).validate(INTEGER_VALUE);
176 public void settingPositiveTrustIsForwardedToOptions() {
177 assertThat(preferences.setPositiveTrust(INTEGER_VALUE), instanceOf(Preferences.class));
178 verify(integerOption).set(INTEGER_VALUE);
182 public void testGettingNegativeTrust() {
183 assertThat(preferences.getNegativeTrust(), is(INTEGER_VALUE));
184 verify(integerOption).get();
188 public void validationOfNegativeTrustIsForwardedToOptions() {
189 preferences.validateNegativeTrust(INTEGER_VALUE);
190 verify(integerOption).validate(INTEGER_VALUE);
194 public void settingNegativeTrustIsForwardedToOptions() {
195 assertThat(preferences.setNegativeTrust(INTEGER_VALUE), instanceOf(Preferences.class));
196 verify(integerOption).set(INTEGER_VALUE);
200 public void gettingTrustCommentIsForwardedToOption() {
201 assertThat(preferences.getTrustComment(), is(STRING_VALUE));
202 verify(stringOption).get();
206 public void settingTrustCommentIsForwardedToOption() {
207 preferences.setTrustComment(STRING_VALUE);
208 verify(stringOption).set(STRING_VALUE);
212 public void gettingFcpInterfaceActiveIsForwardedToOption() {
213 assertThat(preferences.isFcpInterfaceActive(), is(true));
214 verify(booleanOption).get();
218 public void settingFcpInterfaceActiveIsForwardedToEventBus() {
219 preferences.setFcpInterfaceActive(true);
220 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
221 verifyNoMoreInteractions(eventBus);
225 public void settingFcpInterfaceInactiveIsForwardedToEventBus() {
226 preferences.setFcpInterfaceActive(false);
227 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
228 verifyNoMoreInteractions(eventBus);
232 public void settingFcpInterfaceActiveIsForwardedToOption() {
233 preferences.setFcpInterfaceActive(true);
234 verify(booleanOption).set(true);
238 public void gettingFcpFullAccessRequired() {
239 assertThat(preferences.getFcpFullAccessRequired(), is(FullAccessRequired.values()[INTEGER_VALUE]));
240 verify(integerOption).get();
244 public void settingFcpFullAccessRequiredIsForwardedToOption() {
245 preferences.setFcpFullAccessRequired(ALWAYS);
246 verify(integerOption).set(ALWAYS.ordinal());
250 public void settingFcpFullAccessRequiredToNullIsForwardedToOption() {
251 preferences.setFcpFullAccessRequired(null);
252 verify(integerOption).set(null);