Make about page injectable
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jan 2017 21:38:55 +0000 (22:38 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jan 2017 21:38:55 +0000 (22:38 +0100)
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/java/net/pterodactylus/sone/web/AboutPage.java [deleted file]
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/AboutPage.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/AboutPageTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt [new file with mode: 0644]

index 61bd6ce..e76c6ef 100644 (file)
@@ -259,6 +259,9 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                                bind(getOptionalContextTypeLiteral()).toInstance(of(context));
                                bind(SonePlugin.class).toInstance(SonePlugin.this);
                                bind(Version.class).toInstance(VERSION);
+                               bind(PluginVersion.class).toInstance(new PluginVersion(getVersion()));
+                               bind(PluginYear.class).toInstance(new PluginYear(getYear()));
+                               bind(PluginHomepage.class).toInstance(new PluginHomepage(getHomepage()));
                                if (startConfiguration.getBooleanValue("Developer.LoadFromFilesystem").getValue(false)) {
                                        String path = startConfiguration.getStringValue("Developer.FilesystemPath").getValue(null);
                                        if (path != null) {
@@ -412,4 +415,46 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
                return VERSION.toString();
        }
 
+       public static class PluginVersion {
+
+               private final String version;
+
+               public PluginVersion(String version) {
+                       this.version = version;
+               }
+
+               public String getVersion() {
+                       return version;
+               }
+
+       }
+
+       public static class PluginYear {
+
+               private final int year;
+
+               public PluginYear(int year) {
+                       this.year = year;
+               }
+
+               public int getYear() {
+                       return year;
+               }
+
+       }
+
+       public static class PluginHomepage {
+
+               private final String homepage;
+
+               public PluginHomepage(String homepage) {
+                       this.homepage = homepage;
+               }
+
+               public String getHomepage() {
+                       return homepage;
+               }
+
+       }
+
 }
diff --git a/src/main/java/net/pterodactylus/sone/web/AboutPage.java b/src/main/java/net/pterodactylus/sone/web/AboutPage.java
deleted file mode 100644 (file)
index 87d9816..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Sone - AboutPage.java - Copyright © 2010–2016 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web;
-
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.version.Version;
-
-/**
- * Shows some information about Sone.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class AboutPage extends SoneTemplatePage {
-
-       private final String version;
-       private final int year;
-       private final String homepage;
-
-       public AboutPage(Template template, WebInterface webInterface, String version, int year, String homepage) {
-               super("about.html", template, "Page.About.Title", webInterface, false);
-               this.version = version;
-               this.year = year;
-               this.homepage = homepage;
-       }
-
-       @Override
-       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               templateContext.set("version", version);
-               templateContext.set("year", year);
-               templateContext.set("homepage", homepage);
-       }
-
-}
index 766098f..1ee7292 100644 (file)
@@ -73,6 +73,9 @@ import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.main.Loaders;
 import net.pterodactylus.sone.main.ReparseFilter;
 import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.sone.main.SonePlugin.PluginHomepage;
+import net.pterodactylus.sone.main.SonePlugin.PluginVersion;
+import net.pterodactylus.sone.main.SonePlugin.PluginYear;
 import net.pterodactylus.sone.notify.ListNotification;
 import net.pterodactylus.sone.notify.ListNotificationFilter;
 import net.pterodactylus.sone.notify.PostVisibilityFilter;
@@ -732,7 +735,7 @@ public class WebInterface {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
-               pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.getPluginVersion(), SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, new PluginVersion(SonePlugin.getPluginVersion()), new PluginYear(SonePlugin.getYear()), new PluginHomepage(SonePlugin.getHomepage())), "About"));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/AboutPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/AboutPage.kt
new file mode 100644 (file)
index 0000000..6206d31
--- /dev/null
@@ -0,0 +1,27 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.main.SonePlugin.PluginHomepage
+import net.pterodactylus.sone.main.SonePlugin.PluginVersion
+import net.pterodactylus.sone.main.SonePlugin.PluginYear
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+import javax.inject.Inject
+import javax.inject.Singleton
+
+/**
+ * A [SoneTemplatePage] that stores information about Sone in the [TemplateContext].
+ */
+@Singleton
+class AboutPage @Inject constructor(template: Template, webInterface: WebInterface,
+                                    private val pluginVersion: PluginVersion,
+                                    private val pluginYear: PluginYear,
+                                    private val pluginHomepage: PluginHomepage) : SoneTemplatePage("about.html", template, "Page.About.Title", webInterface, false) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+               templateContext["version"] = pluginVersion.version
+               templateContext["year"] = pluginYear.year
+               templateContext["homepage"] = pluginHomepage.homepage
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/web/AboutPageTest.java b/src/test/java/net/pterodactylus/sone/web/AboutPageTest.java
deleted file mode 100644 (file)
index df41e61..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-package net.pterodactylus.sone.web;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
-import org.junit.Test;
-
-/**
- * Unit test for {@link AboutPage}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class AboutPageTest extends WebPageTest {
-
-       private final String version = "0.1.2";
-       private final int year = 1234;
-       private final String homepage = "home://page";
-       private final AboutPage page = new AboutPage(template, webInterface, version, year, homepage);
-
-       @Test
-       public void pageReturnsCorrectPath() {
-               assertThat(page.getPath(), is("about.html"));
-       }
-
-       @Test
-       public void pageSetsCorrectVersionInTemplateContext() throws Exception {
-               page.processTemplate(freenetRequest, templateContext);
-               assertThat(templateContext.get("version"), is((Object) version));
-       }
-
-       @Test
-       public void pageSetsCorrectHomepageInTemplateContext() throws Exception {
-               page.processTemplate(freenetRequest, templateContext);
-               assertThat(templateContext.get("homepage"), is((Object) homepage));
-       }
-
-       @Test
-       public void pageSetsCorrectYearInTemplateContext() throws Exception {
-               page.processTemplate(freenetRequest, templateContext);
-               assertThat(templateContext.get("year"), is((Object) year));
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt
new file mode 100644 (file)
index 0000000..a0a9014
--- /dev/null
@@ -0,0 +1,73 @@
+package net.pterodactylus.sone.web
+
+import com.google.inject.Guice
+import net.pterodactylus.sone.main.SonePlugin.PluginHomepage
+import net.pterodactylus.sone.main.SonePlugin.PluginVersion
+import net.pterodactylus.sone.main.SonePlugin.PluginYear
+import net.pterodactylus.sone.test.getInstance
+import net.pterodactylus.sone.test.isProvidedByMock
+import net.pterodactylus.util.template.Template
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.notNullValue
+import org.hamcrest.Matchers.sameInstance
+import org.junit.Test
+
+/**
+ * Unit test for [AboutPage].
+ */
+class AboutPageTest : WebPageTest() {
+
+       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))
+       private val injector = Guice.createInjector(
+                       Template::class.isProvidedByMock(),
+                       WebInterface::class.isProvidedByMock(),
+                       PluginVersion::class.isProvidedByMock(),
+                       PluginYear::class.isProvidedByMock(),
+                       PluginHomepage::class.isProvidedByMock()
+       )!!
+
+       @Test
+       fun `page returns correct path`() {
+               assertThat(page.path, equalTo("about.html"))
+       }
+
+       @Test
+       fun `page does not require login`() {
+               assertThat(page.requiresLogin(), equalTo(false))
+       }
+
+       @Test
+       fun `page sets correct version in template context`() {
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["version"], equalTo<Any>(version))
+       }
+
+       @Test
+       fun `page sets correct homepage in template context`() {
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["homepage"], equalTo<Any>(homepage))
+       }
+
+       @Test
+       fun `page sets correct year in template context`() {
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["year"], equalTo<Any>(year))
+       }
+
+       @Test
+       fun `page can be created by guice`() {
+               assertThat(injector.getInstance<AboutPage>(), notNullValue())
+       }
+
+       @Test
+       fun `page is created as singleton`() {
+           val firstInstance = injector.getInstance<AboutPage>()
+               val secondInstance = injector.getInstance<AboutPage>()
+               assertThat(firstInstance, sameInstance(secondInstance))
+       }
+
+}