1 package net.pterodactylus.sone.main
3 import com.google.inject.Guice
4 import freenet.client.HighLevelSimpleClient
5 import freenet.clients.http.SessionManager
6 import freenet.node.Node
7 import freenet.pluginmanager.PluginRespirator
8 import net.pterodactylus.sone.test.deepMock
9 import net.pterodactylus.sone.test.getInstance
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.sameInstance
15 import org.mockito.Mockito.verify
18 * Unit test for [FreenetModule].
20 class FreenetModuleTest {
22 private val sessionManager = mock<SessionManager>()
23 private val pluginRespirator = deepMock<PluginRespirator>().apply {
24 whenever(getSessionManager("Sone")).thenReturn(sessionManager)
26 private val node = pluginRespirator.node!!
27 private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!!
28 private val module = FreenetModule(pluginRespirator)
29 private val injector = Guice.createInjector(module)
31 private inline fun <reified T: Any> verifySingletonInstance() {
32 val firstInstance = injector.getInstance<T>()
33 val secondInstance = injector.getInstance<T>()
34 assertThat(firstInstance, sameInstance(secondInstance))
38 fun `plugin respirator is returned correctly`() {
39 assertThat(injector.getInstance(), sameInstance(pluginRespirator))
43 fun `plugin respirator is returned as singleton`() {
44 verifySingletonInstance<PluginRespirator>()
48 fun `node is returned correctly`() {
49 assertThat(injector.getInstance(), sameInstance(node))
53 fun `node is returned as singleton`() {
54 verifySingletonInstance<Node>()
58 fun `high level simply client is returned correctly`() {
59 assertThat(injector.getInstance(), sameInstance(highLevelSimpleClient))
63 fun `high level simply client is returned as singleton`() {
64 verifySingletonInstance<HighLevelSimpleClient>()
68 fun `session manager is returned correctly`() {
69 assertThat(injector.getInstance(), sameInstance(sessionManager))
73 fun `session manager is returned as singleton`() {
74 verifySingletonInstance<SessionManager>()
75 verify(pluginRespirator).getSessionManager("Sone")