1 package net.pterodactylus.sone.main
3 import com.google.common.eventbus.*
4 import com.google.inject.*
5 import freenet.client.async.*
6 import freenet.l10n.BaseL10n.LANGUAGE.*
8 import freenet.pluginmanager.*
9 import net.pterodactylus.sone.core.*
10 import net.pterodactylus.sone.core.event.*
11 import net.pterodactylus.sone.fcp.*
12 import net.pterodactylus.sone.freenet.wot.*
13 import net.pterodactylus.sone.test.*
14 import net.pterodactylus.sone.web.*
15 import net.pterodactylus.sone.web.notification.*
16 import org.hamcrest.MatcherAssert.*
17 import org.hamcrest.Matchers.*
18 import org.mockito.Mockito.*
20 import java.util.concurrent.atomic.*
24 * Unit test for [SonePlugin].
27 class SonePluginTest {
29 private val sonePlugin by lazy { SonePlugin { injector } }
30 private val pluginRespirator = deepMock<PluginRespirator>()
31 private val node = deepMock<Node>()
32 private val clientCore = deepMock<NodeClientCore>()
33 private val uskManager = deepMock<USKManager>()
36 setField(node, "clientCore", clientCore)
37 whenever(pluginRespirator.node).thenReturn(node)
38 setField(clientCore, "uskManager", uskManager)
42 fun `sone plugin can be started`() {
43 sonePlugin.setLanguage(ENGLISH)
44 sonePlugin.runPlugin(pluginRespirator)
48 fun `core can be created`() {
49 val injector: Injector = runSonePluginWithRealInjector()
50 assertThat(injector.getInstance<Core>(), notNullValue())
54 fun `fcp interface can be created`() {
55 val injector: Injector = runSonePluginWithRealInjector()
56 assertThat(injector.getInstance<FcpInterface>(), notNullValue())
60 fun `web interface can be created`() {
61 val injector: Injector = runSonePluginWithRealInjector()
62 assertThat(injector.getInstance<WebInterface>(), notNullValue())
66 fun `web of trust connector can be created`() {
67 val injector: Injector = runSonePluginWithRealInjector()
68 assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
72 fun `notification handler can be created`() {
73 val injector: Injector = runSonePluginWithRealInjector()
74 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
77 private fun runSonePluginWithRealInjector(injectorConsumer: (Injector) -> Unit = {}): Injector {
78 lateinit var injector: Injector
79 val sonePlugin = SonePlugin {
80 Guice.createInjector(*it).also {
85 sonePlugin.setLanguage(ENGLISH)
86 sonePlugin.runPlugin(pluginRespirator)
91 fun `core is being started`() {
92 sonePlugin.runPlugin(pluginRespirator)
93 val core = injector.getInstance<Core>()
98 fun `notification handler is being requested`() {
99 sonePlugin.runPlugin(pluginRespirator)
100 assertThat(getInjected(NotificationHandler::class.java), notNullValue())
103 private class FirstStartListener(private val firstStartReceived: AtomicBoolean) {
105 fun firstStart(firstStart: FirstStart) {
106 firstStartReceived.set(true)
111 fun `first-start event is sent to event bus when first start is true`() {
112 File("sone.properties").delete()
113 val firstStartReceived = AtomicBoolean()
114 runSonePluginWithRealInjector {
115 val eventBus = it.getInstance(EventBus::class.java)
116 eventBus.register(FirstStartListener(firstStartReceived))
118 sonePlugin.runPlugin(pluginRespirator)
119 assertThat(firstStartReceived.get(), equalTo(true))
123 fun `first-start event is not sent to event bus when first start is false`() {
124 File("sone.properties").deleteAfter {
126 val firstStartReceived = AtomicBoolean()
127 runSonePluginWithRealInjector {
128 val eventBus = it.getInstance(EventBus::class.java)
129 eventBus.register(FirstStartListener(firstStartReceived))
131 sonePlugin.runPlugin(pluginRespirator)
132 assertThat(firstStartReceived.get(), equalTo(false))
136 private class ConfigNotReadListener(private val configNotReadReceiver: AtomicBoolean) {
138 fun configNotRead(configNotRead: ConfigNotRead) {
139 configNotReadReceiver.set(true)
144 fun `config-not-read event is sent to event bus when new config is true`() {
145 File("sone.properties").deleteAfter {
147 val configNotReadReceived = AtomicBoolean()
148 runSonePluginWithRealInjector {
149 val eventBus = it.getInstance(EventBus::class.java)
150 eventBus.register(ConfigNotReadListener(configNotReadReceived))
152 sonePlugin.runPlugin(pluginRespirator)
153 assertThat(configNotReadReceived.get(), equalTo(true))
158 fun `config-not-read event is not sent to event bus when first start is true`() {
159 File("sone.properties").delete()
160 val configNotReadReceived = AtomicBoolean()
161 runSonePluginWithRealInjector {
162 val eventBus = it.getInstance(EventBus::class.java)
163 eventBus.register(ConfigNotReadListener(configNotReadReceived))
165 sonePlugin.runPlugin(pluginRespirator)
166 assertThat(configNotReadReceived.get(), equalTo(false))
170 fun `config-not-read event is not sent to event bus when new config is false`() {
171 File("sone.properties").deleteAfter {
172 writeText("# comment")
173 val configNotReadReceived = AtomicBoolean()
174 runSonePluginWithRealInjector {
175 val eventBus = it.getInstance(EventBus::class.java)
176 eventBus.register(ConfigNotReadListener(configNotReadReceived))
178 sonePlugin.runPlugin(pluginRespirator)
179 assertThat(configNotReadReceived.get(), equalTo(false))
183 private fun <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
184 injected[TypeLiteral.get(clazz) to annotation] as? T
186 private val injected =
187 mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
189 private val injector = mock<Injector>().apply {
190 fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz)
191 whenever(getInstance(any<Key<*>>())).then {
192 injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) {
193 it.getArgument<Key<*>>(0).typeLiteral.type.typeName.toClass().let(::mockValue)
196 whenever(getInstance(any<Class<*>>())).then {
197 injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) {
198 it.getArgument<Class<*>>(0).let(::mockValue)
205 private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)
207 private fun File.deleteAfter(action: File.() -> Unit) = try {