From 70b624452d63396246ff200ee9d358725c9fc589 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 20 Jan 2020 07:52:19 -0700 Subject: [PATCH] Fix JOSM-18578 -- crash when adding MapWithAI layer multiple times. The test looks at the last errors and warnings from the Logger, since the issue was not being raised. Signed-off-by: Taylor Smock --- .../mapwithai/backend/MapWithAIDataUtils.java | 16 +++++++++------- .../backend/MapWithAIDataUtilsTest.java | 12 ++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java index b6aa370..461fa33 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java @@ -309,13 +309,15 @@ public final class MapWithAIDataUtils { } final MapWithAILayer tLayer = layer; - if (SwingUtilities.isEventDispatchThread() && create) { - MainApplication.getLayerManager().addLayer(tLayer); - } else if (create) { - try { - SwingUtilities.invokeAndWait(() -> MainApplication.getLayerManager().addLayer(tLayer)); - } catch (InvocationTargetException | InterruptedException e) { - Logging.error(e); + if (!MainApplication.getLayerManager().getLayers().contains(tLayer)) { + if (SwingUtilities.isEventDispatchThread() && create) { + MainApplication.getLayerManager().addLayer(tLayer); + } else if (create) { + try { + SwingUtilities.invokeAndWait(() -> MainApplication.getLayerManager().addLayer(tLayer)); + } catch (InvocationTargetException | InterruptedException e) { + Logging.error(e); + } } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java index 839b24c..43a3d52 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java @@ -3,6 +3,7 @@ package org.openstreetmap.josm.plugins.mapwithai.backend; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -38,6 +39,7 @@ import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; import org.openstreetmap.josm.gui.mappaint.StyleSource; import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.tools.Logging; import com.github.tomakehurst.wiremock.WireMockServer; @@ -220,6 +222,16 @@ public class MapWithAIDataUtilsTest { } } + @Test + public void testDoubleAddLayer() { + Logging.clearLastErrorAndWarnings(); + assertNull(MapWithAIDataUtils.getLayer(false)); + assertNotNull(MapWithAIDataUtils.getLayer(true)); + assertNotNull(MapWithAIDataUtils.getLayer(true)); + assertTrue(Logging.getLastErrorAndWarnings().stream().filter(str -> !str.contains("Failed to locate image")) + .collect(Collectors.toList()).isEmpty()); + } + private static int getExpectedNumberOfBBoxes(BBox bbox) { double width = MapWithAIDataUtils.getWidth(bbox); double height = MapWithAIDataUtils.getHeight(bbox);