From 16fcc489ded63c051d97cdb0609c99baadab7346 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 1 Apr 2020 08:28:56 -0600 Subject: [PATCH] Fix broken tests This was caused by changed behavior in various classes. Some were broken due to changing facts (for example, Canada now has highways, as does most of the rest of the world). Signed-off-by: Taylor Smock --- .../mapwithai/backend/MapWithAIDataUtils.java | 27 ++++++++++--------- .../data/mapwithai/MapWithAILayerInfo.java | 10 ++++--- .../backend/MapWithAIAvailabilityTest.java | 8 +++--- .../frontend/MapWithAIDownloadReaderTest.java | 15 +++-------- 4 files changed, 28 insertions(+), 32 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 660ebe2..a842d77 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 @@ -168,19 +168,20 @@ public final class MapWithAIDataUtils { if ((realBBoxes.size() < TOO_MANY_BBOXES) || confirmBigDownload(realBBoxes)) { final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(); monitor.beginTask(tr("Downloading {0} Data", MapWithAIPlugin.NAME), realBounds.size()); - realBounds.parallelStream().forEach(bound -> MapWithAIPreferenceHelper.getMapWithAIUrl() - .parallelStream().filter(i -> i.getUrl() != null && !i.getUrl().trim().isEmpty()).forEach(i -> { - BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader(bound, i, - DetectTaskingManagerUtils.hasTaskingManagerLayer()); - try { - DataSet ds = downloader.parseOsm(monitor.createSubTaskMonitor(1, false)); - synchronized (MapWithAIDataUtils.class) { - dataSet.mergeFrom(ds); - } - } catch (OsmTransferException e) { - Logging.error(e); - } - })); + realBounds.parallelStream() + .forEach(bound -> new ArrayList<>(MapWithAIPreferenceHelper.getMapWithAIUrl()).parallelStream() + .filter(i -> i.getUrl() != null && !i.getUrl().trim().isEmpty()).forEach(i -> { + BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader( + bound, i, DetectTaskingManagerUtils.hasTaskingManagerLayer()); + try { + DataSet ds = downloader.parseOsm(monitor.createSubTaskMonitor(1, false)); + synchronized (MapWithAIDataUtils.class) { + dataSet.mergeFrom(ds); + } + } catch (OsmTransferException e) { + Logging.error(e); + } + })); monitor.finishTask(); monitor.close(); } diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java index 636b3e7..20abc1c 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java @@ -38,7 +38,7 @@ import org.openstreetmap.josm.tools.Utils; public class MapWithAILayerInfo { /** List of all usable layers */ - private final List layers = new ArrayList<>(); + private final List layers = Collections.synchronizedList(new ArrayList<>()); /** List of layer ids of all usable layers */ private final Map layerIds = new HashMap<>(); /** List of all available default layers */ @@ -378,10 +378,12 @@ public class MapWithAILayerInfo { /** * Save the list of imagery entries to preferences. */ - public void save() { + public synchronized void save() { List entries = new ArrayList<>(); - for (MapWithAIInfo info : layers) { - entries.add(new MapWithAIPreferenceEntry(info)); + synchronized (layers) { + for (MapWithAIInfo info : layers) { + entries.add(new MapWithAIPreferenceEntry(info)); + } } StructUtils.putListOfStructs(Config.getPref(), CONFIG_PREFIX + "entries", entries, MapWithAIPreferenceEntry.class); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java index a655d06..bda658d 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java @@ -71,10 +71,10 @@ public class MapWithAIAvailabilityTest { "The US should have highway data"); assertTrue(DataAvailability.getDataTypes(new LatLon(40, -100)).getOrDefault("building", false), "The US should have building data"); - assertFalse(DataAvailability.getDataTypes(new LatLon(45.424722, -75.695)).getOrDefault("highway", false), - "Canada does not yet have highway data"); - assertTrue(DataAvailability.getDataTypes(new LatLon(45.424722, -75.695)).getOrDefault("building", false), - "Canada does have building data"); + assertFalse(DataAvailability.getDataTypes(new LatLon(71.67, -42.85)).getOrDefault("highway", false), + "Denmark should not have highway data"); + assertFalse(DataAvailability.getDataTypes(new LatLon(71.67, -42.85)).getOrDefault("building", false), + "Denmark does not have building data"); assertTrue(DataAvailability.getDataTypes(new LatLon(19.433333, -99.133333)).getOrDefault("highway", false), "Mexico has highway data"); assertFalse(DataAvailability.getDataTypes(new LatLon(19.433333, -99.133333)).getOrDefault("building", false), diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/frontend/MapWithAIDownloadReaderTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/frontend/MapWithAIDownloadReaderTest.java index cb117a7..2f2d1ef 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/frontend/MapWithAIDownloadReaderTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/frontend/MapWithAIDownloadReaderTest.java @@ -19,34 +19,27 @@ import org.openstreetmap.josm.gui.download.DownloadSettings; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIPreferenceHelper; -import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo; +import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest; import org.openstreetmap.josm.testutils.JOSMTestRules; import com.github.tomakehurst.wiremock.WireMockServer; public class MapWithAIDownloadReaderTest { @Rule - public JOSMTestRules rules = new JOSMTestRules().projection(); + public JOSMTestRules rules = new JOSMTestRules().projection().territories(); WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock")); @Before public void setUp() { wireMock.start(); - MapWithAILayerInfo.instance.getLayers().forEach(i -> i.setUrl(getDefaultMapWithAIAPIForTest(i.getUrl()))); + MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock); } @After public void tearDown() { wireMock.stop(); - } - - private String getDefaultMapWithAIAPIForTest(String url) { - return getDefaultMapWithAIAPIForTest(url, "https://www.mapwith.ai"); - } - - private String getDefaultMapWithAIAPIForTest(String url, String wireMockReplace) { - return url.replace(wireMockReplace, wireMock.baseUrl()); + MapWithAILayerInfoTest.resetMapWithAILayerInfo(); } @Test