Replace web page test base with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jun 2017 20:33:31 +0000 (22:33 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jun 2017 20:33:31 +0000 (22:33 +0200)
46 files changed:
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/web/pages/AboutPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/BookmarkPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/BookmarksPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreateReplyPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreateSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeletePostPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteReplyPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DismissNotificationPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DistrustPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/FollowSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/LikePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/LockSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/LoginPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/LogoutPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/MarkAsKnownPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/OptionsPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/TrustPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UnbookmarkPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UnfollowSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UnlikePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UnlockSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UntrustPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ViewPostPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt [deleted file]

diff --git a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
deleted file mode 100644 (file)
index 9774915..0000000
+++ /dev/null
@@ -1,419 +0,0 @@
-package net.pterodactylus.sone.web.pages;
-
-import static net.pterodactylus.sone.test.GuiceKt.supply;
-import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
-import static net.pterodactylus.util.web.Method.GET;
-import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.anyBoolean;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
-
-import javax.annotation.Nonnull;
-import javax.naming.SizeLimitExceededException;
-
-import net.pterodactylus.sone.core.Core;
-import net.pterodactylus.sone.core.Preferences;
-import net.pterodactylus.sone.core.UpdateChecker;
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
-import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.freenet.wot.OwnIdentity;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
-import net.pterodactylus.util.notify.Notification;
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.web.Method;
-import net.pterodactylus.util.web.Response;
-
-import freenet.clients.http.ToadletContext;
-import freenet.l10n.BaseL10n;
-import freenet.support.SimpleReadOnlyArrayBucket;
-import freenet.support.api.Bucket;
-import freenet.support.api.HTTPRequest;
-import freenet.support.api.HTTPUploadedFile;
-import freenet.support.io.NullBucket;
-
-import com.google.common.base.Optional;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Multimap;
-import com.google.common.eventbus.EventBus;
-import com.google.common.io.ByteStreams;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.rules.ExpectedException;
-import org.mockito.ArgumentMatchers;
-import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-/**
- * Base class for web page tests.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public abstract class WebPageTest {
-
-       @Rule
-       public final ExpectedException expectedException = ExpectedException.none();
-
-       protected final Template template = new Template();
-       protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
-       protected final EventBus eventBus = mock(EventBus.class);
-       protected final Core core = webInterface.getCore();
-       protected final BaseL10n l10n = webInterface.getL10n();
-
-       protected final Sone currentSone = mock(Sone.class);
-
-       protected final TemplateContext templateContext = new TemplateContext();
-       protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
-       protected final Multimap<String, String> requestParameters = ArrayListMultimap.create();
-       protected final Map<String, String> requestParts = new HashMap<>();
-       protected final Map<String, String> requestHeaders = new HashMap<>();
-       private final Map<String, String> uploadedFilesNames = new HashMap<>();
-       private final Map<String, String> uploadedFilesContentTypes = new HashMap<>();
-       private final Map<String, String> uploadedFilesSources = new HashMap<>();
-       protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
-       private final PipedOutputStream responseOutputStream = new PipedOutputStream();
-       private final PipedInputStream responseInputStream;
-       protected final Response response = new Response(responseOutputStream);
-       protected final ToadletContext toadletContext = mock(ToadletContext.class);
-
-       private final Set<OwnIdentity> ownIdentities = new HashSet<>();
-       private final Map<String, Sone> sones = new HashMap<>();
-       protected final List<Sone> localSones = new ArrayList<>();
-       private final ListMultimap<String, PostReply> postReplies = ArrayListMultimap.create();
-
-       protected final Injector injector = Guice.createInjector(
-                       supply(WebInterface.class).byInstance(webInterface),
-                       supply(Template.class).byInstance(template)
-       );
-
-       protected WebPageTest() {
-               try {
-                       responseInputStream = new PipedInputStream(responseOutputStream);
-               } catch (IOException e) {
-                       throw new RuntimeException(e);
-               }
-       }
-
-       @Before
-       public final void setupFreenetRequest() throws SizeLimitExceededException {
-               setMethod(GET);
-               when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
-               when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
-               when(httpRequest.getMultipleParam(anyString())).thenAnswer(new Answer<String[]>() {
-                       @Override
-                       public String[] answer(InvocationOnMock invocation) throws Throwable {
-                               return requestParameters.get(invocation.<String>getArgument(0)).toArray(new String[0]);
-                       }
-               });
-               when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
-                       @Override
-                       public Boolean answer(InvocationOnMock invocation) throws Throwable {
-                               return requestParts.get(invocation.<String>getArgument(0)) != null;
-                       }
-               });
-               when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
-                       @Override
-                       public String[] answer(InvocationOnMock invocation) throws Throwable {
-                               return requestParts.keySet().toArray(new String[requestParts.size()]);
-                       }
-               });
-               when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               String parameter = invocation.getArgument(0);
-                               int maxLength = invocation.getArgument(1);
-                               String value = requestParts.get(parameter);
-                               return requestParts.containsKey(parameter) ? value.substring(0, Math.min(maxLength, value.length())) : "";
-                       }
-               });
-               when(httpRequest.getPartAsStringThrowing(anyString(), anyInt())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               String partName = invocation.getArgument(0);
-                               if (!requestParts.containsKey(partName)) throw new NoSuchElementException();
-                               String partValue = requestParts.get(partName);
-                               if (partValue.length() > invocation.<Integer>getArgument(1)) throw new SizeLimitExceededException();
-                               return partValue;
-                       }
-               });
-               when(httpRequest.hasParameters()).thenAnswer(new Answer<Boolean>() {
-                       @Override
-                       public Boolean answer(InvocationOnMock invocation) throws Throwable {
-                               return !requestParameters.isEmpty();
-                       }
-               });
-               when(httpRequest.getParameterNames()).thenAnswer(new Answer<Collection<String>>() {
-                       @Override
-                       public Collection<String> answer(InvocationOnMock invocation) throws Throwable {
-                               return requestParameters.keySet();
-                       }
-               });
-               when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               String parameter = invocation.getArgument(0);
-                               return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).iterator().next() : "";
-                       }
-               });
-               when(httpRequest.getParam(anyString(), ArgumentMatchers.<String>any())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               String parameter = invocation.getArgument(0);
-                               return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).iterator().next() : invocation.<String>getArgument(1);
-                       }
-               });
-               when(httpRequest.isParameterSet(anyString())).thenAnswer(new Answer<Boolean>() {
-                       @Override
-                       public Boolean answer(InvocationOnMock invocation) throws Throwable {
-                               return requestParameters.containsKey(invocation.<String>getArgument(0)) &&
-                                               requestParameters.get(invocation.<String>getArgument(0)).iterator().next() != null;
-                       }
-               });
-               when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
-                       }
-               });
-               when(httpRequest.getUploadedFile(anyString())).thenAnswer(new Answer<HTTPUploadedFile>() {
-                       @Override
-                       public HTTPUploadedFile answer(InvocationOnMock invocation) throws Throwable {
-                               final String name = invocation.getArgument(0);
-                               if (!uploadedFilesSources.containsKey(name)) {
-                                       return null;
-                               }
-                               return new HTTPUploadedFile() {
-                                       @Override
-                                       public String getContentType() {
-                                               return uploadedFilesContentTypes.get(name);
-                                       }
-
-                                       @Override
-                                       public Bucket getData() {
-                                               try (InputStream inputStream = getClass().getResourceAsStream(uploadedFilesSources.get(name))) {
-                                                       byte[] bytes = ByteStreams.toByteArray(inputStream);
-                                                       return new SimpleReadOnlyArrayBucket(bytes, 0, bytes.length);
-                                               } catch (IOException ioe1) {
-                                                       return new NullBucket();
-                                               }
-                                       }
-
-                                       @Override
-                                       public String getFilename() {
-                                               return uploadedFilesNames.get(name);
-                                       }
-                               };
-                       }
-               });
-       }
-
-       @Before
-       public final void setupCore() {
-               UpdateChecker updateChecker = mock(UpdateChecker.class);
-               when(core.getUpdateChecker()).thenReturn(updateChecker);
-               when(core.getPreferences()).thenReturn(new Preferences(eventBus));
-               when(core.getLocalSone(anyString())).thenReturn(null);
-               when(core.getLocalSones()).thenReturn(localSones);
-               when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
-               when(core.getSones()).thenAnswer(new Answer<Collection<Sone>>() {
-                       @Override
-                       public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
-                               return sones.values();
-                       }
-               });
-               when(core.getSone(anyString())).thenAnswer(new Answer<Optional<Sone>>() {
-                       @Override
-                       public Optional<Sone> answer(InvocationOnMock invocation) throws Throwable {
-                               return Optional.fromNullable(sones.get(invocation.getArgument(0)));
-                       }
-               });
-               when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
-               when(core.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
-               when(core.getReplies(anyString())).thenAnswer(new Answer<List<PostReply>>() {
-                       @Override
-                       public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
-                               return postReplies.get(invocation.<String>getArgument(0));
-                       }
-               });
-               when(core.getAlbum(anyString())).thenReturn(null);
-               when(core.getImage(anyString())).thenReturn(null);
-               when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
-               when(core.getTemporaryImage(anyString())).thenReturn(null);
-       }
-
-       @Before
-       public void setupL10n() {
-               when(l10n.getString(anyString())).thenAnswer(new Answer<String>() {
-                       @Override
-                       public String answer(InvocationOnMock invocation) throws Throwable {
-                               return invocation.getArgument(0);
-                       }
-               });
-       }
-
-       @Before
-       public final void setupIdentityManager() {
-               when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
-       }
-
-       @Before
-       public final void setupWebInterface() {
-               when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
-               when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone);
-               when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone);
-               when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
-               when(webInterface.getNotifications(Mockito.<Sone>any())).thenReturn(new ArrayList<Notification>());
-       }
-
-       @Before
-       public void setupSone() {
-               when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
-       }
-
-       protected SoneTemplatePage getPage() {
-               return null;
-       }
-
-       protected void unsetCurrentSone() {
-               when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
-               when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
-               when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
-       }
-
-       protected void setMethod(Method method) {
-               when(freenetRequest.getMethod()).thenReturn(method);
-               when(httpRequest.getMethod()).thenReturn(method.name());
-       }
-
-       protected void request(String uri) {
-               try {
-                       when(httpRequest.getPath()).thenReturn(uri);
-                       when(freenetRequest.getUri()).thenReturn(new URI(uri));
-               } catch (URISyntaxException e) {
-                       throw new RuntimeException(e);
-               }
-       }
-
-       protected void addHttpRequestHeader(@Nonnull String name, String value) {
-               requestHeaders.put(name.toLowerCase(), value);
-       }
-
-       protected void addHttpRequestParameter(String name, final String value) {
-               requestParameters.put(name, value);
-       }
-
-       protected void addHttpRequestPart(String name, String value) {
-               requestParts.put(name, value);
-       }
-
-       protected void addPost(String postId, Post post) {
-               when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
-       }
-
-       protected void addPostReply(String postReplyId, PostReply postReply) {
-               if (postReply.getPostId() != null) {
-                       postReplies.put(postReply.getPostId(), postReply);
-               }
-               when(core.getPostReply(postReplyId)).thenReturn(Optional.fromNullable(postReply));
-       }
-
-       protected void addSone(String soneId, Sone sone) {
-               sones.put(soneId, sone);
-       }
-
-       protected void addLocalSone(String soneId, Sone sone) {
-               when(core.getLocalSone(eq(soneId))).thenReturn(sone);
-               localSones.add(sone);
-       }
-
-       protected void addOwnIdentity(OwnIdentity ownIdentity) {
-               ownIdentities.add(ownIdentity);
-       }
-
-       protected void addAlbum(String albumId, Album album) {
-               when(core.getAlbum(eq(albumId))).thenReturn(album);
-       }
-
-       protected void addImage(String imageId, Image image) {
-               when(core.getImage(eq(imageId))).thenReturn(image);
-               when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
-       }
-
-       protected void addTemporaryImage(String imageId, TemporaryImage temporaryImage) {
-               when(core.getTemporaryImage(eq(imageId))).thenReturn(temporaryImage);
-       }
-
-       protected void addUploadedFile(@Nonnull String name, @Nonnull String filename, @Nonnull String contentType, @Nonnull String resource) {
-               uploadedFilesNames.put(name, filename);
-               uploadedFilesContentTypes.put(name, contentType);
-               uploadedFilesSources.put(name, resource);
-       }
-
-       protected byte[] getResponseBytes() throws IOException {
-               response.getContent().close();
-               try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
-                       ByteStreams.copy(responseInputStream, outputStream);
-                       return outputStream.toByteArray();
-               }
-       }
-
-       protected void addNotification(String notificationId, Notification notification) {
-               when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
-       }
-
-       protected void verifyRedirect(String target) throws RedirectException {
-               expectedException.expect(redirectsTo(target));
-               getPage().handleRequest(freenetRequest, templateContext);
-       }
-
-       protected void verifyRedirect(String target, Runnable verification) throws RedirectException {
-               expectedException.expect(redirectsTo(target));
-               try {
-                       getPage().handleRequest(freenetRequest, templateContext);
-                       fail();
-               } finally {
-                       verification.run();
-               }
-       }
-
-       protected void verifyNoRedirect(Runnable verification) throws RedirectException {
-               getPage().handleRequest(freenetRequest, templateContext);
-               verification.run();
-       }
-
-       protected void addTranslation(@Nonnull String key, @Nonnull String value) {
-               when(l10n.getString(key)).thenReturn(value);
-       }
-
-}
index c692960..142bf81 100644 (file)
@@ -10,12 +10,13 @@ import org.junit.Test
 /**
  * Unit test for [AboutPage].
  */
-class AboutPageTest: WebPageTest() {
+class AboutPageTest: WebPageTest({ template, webInterface -> AboutPage(template, webInterface, PluginVersion(version), PluginYear(year), PluginHomepage(homepage)) }) {
 
-       private val version = "0.1.2"
-       private val year = 1234
-       private val homepage = "home://page"
-       private val page = AboutPage(template, webInterface, PluginVersion(version), PluginYear(year), PluginHomepage(homepage))
+       companion object {
+               private const val version = "0.1.2"
+               private const val year = 1234
+               private const val homepage = "home://page"
+       }
 
        @Test
        fun `page returns correct path`() {
index c03706b..a805843 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [BookmarkPage].
  */
-class BookmarkPageTest: WebPageTest2(::BookmarkPage) {
+class BookmarkPageTest: WebPageTest(::BookmarkPage) {
 
        @Test
        fun `path is set correctly`() {
index 04446a2..e853199 100644 (file)
@@ -13,7 +13,7 @@ import org.junit.Test
 /**
  * Unit test for [BookmarksPage].
  */
-class BookmarksPageTest: WebPageTest2(::BookmarksPage) {
+class BookmarksPageTest: WebPageTest(::BookmarksPage) {
 
        private val post1 = createLoadedPost(1000)
        private val post2 = createLoadedPost(3000)
index 4d8ec26..5cc463a 100644 (file)
@@ -16,7 +16,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [CreateAlbumPage].
  */
-class CreateAlbumPageTest: WebPageTest2(::CreateAlbumPage) {
+class CreateAlbumPageTest: WebPageTest(::CreateAlbumPage) {
 
        private val parentAlbum = createAlbum("parent-id")
        private val newAlbum = createAlbum("album-id")
index 35ec597..3b1cc8c 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [CreatePostPage].
  */
-class CreatePostPageTest: WebPageTest2(::CreatePostPage) {
+class CreatePostPageTest: WebPageTest(::CreatePostPage) {
 
        @Test
        fun `page returns correct path`() {
index 778de4d..71e83bc 100644 (file)
@@ -12,7 +12,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [CreateReplyPage].
  */
-class CreateReplyPageTest: WebPageTest2(::CreateReplyPage) {
+class CreateReplyPageTest: WebPageTest(::CreateReplyPage) {
 
        @Test
        fun `page returns correct path`() {
index 95afaa8..9cbf2c3 100644 (file)
@@ -16,7 +16,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [CreateSonePage].
  */
-class CreateSonePageTest: WebPageTest2(::CreateSonePage) {
+class CreateSonePageTest: WebPageTest(::CreateSonePage) {
 
        private val localSones_ = listOf(
                        createSone("local-sone1"),
index 6800ef4..969a133 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeleteAlbumPage].
  */
-class DeleteAlbumPageTest: WebPageTest2(::DeleteAlbumPage) {
+class DeleteAlbumPageTest: WebPageTest(::DeleteAlbumPage) {
 
        private val sone = mock<Sone>()
        private val album = mock<Album>()
index 9a0c990..1b90c4a 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeleteImagePage].
  */
-class DeleteImagePageTest: WebPageTest2(::DeleteImagePage) {
+class DeleteImagePageTest: WebPageTest(::DeleteImagePage) {
 
        private val image = mock<Image>()
        private val sone = mock<Sone>()
index 383e38c..5ccd5c2 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeletePostPage].
  */
-class DeletePostPageTest: WebPageTest2(::DeletePostPage) {
+class DeletePostPageTest: WebPageTest(::DeletePostPage) {
 
        private val post = mock<Post>()
        private val sone = mock<Sone>()
index 862fff4..8f344eb 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeleteProfileFieldPage].
  */
-class DeleteProfileFieldPageTest: WebPageTest2(::DeleteProfileFieldPage) {
+class DeleteProfileFieldPageTest: WebPageTest(::DeleteProfileFieldPage) {
 
        private val profile = Profile(currentSone)
        private val field = profile.addField("name")
index ba40988..c332557 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeleteReplyPage].
  */
-class DeleteReplyPageTest: WebPageTest2(::DeleteReplyPage) {
+class DeleteReplyPageTest: WebPageTest(::DeleteReplyPage) {
 
        private val sone = mock<Sone>()
        private val reply = mock<PostReply>()
index 5d73338..415adf6 100644 (file)
@@ -12,7 +12,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DeleteSonePage].
  */
-class DeleteSonePageTest: WebPageTest2(::DeleteSonePage) {
+class DeleteSonePageTest: WebPageTest(::DeleteSonePage) {
 
        @Test
        fun `page returns correct path`() {
index acb3d0a..41f47aa 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DismissNotificationPage].
  */
-class DismissNotificationPageTest: WebPageTest2(::DismissNotificationPage) {
+class DismissNotificationPageTest: WebPageTest(::DismissNotificationPage) {
 
        private val notification = mock<Notification>()
 
index a1a59b9..3261df6 100644 (file)
@@ -12,7 +12,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [DistrustPage].
  */
-class DistrustPageTest: WebPageTest2(::DistrustPage) {
+class DistrustPageTest: WebPageTest(::DistrustPage) {
 
        @Test
        fun `page returns correct path`() {
index 5ef276f..b4328c6 100644 (file)
@@ -17,7 +17,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [EditAlbumPage].
  */
-class EditAlbumPageTest: WebPageTest2(::EditAlbumPage) {
+class EditAlbumPageTest: WebPageTest(::EditAlbumPage) {
 
        private val album = mock<Album>()
        private val parentAlbum = mock<Album>()
index 73b6eca..f6eb02e 100644 (file)
@@ -20,7 +20,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [EditImagePage].
  */
-class EditImagePageTest: WebPageTest2(::EditImagePage) {
+class EditImagePageTest: WebPageTest(::EditImagePage) {
 
        private val image = mock<Image>()
        private val modifier = mockBuilder<Modifier>()
index 8e931e5..b4acc7e 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [EditProfileFieldPage].
  */
-class EditProfileFieldPageTest: WebPageTest2(::EditProfileFieldPage) {
+class EditProfileFieldPageTest: WebPageTest(::EditProfileFieldPage) {
 
        private val profile = Profile(currentSone)
        private val field = profile.addField("Name")
index bb8e179..f76786a 100644 (file)
@@ -17,7 +17,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [EditProfilePage].
  */
-class EditProfilePageTest: WebPageTest2(::EditProfilePage) {
+class EditProfilePageTest: WebPageTest(::EditProfilePage) {
 
        private val profile = Profile(currentSone)
        private val firstField = profile.addField("First Field")
index 3e80cc5..21964aa 100644 (file)
@@ -16,7 +16,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [FollowSonePage].
  */
-class FollowSonePageTest: WebPageTest2(::FollowSonePage) {
+class FollowSonePageTest: WebPageTest(::FollowSonePage) {
 
        @Test
        fun `page returns correct path`() {
index 6c34e3f..b5efeac 100644 (file)
@@ -11,7 +11,7 @@ import org.junit.Test
  */
 class GetImagePageTest {
 
-       private val scaffolding = WebPageTest2()
+       private val scaffolding = WebPageTest()
        private val page = GetImagePage(scaffolding.webInterface)
        private val freenetRequest = scaffolding.freenetRequest
        private val response = scaffolding.response
index d426845..2c581ac 100644 (file)
@@ -13,7 +13,7 @@ import org.junit.Test
 /**
  * Unit test for [ImageBrowserPage].
  */
-class ImageBrowserPageTest: WebPageTest2(::ImageBrowserPage) {
+class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
 
        @Test
        fun `page returns correct path`() {
index ead3cbb..b51627d 100644 (file)
@@ -19,7 +19,7 @@ import org.mockito.ArgumentMatchers
 /**
  * Unit test for [IndexPage].
  */
-class IndexPageTest: WebPageTest2({ template, webInterface -> IndexPage(template, webInterface, postVisibilityFilter) }) {
+class IndexPageTest: WebPageTest({ template, webInterface -> IndexPage(template, webInterface, postVisibilityFilter) }) {
 
        companion object {
                private val postVisibilityFilter = mock<PostVisibilityFilter>()
index 5109818..70a5eea 100644 (file)
@@ -20,7 +20,7 @@ import org.junit.Test
 /**
  * Unit test for [KnownSonesPage].
  */
-class KnownSonesPageTest: WebPageTest2(::KnownSonesPage) {
+class KnownSonesPageTest: WebPageTest(::KnownSonesPage) {
 
        private val sones = listOf(
                        createSone(1000, 4, 7, 2, "sone2", true, true),
index 9a293cc..9a08737 100644 (file)
@@ -10,7 +10,7 @@ import org.mockito.Mockito.verifyNoMoreInteractions
 /**
  * Unit test for [LikePage].
  */
-class LikePageTest: WebPageTest2(::LikePage) {
+class LikePageTest: WebPageTest(::LikePage) {
 
        @Test
        fun `page returns correct path`() {
index 8cc6028..2a9b4ed 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [LockSonePage].
  */
-class LockSonePageTest: WebPageTest2(::LockSonePage) {
+class LockSonePageTest: WebPageTest(::LockSonePage) {
 
        @Test
        fun `page returns correct path`() {
index e9fdb5c..1b409b7 100644 (file)
@@ -19,7 +19,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [LoginPage].
  */
-class LoginPageTest: WebPageTest2(::LoginPage) {
+class LoginPageTest: WebPageTest(::LoginPage) {
 
        private val sones = listOf(createSone("Sone", "Test"), createSone("Test"), createSone("Sone"))
 
index cb53646..9dbc82a 100644 (file)
@@ -9,7 +9,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [LogoutPage].
  */
-class LogoutPageTest: WebPageTest2(::LogoutPage) {
+class LogoutPageTest: WebPageTest(::LogoutPage) {
 
        @Test
        fun `page returns correct path`() {
index 3744b5b..d9a132a 100644 (file)
@@ -13,7 +13,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [MarkAsKnownPage].
  */
-class MarkAsKnownPageTest: WebPageTest2(::MarkAsKnownPage) {
+class MarkAsKnownPageTest: WebPageTest(::MarkAsKnownPage) {
 
        @Test
        fun `page returns correct path`() {
index fb4f46d..96427e7 100644 (file)
@@ -17,7 +17,7 @@ import java.util.Arrays.asList
 /**
  * Unit test for [NewPage].
  */
-class NewPageTest: WebPageTest2(::NewPage) {
+class NewPageTest: WebPageTest(::NewPage) {
 
        @Before
        fun setupNumberOfPostsPerPage() {
index 28844b9..4e4c02e 100644 (file)
@@ -18,7 +18,7 @@ import org.junit.Test
 /**
  * Unit test for [OptionsPage].
  */
-class OptionsPageTest: WebPageTest2(::OptionsPage) {
+class OptionsPageTest: WebPageTest(::OptionsPage) {
 
        @Before
        fun setupPreferences() {
index bb94fbf..e8ec10a 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [RescuePage].
  */
-class RescuePageTest: WebPageTest2(::RescuePage) {
+class RescuePageTest: WebPageTest(::RescuePage) {
 
        private val soneRescuer = mock<SoneRescuer>()
 
index c93ac0c..94af5da 100644 (file)
@@ -22,7 +22,7 @@ import java.util.concurrent.atomic.AtomicInteger
 /**
  * Unit test for [SearchPage].
  */
-class SearchPageTest: WebPageTest2({ template, webInterface -> SearchPage(template, webInterface, ticker) }) {
+class SearchPageTest: WebPageTest({ template, webInterface -> SearchPage(template, webInterface, ticker) }) {
 
        companion object {
                val ticker = mock<Ticker>()
index c872b08..10dfb6e 100644 (file)
@@ -21,7 +21,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [SoneTemplatePage].
  */
-class SoneTemplatePageTest: WebPageTest2({ template, webInterface -> object : SoneTemplatePage("path.html", template, webInterface, true) {}}) {
+class SoneTemplatePageTest: WebPageTest({ template, webInterface -> object : SoneTemplatePage("path.html", template, webInterface, true) {}}) {
 
        @Test
        fun `current sone is retrieved from web interface`() {
index e16d001..cc77203 100644 (file)
@@ -14,7 +14,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [TrustPage].
  */
-class TrustPageTest: WebPageTest2(::TrustPage) {
+class TrustPageTest: WebPageTest(::TrustPage) {
 
        @Test
        fun `page returns correct path`() {
index 5bf7f85..a9f784e 100644 (file)
@@ -17,7 +17,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UnbookmarkPage].
  */
-class UnbookmarkPageTest: WebPageTest2(::UnbookmarkPage) {
+class UnbookmarkPageTest: WebPageTest(::UnbookmarkPage) {
 
        @Test
        fun `page returns correct path`() {
index 444a355..01721cd 100644 (file)
@@ -9,7 +9,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UnfollowSonePage].
  */
-class UnfollowSonePageTest: WebPageTest2(::UnfollowSonePage) {
+class UnfollowSonePageTest: WebPageTest(::UnfollowSonePage) {
 
        @Test
        fun `page returns correct path`() {
index d3dd60a..fe00729 100644 (file)
@@ -11,7 +11,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UnlikePage].
  */
-class UnlikePageTest: WebPageTest2(::UnlikePage) {
+class UnlikePageTest: WebPageTest(::UnlikePage) {
 
        @Test
        fun `page returns correct path`() {
index 9cde13c..6d46922 100644 (file)
@@ -14,7 +14,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UnlockSonePage].
  */
-class UnlockSonePageTest: WebPageTest2(::UnlockSonePage) {
+class UnlockSonePageTest: WebPageTest(::UnlockSonePage) {
 
        @Test
        fun `page returns correct path`() {
index 1f5a31e..9d18162 100644 (file)
@@ -14,7 +14,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UntrustPage].
  */
-class UntrustPageTest: WebPageTest2(::UntrustPage) {
+class UntrustPageTest: WebPageTest(::UntrustPage) {
 
        @Test
        fun `page returns correct path`() {
index 9c0d144..58e63a2 100644 (file)
@@ -20,7 +20,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [UploadImagePage].
  */
-class UploadImagePageTest: WebPageTest2(::UploadImagePage) {
+class UploadImagePageTest: WebPageTest(::UploadImagePage) {
 
        private val parentAlbum = mock<Album>().apply {
                whenever(id).thenReturn("parent-id")
index e3259f5..b66f6cc 100644 (file)
@@ -12,7 +12,7 @@ import org.junit.Test
 /**
  * Unit test for [ViewPostPage].
  */
-class ViewPostPageTest: WebPageTest2(::ViewPostPage) {
+class ViewPostPageTest: WebPageTest(::ViewPostPage) {
 
        private val post = mock<Post>()
 
index 7df6b7e..d7ff46d 100644 (file)
@@ -19,7 +19,7 @@ import org.junit.Test
 /**
  * Unit test for [ViewSonePage].
  */
-class ViewSonePageTest: WebPageTest2(::ViewSonePage) {
+class ViewSonePageTest: WebPageTest(::ViewSonePage) {
 
        init {
                whenever(currentSone.id).thenReturn("sone-id")
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt
new file mode 100644 (file)
index 0000000..fc59639
--- /dev/null
@@ -0,0 +1,251 @@
+package net.pterodactylus.sone.web.pages
+
+import com.google.common.eventbus.EventBus
+import freenet.clients.http.ToadletContext
+import freenet.support.SimpleReadOnlyArrayBucket
+import freenet.support.api.HTTPRequest
+import freenet.support.api.HTTPUploadedFile
+import net.pterodactylus.sone.core.Preferences
+import net.pterodactylus.sone.data.Album
+import net.pterodactylus.sone.data.Image
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.data.TemporaryImage
+import net.pterodactylus.sone.freenet.wot.OwnIdentity
+import net.pterodactylus.sone.test.deepMock
+import net.pterodactylus.sone.test.get
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asList
+import net.pterodactylus.sone.utils.asOptional
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException
+import net.pterodactylus.util.notify.Notification
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+import net.pterodactylus.util.web.Method
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Response
+import org.junit.Assert.fail
+import org.mockito.ArgumentMatchers.anyBoolean
+import org.mockito.ArgumentMatchers.anyInt
+import org.mockito.ArgumentMatchers.anyLong
+import org.mockito.ArgumentMatchers.anyString
+import org.mockito.ArgumentMatchers.eq
+import java.io.ByteArrayOutputStream
+import java.net.URI
+import java.nio.charset.Charset
+import kotlin.text.Charsets.UTF_8
+
+/**
+ * Base class for web page tests.
+ */
+open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePage = { _, _ -> mock<SoneTemplatePage>() }) {
+
+       val currentSone = mock<Sone>()
+       val template = mock<Template>()
+       val webInterface = deepMock<WebInterface>()
+       val core = webInterface.core!!
+       val eventBus = mock<EventBus>()
+       val preferences = Preferences(eventBus)
+       val l10n = webInterface.l10n!!
+
+       val page by lazy { pageSupplier(template, webInterface) }
+       val httpRequest = mock<HTTPRequest>()
+       val freenetRequest = mock<FreenetRequest>()
+       val templateContext = TemplateContext()
+       val toadletContext = deepMock<ToadletContext>()
+       val responseContent = ByteArrayOutputStream()
+       val response = Response(responseContent)
+
+       private val requestHeaders = mutableMapOf<String, String>()
+       private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
+       private val postRequestParameters = mutableMapOf<String, ByteArray>()
+       private val uploadedFileNames = mutableMapOf<String, String>()
+       private val uploadedFileContentTypes = mutableMapOf<String, String>()
+       private val uploadedFileResources = mutableMapOf<String, String>()
+       private val ownIdentities = mutableSetOf<OwnIdentity>()
+       private val allSones = mutableMapOf<String, Sone>()
+       private val localSones = mutableMapOf<String, Sone>()
+       private val allPosts = mutableMapOf<String, Post>()
+       private val allPostReplies = mutableMapOf<String, PostReply>()
+       private val perPostReplies = mutableMapOf<String, PostReply>()
+       private val allAlbums = mutableMapOf<String, Album>()
+       private val allImages = mutableMapOf<String, Image>()
+       private val notifications = mutableMapOf<String, Notification>()
+       private val translations = mutableMapOf<String, String>()
+
+       init {
+               setupCore()
+               setupWebInterface()
+               setupHttpRequest()
+               setupFreenetRequest()
+               setupTranslations()
+       }
+
+       private fun setupCore() {
+               whenever(core.preferences).thenReturn(preferences)
+               whenever(core.identityManager.allOwnIdentities).then { ownIdentities }
+               whenever(core.sones).then { allSones.values }
+               whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
+               whenever(core.localSones).then { localSones.values }
+               whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
+               whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
+               whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]].asOptional() }
+               whenever(core.getReplies(anyString())).then { perPostReplies[it[0]].asList() }
+               whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] }
+               whenever(core.getImage(anyString())).then { allImages[it[0]]}
+               whenever(core.getImage(anyString(), anyBoolean())).then { allImages[it[0]]}
+               whenever(core.getTemporaryImage(anyString())).thenReturn(null)
+       }
+
+       private fun setupWebInterface() {
+               whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
+               whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)
+               whenever(webInterface.getNotifications(currentSone)).then { notifications.values }
+               whenever(webInterface.getNotification(anyString())).then { notifications[it[0]].asOptional() }
+       }
+
+       private fun setupHttpRequest() {
+               whenever(httpRequest.method).thenReturn("GET")
+               whenever(httpRequest.getHeader(anyString())).then { requestHeaders[it.get<String>(0).toLowerCase()] }
+               whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
+               whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
+               whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
+               whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
+               whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
+               whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
+               whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
+               whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
+               whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
+               whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
+               whenever(httpRequest.isPartSet(anyString())).then { it[0] in postRequestParameters }
+               whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode()?.take(it[1]) ?: "" }
+               whenever(httpRequest.getUploadedFile(anyString())).then {
+                       it.get<String>(0).takeIf { it in uploadedFileNames }
+                                       ?.let { name -> UploadedFile(uploadedFileNames[name]!!, uploadedFileContentTypes[name]!!, uploadedFileResources[name]!!)
+                       }
+               }
+       }
+
+       private class UploadedFile(private val filename: String, private val contentType: String, private val resourceName: String): HTTPUploadedFile {
+               override fun getFilename() = filename
+               override fun getContentType() = contentType
+               override fun getData() = javaClass.getResourceAsStream(resourceName).readBytes().let(::SimpleReadOnlyArrayBucket)
+       }
+
+       private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
+
+       private fun setupFreenetRequest() {
+               whenever(freenetRequest.method).thenReturn(GET)
+               whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
+               whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
+       }
+
+       private fun setupTranslations() {
+               whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
+       }
+
+       fun setMethod(method: Method) {
+               whenever(httpRequest.method).thenReturn(method.name)
+               whenever(freenetRequest.method).thenReturn(method)
+       }
+
+       fun request(uri: String) {
+               whenever(httpRequest.path).thenReturn(uri)
+               whenever(freenetRequest.uri).thenReturn(URI(uri))
+       }
+
+       fun addHttpRequestHeader(name: String, value: String) {
+               requestHeaders[name.toLowerCase()] = value
+       }
+
+       fun addHttpRequestParameter(name: String, value: String) {
+               getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
+       }
+
+       fun addHttpRequestPart(name: String, value: String) {
+               postRequestParameters[name] = value.toByteArray(UTF_8)
+       }
+
+       fun unsetCurrentSone() {
+               whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(null)
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
+               whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(null)
+       }
+
+       fun addOwnIdentity(ownIdentity: OwnIdentity) {
+               ownIdentities += ownIdentity
+       }
+
+       fun addSone(id: String, sone: Sone) {
+               allSones[id] = sone
+       }
+
+       fun addLocalSone(id: String, localSone: Sone) {
+               localSones[id] = localSone
+       }
+
+       fun addPost(id: String, post: Post) {
+               allPosts[id] = post
+       }
+
+       fun addPostReply(id: String, postReply: PostReply) {
+               allPostReplies[id] = postReply
+               postReply.postId?.also { perPostReplies[it] = postReply }
+       }
+
+       fun addAlbum(id: String, album: Album) {
+               allAlbums[id] = album
+       }
+
+       fun addImage(id: String, image: Image) {
+               allImages[id] = image
+       }
+
+       fun addTranslation(key: String, value: String) {
+               translations[key] = value
+       }
+
+       fun addNotification(id: String, notification: Notification) {
+               notifications[id] = notification
+       }
+
+       fun addTemporaryImage(id: String, temporaryImage: TemporaryImage) {
+               whenever(core.getTemporaryImage(id)).thenReturn(temporaryImage)
+       }
+
+       fun addUploadedFile(name: String, filename: String, contentType: String, resource: String) {
+               uploadedFileNames[name] = filename
+               uploadedFileContentTypes[name] = contentType
+               uploadedFileResources[name] = resource
+       }
+
+       fun verifyNoRedirect(assertions: () -> Unit) {
+               var caughtException: Exception? = null
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } catch (e: Exception) {
+                       caughtException = e
+               }
+               caughtException?.run { throw this } ?: assertions()
+       }
+
+       fun verifyRedirect(target: String, assertions: () -> Unit = {}) {
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+                       fail()
+               } catch (re: RedirectException) {
+                       if (re.target != target) {
+                               throw re
+                       }
+                       assertions()
+               } catch (e: Exception) {
+                       throw e
+               }
+       }
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt
deleted file mode 100644 (file)
index e8fbd98..0000000
+++ /dev/null
@@ -1,251 +0,0 @@
-package net.pterodactylus.sone.web.pages
-
-import com.google.common.eventbus.EventBus
-import freenet.clients.http.ToadletContext
-import freenet.support.SimpleReadOnlyArrayBucket
-import freenet.support.api.HTTPRequest
-import freenet.support.api.HTTPUploadedFile
-import net.pterodactylus.sone.core.Preferences
-import net.pterodactylus.sone.data.Album
-import net.pterodactylus.sone.data.Image
-import net.pterodactylus.sone.data.Post
-import net.pterodactylus.sone.data.PostReply
-import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.data.TemporaryImage
-import net.pterodactylus.sone.freenet.wot.OwnIdentity
-import net.pterodactylus.sone.test.deepMock
-import net.pterodactylus.sone.test.get
-import net.pterodactylus.sone.test.mock
-import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.sone.utils.asList
-import net.pterodactylus.sone.utils.asOptional
-import net.pterodactylus.sone.web.WebInterface
-import net.pterodactylus.sone.web.page.FreenetRequest
-import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException
-import net.pterodactylus.util.notify.Notification
-import net.pterodactylus.util.template.Template
-import net.pterodactylus.util.template.TemplateContext
-import net.pterodactylus.util.web.Method
-import net.pterodactylus.util.web.Method.GET
-import net.pterodactylus.util.web.Response
-import org.junit.Assert.fail
-import org.mockito.ArgumentMatchers.anyBoolean
-import org.mockito.ArgumentMatchers.anyInt
-import org.mockito.ArgumentMatchers.anyLong
-import org.mockito.ArgumentMatchers.anyString
-import org.mockito.ArgumentMatchers.eq
-import java.io.ByteArrayOutputStream
-import java.net.URI
-import java.nio.charset.Charset
-import kotlin.text.Charsets.UTF_8
-
-/**
- * Base class for web page tests.
- */
-open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage = { _, _ -> mock<SoneTemplatePage>() }) {
-
-       val currentSone = mock<Sone>()
-       val template = mock<Template>()
-       val webInterface = deepMock<WebInterface>()
-       val core = webInterface.core!!
-       val eventBus = mock<EventBus>()
-       val preferences = Preferences(eventBus)
-       val l10n = webInterface.l10n!!
-
-       val page by lazy { pageSupplier(template, webInterface) }
-       val httpRequest = mock<HTTPRequest>()
-       val freenetRequest = mock<FreenetRequest>()
-       val templateContext = TemplateContext()
-       val toadletContext = deepMock<ToadletContext>()
-       val responseContent = ByteArrayOutputStream()
-       val response = Response(responseContent)
-
-       private val requestHeaders = mutableMapOf<String, String>()
-       private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
-       private val postRequestParameters = mutableMapOf<String, ByteArray>()
-       private val uploadedFileNames = mutableMapOf<String, String>()
-       private val uploadedFileContentTypes = mutableMapOf<String, String>()
-       private val uploadedFileResources = mutableMapOf<String, String>()
-       private val ownIdentities = mutableSetOf<OwnIdentity>()
-       private val allSones = mutableMapOf<String, Sone>()
-       private val localSones = mutableMapOf<String, Sone>()
-       private val allPosts = mutableMapOf<String, Post>()
-       private val allPostReplies = mutableMapOf<String, PostReply>()
-       private val perPostReplies = mutableMapOf<String, PostReply>()
-       private val allAlbums = mutableMapOf<String, Album>()
-       private val allImages = mutableMapOf<String, Image>()
-       private val notifications = mutableMapOf<String, Notification>()
-       private val translations = mutableMapOf<String, String>()
-
-       init {
-               setupCore()
-               setupWebInterface()
-               setupHttpRequest()
-               setupFreenetRequest()
-               setupTranslations()
-       }
-
-       private fun setupCore() {
-               whenever(core.preferences).thenReturn(preferences)
-               whenever(core.identityManager.allOwnIdentities).then { ownIdentities }
-               whenever(core.sones).then { allSones.values }
-               whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
-               whenever(core.localSones).then { localSones.values }
-               whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
-               whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
-               whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]].asOptional() }
-               whenever(core.getReplies(anyString())).then { perPostReplies[it[0]].asList() }
-               whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] }
-               whenever(core.getImage(anyString())).then { allImages[it[0]]}
-               whenever(core.getImage(anyString(), anyBoolean())).then { allImages[it[0]]}
-               whenever(core.getTemporaryImage(anyString())).thenReturn(null)
-       }
-
-       private fun setupWebInterface() {
-               whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
-               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
-               whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)
-               whenever(webInterface.getNotifications(currentSone)).then { notifications.values }
-               whenever(webInterface.getNotification(anyString())).then { notifications[it[0]].asOptional() }
-       }
-
-       private fun setupHttpRequest() {
-               whenever(httpRequest.method).thenReturn("GET")
-               whenever(httpRequest.getHeader(anyString())).then { requestHeaders[it.get<String>(0).toLowerCase()] }
-               whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
-               whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
-               whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
-               whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
-               whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
-               whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
-               whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
-               whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
-               whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
-               whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
-               whenever(httpRequest.isPartSet(anyString())).then { it[0] in postRequestParameters }
-               whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode()?.take(it[1]) ?: "" }
-               whenever(httpRequest.getUploadedFile(anyString())).then {
-                       it.get<String>(0).takeIf { it in uploadedFileNames }
-                                       ?.let { name -> UploadedFile(uploadedFileNames[name]!!, uploadedFileContentTypes[name]!!, uploadedFileResources[name]!!)
-                       }
-               }
-       }
-
-       private class UploadedFile(private val filename: String, private val contentType: String, private val resourceName: String): HTTPUploadedFile {
-               override fun getFilename() = filename
-               override fun getContentType() = contentType
-               override fun getData() = javaClass.getResourceAsStream(resourceName).readBytes().let(::SimpleReadOnlyArrayBucket)
-       }
-
-       private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
-
-       private fun setupFreenetRequest() {
-               whenever(freenetRequest.method).thenReturn(GET)
-               whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
-               whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
-       }
-
-       private fun setupTranslations() {
-               whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
-       }
-
-       fun setMethod(method: Method) {
-               whenever(httpRequest.method).thenReturn(method.name)
-               whenever(freenetRequest.method).thenReturn(method)
-       }
-
-       fun request(uri: String) {
-               whenever(httpRequest.path).thenReturn(uri)
-               whenever(freenetRequest.uri).thenReturn(URI(uri))
-       }
-
-       fun addHttpRequestHeader(name: String, value: String) {
-               requestHeaders[name.toLowerCase()] = value
-       }
-
-       fun addHttpRequestParameter(name: String, value: String) {
-               getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
-       }
-
-       fun addHttpRequestPart(name: String, value: String) {
-               postRequestParameters[name] = value.toByteArray(UTF_8)
-       }
-
-       fun unsetCurrentSone() {
-               whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(null)
-               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
-               whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(null)
-       }
-
-       fun addOwnIdentity(ownIdentity: OwnIdentity) {
-               ownIdentities += ownIdentity
-       }
-
-       fun addSone(id: String, sone: Sone) {
-               allSones[id] = sone
-       }
-
-       fun addLocalSone(id: String, localSone: Sone) {
-               localSones[id] = localSone
-       }
-
-       fun addPost(id: String, post: Post) {
-               allPosts[id] = post
-       }
-
-       fun addPostReply(id: String, postReply: PostReply) {
-               allPostReplies[id] = postReply
-               postReply.postId?.also { perPostReplies[it] = postReply }
-       }
-
-       fun addAlbum(id: String, album: Album) {
-               allAlbums[id] = album
-       }
-
-       fun addImage(id: String, image: Image) {
-               allImages[id] = image
-       }
-
-       fun addTranslation(key: String, value: String) {
-               translations[key] = value
-       }
-
-       fun addNotification(id: String, notification: Notification) {
-               notifications[id] = notification
-       }
-
-       fun addTemporaryImage(id: String, temporaryImage: TemporaryImage) {
-               whenever(core.getTemporaryImage(id)).thenReturn(temporaryImage)
-       }
-
-       fun addUploadedFile(name: String, filename: String, contentType: String, resource: String) {
-               uploadedFileNames[name] = filename
-               uploadedFileContentTypes[name] = contentType
-               uploadedFileResources[name] = resource
-       }
-
-       fun verifyNoRedirect(assertions: () -> Unit) {
-               var caughtException: Exception? = null
-               try {
-                       page.handleRequest(freenetRequest, templateContext)
-               } catch (e: Exception) {
-                       caughtException = e
-               }
-               caughtException?.run { throw this } ?: assertions()
-       }
-
-       fun verifyRedirect(target: String, assertions: () -> Unit = {}) {
-               try {
-                       page.handleRequest(freenetRequest, templateContext)
-                       fail()
-               } catch (re: RedirectException) {
-                       if (re.target != target) {
-                               throw re
-                       }
-                       assertions()
-               } catch (e: Exception) {
-                       throw e
-               }
-       }
-
-}