🎨 Remove template context factory from web interface API
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Jul 2020 17:09:04 +0000 (19:09 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Jul 2020 17:09:04 +0000 (19:09 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt

index 8585650..27ef8d5 100644 (file)
@@ -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));
index c60fe56..adf3e26 100644 (file)
@@ -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
index 1aedd49..d5595d2 100644 (file)
@@ -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)
index ec5a5ac..43960ab 100644 (file)
@@ -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)
index 8f67ecf..c9afbf0 100644 (file)
@@ -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))
index 130bdc2..9597b37 100644 (file)
@@ -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()
index 6010e9f..c59e65c 100644 (file)
@@ -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()
index e8e660d..17fc9a9 100644 (file)
@@ -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 }