♻️ Move generic error pages to custom classes
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 17 May 2019 21:16:23 +0000 (23:16 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 17 May 2019 21:16:23 +0000 (23:16 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/pages/ErrorPages.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/ErrorPagesTest.kt [new file with mode: 0644]

index efc4291..d247993 100644 (file)
@@ -132,10 +132,13 @@ import net.pterodactylus.sone.web.pages.EditAlbumPage;
 import net.pterodactylus.sone.web.pages.EditImagePage;
 import net.pterodactylus.sone.web.pages.EditProfileFieldPage;
 import net.pterodactylus.sone.web.pages.EditProfilePage;
+import net.pterodactylus.sone.web.pages.EmptyAlbumTitlePage;
+import net.pterodactylus.sone.web.pages.EmptyImageTitlePage;
 import net.pterodactylus.sone.web.pages.FollowSonePage;
 import net.pterodactylus.sone.web.pages.GetImagePage;
 import net.pterodactylus.sone.web.pages.ImageBrowserPage;
 import net.pterodactylus.sone.web.pages.IndexPage;
+import net.pterodactylus.sone.web.pages.InvalidPage;
 import net.pterodactylus.sone.web.pages.KnownSonesPage;
 import net.pterodactylus.sone.web.pages.LikePage;
 import net.pterodactylus.sone.web.pages.LockSonePage;
@@ -143,6 +146,7 @@ import net.pterodactylus.sone.web.pages.LoginPage;
 import net.pterodactylus.sone.web.pages.LogoutPage;
 import net.pterodactylus.sone.web.pages.MarkAsKnownPage;
 import net.pterodactylus.sone.web.pages.NewPage;
+import net.pterodactylus.sone.web.pages.NoPermissionPage;
 import net.pterodactylus.sone.web.pages.OptionsPage;
 import net.pterodactylus.sone.web.pages.RescuePage;
 import net.pterodactylus.sone.web.pages.SearchPage;
@@ -627,9 +631,6 @@ public class WebInterface implements SessionProvider {
                Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
                Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
                Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
-               Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
-               Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
-               Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
                Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
                Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
                Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
@@ -679,11 +680,11 @@ public class WebInterface implements SessionProvider {
                pageToadletRegistry.addPage(new OptionsPage(optionsTemplate, this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new RescuePage(rescueTemplate, this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new AboutPage(aboutTemplate, this, loaders, templateRenderer, new PluginVersion(SonePlugin.getPluginVersion()), new PluginYear(sonePlugin.getYear()), new PluginHomepage(sonePlugin.getHomepage())));
-               pageToadletRegistry.addPage(new SoneTemplatePage("noPermission.html", this, loaders, noPermissionTemplate, templateRenderer, "Page.NoPermission.Title"));
-               pageToadletRegistry.addPage(new SoneTemplatePage("emptyImageTitle.html", this, loaders, emptyImageTitleTemplate, templateRenderer, "Page.EmptyImageTitle.Title"));
-               pageToadletRegistry.addPage(new SoneTemplatePage("emptyAlbumTitle.html", this, loaders, emptyAlbumTitleTemplate, templateRenderer, "Page.EmptyAlbumTitle.Title"));
+               pageToadletRegistry.addPage(new InvalidPage(this, loaders, templateRenderer));
+               pageToadletRegistry.addPage(new NoPermissionPage(this, loaders, templateRenderer));
+               pageToadletRegistry.addPage(new EmptyImageTitlePage(this, loaders, templateRenderer));
+               pageToadletRegistry.addPage(new EmptyAlbumTitlePage(this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new DismissNotificationPage(emptyTemplate, this, loaders, templateRenderer));
-               pageToadletRegistry.addPage(new SoneTemplatePage("invalid.html", this, loaders, invalidTemplate, templateRenderer, "Page.Invalid.Title"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png"));
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ErrorPages.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ErrorPages.kt
new file mode 100644 (file)
index 0000000..91893d3
--- /dev/null
@@ -0,0 +1,18 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.main.*
+import net.pterodactylus.sone.web.*
+import net.pterodactylus.sone.web.page.*
+import net.pterodactylus.util.template.*
+
+@TemplatePath("/templates/invalid.html")
+class InvalidPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) : SoneTemplatePage("invalid.html", webInterface, loaders, Template(), templateRenderer, "Page.Invalid.Title")
+
+@TemplatePath("/templates/noPermission.html")
+class NoPermissionPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) : SoneTemplatePage("noPermission.html", webInterface, loaders, Template(), templateRenderer, "Page.NoPermission.Title")
+
+@TemplatePath("/templates/emptyImageTitle.html")
+class EmptyImageTitlePage(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) : SoneTemplatePage("emptyImageTitle.html", webInterface, loaders, Template(), templateRenderer, "Page.EmptyImageTitle.Title")
+
+@TemplatePath("/templates/emptyAlbumTitle.html")
+class EmptyAlbumTitlePage(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) : SoneTemplatePage("emptyAlbumTitle.html", webInterface, loaders, Template(), templateRenderer, "Page.EmptyAlbumTitle.Title")
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ErrorPagesTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ErrorPagesTest.kt
new file mode 100644 (file)
index 0000000..6f8bfa1
--- /dev/null
@@ -0,0 +1,103 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.main.*
+import net.pterodactylus.sone.web.*
+import net.pterodactylus.sone.web.page.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+
+class ErrorPagesTest {
+
+       private val webPageTest = WebPageTest()
+
+       private fun testPage(page: (WebInterface, Loaders, TemplateRenderer) -> FreenetTemplatePage, test: (FreenetTemplatePage) -> Unit) =
+                       with(webPageTest) {
+                               test(page(webInterface, loaders, templateRenderer))
+                       }
+
+       @Test
+       fun `invalid page returns correct path`() {
+               testPage(::InvalidPage) { page ->
+                       assertThat(page.path, equalTo("invalid.html"))
+               }
+       }
+
+       @Test
+       fun `invalid page returns correct title`() {
+               testPage(::InvalidPage) { page ->
+                       assertThat(page.getPageTitle(webPageTest.freenetRequest), equalTo("Page.Invalid.Title"))
+               }
+       }
+
+       @Test
+       fun `invalid page is annotated with correct template path`() {
+               testPage(::InvalidPage) { page ->
+                       assertThat(page.templatePath, equalTo("/templates/invalid.html"))
+               }
+       }
+
+       @Test
+       fun `no permission page returns correct path`() {
+               testPage(::NoPermissionPage) { page ->
+                       assertThat(page.path, equalTo("noPermission.html"))
+               }
+       }
+
+       @Test
+       fun `no permission page returns correct title`() {
+               testPage(::NoPermissionPage) { page ->
+                       assertThat(page.getPageTitle(webPageTest.freenetRequest), equalTo("Page.NoPermission.Title"))
+               }
+       }
+
+       @Test
+       fun `no permission page is annotated with correct template path`() {
+               testPage(::NoPermissionPage) { page ->
+                       assertThat(page.templatePath, equalTo("/templates/noPermission.html"))
+               }
+       }
+
+       @Test
+       fun `empty image title page returns correct path`() {
+               testPage(::EmptyImageTitlePage) { page ->
+                       assertThat(page.path, equalTo("emptyImageTitle.html"))
+               }
+       }
+
+       @Test
+       fun `empty image title page returns correct page title`() {
+               testPage(::EmptyImageTitlePage) { page ->
+                       assertThat(page.getPageTitle(webPageTest.freenetRequest), equalTo("Page.EmptyImageTitle.Title"))
+               }
+       }
+
+       @Test
+       fun `empty image title page is annotated with correct template path`() {
+               testPage(::EmptyImageTitlePage) { page ->
+                       assertThat(page.templatePath, equalTo("/templates/emptyImageTitle.html"))
+               }
+       }
+
+       @Test
+       fun `empty album title page returns correct path`() {
+               testPage(::EmptyAlbumTitlePage) { page ->
+                       assertThat(page.path, equalTo("emptyAlbumTitle.html"))
+               }
+       }
+
+       @Test
+       fun `empty album title page returns correct page title`() {
+               testPage(::EmptyAlbumTitlePage) { page ->
+                       assertThat(page.getPageTitle(webPageTest.freenetRequest), equalTo("Page.EmptyAlbumTitle.Title"))
+               }
+       }
+
+       @Test
+       fun `empty album title page is annotated with correct template path`() {
+               testPage(::EmptyAlbumTitlePage) { page ->
+                       assertThat(page.templatePath, equalTo("/templates/emptyAlbumTitle.html"))
+               }
+       }
+
+}