+++ /dev/null
-package net.pterodactylus.sone.core;
-
-import static java.lang.Long.MAX_VALUE;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.instanceOf;
-import static org.hamcrest.Matchers.is;
-import static org.mockito.ArgumentCaptor.forClass;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.mockito.hamcrest.MockitoHamcrest.argThat;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import net.pterodactylus.sone.core.FreenetInterface.Callback;
-import net.pterodactylus.sone.core.event.UpdateFoundEvent;
-import net.pterodactylus.sone.main.PluginHomepage;
-import net.pterodactylus.util.version.Version;
-
-import freenet.client.ClientMetadata;
-import freenet.client.FetchResult;
-import freenet.keys.FreenetURI;
-import freenet.support.api.Bucket;
-import freenet.support.io.ArrayBucket;
-
-import com.google.common.eventbus.EventBus;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.ArgumentCaptor;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-/**
- * Unit test for {@link UpdateChecker}.
- */
-public class UpdateCheckerTest {
-
- private final EventBus eventBus = mock(EventBus.class);
- private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
- private final Version currentVersion = new Version(1, 0, 0);
- private final PluginHomepage pluginHomepage = new PluginHomepage("KSK@homepage");
- private final UpdateChecker updateChecker = new UpdateChecker(eventBus, freenetInterface, currentVersion, pluginHomepage);
-
- @Before
- public void startUpdateChecker() {
- updateChecker.start();
- }
-
- @Test
- public void newUpdateCheckerDoesNotHaveALatestVersion() {
- assertThat(updateChecker.hasLatestVersion(), is(false));
- assertThat(updateChecker.getLatestVersion(), is(currentVersion));
- }
-
- @Test
- public void startingAnUpdateCheckerRegisterAUsk() {
- verify(freenetInterface).registerUsk(any(FreenetURI.class), any(Callback.class));
- }
-
- @Test
- public void stoppingAnUpdateCheckerUnregistersAUsk() {
- updateChecker.stop();
- verify(freenetInterface).unregisterUsk(any(FreenetURI.class));
- }
-
- @Test
- public void callbackDoesNotDownloadIfNewEditionIsNotFound() {
- setupCallbackWithEdition(MAX_VALUE, false, false);
- verify(freenetInterface, never()).fetchUri(any(FreenetURI.class));
- verify(eventBus, never()).post(argThat(instanceOf(UpdateFoundEvent.class)));
- }
-
- private void setupCallbackWithEdition(long edition, boolean newKnownGood, boolean newSlot) {
- ArgumentCaptor<FreenetURI> uri = forClass(FreenetURI.class);
- ArgumentCaptor<Callback> callback = forClass(Callback.class);
- verify(freenetInterface).registerUsk(uri.capture(), callback.capture());
- callback.getValue().editionFound(uri.getValue(), edition, newKnownGood, newSlot);
- }
-
- @Test
- public void callbackStartsIfNewEditionIsFound() {
- setupFetchResult(createFutureFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyEventIsFired(new Version(99, 0, 0), 11865368297000L, false);
- verifyThatUpdateCheckerKnowsLatestVersion(new Version(99, 0, 0), 11865368297000L);
- }
-
- private FetchResult createFutureFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 99.0.0\n" +
- "CurrentVersion/ReleaseTime: 11865368297000\n" +
- "DisruptiveVersion/0.1.2: true").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- private void verifyEventIsFired(Version version, long releaseTime, boolean disruptive) {
- ArgumentCaptor<UpdateFoundEvent> updateFoundEvent = forClass(UpdateFoundEvent.class);
- verify(eventBus, times(1)).post(updateFoundEvent.capture());
- assertThat(updateFoundEvent.getValue().getVersion(), is(version));
- assertThat(updateFoundEvent.getValue().getReleaseTime(), is(releaseTime));
- assertThat(updateFoundEvent.getValue().isDisruptive(), is(disruptive));
- }
-
- private void verifyThatUpdateCheckerKnowsLatestVersion(Version version, long releaseTime) {
- assertThat(updateChecker.getLatestVersion(), is(version));
- assertThat(updateChecker.getLatestVersionDate(), is(releaseTime));
- assertThat(updateChecker.hasLatestVersion(), is(true));
- }
-
- @Test
- public void callbackDoesNotStartIfNoNewEditionIsFound() {
- setupFetchResult(createPastFetchResult());
- setupCallbackWithEdition(updateChecker.getLatestEdition(), true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private void setupFetchResult(final FetchResult pastFetchResult) {
- when(freenetInterface.fetchUri(any(FreenetURI.class))).thenAnswer(new Answer<Fetched>() {
- @Override
- public Fetched answer(InvocationOnMock invocation) throws Throwable {
- FreenetURI freenetUri = (FreenetURI) invocation.getArguments()[0];
- return new Fetched(freenetUri, pastFetchResult);
- }
- });
- }
-
- private FetchResult createPastFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 0.2\n" +
- "CurrentVersion/ReleaseTime: 1289417883000").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void invalidUpdateFileDoesNotStartCallback() {
- setupFetchResult(createInvalidFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private FetchResult createInvalidFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("text/plain");
- Bucket fetched = new ArrayBucket("Some other data.".getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void nonExistingPropertiesWillNotCauseUpdateToBeFound() {
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private void verifyNoUpdateFoundEventIsFired() {
- verify(eventBus, never()).post(any(UpdateFoundEvent.class));
- }
-
- private void verifyAFreenetUriIsFetched() {
- verify(freenetInterface).fetchUri(any(FreenetURI.class));
- }
-
- @Test
- public void brokenBucketDoesNotCauseUpdateToBeFound() {
- setupFetchResult(createBrokenBucketFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private FetchResult createBrokenBucketFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("text/plain");
- Bucket fetched = new ArrayBucket("Some other data.".getBytes()) {
- @Override
- public InputStream getInputStream() {
- try {
- return when(mock(InputStream.class).read()).thenThrow(IOException.class).getMock();
- } catch (IOException ioe1) {
- /* won’t throw here. */
- return null;
- }
- }
- };
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void invalidTimeDoesNotCauseAnUpdateToBeFound() {
- setupFetchResult(createInvalidTimeFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private FetchResult createInvalidTimeFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 0.2\n" +
- "CurrentVersion/ReleaseTime: invalid").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void invalidPropertiesDoesNotCauseAnUpdateToBeFound() {
- setupFetchResult(createMissingTimeFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private FetchResult createMissingTimeFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 0.2\n").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void invalidVersionDoesNotCauseAnUpdateToBeFound() {
- setupFetchResult(createInvalidVersionFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyNoUpdateFoundEventIsFired();
- }
-
- private FetchResult createInvalidVersionFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: foo\n" +
- "CurrentVersion/ReleaseTime: 1289417883000").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
- @Test
- public void disruptiveVersionGetsNotification() {
- setupFetchResult(createDisruptiveVersionFetchResult());
- setupCallbackWithEdition(MAX_VALUE, true, false);
- verifyAFreenetUriIsFetched();
- verifyEventIsFired(new Version(1, 2, 3), 1289417883000L, true);
- verifyThatUpdateCheckerKnowsLatestVersion(new Version(1, 2, 3), 1289417883000L);
- }
-
- private FetchResult createDisruptiveVersionFetchResult() {
- ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 1.2.3\n" +
- "CurrentVersion/ReleaseTime: 1289417883000\n" +
- "DisruptiveVersion/1.2.3: true").getBytes());
- return new FetchResult(clientMetadata, fetched);
- }
-
-}
--- /dev/null
+package net.pterodactylus.sone.core
+
+import com.google.common.eventbus.*
+import freenet.client.*
+import freenet.keys.*
+import freenet.support.io.*
+import net.pterodactylus.sone.core.FreenetInterface.*
+import net.pterodactylus.sone.core.event.*
+import net.pterodactylus.sone.main.*
+import net.pterodactylus.sone.test.*
+import net.pterodactylus.util.version.Version
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.instanceOf
+import org.junit.*
+import org.mockito.ArgumentCaptor.*
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito.never
+import org.mockito.Mockito.times
+import org.mockito.Mockito.verify
+import org.mockito.hamcrest.MockitoHamcrest.*
+import java.io.*
+import kotlin.Long.Companion.MAX_VALUE
+
+/**
+ * Unit test for [UpdateChecker].
+ */
+class UpdateCheckerTest {
+
+ private val eventBus = mock<EventBus>()
+ private val freenetInterface = mock<FreenetInterface>()
+ private val currentVersion = Version(1, 0, 0)
+ private val pluginHomepage = PluginHomepage("KSK@homepage")
+ private val updateChecker = UpdateChecker(eventBus, freenetInterface, currentVersion, pluginHomepage)
+
+ @Before
+ fun startUpdateChecker() {
+ updateChecker.start()
+ }
+
+ @Test
+ fun `new update checker does not have a latest version`() {
+ assertThat(updateChecker.hasLatestVersion(), equalTo(false))
+ assertThat(updateChecker.latestVersion, equalTo(currentVersion))
+ }
+
+ @Test
+ fun `starting an update checker register a usk`() {
+ verify(freenetInterface).registerUsk(any(FreenetURI::class.java), any(Callback::class.java))
+ }
+
+ @Test
+ fun `stopping an update checker unregisters a usk`() {
+ updateChecker.stop()
+ verify(freenetInterface).unregisterUsk(any(FreenetURI::class.java))
+ }
+
+ @Test
+ fun `callback does not download if new edition is not found`() {
+ setupCallbackWithEdition(MAX_VALUE, false)
+ verify(freenetInterface, never()).fetchUri(any(FreenetURI::class.java))
+ verify(eventBus, never()).post(argThat(instanceOf(UpdateFoundEvent::class.java)))
+ }
+
+ private fun setupCallbackWithEdition(edition: Long, newKnownGood: Boolean, newSlot: Boolean = false) {
+ val uri = forClass(FreenetURI::class.java)
+ val callback = forClass(Callback::class.java)
+ verify(freenetInterface).registerUsk(uri.capture(), callback.capture())
+ callback.value.editionFound(uri.value, edition, newKnownGood, newSlot)
+ }
+
+ @Test
+ fun `callback starts if new edition is found`() {
+ setupFetchResult(createFutureFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyEventIsFired(Version(99, 0, 0), 11865368297000L, false)
+ verifyThatUpdateCheckerKnowsLatestVersion(Version(99, 0, 0), 11865368297000L)
+ }
+
+ private fun createFutureFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: 99.0.0\n" +
+ "CurrentVersion/ReleaseTime: 11865368297000\n" +
+ "DisruptiveVersion/0.1.2: true").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ private fun verifyEventIsFired(version: Version, releaseTime: Long, disruptive: Boolean) {
+ val updateFoundEvent = forClass(UpdateFoundEvent::class.java)
+ verify(eventBus, times(1)).post(updateFoundEvent.capture())
+ assertThat(updateFoundEvent.value.version, equalTo(version))
+ assertThat(updateFoundEvent.value.releaseTime, equalTo(releaseTime))
+ assertThat(updateFoundEvent.value.isDisruptive, equalTo(disruptive))
+ }
+
+ private fun verifyThatUpdateCheckerKnowsLatestVersion(version: Version, releaseTime: Long) {
+ assertThat(updateChecker.latestVersion, equalTo(version))
+ assertThat(updateChecker.latestVersionDate, equalTo(releaseTime))
+ assertThat(updateChecker.hasLatestVersion(), equalTo(true))
+ }
+
+ @Test
+ fun `callback does not start if no new edition is found`() {
+ setupFetchResult(createPastFetchResult())
+ setupCallbackWithEdition(updateChecker.latestEdition, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun setupFetchResult(pastFetchResult: FetchResult) {
+ whenever(freenetInterface.fetchUri(any(FreenetURI::class.java))).thenAnswer { invocation ->
+ val freenetUri = invocation.arguments[0] as FreenetURI
+ Fetched(freenetUri, pastFetchResult)
+ }
+ }
+
+ private fun createPastFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: 0.2\n" +
+ "CurrentVersion/ReleaseTime: 1289417883000").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `invalid update file does not start callback`() {
+ setupFetchResult(createInvalidFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun createInvalidFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("text/plain")
+ val fetched = ArrayBucket("Some other data.".toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `non existing properties will not cause update to be found`() {
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun verifyNoUpdateFoundEventIsFired() {
+ verify(eventBus, never()).post(any(UpdateFoundEvent::class.java))
+ }
+
+ private fun verifyAFreenetUriIsFetched() {
+ verify(freenetInterface).fetchUri(any(FreenetURI::class.java))
+ }
+
+ @Test
+ fun `broken bucket does not cause update to be found`() {
+ setupFetchResult(createBrokenBucketFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun createBrokenBucketFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("text/plain")
+ val fetched = object : ArrayBucket("Some other data.".toByteArray()) {
+ override fun getInputStream() =
+ whenever(mock<InputStream>().read()).thenThrow(IOException()).getMock<InputStream>()
+ }
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `invalid time does not cause an update to be found`() {
+ setupFetchResult(createInvalidTimeFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun createInvalidTimeFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: 0.2\n" +
+ "CurrentVersion/ReleaseTime: invalid").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `invalid properties does not cause an update to be found`() {
+ setupFetchResult(createMissingTimeFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun createMissingTimeFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\nCurrentVersion/Version: 0.2\n").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `invalid version does not cause an update to be found`() {
+ setupFetchResult(createInvalidVersionFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyNoUpdateFoundEventIsFired()
+ }
+
+ private fun createInvalidVersionFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: foo\n" +
+ "CurrentVersion/ReleaseTime: 1289417883000").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+ @Test
+ fun `disruptive version gets notification`() {
+ setupFetchResult(createDisruptiveVersionFetchResult())
+ setupCallbackWithEdition(MAX_VALUE, true)
+ verifyAFreenetUriIsFetched()
+ verifyEventIsFired(Version(1, 2, 3), 1289417883000L, true)
+ verifyThatUpdateCheckerKnowsLatestVersion(Version(1, 2, 3), 1289417883000L)
+ }
+
+ private fun createDisruptiveVersionFetchResult(): FetchResult {
+ val clientMetadata = ClientMetadata("application/xml")
+ val fetched = ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: 1.2.3\n" +
+ "CurrentVersion/ReleaseTime: 1289417883000\n" +
+ "DisruptiveVersion/1.2.3: true").toByteArray())
+ return FetchResult(clientMetadata, fetched)
+ }
+
+}