1 package net.pterodactylus.sone.main
3 import com.google.common.base.*
4 import com.google.common.eventbus.*
5 import com.google.inject.*
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 SoneModuleCreatorTest {
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)
32 fun removePropertiesFromCurrentDirectory() {
33 File(currentDir, "sone.properties").delete()
37 fun `creator binds configuration when no file is present`() {
38 File(currentDir, "sone.properties").delete()
39 assertThat(getInstance<Configuration>(), notNullValue())
43 fun `creator binds first start to true when no file is present`() {
44 File(currentDir, "sone.properties").delete()
45 assertThat(getInstance(named("FirstStart")), equalTo(true))
49 fun `config file is created in current directory if not present`() {
50 File(currentDir, "sone.properties").delete()
51 val configuration = getInstance<Configuration>()
53 assertThat(File(currentDir, "sone.properties").exists(), equalTo(true))
57 fun `creator binds configuration when file is present`() {
58 File(currentDir, "sone.properties").writeText("Option=old")
59 assertThat(getInstance<Configuration>().getStringValue("Option").value, equalTo("old"))
63 fun `creator binds first start to false when file is present`() {
64 File(currentDir, "sone.properties").writeText("Option=old")
65 assertThat(getInstance(named("FirstStart")), equalTo(false))
69 fun `invalid config file leads to new config being created`() {
70 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
71 val configuration = getInstance<Configuration>()
72 assertThat(configuration.getStringValue("Option").getValue(null), nullValue())
76 fun `invalid config file leads to new config being set to true`() {
77 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
78 assertThat(getInstance(named("NewConfig")), equalTo(true))
82 fun `valid config file leads to new config being set to false`() {
83 File(currentDir, "sone.properties").writeText("Option=old")
84 assertThat(getInstance(named("NewConfig")), equalTo(false))
88 fun `event bus is bound`() {
89 assertThat(getInstance<EventBus>(), notNullValue())
93 fun `context is bound`() {
94 assertThat(getInstance<Context>().context, equalTo("Sone"))
98 fun `optional context is bound`() {
99 assertThat(getInstance<Optional<Context>>().get().context, equalTo("Sone"))
103 fun `sone plugin is bound`() {
104 assertThat(getInstance(), sameInstance(sonePlugin))
108 fun `version is bound`() {
109 assertThat(getInstance(), equalTo(pluginVersion))
113 fun `plugin version is bound`() {
114 assertThat(getInstance(), equalTo(PluginVersion(pluginVersion.toString())))
118 fun `plugin year is bound`() {
119 assertThat(getInstance(), equalTo(PluginYear(pluginYear)))
123 fun `plugin homepage in bound`() {
124 assertThat(getInstance(), equalTo(PluginHomepage(pluginHomepage)))
128 fun `database is bound correctly`() {
129 assertThat(getInstance<Database>(), instanceOf(MemoryDatabase::class.java))
133 fun `default loader is used without dev options`() {
134 assertThat(getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
138 fun `default loaders are used if no path is given`() {
139 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true")
140 assertThat(getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
144 fun `debug loaders are used if path is given`() {
145 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true\nDeveloper.FilesystemPath=/tmp")
146 assertThat(getInstance<Loaders>(), instanceOf(DebugLoaders::class.java))
150 val ref: AtomicReference<Any?> = AtomicReference()
152 fun testEvent(event: Any?) {
158 fun `created objects are registered with event bus`() {
159 val injector = createInjector()
160 val eventBus: EventBus = getInstance(injector = injector)
161 val testObject = getInstance<TestObject>(injector = injector)
164 assertThat(testObject.ref.get(), sameInstance(event))
167 private fun createInjector(): Injector = SoneModuleCreator()
168 .createModule(sonePlugin)
169 .let { Guice.createInjector(it) }
171 private inline fun <reified R : Any> getInstance(annotation: Annotation? = null, injector: Injector = createInjector()): R =
173 ?.let { injector.getInstance(Key.get(object : TypeLiteral<R>() {}, it)) }
174 ?: injector.getInstance(Key.get(object : TypeLiteral<R>() {}))