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() {
fun getImageLinkFilter(core: Core) =
ImageLinkFilter(core)
+ @Provides
+ @Named("toadletPathPrefix")
+ fun getPathPrefix(): String = "/Sone/"
+
}
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<FreenetRequest>, menuName: String? = null) =
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 <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider<T> { instance }) }
+fun <T : Any> KClass<T>.withNameIsProvidedBy(instance: T, name: String) = Module { it.bind(this.java).annotatedWith(Names.named(name)).toProvider(Provider<T> { instance }) }
fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider<T> { mock() }) }
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.*
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.*
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<TemplateContextFactory>().createTemplateContext()!! }
assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
}
+ @Test
+ fun `page toadlet factory is created with correct prefix`() {
+ val page = mock<Page<FreenetRequest>>()
+ assertThat(injector.getInstance<PageToadletFactory>().createPageToadlet(page).path(), startsWith("/Sone/"))
+ }
+
}
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.*
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<PageToadletFactory>(), notNullValue())
+ }
+
}
@MenuName("testName")