✨ Register debug pages that can be added later
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jul 2019 14:19:36 +0000 (16:19 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jul 2019 19:03:48 +0000 (21:03 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/PageToadletRegistry.kt
src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt

index ee1bd1e..d7dd16c 100644 (file)
@@ -616,9 +616,7 @@ public class WebInterface implements SessionProvider {
                pageToadletRegistry.addPage(new EmptyImageTitlePage(this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new EmptyAlbumTitlePage(this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new DismissNotificationPage(this, loaders, templateRenderer));
-               if (getCore().getDebug()) {
-                       pageToadletRegistry.addPage(new MetricsPage(this, loaders, templateRenderer, metricRegistry));
-               }
+               pageToadletRegistry.addDebugPage(new MetricsPage(this, loaders, templateRenderer, metricRegistry));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png"));
index 97e6cfe..70105bc 100644 (file)
@@ -18,6 +18,7 @@ class PageToadletRegistry @Inject constructor(
 ) {
 
        private val pages = mutableListOf<Page<FreenetRequest>>()
+       private val debugPages = mutableListOf<Page<FreenetRequest>>()
        private val registeredToadlets = mutableListOf<PageToadlet>()
        private val registered = AtomicBoolean(false)
 
@@ -26,13 +27,18 @@ class PageToadletRegistry @Inject constructor(
                pages += page
        }
 
+       fun addDebugPage(page: Page<FreenetRequest>) {
+               if (registered.get()) throw IllegalStateException()
+               debugPages += page
+       }
+
        fun registerToadlets() {
                registered.set(true)
                pageMaker.addNavigationCategory("/Sone/index.html", soneMenuName, "$soneMenu.Tooltip", sonePlugin)
                addPages()
        }
 
-       private fun addPages() =
+       private fun addPages(pages: List<Page<FreenetRequest>> = this.pages) =
                        pages
                                        .map { pageToadletFactory.createPageToadlet(it) }
                                        .onEach(registeredToadlets::plusAssign)
@@ -55,4 +61,7 @@ class PageToadletRegistry @Inject constructor(
                registeredToadlets.forEach(toadletContainer::unregister)
        }
 
+       fun activateDebugMode() =
+                       addPages(debugPages)
+
 }
index d242b32..5a19c7f 100644 (file)
@@ -9,6 +9,7 @@ import net.pterodactylus.util.web.*
 import org.junit.*
 import org.junit.rules.*
 import org.mockito.Mockito.*
+import kotlin.test.Test
 
 class PageToadletRegistryTest {
 
@@ -80,6 +81,55 @@ class PageToadletRegistryTest {
                verify(toadletContainer).unregister(toadletWithMenuname)
        }
 
+       @Test
+       fun `adding a debug page will not add it to the container`() {
+               val toadlet = createPageToadlet()
+               whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
+               pageToadletRegistry.addDebugPage(page)
+               pageToadletRegistry.registerToadlets()
+               verify(toadletContainer, never()).register(toadlet, null, "/Sone/", true, false)
+       }
+
+       @Test
+       fun `adding a debug page and activating debug mode will add it to the container`() {
+               val toadlet = createPageToadlet()
+               whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
+               pageToadletRegistry.addDebugPage(page)
+               pageToadletRegistry.registerToadlets()
+               pageToadletRegistry.activateDebugMode()
+               verify(toadletContainer).register(toadlet, null, "/Sone/", true, false)
+       }
+
+       @Test
+       fun `debug pages are ungegistered from the container`() {
+               val toadlet = createPageToadlet()
+               whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
+               pageToadletRegistry.addDebugPage(page)
+               pageToadletRegistry.registerToadlets()
+               pageToadletRegistry.activateDebugMode()
+               pageToadletRegistry.unregisterToadlets()
+               verify(toadletContainer).unregister(toadlet)
+       }
+
+       @Test
+       fun `inactive debug pages are not ungegistered from the container`() {
+               val toadlet = createPageToadlet()
+               whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
+               pageToadletRegistry.addDebugPage(page)
+               pageToadletRegistry.registerToadlets()
+               pageToadletRegistry.unregisterToadlets()
+               verify(toadletContainer, never()).unregister(toadlet)
+       }
+
+       @Test
+       fun `debug page can not be added after registering`() {
+               val toadlet = createPageToadlet()
+               whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
+               pageToadletRegistry.registerToadlets()
+               expectedException.expect(IllegalStateException::class.java)
+               pageToadletRegistry.addDebugPage(page)
+       }
+
        private fun createPageToadlet(menuName: String? = null) =
                        mock<PageToadlet>().apply {
                                whenever(this.path()).thenReturn("/Sone/")