1 package net.pterodactylus.sone.main
3 import com.google.common.base.*
4 import com.google.common.eventbus.*
5 import com.google.inject.Guice.*
6 import com.google.inject.name.Names.*
7 import net.pterodactylus.sone.database.*
8 import net.pterodactylus.sone.database.memory.*
9 import net.pterodactylus.sone.freenet.wot.*
10 import net.pterodactylus.sone.test.*
11 import net.pterodactylus.util.config.*
12 import net.pterodactylus.util.version.Version
13 import org.hamcrest.MatcherAssert.*
14 import org.hamcrest.Matchers.*
17 import java.util.concurrent.atomic.*
19 class SoneModuleTest {
21 private val currentDir: File = File(".")
22 private val pluginVersion = Version("", 0, 1, 2)
23 private val pluginYear = 2019
24 private val pluginHomepage = "home://page"
25 private val sonePlugin = mock<SonePlugin>().apply {
26 whenever(version).thenReturn(pluginVersion.toString())
27 whenever(year).thenReturn(pluginYear)
28 whenever(homepage).thenReturn(pluginHomepage)
31 private val injector by lazy { createInjector(SoneModule(sonePlugin)) }
34 fun removePropertiesFromCurrentDirectory() {
35 File(currentDir, "sone.properties").delete()
39 fun `creator binds configuration when no file is present`() {
40 File(currentDir, "sone.properties").delete()
41 assertThat(injector.getInstance<Configuration>(), notNullValue())
45 fun `creator binds first start to true when no file is present`() {
46 File(currentDir, "sone.properties").delete()
47 assertThat(injector.getInstance(named("FirstStart")), equalTo(true))
51 fun `config file is created in current directory if not present`() {
52 File(currentDir, "sone.properties").delete()
53 val configuration = injector.getInstance<Configuration>()
55 assertThat(File(currentDir, "sone.properties").exists(), equalTo(true))
59 fun `creator binds configuration when file is present`() {
60 File(currentDir, "sone.properties").writeText("Option=old")
61 assertThat(injector.getInstance<Configuration>().getStringValue("Option").value, equalTo("old"))
65 fun `creator binds first start to false when file is present`() {
66 File(currentDir, "sone.properties").writeText("Option=old")
67 assertThat(injector.getInstance(named("FirstStart")), equalTo(false))
71 fun `invalid config file leads to new config being created`() {
72 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
73 val configuration = injector.getInstance<Configuration>()
74 assertThat(configuration.getStringValue("Option").getValue(null), nullValue())
78 fun `invalid config file leads to new config being set to true`() {
79 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
80 assertThat(injector.getInstance(named("NewConfig")), equalTo(true))
84 fun `valid config file leads to new config being set to false`() {
85 File(currentDir, "sone.properties").writeText("Option=old")
86 assertThat(injector.getInstance(named("NewConfig")), equalTo(false))
90 fun `event bus is bound`() {
91 assertThat(injector.getInstance<EventBus>(), notNullValue())
95 fun `context is bound`() {
96 assertThat(injector.getInstance<Context>().context, equalTo("Sone"))
100 fun `optional context is bound`() {
101 assertThat(injector.getInstance<Optional<Context>>().get().context, equalTo("Sone"))
105 fun `sone plugin is bound`() {
106 assertThat(injector.getInstance(), sameInstance(sonePlugin))
110 fun `version is bound`() {
111 assertThat(injector.getInstance(), equalTo(pluginVersion))
115 fun `plugin version is bound`() {
116 assertThat(injector.getInstance(), equalTo(PluginVersion(pluginVersion.toString())))
120 fun `plugin year is bound`() {
121 assertThat(injector.getInstance(), equalTo(PluginYear(pluginYear)))
125 fun `plugin homepage in bound`() {
126 assertThat(injector.getInstance(), equalTo(PluginHomepage(pluginHomepage)))
130 fun `database is bound correctly`() {
131 assertThat(injector.getInstance<Database>(), instanceOf(MemoryDatabase::class.java))
135 fun `default loader is used without dev options`() {
136 assertThat(injector.getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
140 fun `default loaders are used if no path is given`() {
141 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true")
142 assertThat(injector.getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
146 fun `debug loaders are used if path is given`() {
147 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true\nDeveloper.FilesystemPath=/tmp")
148 assertThat(injector.getInstance<Loaders>(), instanceOf(DebugLoaders::class.java))
152 val ref: AtomicReference<Any?> = AtomicReference()
154 fun testEvent(event: Any?) {
160 fun `created objects are registered with event bus`() {
161 val eventBus: EventBus = injector.getInstance()
162 val testObject = injector.getInstance<TestObject>()
165 assertThat(testObject.ref.get(), sameInstance(event))