From bf509980d5097e67d1a32f6b53bef052b396137f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 2 Jul 2020 19:09:04 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Remove=20template=20context=20fac?= =?utf8?q?tory=20from=20web=20interface=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/web/WebInterface.java | 15 +++------------ .../sone/web/ajax/GetNotificationsAjaxPage.kt | 5 +++-- .../net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt | 5 +++-- .../net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt | 5 +++-- .../sone/web/ajax/GetNotificationsAjaxPageTest.kt | 5 +++-- .../pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt | 12 +++++++----- .../pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt | 12 +++++++----- .../kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt | 1 - 8 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 8585650..27ef8d5 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -174,15 +174,6 @@ public class WebInterface implements SessionProvider { return sonePlugin.core(); } - /** - * Returns the template context factory of the web interface. - * - * @return The template context factory - */ - public TemplateContextFactory getTemplateContextFactory() { - return templateContextFactory; - } - @Nullable @Override public Sone getCurrentSone(@Nonnull ToadletContext toadletContext) { @@ -308,12 +299,12 @@ public class WebInterface implements SessionProvider { pageToadletRegistry.addPage(new GetImagePage(this)); pageToadletRegistry.addPage(new GetTranslationAjaxPage(this)); pageToadletRegistry.addPage(new GetStatusAjaxPage(this, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault())); - pageToadletRegistry.addPage(new GetNotificationsAjaxPage(this)); + pageToadletRegistry.addPage(new GetNotificationsAjaxPage(this, templateContextFactory)); pageToadletRegistry.addPage(new DismissNotificationAjaxPage(this)); pageToadletRegistry.addPage(new CreatePostAjaxPage(this)); pageToadletRegistry.addPage(new CreateReplyAjaxPage(this)); - pageToadletRegistry.addPage(new GetReplyAjaxPage(this, replyTemplate)); - pageToadletRegistry.addPage(new GetPostAjaxPage(this, postTemplate)); + pageToadletRegistry.addPage(new GetReplyAjaxPage(this, templateContextFactory, replyTemplate)); + pageToadletRegistry.addPage(new GetPostAjaxPage(this, templateContextFactory, postTemplate)); pageToadletRegistry.addPage(new GetLinkedElementAjaxPage(this, elementLoader, linkedElementRenderFilter)); pageToadletRegistry.addPage(new GetTimesAjaxPage(this, timeTextConverter, l10nFilter, TimeZone.getDefault())); pageToadletRegistry.addPage(new MarkAsKnownAjaxPage(this)); diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt index c60fe56..adf3e26 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt @@ -8,6 +8,7 @@ import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.notify.TemplateNotification +import net.pterodactylus.util.template.TemplateContextFactory import java.io.StringWriter import javax.inject.Inject @@ -15,7 +16,7 @@ import javax.inject.Inject * AJAX handler to return all current notifications. */ @ToadletPath("getNotifications.ajax") -class GetNotificationsAjaxPage @Inject constructor(webInterface: WebInterface) : JsonPage(webInterface) { +class GetNotificationsAjaxPage @Inject constructor(webInterface: WebInterface, private val templateContextFactory: TemplateContextFactory) : JsonPage(webInterface) { override val needsFormPassword = false override val requiresLogin = false @@ -46,7 +47,7 @@ class GetNotificationsAjaxPage @Inject constructor(webInterface: WebInterface) : ) private fun TemplateNotification.render(currentSone: Sone?, freenetRequest: FreenetRequest) = StringWriter().use { - val mergedTemplateContext = webInterface.templateContextFactory.createTemplateContext() + val mergedTemplateContext = templateContextFactory.createTemplateContext() .mergeContext(templateContext) .apply { this["core"] = core diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt index 1aedd49..d5595d2 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt @@ -8,13 +8,14 @@ import net.pterodactylus.sone.utils.render import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContextFactory import javax.inject.Inject /** * This AJAX handler retrieves information and rendered representation of a [Post]. */ @ToadletPath("getPost.ajax") -class GetPostAjaxPage @Inject constructor(webInterface: WebInterface, private val postTemplate: Template) : LoggedInJsonPage(webInterface) { +class GetPostAjaxPage @Inject constructor(webInterface: WebInterface, private val templateContextFactory: TemplateContextFactory, private val postTemplate: Template) : LoggedInJsonPage(webInterface) { override val needsFormPassword = false @@ -33,7 +34,7 @@ class GetPostAjaxPage @Inject constructor(webInterface: WebInterface, private va } ?: createErrorJsonObject("invalid-post-id") private fun Post.render(currentSone: Sone, request: FreenetRequest) = - webInterface.templateContextFactory.createTemplateContext().apply { + templateContextFactory.createTemplateContext().apply { set("core", core) set("request", request) set("post", this@render) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt index ec5a5ac..43960ab 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt @@ -8,13 +8,14 @@ import net.pterodactylus.sone.utils.render import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContextFactory import javax.inject.Inject /** * This AJAX page returns the details of a reply. */ @ToadletPath("getReply.ajax") -class GetReplyAjaxPage @Inject constructor(webInterface: WebInterface, private val template: Template) : LoggedInJsonPage(webInterface) { +class GetReplyAjaxPage @Inject constructor(webInterface: WebInterface, val templateContextFactory: TemplateContextFactory, private val template: Template) : LoggedInJsonPage(webInterface) { override val needsFormPassword = false @@ -37,7 +38,7 @@ class GetReplyAjaxPage @Inject constructor(webInterface: WebInterface, private v ).toList().toTypedArray()) private fun PostReply.render(currentSone: Sone, request: FreenetRequest) = - webInterface.templateContextFactory.createTemplateContext().apply { + templateContextFactory.createTemplateContext().apply { set("core", core) set("request", request) set("reply", this@render) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt index 8f67ecf..c9afbf0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt @@ -25,7 +25,9 @@ import java.io.Writer /** * Unit test for [GetNotificationsAjaxPage]. */ -class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false, pageSupplier = ::GetNotificationsAjaxPage) { +class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requiresLogin = false, needsFormPassword = false) { + + override val page: JsonPage by lazy { GetNotificationsAjaxPage(webInterface, TemplateContextFactory()) } private val testNotifications = listOf( createNotification("n1", 2000, "t1", 5000, true), @@ -89,7 +91,6 @@ class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requi @Test fun `template notifications are rendered correctly`() { - whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory()) whenever(updateChecker.hasLatestVersion()).thenReturn(true) whenever(updateChecker.latestEdition).thenReturn(999) whenever(updateChecker.latestVersion).thenReturn(Version(0, 1, 2)) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt index 130bdc2..9597b37 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt @@ -9,6 +9,7 @@ import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.utils.asTemplate import net.pterodactylus.sone.web.baseInjector import net.pterodactylus.util.template.ReflectionAccessor +import net.pterodactylus.util.template.TemplateContextFactory import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue @@ -17,10 +18,12 @@ import org.junit.Test /** * Unit test for [GetPostAjaxPage]. */ -class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false, - pageSupplier = { webInterface -> - GetPostAjaxPage(webInterface, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate()) - }) { +class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false) { + + private val templateContextFactory = TemplateContextFactory().apply { + addAccessor(Any::class.java, ReflectionAccessor()) + } + override val page: JsonPage by lazy { GetPostAjaxPage(webInterface, templateContextFactory, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate()) } @Test fun `request with missing post results in invalid-post-id`() { @@ -37,7 +40,6 @@ class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = fal whenever(recipientId).thenReturn("recipient-id".asOptional()) whenever(text).thenReturn("post text") } - webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor()) addPost(post) addRequestParameter("post", "post-id") assertThatJsonIsSuccessful() diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt index 6010e9f..c59e65c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt @@ -8,6 +8,7 @@ import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.utils.asTemplate import net.pterodactylus.sone.web.baseInjector import net.pterodactylus.util.template.ReflectionAccessor +import net.pterodactylus.util.template.TemplateContextFactory import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue @@ -16,10 +17,12 @@ import org.junit.Test /** * Unit test for [GetReplyAjaxPage]. */ -class GetReplyAjaxPageTest : JsonPageTest("getReply.ajax", needsFormPassword = false, - pageSupplier = { webInterface -> - GetReplyAjaxPage(webInterface, "<%core>\n<%request>\n<%reply.text>\n<%currentSone>".asTemplate()) - }) { +class GetReplyAjaxPageTest : JsonPageTest("getReply.ajax", needsFormPassword = false) { + + private val templateContextFactory = TemplateContextFactory().apply { + addAccessor(Any::class.java, ReflectionAccessor()) + } + override val page: JsonPage by lazy { GetReplyAjaxPage(webInterface, templateContextFactory, "<%core>\n<%request>\n<%reply.text>\n<%currentSone>".asTemplate()) } @Test fun `request without reply id results in invalid-reply-id`() { @@ -36,7 +39,6 @@ class GetReplyAjaxPageTest : JsonPageTest("getReply.ajax", needsFormPassword = f whenever(time).thenReturn(1000) whenever(text).thenReturn("reply text") } - webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor()) addReply(reply) addRequestParameter("reply", "reply-id") assertThatJsonIsSuccessful() diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt index e8e660d..17fc9a9 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -81,7 +81,6 @@ open class TestObjects { } init { - whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory()) whenever(webInterface.getCurrentSone(ArgumentMatchers.eq(toadletContext))).thenReturn(currentSone) whenever(webInterface.core).thenReturn(core) whenever(webInterface.formPassword).then { formPassword } -- 2.7.4