X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FUpdateCheckerTest.java;h=548051a1177280507cf1bf140b291e208b422844;hb=a57dae72e505fec934a9516b88fea75c473f7641;hp=b3f8c08e00a4d11a0fa44f3518bbfa907b8d483e;hpb=fe9f4dae729fa402178990dc717f0ca8d5f5cdb0;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java
index b3f8c08..548051a 100644
--- a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java
@@ -1,32 +1,31 @@
package net.pterodactylus.sone.core;
import static java.lang.Long.MAX_VALUE;
-import static net.pterodactylus.sone.main.SonePlugin.VERSION;
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.Matchers.any;
-import static org.mockito.Matchers.argThat;
+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.FreenetInterface.Fetched;
import net.pterodactylus.sone.core.event.UpdateFoundEvent;
-import net.pterodactylus.sone.freenet.StringBucket;
+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;
@@ -37,14 +36,14 @@ import org.mockito.stubbing.Answer;
/**
* Unit test for {@link UpdateChecker}.
- *
- * @author David âBombeâ Roden
*/
public class UpdateCheckerTest {
private final EventBus eventBus = mock(EventBus.class);
private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
- private final UpdateChecker updateChecker = new UpdateChecker(eventBus, freenetInterface);
+ 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() {
@@ -54,7 +53,7 @@ public class UpdateCheckerTest {
@Test
public void newUpdateCheckerDoesNotHaveALatestVersion() {
assertThat(updateChecker.hasLatestVersion(), is(false));
- assertThat(updateChecker.getLatestVersion(), is(VERSION));
+ assertThat(updateChecker.getLatestVersion(), is(currentVersion));
}
@Test
@@ -87,23 +86,33 @@ public class UpdateCheckerTest {
setupFetchResult(createFutureFetchResult());
setupCallbackWithEdition(MAX_VALUE, true, false);
verifyAFreenetUriIsFetched();
- ArgumentCaptor updateFoundEvent = forClass(UpdateFoundEvent.class);
- verify(eventBus, times(1)).post(updateFoundEvent.capture());
- assertThat(updateFoundEvent.getValue().version(), is(new Version(99, 0, 0)));
- assertThat(updateFoundEvent.getValue().releaseTime(), is(11865368297000L));
- assertThat(updateChecker.getLatestVersion(), is(new Version(99, 0, 0)));
- assertThat(updateChecker.getLatestVersionDate(), is(11865368297000L));
- assertThat(updateChecker.hasLatestVersion(), is(true));
+ 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 StringBucket("# MapConfigurationBackendVersion=1\n" +
+ Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
"CurrentVersion/Version: 99.0.0\n" +
- "CurrentVersion/ReleaseTime: 11865368297000");
+ "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 = 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());
@@ -124,9 +133,9 @@ public class UpdateCheckerTest {
private FetchResult createPastFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
+ Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
"CurrentVersion/Version: 0.2\n" +
- "CurrentVersion/ReleaseTime: 1289417883000");
+ "CurrentVersion/ReleaseTime: 1289417883000").getBytes());
return new FetchResult(clientMetadata, fetched);
}
@@ -140,7 +149,7 @@ public class UpdateCheckerTest {
private FetchResult createInvalidFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("text/plain");
- Bucket fetched = new StringBucket("Some other data.");
+ Bucket fetched = new ArrayBucket("Some other data.".getBytes());
return new FetchResult(clientMetadata, fetched);
}
@@ -169,7 +178,7 @@ public class UpdateCheckerTest {
private FetchResult createBrokenBucketFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("text/plain");
- Bucket fetched = new StringBucket("Some other data.") {
+ Bucket fetched = new ArrayBucket("Some other data.".getBytes()) {
@Override
public InputStream getInputStream() {
try {
@@ -193,9 +202,9 @@ public class UpdateCheckerTest {
private FetchResult createInvalidTimeFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
+ Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
"CurrentVersion/Version: 0.2\n" +
- "CurrentVersion/ReleaseTime: invalid");
+ "CurrentVersion/ReleaseTime: invalid").getBytes());
return new FetchResult(clientMetadata, fetched);
}
@@ -209,8 +218,8 @@ public class UpdateCheckerTest {
private FetchResult createMissingTimeFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
- "CurrentVersion/Version: 0.2\n");
+ Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
+ "CurrentVersion/Version: 0.2\n").getBytes());
return new FetchResult(clientMetadata, fetched);
}
@@ -224,9 +233,27 @@ public class UpdateCheckerTest {
private FetchResult createInvalidVersionFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("application/xml");
- Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
+ Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" +
"CurrentVersion/Version: foo\n" +
- "CurrentVersion/ReleaseTime: 1289417883000");
+ "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);
}