From: David ‘Bombe’ Roden Date: Mon, 2 Dec 2019 20:40:16 +0000 (+0100) Subject: 🚚 Move verification method to Guice helpers X-Git-Tag: v81^2~5^2~85 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a782ae867f056f7678ccc4665f3516901481aafc;p=Sone.git 🚚 Move verification method to Guice helpers --- diff --git a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt index e94e34e..c6aed88 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt @@ -34,12 +34,6 @@ class FreenetModuleTest { private val module = FreenetModule(pluginRespirator) private val injector = Guice.createInjector(module) - private inline fun verifySingletonInstance() { - val firstInstance = injector.getInstance() - val secondInstance = injector.getInstance() - assertThat(firstInstance, sameInstance(secondInstance)) - } - @Test fun `plugin respirator is not bound`() { expectedException.expect(Exception::class.java) @@ -53,7 +47,7 @@ class FreenetModuleTest { @Test fun `node is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() } @Test @@ -63,7 +57,7 @@ class FreenetModuleTest { @Test fun `high level simply client is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() } @Test @@ -73,7 +67,7 @@ class FreenetModuleTest { @Test fun `session manager is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() verify(pluginRespirator).getSessionManager("Sone") } @@ -84,7 +78,7 @@ class FreenetModuleTest { @Test fun `toadlet container is returned as singleten`() { - verifySingletonInstance() + injector.verifySingletonInstance() } @Test @@ -94,7 +88,7 @@ class FreenetModuleTest { @Test fun `page maker is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() } @Test @@ -106,7 +100,7 @@ class FreenetModuleTest { @Test fun `plugin respirator facade is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() } @Test @@ -116,7 +110,7 @@ class FreenetModuleTest { @Test fun `plugin connector facade is returned as singleton`() { - verifySingletonInstance() + injector.verifySingletonInstance() } } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index c8f2417..a4ae1d5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -2,6 +2,8 @@ package net.pterodactylus.sone.test import com.google.inject.* import com.google.inject.name.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* import org.mockito.* import javax.inject.Provider import kotlin.reflect.* @@ -17,6 +19,13 @@ inline fun Injector.getInstance(annotation: Annotation? = null ?.let { getInstance(Key.get(object : TypeLiteral() {}, it)) } ?: getInstance(Key.get(object : TypeLiteral() {})) + +inline fun Injector.verifySingletonInstance() { + val firstInstance = getInstance() + val secondInstance = getInstance() + assertThat(firstInstance, sameInstance(secondInstance)) +} + fun supply(javaClass: Class): Source = object : Source { override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) } override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider(Provider { instance }) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt index c03d9f8..53dc19d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/WebInterfaceModuleTest.kt @@ -256,9 +256,7 @@ class WebInterfaceModuleTest { @Test fun `template context factory is created as singleton`() { - val factory1 = injector.getInstance() - val factory2 = injector.getInstance() - assertThat(factory1, sameInstance(factory2)) + injector.verifySingletonInstance() } @Test @@ -285,9 +283,7 @@ class WebInterfaceModuleTest { @Test fun `notification manager is created as singleton`() { - val firstNotificationManager = injector.getInstance() - val secondNotificationManager = injector.getInstance() - assertThat(firstNotificationManager, sameInstance(secondNotificationManager)) + injector.verifySingletonInstance() } @Test @@ -297,9 +293,7 @@ class WebInterfaceModuleTest { @Test fun `notification handler is created as singleton`() { - val firstNotificationHandler = injector.getInstance() - val secondNotificationHandler = injector.getInstance() - assertThat(firstNotificationHandler, sameInstance(secondNotificationHandler)) + injector.verifySingletonInstance() } }