From c12bed3557f3f1d80c9694a778fcaf3429b04073 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 18 May 2019 01:51:07 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20path=20prefix=20for=20page?= =?utf8?q?=20toadlet=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterfaceModule.kt | 5 +++++ .../net/pterodactylus/sone/web/page/PageToadletFactory.kt | 6 +++++- src/test/kotlin/net/pterodactylus/sone/test/Guice.kt | 2 ++ .../net/pterodactylus/sone/web/WebInterfaceModuleTest.kt | 14 +++++++++++++- .../pterodactylus/sone/web/page/PageToadletFactoryTest.kt | 12 ++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt index 16f6e86..dd5e9f6 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt @@ -12,6 +12,7 @@ import net.pterodactylus.sone.main.* import net.pterodactylus.sone.template.* import net.pterodactylus.sone.text.* import net.pterodactylus.util.template.* +import javax.inject.* import javax.inject.Singleton class WebInterfaceModule : AbstractModule() { @@ -120,4 +121,8 @@ class WebInterfaceModule : AbstractModule() { fun getImageLinkFilter(core: Core) = ImageLinkFilter(core) + @Provides + @Named("toadletPathPrefix") + fun getPathPrefix(): String = "/Sone/" + } 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 3a01754..ace5f8a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt @@ -22,7 +22,11 @@ import freenet.clients.http.* import net.pterodactylus.util.web.* import javax.inject.* -class PageToadletFactory @Inject constructor(private val highLevelSimpleClient: HighLevelSimpleClient, private val sessionManager: SessionManager, private val pathPrefix: String) { +class PageToadletFactory @Inject constructor( + private val highLevelSimpleClient: HighLevelSimpleClient, + private val sessionManager: SessionManager, + @Named("toadletPathPrefix") private val pathPrefix: String +) { @JvmOverloads fun createPageToadlet(page: Page, menuName: String? = null) = diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index abf0476..2fc4a96 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -2,11 +2,13 @@ package net.pterodactylus.sone.test import com.google.inject.Injector import com.google.inject.Module +import com.google.inject.name.* import org.mockito.* import javax.inject.Provider import kotlin.reflect.KClass fun KClass.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider { instance }) } +fun KClass.withNameIsProvidedBy(instance: T, name: String) = Module { it.bind(this.java).annotatedWith(Names.named(name)).toProvider(Provider { instance }) } fun KClass.isProvidedBy(provider: com.google.inject.Provider) = Module { it.bind(this.java).toProvider(provider) } fun KClass.isProvidedBy(provider: KClass>) = Module { it.bind(this.java).toProvider(provider.java) } inline fun KClass.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider { mock() }) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt index fc8964a..9137da3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt @@ -1,6 +1,8 @@ package net.pterodactylus.sone.web import com.google.inject.Guice.* +import freenet.client.* +import freenet.clients.http.* import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.core.* @@ -12,7 +14,9 @@ import net.pterodactylus.sone.main.* import net.pterodactylus.sone.template.* import net.pterodactylus.sone.test.* import net.pterodactylus.sone.text.* +import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.template.* +import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* @@ -28,7 +32,9 @@ class WebInterfaceModuleTest { BaseL10n::class.isProvidedBy(l10n), SoneTextParser::class.isProvidedByMock(), ElementLoader::class.isProvidedByMock(), - Loaders::class.isProvidedBy(loaders) + Loaders::class.isProvidedBy(loaders), + HighLevelSimpleClient::class.isProvidedByMock(), + SessionManager::class.isProvidedByMock() ) private val injector = createInjector(webInterfaceModule, *additionalModules)!! private val templateContext by lazy { injector.getInstance().createTemplateContext()!! } @@ -257,4 +263,10 @@ class WebInterfaceModuleTest { assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template)) } + @Test + fun `page toadlet factory is created with correct prefix`() { + val page = mock>() + assertThat(injector.getInstance().createPageToadlet(page).path(), startsWith("/Sone/")) + } + } 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 c9ecafe..7ac8c01 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt @@ -1,8 +1,10 @@ package net.pterodactylus.sone.web.page +import com.google.inject.* import freenet.client.* import freenet.clients.http.* import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.web.* import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* @@ -53,6 +55,16 @@ class PageToadletFactoryTest { assertThat(pageToadlet.menuName, equalTo("foo")) } + @Test + fun `page toadlet factory can be created by guice`() { + val injector = Guice.createInjector( + HighLevelSimpleClient::class.isProvidedBy(highLevelSimpleClient), + SessionManager::class.isProvidedBy(sessionManager), + String::class.withNameIsProvidedBy("/Sone/", "toadletPathPrefix") + ) + assertThat(injector.getInstance(), notNullValue()) + } + } @MenuName("testName") -- 2.7.4