šŸš§ Send ā€œconfig not readā€ event in plugin
authorDavid ā€˜Bombeā€™ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 16:31:15 +0000 (17:31 +0100)
committerDavid ā€˜Bombeā€™ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 18:47:25 +0000 (19:47 +0100)
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/kotlin/net/pterodactylus/sone/core/event/ConfigNotRead.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt

index 3f010f6..a6ade6b 100644 (file)
@@ -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 (file)
index 0000000..c8f1573
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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
index 9e5158b..5795285 100644 (file)
@@ -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 <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
                        injected[TypeLiteral.get(clazz) to annotation] as? T