From 8281d81a4412af542753a4fc56b2c1a8c52000d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 11 Dec 2019 16:44:24 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20event=20for=20first=20start?= =?utf8?q?=20of=20Sone?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/main/SonePlugin.java | 6 +++ .../pterodactylus/sone/core/event/FirstStart.kt | 24 +++++++++++ .../net/pterodactylus/sone/main/SonePluginTest.kt | 49 +++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/core/event/FirstStart.kt diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index b79df7c..4fd40f5 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -23,6 +23,7 @@ import java.util.logging.Logger; import java.util.logging.*; import net.pterodactylus.sone.core.*; +import net.pterodactylus.sone.core.event.*; import net.pterodactylus.sone.fcp.*; import net.pterodactylus.sone.freenet.wot.*; import net.pterodactylus.sone.web.*; @@ -209,6 +210,11 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr webInterface.start(); webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))); webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig")))); + + /* first start? */ + if (injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))) { + injector.getInstance(EventBus.class).post(new FirstStart()); + } } @VisibleForTesting diff --git a/src/main/kotlin/net/pterodactylus/sone/core/event/FirstStart.kt b/src/main/kotlin/net/pterodactylus/sone/core/event/FirstStart.kt new file mode 100644 index 0000000..4ff81f3 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/event/FirstStart.kt @@ -0,0 +1,24 @@ +/** + * Sone - FirstStart.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.core.event + +/** + * Event that signals that Sone was started for the first time. This event + * will only be triggered once, on startup. + */ +class FirstStart diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index 23c679b..677823c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -1,11 +1,13 @@ package net.pterodactylus.sone.main +import com.google.common.eventbus.* import com.google.inject.* import freenet.client.async.* import freenet.l10n.BaseL10n.LANGUAGE.* import freenet.node.* import freenet.pluginmanager.* import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.core.event.* import net.pterodactylus.sone.fcp.* import net.pterodactylus.sone.freenet.wot.* import net.pterodactylus.sone.test.* @@ -14,6 +16,8 @@ import net.pterodactylus.sone.web.notification.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.mockito.Mockito.* +import java.io.* +import java.util.concurrent.atomic.* import kotlin.test.* /** @@ -70,11 +74,12 @@ class SonePluginTest { assertThat(injector.getInstance(), notNullValue()) } - private fun runSonePluginWithRealInjector(): Injector { + private fun runSonePluginWithRealInjector(injectorConsumer: (Injector) -> Unit = {}): Injector { lateinit var injector: Injector val sonePlugin = SonePlugin { Guice.createInjector(*it).also { injector = it + injectorConsumer(it) } } sonePlugin.setLanguage(ENGLISH) @@ -95,6 +100,42 @@ class SonePluginTest { assertThat(getInjected(NotificationHandler::class.java), notNullValue()) } + @Test + fun `first-start event is sent to event bus when first start is true`() { + File("sone.properties").delete() + val firstStartReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(object : Any() { + @Subscribe + fun firstStart(firstStart: FirstStart) { + firstStartReceived.set(true) + } + }) + } + sonePlugin.runPlugin(pluginRespirator) + assertThat(firstStartReceived.get(), equalTo(true)) + } + + @Test + fun `first-start event is not sent to event bus when first start is false`() { + File("sone.properties").deleteAfter { + writeText("# empty") + val firstStartReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(object : Any() { + @Subscribe + fun firstStart(firstStart: FirstStart) { + firstStartReceived.set(true) + } + }) + } + sonePlugin.runPlugin(pluginRespirator) + assertThat(firstStartReceived.get(), equalTo(false)) + } + } + private fun getInjected(clazz: Class, annotation: Annotation? = null): T? = injected[TypeLiteral.get(clazz) to annotation] as? T @@ -118,3 +159,9 @@ class SonePluginTest { } private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this) + +private fun File.deleteAfter(action: File.() -> Unit) = try { + action(this) +} finally { + this.delete() +} -- 2.7.4