1 package net.pterodactylus.sone.core;
3 import static java.lang.Long.MAX_VALUE;
4 import static net.pterodactylus.sone.main.SonePlugin.VERSION;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.instanceOf;
7 import static org.hamcrest.Matchers.is;
8 import static org.mockito.ArgumentCaptor.forClass;
9 import static org.mockito.Matchers.any;
10 import static org.mockito.Matchers.argThat;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.io.IOException;
18 import java.io.InputStream;
20 import net.pterodactylus.sone.core.FreenetInterface.Callback;
21 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
22 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
23 import net.pterodactylus.sone.freenet.StringBucket;
24 import net.pterodactylus.util.version.Version;
26 import freenet.client.ClientMetadata;
27 import freenet.client.FetchResult;
28 import freenet.keys.FreenetURI;
29 import freenet.support.api.Bucket;
31 import com.google.common.eventbus.EventBus;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
39 * Unit test for {@link UpdateChecker}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class UpdateCheckerTest {
45 private final EventBus eventBus = mock(EventBus.class);
46 private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
47 private final UpdateChecker updateChecker = new UpdateChecker(eventBus, freenetInterface);
50 public void startUpdateChecker() {
51 updateChecker.start();
55 public void newUpdateCheckerDoesNotHaveALatestVersion() {
56 assertThat(updateChecker.hasLatestVersion(), is(false));
57 assertThat(updateChecker.getLatestVersion(), is(VERSION));
61 public void startingAnUpdateCheckerRegisterAUsk() {
62 verify(freenetInterface).registerUsk(any(FreenetURI.class), any(Callback.class));
66 public void stoppingAnUpdateCheckerUnregistersAUsk() {
68 verify(freenetInterface).unregisterUsk(any(FreenetURI.class));
72 public void callbackDoesNotDownloadIfNewEditionIsNotFound() {
73 setupCallbackWithEdition(MAX_VALUE, false, false);
74 verify(freenetInterface, never()).fetchUri(any(FreenetURI.class));
75 verify(eventBus, never()).post(argThat(instanceOf(UpdateFoundEvent.class)));
78 private void setupCallbackWithEdition(long edition, boolean newKnownGood, boolean newSlot) {
79 ArgumentCaptor<FreenetURI> uri = forClass(FreenetURI.class);
80 ArgumentCaptor<Callback> callback = forClass(Callback.class);
81 verify(freenetInterface).registerUsk(uri.capture(), callback.capture());
82 callback.getValue().editionFound(uri.getValue(), edition, newKnownGood, newSlot);
86 public void callbackStartsIfNewEditionIsFound() {
87 setupFetchResult(createFutureFetchResult());
88 setupCallbackWithEdition(MAX_VALUE, true, false);
89 verifyAFreenetUriIsFetched();
90 ArgumentCaptor<UpdateFoundEvent> updateFoundEvent = forClass(UpdateFoundEvent.class);
91 verify(eventBus, times(1)).post(updateFoundEvent.capture());
92 assertThat(updateFoundEvent.getValue().version(), is(new Version(99, 0, 0)));
93 assertThat(updateFoundEvent.getValue().releaseTime(), is(11865368297000L));
94 assertThat(updateChecker.getLatestVersion(), is(new Version(99, 0, 0)));
95 assertThat(updateChecker.getLatestVersionDate(), is(11865368297000L));
98 private FetchResult createFutureFetchResult() {
99 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
100 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
101 "CurrentVersion/Version: 99.0.0\n" +
102 "CurrentVersion/ReleaseTime: 11865368297000");
103 return new FetchResult(clientMetadata, fetched);
107 public void callbackDoesNotStartIfNoNewEditionIsFound() {
108 setupFetchResult(createPastFetchResult());
109 setupCallbackWithEdition(updateChecker.getLatestEdition(), true, false);
110 verifyAFreenetUriIsFetched();
111 verifyNoUpdateFoundEventIsFired();
114 private void setupFetchResult(final FetchResult pastFetchResult) {
115 when(freenetInterface.fetchUri(any(FreenetURI.class))).thenAnswer(new Answer<Fetched>() {
117 public Fetched answer(InvocationOnMock invocation) throws Throwable {
118 FreenetURI freenetUri = (FreenetURI) invocation.getArguments()[0];
119 return new Fetched(freenetUri, pastFetchResult);
124 private FetchResult createPastFetchResult() {
125 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
126 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
127 "CurrentVersion/Version: 0.2\n" +
128 "CurrentVersion/ReleaseTime: 1289417883000");
129 return new FetchResult(clientMetadata, fetched);
133 public void invalidUpdateFileDoesNotStartCallback() {
134 setupFetchResult(createInvalidFetchResult());
135 setupCallbackWithEdition(MAX_VALUE, true, false);
136 verifyAFreenetUriIsFetched();
137 verifyNoUpdateFoundEventIsFired();
140 private FetchResult createInvalidFetchResult() {
141 ClientMetadata clientMetadata = new ClientMetadata("text/plain");
142 Bucket fetched = new StringBucket("Some other data.");
143 return new FetchResult(clientMetadata, fetched);
147 public void nonExistingPropertiesWillNotCauseUpdateToBeFound() {
148 setupCallbackWithEdition(MAX_VALUE, true, false);
149 verifyAFreenetUriIsFetched();
150 verifyNoUpdateFoundEventIsFired();
153 private void verifyNoUpdateFoundEventIsFired() {
154 verify(eventBus, never()).post(any(UpdateFoundEvent.class));
157 private void verifyAFreenetUriIsFetched() {
158 verify(freenetInterface).fetchUri(any(FreenetURI.class));
162 public void brokenBucketDoesNotCauseUpdateToBeFound() {
163 setupFetchResult(createBrokenBucketFetchResult());
164 setupCallbackWithEdition(MAX_VALUE, true, false);
165 verifyAFreenetUriIsFetched();
166 verifyNoUpdateFoundEventIsFired();
169 private FetchResult createBrokenBucketFetchResult() {
170 ClientMetadata clientMetadata = new ClientMetadata("text/plain");
171 Bucket fetched = new StringBucket("Some other data.") {
173 public InputStream getInputStream() {
175 return when(mock(InputStream.class).read()).thenThrow(IOException.class).getMock();
176 } catch (IOException ioe1) {
177 /* won’t throw here. */
182 return new FetchResult(clientMetadata, fetched);
186 public void invalidTimeDoesNotCauseAnUpdateToBeFound() {
187 setupFetchResult(createInvalidTimeFetchResult());
188 setupCallbackWithEdition(MAX_VALUE, true, false);
189 verifyAFreenetUriIsFetched();
190 verifyNoUpdateFoundEventIsFired();
193 private FetchResult createInvalidTimeFetchResult() {
194 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
195 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
196 "CurrentVersion/Version: 0.2\n" +
197 "CurrentVersion/ReleaseTime: invalid");
198 return new FetchResult(clientMetadata, fetched);