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.when;
12 import net.pterodactylus.sone.core.Options.Option;
13 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
14 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
16 import com.google.common.eventbus.EventBus;
17 import org.junit.Before;
18 import org.junit.Test;
21 * Unit test for {@link Preferences}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class PreferencesTest {
27 private static final int INTEGER_VALUE = 1;
28 private static final String STRING_VALUE = "string-value";
29 private final Options options = mock(Options.class);
30 private final EventBus eventBus = mock(EventBus.class);
31 private final Preferences preferences = new Preferences(eventBus, options);
32 private final Option<Integer> integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock();
33 private final Option<Boolean> booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock();
34 private final Option<String> stringOption = when(mock(Option.class).get()).thenReturn(STRING_VALUE).getMock();
37 public void setupOptions() {
38 when(integerOption.validate(INTEGER_VALUE)).thenReturn(true);
39 when(options.getIntegerOption("InsertionDelay")).thenReturn(integerOption);
40 when(options.getIntegerOption("PostsPerPage")).thenReturn(integerOption);
41 when(options.getIntegerOption("ImagesPerPage")).thenReturn(integerOption);
42 when(options.getIntegerOption("CharactersPerPost")).thenReturn(integerOption);
43 when(options.getIntegerOption("PostCutOffLength")).thenReturn(integerOption);
44 when(options.getBooleanOption("RequireFullAccess")).thenReturn(booleanOption);
45 when(options.getIntegerOption("PositiveTrust")).thenReturn(integerOption);
46 when(options.getIntegerOption("NegativeTrust")).thenReturn(integerOption);
47 when(options.getStringOption("TrustComment")).thenReturn(stringOption);
48 when(options.getBooleanOption("ActivateFcpInterface")).thenReturn(booleanOption);
49 when(options.getIntegerOption("FcpFullAccessRequired")).thenReturn(integerOption);
53 public void testGettingInsertionDelay() {
54 assertThat(preferences.getInsertionDelay(), is(INTEGER_VALUE));
55 verify(integerOption).get();
59 public void validationOfInsertionDelayIsForwardedToOptions() {
60 preferences.validateInsertionDelay(INTEGER_VALUE);
61 verify(integerOption).validate(INTEGER_VALUE);
65 public void settingInsertionDelayIsForwardedToOptions() {
66 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
67 verify(integerOption).set(INTEGER_VALUE);
71 public void settingInsertionDelayIsForwardedToEventBus() {
72 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
73 verify(eventBus).post(any(InsertionDelayChangedEvent.class));
77 public void testGettingPostsPerPage() {
78 assertThat(preferences.getPostsPerPage(), is(INTEGER_VALUE));
79 verify(integerOption).get();
83 public void validationOfPostsPerPageIsForwardedToOptions() {
84 preferences.validatePostsPerPage(INTEGER_VALUE);
85 verify(integerOption).validate(INTEGER_VALUE);
89 public void settingPostsPerPageIsForwardedToOptions() {
90 assertThat(preferences.setPostsPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
91 verify(integerOption).set(INTEGER_VALUE);
95 public void testGettingImagesPerPage() {
96 assertThat(preferences.getImagesPerPage(), is(INTEGER_VALUE));
97 verify(integerOption).get();
101 public void validationOfImagesPerPageIsForwardedToOptions() {
102 preferences.validateImagesPerPage(INTEGER_VALUE);
103 verify(integerOption).validate(INTEGER_VALUE);
107 public void settingImagesPerPageIsForwardedToOptions() {
108 assertThat(preferences.setImagesPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
109 verify(integerOption).set(INTEGER_VALUE);
113 public void testGettingCharactersPerPost() {
114 assertThat(preferences.getCharactersPerPost(), is(INTEGER_VALUE));
115 verify(integerOption).get();
119 public void validationOfCharactersPerPostIsForwardedToOptions() {
120 preferences.validateCharactersPerPost(INTEGER_VALUE);
121 verify(integerOption).validate(INTEGER_VALUE);
125 public void settingCharactersPerPostIsForwardedToOptions() {
126 assertThat(preferences.setCharactersPerPost(INTEGER_VALUE), instanceOf(Preferences.class));
127 verify(integerOption).set(INTEGER_VALUE);
131 public void testGettingPostCutOffLength() {
132 assertThat(preferences.getPostCutOffLength(), is(INTEGER_VALUE));
133 verify(integerOption).get();
137 public void validationOfPostCutOffLengthIsForwardedToOptions() {
138 preferences.validatePostCutOffLength(INTEGER_VALUE);
139 verify(integerOption).validate(INTEGER_VALUE);
143 public void settingPostCutOffLengthIsForwardedToOptions() {
144 assertThat(preferences.setPostCutOffLength(INTEGER_VALUE), instanceOf(Preferences.class));
145 verify(integerOption).set(INTEGER_VALUE);
149 public void testGettingRequireFullAccess() {
150 assertThat(preferences.isRequireFullAccess(), is(true));
151 verify(booleanOption).get();
155 public void settingRequireFullAccessIsForwardedToOption() {
156 preferences.setRequireFullAccess(true);
157 verify(booleanOption).set(true);
161 public void testGettingPositiveTrust() {
162 assertThat(preferences.getPositiveTrust(), is(INTEGER_VALUE));
163 verify(integerOption).get();
167 public void validationOfPositiveTrustIsForwardedToOptions() {
168 preferences.validatePositiveTrust(INTEGER_VALUE);
169 verify(integerOption).validate(INTEGER_VALUE);
173 public void settingPositiveTrustIsForwardedToOptions() {
174 assertThat(preferences.setPositiveTrust(INTEGER_VALUE), instanceOf(Preferences.class));
175 verify(integerOption).set(INTEGER_VALUE);
179 public void testGettingNegativeTrust() {
180 assertThat(preferences.getNegativeTrust(), is(INTEGER_VALUE));
181 verify(integerOption).get();
185 public void validationOfNegativeTrustIsForwardedToOptions() {
186 preferences.validateNegativeTrust(INTEGER_VALUE);
187 verify(integerOption).validate(INTEGER_VALUE);
191 public void settingNegativeTrustIsForwardedToOptions() {
192 assertThat(preferences.setNegativeTrust(INTEGER_VALUE), instanceOf(Preferences.class));
193 verify(integerOption).set(INTEGER_VALUE);
197 public void gettingTrustCommentIsForwardedToOption() {
198 assertThat(preferences.getTrustComment(), is(STRING_VALUE));
199 verify(stringOption).get();
203 public void settingTrustCommentIsForwardedToOption() {
204 preferences.setTrustComment(STRING_VALUE);
205 verify(stringOption).set(STRING_VALUE);
209 public void gettingFcpInterfaceActiveIsForwardedToOption() {
210 assertThat(preferences.isFcpInterfaceActive(), is(true));
211 verify(booleanOption).get();
215 public void settingFcpInterfaceActiveIsForwardedToOption() {
216 preferences.setFcpInterfaceActive(true);
217 verify(booleanOption).set(true);
221 public void gettingFcpFullAccessRequired() {
222 assertThat(preferences.getFcpFullAccessRequired(), is(FullAccessRequired.values()[INTEGER_VALUE]));
223 verify(integerOption).get();
227 public void settingFcpFullAccessRequiredIsForwardedToOption() {
228 preferences.setFcpFullAccessRequired(ALWAYS);
229 verify(integerOption).set(ALWAYS.ordinal());
233 public void settingFcpFullAccessRequiredToNullIsForwardedToOption() {
234 preferences.setFcpFullAccessRequired(null);
235 verify(integerOption).set(null);