From b739f34bb13029472389137f4cb3632ddec6c57c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Jan 2017 22:38:55 +0100 Subject: [PATCH] Make about page injectable --- .../net/pterodactylus/sone/main/SonePlugin.java | 45 +++++++++++++ .../java/net/pterodactylus/sone/web/AboutPage.java | 50 --------------- .../net/pterodactylus/sone/web/WebInterface.java | 5 +- .../kotlin/net/pterodactylus/sone/web/AboutPage.kt | 27 ++++++++ .../net/pterodactylus/sone/web/AboutPageTest.java | 43 ------------- .../net/pterodactylus/sone/web/AboutPageTest.kt | 73 ++++++++++++++++++++++ 6 files changed, 149 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/AboutPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/AboutPage.kt delete mode 100644 src/test/java/net/pterodactylus/sone/web/AboutPageTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 61bd6ce..e76c6ef 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -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 index 87d9816..0000000 --- a/src/main/java/net/pterodactylus/sone/web/AboutPage.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 766098f..1ee7292 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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 index 0000000..6206d31 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/AboutPage.kt @@ -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 index df41e61..0000000 --- a/src/test/java/net/pterodactylus/sone/web/AboutPageTest.java +++ /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 David ‘Bombe’ Roden - */ -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 index 0000000..a0a9014 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt @@ -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(version)) + } + + @Test + fun `page sets correct homepage in template context`() { + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["homepage"], equalTo(homepage)) + } + + @Test + fun `page sets correct year in template context`() { + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["year"], equalTo(year)) + } + + @Test + fun `page can be created by guice`() { + assertThat(injector.getInstance(), notNullValue()) + } + + @Test + fun `page is created as singleton`() { + val firstInstance = injector.getInstance() + val secondInstance = injector.getInstance() + assertThat(firstInstance, sameInstance(secondInstance)) + } + +} -- 2.7.4