From d33e7e73ccdc7a1ed8b32b13bbe12f84aa076e6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 11 Dec 2019 17:31:15 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Send=20=E2=80=9Cconfig=20not=20re?= =?utf8?q?ad=E2=80=9D=20event=20in=20plugin?= 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/ConfigNotRead.kt | 26 ++++++++++++ .../net/pterodactylus/sone/main/SonePluginTest.kt | 47 ++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/core/event/ConfigNotRead.kt diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 3f010f6..a6ade6b 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -208,11 +208,15 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr /* start the web interface! */ webInterface.start(); - 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()); + } else { + /* new config? */ + if (injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig")))) { + injector.getInstance(EventBus.class).post(new ConfigNotRead()); + } } } diff --git a/src/main/kotlin/net/pterodactylus/sone/core/event/ConfigNotRead.kt b/src/main/kotlin/net/pterodactylus/sone/core/event/ConfigNotRead.kt new file mode 100644 index 0000000..c8f1573 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/event/ConfigNotRead.kt @@ -0,0 +1,26 @@ +/** + * Sone - ConfigNotRead.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 could not read an existing configuration + * successfully, and a new configuration was created. This is different from + * [FirstStart] in that `FirstStart` signals that there *was* no existing + * configuration to be read. + */ +class ConfigNotRead diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index 9e5158b..5795285 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -133,6 +133,53 @@ class SonePluginTest { } } + private class ConfigNotReadListener(private val configNotReadReceiver: AtomicBoolean) { + @Subscribe + fun configNotRead(configNotRead: ConfigNotRead) { + configNotReadReceiver.set(true) + } + } + + @Test + fun `config-not-read event is sent to event bus when new config is true`() { + File("sone.properties").deleteAfter { + writeText("Invalid") + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + sonePlugin.runPlugin(pluginRespirator) + assertThat(configNotReadReceived.get(), equalTo(true)) + } + } + + @Test + fun `config-not-read event is not sent to event bus when first start is true`() { + File("sone.properties").delete() + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + sonePlugin.runPlugin(pluginRespirator) + assertThat(configNotReadReceived.get(), equalTo(false)) + } + + @Test + fun `config-not-read event is not sent to event bus when new config is false`() { + File("sone.properties").deleteAfter { + writeText("# comment") + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + sonePlugin.runPlugin(pluginRespirator) + assertThat(configNotReadReceived.get(), equalTo(false)) + } + } + private fun getInjected(clazz: Class, annotation: Annotation? = null): T? = injected[TypeLiteral.get(clazz) to annotation] as? T -- 2.7.4