✨ Use new @MenuName annotation in factory
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Apr 2019 14:31:23 +0000 (16:31 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Apr 2019 14:31:23 +0000 (16:31 +0200)
src/main/kotlin/net/pterodactylus/sone/web/page/MenuName.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt
src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.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 (file)
index 0000000..6ee4ca6
--- /dev/null
@@ -0,0 +1,3 @@
+package net.pterodactylus.sone.web.page
+
+annotation class MenuName(val value: String)
index 48ea39f..bb9fd3d 100644 (file)
@@ -24,6 +24,8 @@ class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClien
 
        @JvmOverloads
        fun createPageToadlet(page: Page<FreenetRequest>, 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
index 93047cb..a5160db 100644 (file)
@@ -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<FreenetRequest> {
+
+       override fun getPath() = ""
+       override fun isPrefixPage() = false
+       override fun handleRequest(request: FreenetRequest, response: Response) = response
+
 }