From b447a1e0b8ce1fb977faf3fc5a7f4c24ac4639ff Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 1 Jul 2019 16:33:31 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Improve=20getInstance()=20m?= =?utf8?q?ethod,=20use=20it=20in=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/main/SoneModuleTest.kt | 57 ++++++++++------------ .../kotlin/net/pterodactylus/sone/test/Guice.kt | 9 ++-- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt index 77633bd..83d3535 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt @@ -2,7 +2,7 @@ package net.pterodactylus.sone.main import com.google.common.base.* import com.google.common.eventbus.* -import com.google.inject.* +import com.google.inject.Guice.* import com.google.inject.name.Names.* import net.pterodactylus.sone.database.* import net.pterodactylus.sone.database.memory.* @@ -28,6 +28,8 @@ class SoneModuleTest { whenever(homepage).thenReturn(pluginHomepage) } + private val injector by lazy { createInjector(SoneModule(sonePlugin)) } + @After fun removePropertiesFromCurrentDirectory() { File(currentDir, "sone.properties").delete() @@ -36,19 +38,19 @@ class SoneModuleTest { @Test fun `creator binds configuration when no file is present`() { File(currentDir, "sone.properties").delete() - assertThat(getInstance(), notNullValue()) + assertThat(injector.getInstance(), notNullValue()) } @Test fun `creator binds first start to true when no file is present`() { File(currentDir, "sone.properties").delete() - assertThat(getInstance(named("FirstStart")), equalTo(true)) + assertThat(injector.getInstance(named("FirstStart")), equalTo(true)) } @Test fun `config file is created in current directory if not present`() { File(currentDir, "sone.properties").delete() - val configuration = getInstance() + val configuration = injector.getInstance() configuration.save() assertThat(File(currentDir, "sone.properties").exists(), equalTo(true)) } @@ -56,94 +58,94 @@ class SoneModuleTest { @Test fun `creator binds configuration when file is present`() { File(currentDir, "sone.properties").writeText("Option=old") - assertThat(getInstance().getStringValue("Option").value, equalTo("old")) + assertThat(injector.getInstance().getStringValue("Option").value, equalTo("old")) } @Test fun `creator binds first start to false when file is present`() { File(currentDir, "sone.properties").writeText("Option=old") - assertThat(getInstance(named("FirstStart")), equalTo(false)) + assertThat(injector.getInstance(named("FirstStart")), equalTo(false)) } @Test fun `invalid config file leads to new config being created`() { File(currentDir, "sone.properties").writeText("Option=old\nbroken") - val configuration = getInstance() + val configuration = injector.getInstance() assertThat(configuration.getStringValue("Option").getValue(null), nullValue()) } @Test fun `invalid config file leads to new config being set to true`() { File(currentDir, "sone.properties").writeText("Option=old\nbroken") - assertThat(getInstance(named("NewConfig")), equalTo(true)) + assertThat(injector.getInstance(named("NewConfig")), equalTo(true)) } @Test fun `valid config file leads to new config being set to false`() { File(currentDir, "sone.properties").writeText("Option=old") - assertThat(getInstance(named("NewConfig")), equalTo(false)) + assertThat(injector.getInstance(named("NewConfig")), equalTo(false)) } @Test fun `event bus is bound`() { - assertThat(getInstance(), notNullValue()) + assertThat(injector.getInstance(), notNullValue()) } @Test fun `context is bound`() { - assertThat(getInstance().context, equalTo("Sone")) + assertThat(injector.getInstance().context, equalTo("Sone")) } @Test fun `optional context is bound`() { - assertThat(getInstance>().get().context, equalTo("Sone")) + assertThat(injector.getInstance>().get().context, equalTo("Sone")) } @Test fun `sone plugin is bound`() { - assertThat(getInstance(), sameInstance(sonePlugin)) + assertThat(injector.getInstance(), sameInstance(sonePlugin)) } @Test fun `version is bound`() { - assertThat(getInstance(), equalTo(pluginVersion)) + assertThat(injector.getInstance(), equalTo(pluginVersion)) } @Test fun `plugin version is bound`() { - assertThat(getInstance(), equalTo(PluginVersion(pluginVersion.toString()))) + assertThat(injector.getInstance(), equalTo(PluginVersion(pluginVersion.toString()))) } @Test fun `plugin year is bound`() { - assertThat(getInstance(), equalTo(PluginYear(pluginYear))) + assertThat(injector.getInstance(), equalTo(PluginYear(pluginYear))) } @Test fun `plugin homepage in bound`() { - assertThat(getInstance(), equalTo(PluginHomepage(pluginHomepage))) + assertThat(injector.getInstance(), equalTo(PluginHomepage(pluginHomepage))) } @Test fun `database is bound correctly`() { - assertThat(getInstance(), instanceOf(MemoryDatabase::class.java)) + assertThat(injector.getInstance(), instanceOf(MemoryDatabase::class.java)) } @Test fun `default loader is used without dev options`() { - assertThat(getInstance(), instanceOf(DefaultLoaders::class.java)) + assertThat(injector.getInstance(), instanceOf(DefaultLoaders::class.java)) } @Test fun `default loaders are used if no path is given`() { File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true") - assertThat(getInstance(), instanceOf(DefaultLoaders::class.java)) + assertThat(injector.getInstance(), instanceOf(DefaultLoaders::class.java)) } @Test fun `debug loaders are used if path is given`() { File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true\nDeveloper.FilesystemPath=/tmp") - assertThat(getInstance(), instanceOf(DebugLoaders::class.java)) + assertThat(injector.getInstance(), instanceOf(DebugLoaders::class.java)) } class TestObject { @@ -156,20 +158,11 @@ class SoneModuleTest { @Test fun `created objects are registered with event bus`() { - val injector = createInjector() - val eventBus: EventBus = getInstance(injector = injector) - val testObject = getInstance(injector = injector) + val eventBus: EventBus = injector.getInstance() + val testObject = injector.getInstance() val event = Any() eventBus.post(event) assertThat(testObject.ref.get(), sameInstance(event)) } - private fun createInjector(): Injector = SoneModule(sonePlugin) - .let { Guice.createInjector(it) } - - private inline fun getInstance(annotation: Annotation? = null, injector: Injector = createInjector()): R = - annotation - ?.let { injector.getInstance(Key.get(object : TypeLiteral() {}, it)) } - ?: injector.getInstance(Key.get(object : TypeLiteral() {})) - } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index 2fc4a96..c8f2417 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -1,11 +1,10 @@ package net.pterodactylus.sone.test -import com.google.inject.Injector -import com.google.inject.Module +import com.google.inject.* import com.google.inject.name.* import org.mockito.* import javax.inject.Provider -import kotlin.reflect.KClass +import kotlin.reflect.* 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 }) } @@ -14,7 +13,9 @@ fun KClass.isProvidedBy(provider: KClass>) = Module inline fun KClass.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider { mock() }) } inline fun KClass.isProvidedByDeepMock() = Module { it.bind(this.java).toProvider(Provider { deepMock() }) } -inline fun Injector.getInstance() = getInstance(T::class.java)!! +inline fun Injector.getInstance(annotation: Annotation? = null): T = annotation + ?.let { getInstance(Key.get(object : TypeLiteral() {}, it)) } + ?: getInstance(Key.get(object : TypeLiteral() {})) fun supply(javaClass: Class): Source = object : Source { override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) } -- 2.7.4