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 <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-01-20 07:52:19 -07:00
rodzic 51dc46d2c1
commit 70b624452d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 21 dodań i 7 usunięć

Wyświetl plik

@ -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);
}
}
}

Wyświetl plik

@ -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);