🎨 Fix imports
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / main / SoneModuleCreatorTest.kt
1 package net.pterodactylus.sone.main
2
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.*
15 import org.junit.*
16 import java.io.*
17 import java.util.concurrent.atomic.*
18
19 class SoneModuleCreatorTest {
20
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)
29         }
30
31         @After
32         fun removePropertiesFromCurrentDirectory() {
33                 File(currentDir, "sone.properties").delete()
34         }
35
36         @Test
37         fun `creator binds configuration when no file is present`() {
38                 File(currentDir, "sone.properties").delete()
39                 assertThat(getInstance<Configuration>(), notNullValue())
40         }
41
42         @Test
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))
46         }
47
48         @Test
49         fun `config file is created in current directory if not present`() {
50                 File(currentDir, "sone.properties").delete()
51                 val configuration = getInstance<Configuration>()
52                 configuration.save()
53                 assertThat(File(currentDir, "sone.properties").exists(), equalTo(true))
54         }
55
56         @Test
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"))
60         }
61
62         @Test
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))
66         }
67
68         @Test
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())
73         }
74
75         @Test
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))
79         }
80
81         @Test
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))
85         }
86
87         @Test
88         fun `event bus is bound`() {
89                 assertThat(getInstance<EventBus>(), notNullValue())
90         }
91
92         @Test
93         fun `context is bound`() {
94                 assertThat(getInstance<Context>().context, equalTo("Sone"))
95         }
96
97         @Test
98         fun `optional context is bound`() {
99                 assertThat(getInstance<Optional<Context>>().get().context, equalTo("Sone"))
100         }
101
102         @Test
103         fun `sone plugin is bound`() {
104                 assertThat(getInstance(), sameInstance(sonePlugin))
105         }
106
107         @Test
108         fun `version is bound`() {
109                 assertThat(getInstance(), equalTo(pluginVersion))
110         }
111
112         @Test
113         fun `plugin version is bound`() {
114                 assertThat(getInstance(), equalTo(PluginVersion(pluginVersion.toString())))
115         }
116
117         @Test
118         fun `plugin year is bound`() {
119                 assertThat(getInstance(), equalTo(PluginYear(pluginYear)))
120         }
121
122         @Test
123         fun `plugin homepage in bound`() {
124                 assertThat(getInstance(), equalTo(PluginHomepage(pluginHomepage)))
125         }
126
127         @Test
128         fun `database is bound correctly`() {
129                 assertThat(getInstance<Database>(), instanceOf(MemoryDatabase::class.java))
130         }
131
132         @Test
133         fun `default loader is used without dev options`() {
134                 assertThat(getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
135         }
136
137         @Test
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))
141         }
142
143         @Test
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))
147         }
148
149         class TestObject {
150                 val ref: AtomicReference<Any?> = AtomicReference()
151                 @Subscribe
152                 fun testEvent(event: Any?) {
153                         ref.set(event)
154                 }
155         }
156
157         @Test
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)
162                 val event = Any()
163                 eventBus.post(event)
164                 assertThat(testObject.ref.get(), sameInstance(event))
165         }
166
167         private fun createInjector(): Injector = SoneModuleCreator()
168                         .createModule(sonePlugin)
169                         .let { Guice.createInjector(it) }
170
171         private inline fun <reified R : Any> getInstance(annotation: Annotation? = null, injector: Injector = createInjector()): R =
172                         annotation
173                                         ?.let { injector.getInstance(Key.get(object : TypeLiteral<R>() {}, it)) }
174                                         ?: injector.getInstance(Key.get(object : TypeLiteral<R>() {}))
175
176 }