From 5b4d06b96a626643ab695c470629c3722d36329b Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 24 Jun 2020 15:00:33 -0600 Subject: [PATCH] Ensure that MapWithAILayerInfo is always called when test rules set it up Signed-off-by: Taylor Smock --- .../data/mapwithai/MapWithAILayerInfo.java | 16 ++++++++--- .../mapwithai/MapWithAIPluginTest.java | 2 +- .../backend/DownloadMapWithAITaskTest.java | 3 ++- .../backend/GetDataRunnableTest.java | 2 +- .../backend/MapWithAIActionTest.java | 3 ++- .../backend/MapWithAIAvailabilityTest.java | 2 +- .../mapwithai/backend/MapWithAILayerTest.java | 4 +-- .../backend/MapWithAIRemoteControlTest.java | 2 +- .../backend/MapWithAIUploadHookTest.java | 4 ++- .../CreateConnectionsCommandTest.java | 2 +- .../commands/MapWithAIAddComandTest.java | 2 +- .../io/mapwithai/ESRISourceReaderTest.java | 2 +- .../testutils/MapWithAITestRules.java | 27 +++++++++---------- 13 files changed, 41 insertions(+), 30 deletions(-) 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 615c51d..269b1ff 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 @@ -16,6 +16,7 @@ import java.util.Objects; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicBoolean; import org.openstreetmap.josm.data.StructUtils; import org.openstreetmap.josm.data.imagery.ImageryInfo; @@ -62,7 +63,16 @@ public class MapWithAILayerInfo { public static MapWithAILayerInfo getInstance() { synchronized (DEFAULT_LAYER_SITES) { if (instance == null) { - instance = new MapWithAILayerInfo(); + AtomicBoolean finished = new AtomicBoolean(); + instance = new MapWithAILayerInfo(() -> finished.set(true)); + while (!finished.get()) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Logging.error(e); + Thread.currentThread().interrupt(); + } + } } } return instance; @@ -89,8 +99,8 @@ public class MapWithAILayerInfo { return Config.getPref().putList(CONFIG_PREFIX + "layers.sites", new ArrayList<>(sites)); } - private MapWithAILayerInfo() { - load(false); + private MapWithAILayerInfo(FinishListener listener) { + load(false, listener); } /** diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java index 80dc863..ec2270c 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java @@ -37,7 +37,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class MapWithAIPluginTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().preferences().main(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().preferences().main(); public PluginInformation info; public MapWithAIPlugin plugin; diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DownloadMapWithAITaskTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DownloadMapWithAITaskTest.java index d0ebd40..280f9d3 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DownloadMapWithAITaskTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DownloadMapWithAITaskTest.java @@ -24,7 +24,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class DownloadMapWithAITaskTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules rule = new MapWithAITestRules().wiremock().preferences().fakeAPI().projection().territories(); + public JOSMTestRules rule = new MapWithAITestRules().sources().wiremock().preferences().fakeAPI().projection() + .territories(); @Test public void testDownloadOsmServerReaderDownloadParamsBoundsProgressMonitor() diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java index 4ebf724..63b89e7 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java @@ -37,7 +37,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class GetDataRunnableTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules rule = new MapWithAITestRules().wiremock().projection().fakeAPI().territories(); + public JOSMTestRules rule = new MapWithAITestRules().sources().wiremock().projection().fakeAPI().territories(); public static String getDefaultMapWithAIAPIForTest(WireMockServer wireMock, String url) { return getDefaultMapWithAIAPIForTest(wireMock, url, "https://www.mapwith.ai"); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java index 12cf828..53da25f 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java @@ -32,7 +32,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class MapWithAIActionTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().main().projection().territories().timeout(100000); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().main().projection().territories() + .timeout(100000); private MapWithAIAction action; 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 0fb718d..2c63318 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java @@ -27,7 +27,7 @@ public class MapWithAIAvailabilityTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().projection().territories(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().projection().territories(); @Before public void setUp() { diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java index d44a382..1b5f423 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java @@ -58,8 +58,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class MapWithAILayerTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().preferences().main().projection().fakeAPI() - .territories(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().preferences().main().projection() + .fakeAPI().territories(); MapWithAILayer layer; diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java index 4f976d4..d48ec5b 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java @@ -34,7 +34,7 @@ public class MapWithAIRemoteControlTest { */ @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().main().projection().territories(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().main().projection().territories(); private static MapWithAIRemoteControl newHandler(String url) throws RequestHandlerBadRequestException { final MapWithAIRemoteControl req = new MapWithAIRemoteControl(); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java index 13a011e..cd2e80e 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java @@ -30,6 +30,7 @@ import org.openstreetmap.josm.plugins.PluginException; import org.openstreetmap.josm.plugins.PluginInformation; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo; +import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules; import org.openstreetmap.josm.testutils.JOSMTestRules; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -41,7 +42,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class MapWithAIUploadHookTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().main().projection().preferences().territories(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().main().projection().preferences() + .territories(); private PluginInformation info; private OsmDataLayer osmLayer; private MapWithAILayer aiLayer; diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java index 30c25c6..8464169 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java @@ -39,7 +39,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class CreateConnectionsCommandTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().projection(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().projection(); /** * Test method for diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java index 9cfa730..6e3aeb8 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java @@ -52,7 +52,7 @@ public class MapWithAIAddComandTest { @Rule @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new MapWithAITestRules().wiremock().projection().assertionsInEDT().main(); + public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().projection().assertionsInEDT().main(); @Before public void setUp() { diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/ESRISourceReaderTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/ESRISourceReaderTest.java index 81e43ad..9cb73e6 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/ESRISourceReaderTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/ESRISourceReaderTest.java @@ -16,7 +16,7 @@ import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules; public class ESRISourceReaderTest { @Rule - public MapWithAITestRules rule = (MapWithAITestRules) new MapWithAITestRules().wiremock().projection(); + public MapWithAITestRules rule = (MapWithAITestRules) new MapWithAITestRules().sources().wiremock().projection(); /** * Test that ESRI servers are properly added diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java index de9110b..d33787b 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java @@ -22,7 +22,6 @@ import org.openstreetmap.josm.io.OsmApi; import org.openstreetmap.josm.io.OsmApiInitializationException; import org.openstreetmap.josm.io.OsmTransferCanceledException; import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability; -import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnableTest; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo; @@ -38,6 +37,8 @@ import com.github.tomakehurst.wiremock.http.Request; import com.github.tomakehurst.wiremock.http.Response; import com.github.tomakehurst.wiremock.verification.LoggedRequest; +import mockit.Mock; +import mockit.MockUp; import mockit.integration.TestRunnerDecorator; public class MapWithAITestRules extends JOSMTestRules { @@ -89,8 +90,6 @@ public class MapWithAITestRules extends JOSMTestRules { wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock") .extensions(new WireMockUrlTransformer()).dynamicPort()); wireMock.start(); - resetMapWithAILayerInfo(); - setupMapWithAILayerInfo(wireMock); MapWithAIDataUtils.setPaintStyleUrl(MapWithAIDataUtils.getPaintStyleUrl() .replace(Config.getUrls().getJOSMWebsite(), wireMock.baseUrl())); currentReleaseUrl = DataAvailability.getReleaseUrl(); @@ -107,8 +106,6 @@ public class MapWithAITestRules extends JOSMTestRules { } return null; }).filter(Objects::nonNull).collect(Collectors.toList())); - MapWithAILayerInfo.getInstance().getLayers() - .forEach(l -> l.setUrl(l.getUrl().replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/"))); try { OsmApi.getOsmApi().initialize(NullProgressMonitor.INSTANCE); } catch (OsmTransferCanceledException | OsmApiInitializationException e) { @@ -119,6 +116,15 @@ public class MapWithAITestRules extends JOSMTestRules { AtomicBoolean finished = new AtomicBoolean(); MapWithAILayerInfo.getInstance().load(false, () -> finished.set(true)); Awaitility.await().atMost(Durations.TEN_SECONDS).until(finished::get); + } else { + // This only exists to ensure that if MapWithAILayerInfo is called, things + // happen... + new MockUp() { + @Mock + public MapWithAILayerInfo getInstance() { + return null; + } + }; } if (workerExceptions) { currentExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); @@ -137,10 +143,10 @@ public class MapWithAITestRules extends JOSMTestRules { List requests = wireMock.findUnmatchedRequests().getRequests(); requests.forEach(r -> Logging.error(r.getAbsoluteUrl())); assertTrue(requests.isEmpty()); - resetMapWithAILayerInfo(); DataAvailability.setReleaseUrl(currentReleaseUrl); Config.getPref().put("osm-server.url", null); MapWithAILayerInfo.setImageryLayersSites(sourceSites); + resetMapWithAILayerInfo(); } if (workerExceptions) { Thread.setDefaultUncaughtExceptionHandler(currentExceptionHandler); @@ -148,15 +154,6 @@ public class MapWithAITestRules extends JOSMTestRules { TestRunnerDecorator.cleanUpAllMocks(); } - private static void setupMapWithAILayerInfo(WireMockServer wireMockServer) { - synchronized (MapWithAITestRules.class) { - resetMapWithAILayerInfo(); - MapWithAILayerInfo.getInstance().getLayers().stream().forEach( - i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMockServer, i.getUrl()))); - MapWithAILayerInfo.getInstance().save(); - } - } - private static void resetMapWithAILayerInfo() { synchronized (MapWithAILayerInfo.class) { MapWithAILayerInfo.getInstance().clear();