1 package net.pterodactylus.sone.main
3 import com.codahale.metrics.*
4 import com.google.common.base.*
5 import com.google.common.eventbus.*
6 import com.google.inject.Guice.*
7 import com.google.inject.name.Names.*
9 import freenet.pluginmanager.*
10 import net.pterodactylus.sone.core.*
11 import net.pterodactylus.sone.database.*
12 import net.pterodactylus.sone.database.memory.*
13 import net.pterodactylus.sone.freenet.wot.*
14 import net.pterodactylus.sone.test.*
15 import net.pterodactylus.util.config.*
16 import net.pterodactylus.util.version.Version
17 import org.hamcrest.MatcherAssert.*
18 import org.hamcrest.Matchers.*
19 import org.mockito.Mockito.*
21 import java.util.concurrent.atomic.*
24 const val versionString = "v80"
26 class SoneModuleTest {
28 private val currentDir: File = File(".")
29 private val pluginVersion = Version("", 80)
30 private val pluginYear = 2019
31 private val pluginHomepage = "home://page"
32 private val l10n = deepMock<PluginL10n>()
33 private val sonePlugin = mock<SonePlugin>().apply {
34 whenever(version).thenReturn(versionString)
35 whenever(year).thenReturn(pluginYear)
36 whenever(homepage).thenReturn(pluginHomepage)
37 whenever(l10n()).thenReturn(l10n)
40 private val injector by lazy {
42 SoneModule(sonePlugin, EventBus()),
43 FreenetInterface::class.isProvidedByDeepMock(),
44 PluginRespirator::class.isProvidedByDeepMock()
49 fun removePropertiesFromCurrentDirectory() {
50 File(currentDir, "sone.properties").delete()
54 fun `creator binds configuration when no file is present`() {
55 File(currentDir, "sone.properties").delete()
56 assertThat(injector.getInstance<Configuration>(), notNullValue())
60 fun `creator binds first start to true when no file is present`() {
61 File(currentDir, "sone.properties").delete()
62 assertThat(injector.getInstance(named("FirstStart")), equalTo(true))
66 fun `config file is created in current directory if not present`() {
67 File(currentDir, "sone.properties").delete()
68 val configuration = injector.getInstance<Configuration>()
70 assertThat(File(currentDir, "sone.properties").exists(), equalTo(true))
74 fun `creator binds configuration when file is present`() {
75 File(currentDir, "sone.properties").writeText("Option=old")
76 assertThat(injector.getInstance<Configuration>().getStringValue("Option").value, equalTo("old"))
80 fun `creator binds first start to false when file is present`() {
81 File(currentDir, "sone.properties").writeText("Option=old")
82 assertThat(injector.getInstance(named("FirstStart")), equalTo(false))
86 fun `invalid config file leads to new config being created`() {
87 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
88 val configuration = injector.getInstance<Configuration>()
89 assertThat(configuration.getStringValue("Option").getValue(null), nullValue())
93 fun `invalid config file leads to new config being set to true`() {
94 File(currentDir, "sone.properties").writeText("Option=old\nbroken")
95 assertThat(injector.getInstance(named("NewConfig")), equalTo(true))
99 fun `valid config file leads to new config being set to false`() {
100 File(currentDir, "sone.properties").writeText("Option=old")
101 assertThat(injector.getInstance(named("NewConfig")), equalTo(false))
105 fun `event bus is bound`() {
106 assertThat(injector.getInstance<EventBus>(), notNullValue())
110 fun `context is bound`() {
111 assertThat(injector.getInstance<Context>().context, equalTo("Sone"))
115 fun `optional context is bound`() {
116 assertThat(injector.getInstance<Optional<Context>>().get().context, equalTo("Sone"))
120 fun `sone plugin is bound`() {
121 assertThat(injector.getInstance(), sameInstance(sonePlugin))
125 fun `version is bound`() {
126 assertThat(injector.getInstance(), equalTo(pluginVersion))
130 fun `plugin version is bound`() {
131 assertThat(injector.getInstance(), equalTo(PluginVersion(versionString)))
135 fun `plugin year is bound`() {
136 assertThat(injector.getInstance(), equalTo(PluginYear(pluginYear)))
140 fun `plugin homepage in bound`() {
141 assertThat(injector.getInstance(), equalTo(PluginHomepage(pluginHomepage)))
145 fun `database is bound correctly`() {
146 assertThat(injector.getInstance<Database>(), instanceOf(MemoryDatabase::class.java))
150 fun `base l10n is bound correctly`() {
151 assertThat(injector.getInstance(), sameInstance(l10n.base))
155 fun `default loader is used without dev options`() {
156 assertThat(injector.getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
160 fun `default loaders are used if no path is given`() {
161 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true")
162 assertThat(injector.getInstance<Loaders>(), instanceOf(DefaultLoaders::class.java))
166 fun `debug loaders are used if path is given`() {
167 File(currentDir, "sone.properties").writeText("Developer.LoadFromFilesystem=true\nDeveloper.FilesystemPath=/tmp")
168 assertThat(injector.getInstance<Loaders>(), instanceOf(DebugLoaders::class.java))
172 val ref: AtomicReference<Any?> = AtomicReference()
174 fun testEvent(event: Any?) {
180 fun `created objects are registered with event bus`() {
181 val eventBus: EventBus = injector.getInstance()
182 val testObject = injector.getInstance<TestObject>()
185 assertThat(testObject.ref.get(), sameInstance(event))
189 fun `core is created as singleton`() {
190 val firstCore = injector.getInstance<Core>()
191 val secondCore = injector.getInstance<Core>()
192 assertThat(secondCore, sameInstance(firstCore))
196 fun `core is registered with event bus`() {
197 val eventBus = mock<EventBus>()
198 val injector = createInjector(
199 SoneModule(sonePlugin, eventBus),
200 FreenetInterface::class.isProvidedByDeepMock(),
201 PluginRespirator::class.isProvidedByDeepMock()
203 val core = injector.getInstance<Core>()
204 verify(eventBus).register(core)
208 fun `metrics registry can be created`() {
209 assertThat(injector.getInstance<MetricRegistry>(), notNullValue())
213 fun `metrics registry is created as singleton`() {
214 val firstMetricRegistry = injector.getInstance<MetricRegistry>()
215 val secondMetricRegistry = injector.getInstance<MetricRegistry>()
216 assertThat(firstMetricRegistry, sameInstance(secondMetricRegistry))