From 2e367a493f70dcd50da7de6af78126ea3c946582 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 12 Apr 2019 16:31:23 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Use=20new=20@MenuName=20annotation?= =?utf8?q?=20in=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/page/MenuName.kt | 3 +++ .../sone/web/page/PageToadletFactory.kt | 4 +++- .../sone/web/page/PageToadletFactoryTest.kt | 24 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/page/MenuName.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/MenuName.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/MenuName.kt new file mode 100644 index 0000000..6ee4ca6 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/MenuName.kt @@ -0,0 +1,3 @@ +package net.pterodactylus.sone.web.page + +annotation class MenuName(val value: String) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt index 48ea39f..bb9fd3d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt @@ -24,6 +24,8 @@ class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClien @JvmOverloads fun createPageToadlet(page: Page, menuName: String? = null) = - PageToadlet(highLevelSimpleClient, menuName, page, pathPrefix) + PageToadlet(highLevelSimpleClient, menuName ?: page.menuName, page, pathPrefix) } + +private val Page<*>.menuName get() = javaClass.getAnnotation(MenuName::class.java)?.value diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt index 93047cb..a5160db 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt @@ -4,6 +4,7 @@ import freenet.client.HighLevelSimpleClient import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import net.pterodactylus.util.web.Page +import net.pterodactylus.util.web.Response import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.nullValue @@ -39,4 +40,27 @@ class PageToadletFactoryTest { assertThat(pageToadlet.path(), equalTo("/some/prefix/path")) } + @Test + fun `menu name is added from annotation when no menu name is given`() { + val page = TestPageWithMenuName() + val pageToadlet = pageToadletFactory.createPageToadlet(page) + assertThat(pageToadlet.menuName, equalTo("testName")) + } + + @Test + fun `menu name from annotation is ignored when menu name is given`() { + val page = TestPageWithMenuName() + val pageToadlet = pageToadletFactory.createPageToadlet(page, "foo") + assertThat(pageToadlet.menuName, equalTo("foo")) + } + +} + +@MenuName("testName") +private class TestPageWithMenuName : Page { + + override fun getPath() = "" + override fun isPrefixPage() = false + override fun handleRequest(request: FreenetRequest, response: Response) = response + } -- 2.7.4