From 1b4970dad8b4b2daacc19ad045706880872d30d7 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Tue, 19 Nov 2019 07:55:23 -0700 Subject: [PATCH] Add more tests for restartless updates Signed-off-by: Taylor Smock --- .../plugins/mapwithai/MapWithAIPlugin.java | 4 ++-- .../mapwithai/MapWithAIPluginTest.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java index cd966bd..cdcdbf2 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java @@ -41,7 +41,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { /** The name of the plugin */ public static final String NAME = "MapWithAI"; - private static final String PAINTSTYLE_PREEXISTS = NAME.concat(".paintstyleprexists"); + protected static final String PAINTSTYLE_PREEXISTS = NAME.concat(".paintstyleprexists"); private static String versionInfo; private final PreferenceSetting preferenceSetting; @@ -142,7 +142,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).stream() .forEach(layer -> MainApplication.getLayerManager().removeLayer(layer)); - if (Config.getPref().getBoolean(PAINTSTYLE_PREEXISTS)) { + if (!Config.getPref().getBoolean(PAINTSTYLE_PREEXISTS)) { MapWithAIDataUtils.removeMapWithAIPaintStyles(); } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java index 42aa419..2746fdd 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java @@ -4,9 +4,12 @@ package org.openstreetmap.josm.plugins.mapwithai; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; import javax.swing.JMenu; +import org.awaitility.Awaitility; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -14,6 +17,8 @@ import org.junit.Test; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; import org.openstreetmap.josm.plugins.PluginInformation; +import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; +import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.testutils.JOSMTestRules; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -61,6 +66,25 @@ public class MapWithAIPluginTest { Assert.assertEquals(dataMenuSize + 4, dataMenu.getMenuComponentCount()); Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + + for (boolean existed : Arrays.asList(false, true)) { // false, true order is important + plugin = new MapWithAIPlugin(info); + Config.getPref().putBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS, existed); + plugin.destroy(); + Assert.assertEquals(dataMenuSize, dataMenu.getMenuComponentCount()); + Awaitility.await().atMost(5, TimeUnit.SECONDS) + .until(() -> existed == MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); + Assert.assertEquals(Config.getPref().getBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS) ? 1 : 0, + MapPaintStyles.getStyles().getStyleSources().parallelStream() + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + } + + for (int i = 0; i < 3; i++) { + plugin = new MapWithAIPlugin(info); + Assert.assertEquals(dataMenuSize + 4, dataMenu.getMenuComponentCount()); + Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + } } /**