🔀 Merge changes from other next branch
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / main / SoneModuleTest.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.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.*
15 import org.junit.*
16 import java.io.*
17 import java.util.concurrent.atomic.*
18
19 class SoneModuleTest {
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         private val injector by lazy { createInjector(SoneModule(sonePlugin)) }
32
33         @After
34         fun removePropertiesFromCurrentDirectory() {
35                 File(currentDir, "sone.properties").delete()
36         }
37
38         @Test
39         fun `creator binds configuration when no file is present`() {
40                 File(currentDir, "sone.properties").delete()
41                 assertThat(injector.getInstance<Configuration>(), notNullValue())
42         }
43
44         @Test
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))
48         }
49
50         @Test
51         fun `config file is created in current directory if not present`() {
52                 File(currentDir, "sone.properties").delete()
53                 val configuration = injector.getInstance<Configuration>()
54                 configuration.save()
55                 assertThat(File(currentDir, "sone.properties").exists(), equalTo(true))
56         }
57
58         @Test
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"))
62         }
63
64         @Test
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))
68         }
69
70         @Test
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())
75         }
76
77         @Test
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))
81         }
82
83         @Test
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))
87         }
88
89         @Test
90         fun `event bus is bound`() {
91                 assertThat(injector.getInstance<EventBus>(), notNullValue())
92         }
93
94         @Test
95         fun `context is bound`() {
96                 assertThat(injector.getInstance<Context>().context, equalTo("Sone"))
97         }
98
99         @Test
100         fun `optional context is bound`() {
101                 assertThat(injector.getInstance<Optional<Context>>().get().context, equalTo("Sone"))
102         }
103
104         @Test
105         fun `sone plugin is bound`() {
106                 assertThat(injector.getInstance(), sameInstance(sonePlugin))
107         }
108
109         @Test
110         fun `version is bound`() {
111                 assertThat(injector.getInstance(), equalTo(pluginVersion))
112         }
113
114         @Test
115         fun `plugin version is bound`() {
116                 assertThat(injector.getInstance(), equalTo(PluginVersion(pluginVersion.toString())))
117         }
118
119         @Test
120         fun `plugin year is bound`() {
121                 assertThat(injector.getInstance(), equalTo(PluginYear(pluginYear)))
122         }
123
124         @Test
125         fun `plugin homepage in bound`() {
126                 assertThat(injector.getInstance(), equalTo(PluginHomepage(pluginHomepage)))
127         }
128
129         @Test
130         fun `database is bound correctly`() {
131                 assertThat(injector.getInstance<Database>(), instanceOf(MemoryDatabase::class.java))
132         }
133
134         @Test
135         fun `default loader is used without dev options`() {
136                 assertThat(injector.getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
137         }
138
139         @Test
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))
143         }
144
145         @Test
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))
149         }
150
151         class TestObject {
152                 val ref: AtomicReference<Any?> = AtomicReference()
153                 @Subscribe
154                 fun testEvent(event: Any?) {
155                         ref.set(event)
156                 }
157         }
158
159         @Test
160         fun `created objects are registered with event bus`() {
161                 val eventBus: EventBus = injector.getInstance()
162                 val testObject = injector.getInstance<TestObject>()
163                 val event = Any()
164                 eventBus.post(event)
165                 assertThat(testObject.ref.get(), sameInstance(event))
166         }
167
168 }