X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=9391bde8d73a570486761304067bd709840ec91c;hb=a57dae72e505fec934a9516b88fea75c473f7641;hp=195876fb986dde6bbc70b3cd9e821de9c3045061;hpb=6e91e9356231217afcecb12275882c0ebf0f34a8;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
index 195876f..9391bde 100644
--- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
@@ -4,8 +4,8 @@ import static freenet.client.FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUN
import static freenet.keys.InsertableClientSSK.createRandom;
import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS;
import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS;
-import static net.pterodactylus.sone.Matchers.delivers;
-import static net.pterodactylus.sone.TestUtil.setFinalField;
+import static net.pterodactylus.sone.test.Matchers.delivers;
+import static net.pterodactylus.sone.test.TestUtil.setFinalField;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@@ -29,10 +29,8 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
-import net.pterodactylus.sone.TestUtil;
import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback;
import net.pterodactylus.sone.core.FreenetInterface.Callback;
-import net.pterodactylus.sone.core.FreenetInterface.Fetched;
import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier;
import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
@@ -43,6 +41,7 @@ import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.data.impl.ImageImpl;
+import net.pterodactylus.sone.test.TestUtil;
import freenet.client.ClientMetadata;
import freenet.client.FetchContext;
@@ -76,6 +75,7 @@ import freenet.support.io.ArrayBucket;
import freenet.support.io.ResumeFailedException;
import com.google.common.eventbus.EventBus;
+import org.hamcrest.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@@ -83,8 +83,6 @@ import org.mockito.ArgumentMatchers;
/**
* Unit test for {@link FreenetInterface}.
- *
- * @author David âBombeâ Roden
*/
public class FreenetInterfaceTest {
@@ -215,7 +213,7 @@ public class FreenetInterfaceTest {
@Test
public void insertingADirectory() throws InsertException, SoneException {
FreenetURI freenetUri = mock(FreenetURI.class);
- HashMap manifestEntries = new HashMap();
+ HashMap manifestEntries = new HashMap<>();
String defaultFile = "index.html";
FreenetURI resultingUri = mock(FreenetURI.class);
when(highLevelSimpleClient.insertManifest(eq(freenetUri), eq(manifestEntries), eq(defaultFile))).thenReturn(resultingUri);
@@ -240,7 +238,7 @@ public class FreenetInterfaceTest {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
Callback callback = mock(Callback.class);
freenetInterface.registerUsk(freenetUri, callback);
- verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -248,7 +246,7 @@ public class FreenetInterfaceTest {
FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt");
Callback callback = mock(Callback.class);
freenetInterface.registerUsk(freenetUri, callback);
- verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -272,9 +270,7 @@ public class FreenetInterfaceTest {
throws MalformedURLException {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
freenetInterface.registerActiveUsk(freenetUri, null);
- verify(uskManager, never()).subscribe(any(USK.class),
- any(USKCallback.class), anyBoolean(),
- eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -282,9 +278,7 @@ public class FreenetInterfaceTest {
throws MalformedURLException {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
freenetInterface.registerPassiveUsk(freenetUri, null);
- verify(uskManager, never()).subscribe(any(USK.class),
- any(USKCallback.class), anyBoolean(),
- eq((RequestClient) highLevelSimpleClient));
+ verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
@@ -360,22 +354,23 @@ public class FreenetInterfaceTest {
insertToken.setClientPutter(clientPutter);
ArgumentCaptor imageInsertStartedEvent = forClass(ImageInsertStartedEvent.class);
verify(eventBus).post(imageInsertStartedEvent.capture());
- assertThat(imageInsertStartedEvent.getValue().image(), is(image));
+ assertThat(imageInsertStartedEvent.getValue().getImage(), is(image));
insertToken.cancel();
ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ assertThat(imageInsertAbortedEvent.getValue().getImage(), is(image));
}
@Test
public void failureWithoutExceptionSendsFailedEvent() {
- insertToken.onFailure(null, null);
+ InsertException insertException = new InsertException(mock(InsertException.class));
+ insertToken.onFailure(insertException, null);
ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
verify(eventBus).post(imageInsertFailedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFailedEvent.getValue().image(), is(image));
- assertThat(imageInsertFailedEvent.getValue().cause(), nullValue());
+ assertThat(imageInsertFailedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().getCause(), Matchers.is(insertException));
}
@Test
@@ -385,8 +380,8 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
verify(eventBus).post(imageInsertFailedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFailedEvent.getValue().image(), is(image));
- assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException));
+ assertThat(imageInsertFailedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().getCause(), is((Throwable) insertException));
}
@Test
@@ -396,7 +391,7 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
verify(eventBus).post(imageInsertAbortedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ assertThat(imageInsertAbortedEvent.getValue().getImage(), is(image));
}
@Test
@@ -414,13 +409,13 @@ public class FreenetInterfaceTest {
ArgumentCaptor imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class);
verify(eventBus).post(imageInsertFinishedEvent.capture());
verify(bucket).free();
- assertThat(imageInsertFinishedEvent.getValue().image(), is(image));
- assertThat(imageInsertFinishedEvent.getValue().resultingUri(), is(generatedUri));
+ assertThat(imageInsertFinishedEvent.getValue().getImage(), is(image));
+ assertThat(imageInsertFinishedEvent.getValue().getResultingUri(), is(generatedUri));
}
@Test
public void insertTokenSupplierSuppliesInsertTokens() {
- InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier();
+ InsertTokenSupplier insertTokenSupplier = new InsertTokenSupplier(freenetInterface);
assertThat(insertTokenSupplier.apply(image), notNullValue());
}
@@ -440,31 +435,36 @@ public class FreenetInterfaceTest {
@Test
public void requestIsNotCancelledForImageMimeType() {
verifySnoopCancelsRequestForMimeType("image/png", false);
+ verify(backgroundFetchCallback, never()).failed(uri);
}
@Test
public void requestIsCancelledForNullMimeType() {
verifySnoopCancelsRequestForMimeType(null, true);
- verify(backgroundFetchCallback, never()).cancelForMimeType(eq(uri), ArgumentMatchers.any());
+ verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), ArgumentMatchers.any(), anyLong());
+ verify(backgroundFetchCallback).failed(uri);
}
@Test
public void requestIsCancelledForVideoMimeType() {
verifySnoopCancelsRequestForMimeType("video/mkv", true);
+ verify(backgroundFetchCallback).failed(uri);
}
@Test
public void requestIsCancelledForAudioMimeType() {
verifySnoopCancelsRequestForMimeType("audio/mpeg", true);
+ verify(backgroundFetchCallback).failed(uri);
}
@Test
public void requestIsCancelledForTextMimeType() {
verifySnoopCancelsRequestForMimeType("text/plain", true);
+ verify(backgroundFetchCallback).failed(uri);
}
private void verifySnoopCancelsRequestForMimeType(String mimeType, boolean cancel) {
- when(backgroundFetchCallback.cancelForMimeType(uri, mimeType)).thenReturn(cancel);
+ when(backgroundFetchCallback.shouldCancel(eq(uri), eq(mimeType), anyLong())).thenReturn(cancel);
freenetInterface.startFetch(uri, backgroundFetchCallback);
ArgumentCaptor snoopMetadata = forClass(SnoopMetadata.class);
verify(clientGetter).setMetaSnoop(snoopMetadata.capture());