From c557e36a80799ed81831e246bcd6b0345a30259b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 1 Jul 2019 14:59:00 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=9A=20Rename=20=E2=80=9CSoneModuleCreat?= =?utf8?q?or=E2=80=9D=20to=20=E2=80=9CSoneModule=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/main/SoneModule.kt | 63 ++++++++ .../pterodactylus/sone/main/SoneModuleCreator.kt | 65 -------- .../sone/main/SoneModuleCreatorTest.kt | 176 --------------------- .../net/pterodactylus/sone/main/SoneModuleTest.kt | 175 ++++++++++++++++++++ 4 files changed, 238 insertions(+), 241 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt delete mode 100644 src/main/kotlin/net/pterodactylus/sone/main/SoneModuleCreator.kt delete mode 100644 src/test/kotlin/net/pterodactylus/sone/main/SoneModuleCreatorTest.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt new file mode 100644 index 0000000..a3fe9be --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt @@ -0,0 +1,63 @@ +package net.pterodactylus.sone.main + +import com.google.common.base.* +import com.google.common.eventbus.* +import com.google.inject.* +import com.google.inject.matcher.* +import com.google.inject.name.Names.* +import com.google.inject.spi.* +import net.pterodactylus.sone.database.* +import net.pterodactylus.sone.database.memory.* +import net.pterodactylus.sone.freenet.wot.* +import net.pterodactylus.util.config.* +import net.pterodactylus.util.config.ConfigurationException +import net.pterodactylus.util.version.Version +import java.io.* + +class SoneModule(private val sonePlugin: SonePlugin) : AbstractModule() { + + override fun configure() { + val sonePropertiesFile = File("sone.properties") + val firstStart = !sonePropertiesFile.exists() + var newConfig = false + val configuration = try { + Configuration(MapConfigurationBackend(sonePropertiesFile, false)) + } catch (ce: ConfigurationException) { + sonePropertiesFile.delete() + newConfig = true + Configuration(MapConfigurationBackend(sonePropertiesFile, true)) + } + val context = Context("Sone") + val loaders = configuration.getStringValue("Developer.LoadFromFilesystem") + .getValue(null) + ?.let { + configuration.getStringValue("Developer.FilesystemPath") + .getValue(null) + ?.let { DebugLoaders(it) } + } + val eventBus = EventBus() + + bind(Configuration::class.java).toInstance(configuration) + bind(EventBus::class.java).toInstance(eventBus) + bind(Boolean::class.java).annotatedWith(named("FirstStart")).toInstance(firstStart) + bind(Boolean::class.java).annotatedWith(named("NewConfig")).toInstance(newConfig) + bind(Context::class.java).toInstance(context) + bind(object : TypeLiteral>() {}).toInstance(Optional.of(context)) + bind(SonePlugin::class.java).toInstance(sonePlugin) + bind(Version::class.java).toInstance(sonePlugin.version.parseVersion()) + bind(PluginVersion::class.java).toInstance(PluginVersion(sonePlugin.version)) + bind(PluginYear::class.java).toInstance(PluginYear(sonePlugin.year)) + bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage)) + bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java) + loaders?.let { bind(Loaders::class.java).toInstance(it) } + + bindListener(Matchers.any(), object : TypeListener { + override fun hear(typeLiteral: TypeLiteral, typeEncounter: TypeEncounter) { + typeEncounter.register(InjectionListener { injectee -> eventBus.register(injectee) }) + } + }) + } + +} + +private fun String.parseVersion(): Version = Version.parse(this) diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModuleCreator.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModuleCreator.kt deleted file mode 100644 index 58c8d9e..0000000 --- a/src/main/kotlin/net/pterodactylus/sone/main/SoneModuleCreator.kt +++ /dev/null @@ -1,65 +0,0 @@ -package net.pterodactylus.sone.main - -import com.google.common.base.* -import com.google.common.eventbus.* -import com.google.inject.* -import com.google.inject.matcher.* -import com.google.inject.name.Names.* -import com.google.inject.spi.* -import net.pterodactylus.sone.database.* -import net.pterodactylus.sone.database.memory.* -import net.pterodactylus.sone.freenet.wot.* -import net.pterodactylus.util.config.* -import net.pterodactylus.util.config.ConfigurationException -import net.pterodactylus.util.version.Version -import java.io.* - -class SoneModuleCreator { - - fun createModule(sonePlugin: SonePlugin) = object : AbstractModule() { - override fun configure() { - val sonePropertiesFile = File("sone.properties") - val firstStart = !sonePropertiesFile.exists() - var newConfig = false - val configuration = try { - Configuration(MapConfigurationBackend(sonePropertiesFile, false)) - } catch (ce: ConfigurationException) { - sonePropertiesFile.delete() - newConfig = true - Configuration(MapConfigurationBackend(sonePropertiesFile, true)) - } - val context = Context("Sone") - val loaders = configuration.getStringValue("Developer.LoadFromFilesystem") - .getValue(null) - ?.let { - configuration.getStringValue("Developer.FilesystemPath") - .getValue(null) - ?.let { DebugLoaders(it) } - } - val eventBus = EventBus() - - bind(Configuration::class.java).toInstance(configuration) - bind(EventBus::class.java).toInstance(eventBus) - bind(Boolean::class.java).annotatedWith(named("FirstStart")).toInstance(firstStart) - bind(Boolean::class.java).annotatedWith(named("NewConfig")).toInstance(newConfig) - bind(Context::class.java).toInstance(context) - bind(object : TypeLiteral>() {}).toInstance(Optional.of(context)) - bind(SonePlugin::class.java).toInstance(sonePlugin) - bind(Version::class.java).toInstance(sonePlugin.version.parseVersion()) - bind(PluginVersion::class.java).toInstance(PluginVersion(sonePlugin.version)) - bind(PluginYear::class.java).toInstance(PluginYear(sonePlugin.year)) - bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage)) - bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java) - loaders?.let { bind(Loaders::class.java).toInstance(it) } - - bindListener(Matchers.any(), object : TypeListener { - override fun hear(typeLiteral: TypeLiteral, typeEncounter: TypeEncounter) { - typeEncounter.register(InjectionListener { injectee -> eventBus.register(injectee) }) - } - }) - } - } - -} - -private fun String.parseVersion(): Version = Version.parse(this) diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleCreatorTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleCreatorTest.kt deleted file mode 100644 index 0286032..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleCreatorTest.kt +++ /dev/null @@ -1,176 +0,0 @@ -package net.pterodactylus.sone.main - -import com.google.common.base.* -import com.google.common.eventbus.* -import com.google.inject.* -import com.google.inject.name.Names.* -import net.pterodactylus.sone.database.* -import net.pterodactylus.sone.database.memory.* -import net.pterodactylus.sone.freenet.wot.* -import net.pterodactylus.sone.test.* -import net.pterodactylus.util.config.* -import net.pterodactylus.util.version.Version -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import org.junit.* -import java.io.* -import java.util.concurrent.atomic.* - -class SoneModuleCreatorTest { - - private val currentDir: File = File(".") - private val pluginVersion = Version("", 0, 1, 2) - private val pluginYear = 2019 - private val pluginHomepage = "home://page" - private val sonePlugin = mock().apply { - whenever(version).thenReturn(pluginVersion.toString()) - whenever(year).thenReturn(pluginYear) - whenever(homepage).thenReturn(pluginHomepage) - } - - @After - fun removePropertiesFromCurrentDirectory() { - File(currentDir, "sone.properties").delete() - } - - @Test - fun `creator binds configuration when no file is present`() { - File(currentDir, "sone.properties").delete() - assertThat(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)) - } - - @Test - fun `config file is created in current directory if not present`() { - File(currentDir, "sone.properties").delete() - val configuration = getInstance() - configuration.save() - assertThat(File(currentDir, "sone.properties").exists(), equalTo(true)) - } - - @Test - fun `creator binds configuration when file is present`() { - File(currentDir, "sone.properties").writeText("Option=old") - assertThat(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)) - } - - @Test - fun `invalid config file leads to new config being created`() { - File(currentDir, "sone.properties").writeText("Option=old\nbroken") - val configuration = 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)) - } - - @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)) - } - - @Test - fun `event bus is bound`() { - assertThat(getInstance(), notNullValue()) - } - - @Test - fun `context is bound`() { - assertThat(getInstance().context, equalTo("Sone")) - } - - @Test - fun `optional context is bound`() { - assertThat(getInstance>().get().context, equalTo("Sone")) - } - - @Test - fun `sone plugin is bound`() { - assertThat(getInstance(), sameInstance(sonePlugin)) - } - - @Test - fun `version is bound`() { - assertThat(getInstance(), equalTo(pluginVersion)) - } - - @Test - fun `plugin version is bound`() { - assertThat(getInstance(), equalTo(PluginVersion(pluginVersion.toString()))) - } - - @Test - fun `plugin year is bound`() { - assertThat(getInstance(), equalTo(PluginYear(pluginYear))) - } - - @Test - fun `plugin homepage in bound`() { - assertThat(getInstance(), equalTo(PluginHomepage(pluginHomepage))) - } - - @Test - fun `database is bound correctly`() { - assertThat(getInstance(), instanceOf(MemoryDatabase::class.java)) - } - - @Test - fun `default loader is used without dev options`() { - assertThat(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)) - } - - @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)) - } - - class TestObject { - val ref: AtomicReference = AtomicReference() - @Subscribe - fun testEvent(event: Any?) { - ref.set(event) - } - } - - @Test - fun `created objects are registered with event bus`() { - val injector = createInjector() - val eventBus: EventBus = getInstance(injector = injector) - val testObject = getInstance(injector = injector) - val event = Any() - eventBus.post(event) - assertThat(testObject.ref.get(), sameInstance(event)) - } - - private fun createInjector(): Injector = SoneModuleCreator() - .createModule(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/main/SoneModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt new file mode 100644 index 0000000..77633bd --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt @@ -0,0 +1,175 @@ +package net.pterodactylus.sone.main + +import com.google.common.base.* +import com.google.common.eventbus.* +import com.google.inject.* +import com.google.inject.name.Names.* +import net.pterodactylus.sone.database.* +import net.pterodactylus.sone.database.memory.* +import net.pterodactylus.sone.freenet.wot.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.config.* +import net.pterodactylus.util.version.Version +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import java.io.* +import java.util.concurrent.atomic.* + +class SoneModuleTest { + + private val currentDir: File = File(".") + private val pluginVersion = Version("", 0, 1, 2) + private val pluginYear = 2019 + private val pluginHomepage = "home://page" + private val sonePlugin = mock().apply { + whenever(version).thenReturn(pluginVersion.toString()) + whenever(year).thenReturn(pluginYear) + whenever(homepage).thenReturn(pluginHomepage) + } + + @After + fun removePropertiesFromCurrentDirectory() { + File(currentDir, "sone.properties").delete() + } + + @Test + fun `creator binds configuration when no file is present`() { + File(currentDir, "sone.properties").delete() + assertThat(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)) + } + + @Test + fun `config file is created in current directory if not present`() { + File(currentDir, "sone.properties").delete() + val configuration = getInstance() + configuration.save() + assertThat(File(currentDir, "sone.properties").exists(), equalTo(true)) + } + + @Test + fun `creator binds configuration when file is present`() { + File(currentDir, "sone.properties").writeText("Option=old") + assertThat(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)) + } + + @Test + fun `invalid config file leads to new config being created`() { + File(currentDir, "sone.properties").writeText("Option=old\nbroken") + val configuration = 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)) + } + + @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)) + } + + @Test + fun `event bus is bound`() { + assertThat(getInstance(), notNullValue()) + } + + @Test + fun `context is bound`() { + assertThat(getInstance().context, equalTo("Sone")) + } + + @Test + fun `optional context is bound`() { + assertThat(getInstance>().get().context, equalTo("Sone")) + } + + @Test + fun `sone plugin is bound`() { + assertThat(getInstance(), sameInstance(sonePlugin)) + } + + @Test + fun `version is bound`() { + assertThat(getInstance(), equalTo(pluginVersion)) + } + + @Test + fun `plugin version is bound`() { + assertThat(getInstance(), equalTo(PluginVersion(pluginVersion.toString()))) + } + + @Test + fun `plugin year is bound`() { + assertThat(getInstance(), equalTo(PluginYear(pluginYear))) + } + + @Test + fun `plugin homepage in bound`() { + assertThat(getInstance(), equalTo(PluginHomepage(pluginHomepage))) + } + + @Test + fun `database is bound correctly`() { + assertThat(getInstance(), instanceOf(MemoryDatabase::class.java)) + } + + @Test + fun `default loader is used without dev options`() { + assertThat(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)) + } + + @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)) + } + + class TestObject { + val ref: AtomicReference = AtomicReference() + @Subscribe + fun testEvent(event: Any?) { + ref.set(event) + } + } + + @Test + fun `created objects are registered with event bus`() { + val injector = createInjector() + val eventBus: EventBus = getInstance(injector = injector) + val testObject = getInstance(injector = injector) + 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() {})) + +} -- 2.7.4