From: David ‘Bombe’ Roden Date: Fri, 12 Apr 2019 14:31:23 +0000 (+0200) Subject: ✨ Use new @MenuName annotation in factory X-Git-Tag: v79^2~64 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2e367a493f70dcd50da7de6af78126ea3c946582;p=Sone.git ✨ Use new @MenuName annotation in factory --- 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 + }