From a3fb91b2a8d76f799db25cf51d02864d7cb10a13 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 6 Apr 2020 18:57:10 -0600 Subject: [PATCH] Tests: Fixes due to changes in threading Signed-off-by: Taylor Smock --- .../plugins/mapwithai/UpdateProdTest.java | 8 +++++++- .../backend/MapWithAIActionTest.java | 2 ++ .../mapwithai/backend/MapWithAILayerTest.java | 3 +++ .../backend/MapWithAIMoveActionTest.java | 13 ++++++++++++ .../backend/MapWithAIRemoteControlTest.java | 12 +++++++++++ .../commands/MapWithAIAddComandTest.java | 13 ++++++++++++ .../preferences/MapWithAILayerInfoTest.java | 20 +++++++++++-------- 7 files changed, 62 insertions(+), 9 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java index 89194e6..61fa95c 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java @@ -5,12 +5,14 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.awt.GraphicsEnvironment; +import java.lang.reflect.Field; import javax.swing.JOptionPane; import org.junit.Rule; import org.junit.Test; import org.openstreetmap.josm.TestUtils; +import org.openstreetmap.josm.data.Version; import org.openstreetmap.josm.plugins.mapwithai.testutils.OpenBrowserMocker; import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.testutils.JOSMTestRules; @@ -26,12 +28,16 @@ public class UpdateProdTest { public JOSMTestRules rule = new JOSMTestRules().preferences(); @Test - public void testDoProd() { + public void testDoProd() + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { TestUtils.assumeWorkingJMockit(); new OpenBrowserMocker(); if (GraphicsEnvironment.isHeadless()) { new WindowMocker(); } + Field version = Version.class.getDeclaredField("version"); + version.setAccessible(true); + version.setInt(Version.getInstance(), 15000); String booleanKey = "message.".concat(MapWithAIPlugin.NAME.concat(".ignore_next_version")); String intKey = "message.".concat(MapWithAIPlugin.NAME.concat(".ignore_next_version")).concat(".value"); // "message.MapWithAI.ignore_next_version.value"; Config.getPref().putBoolean(booleanKey, false); 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 93692e6..abda8e9 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIActionTest.java @@ -94,6 +94,7 @@ public class MapWithAIActionTest { MapWithAIAction.toggleLayer(layer); assertSame(layer, MainApplication.getLayerManager().getActiveLayer()); MapWithAIDataUtils.getLayer(true); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> MapWithAIDataUtils.getLayer(false) != null); // Adding the MapWithAI layer switches to it assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer()); MapWithAIAction.toggleLayer(layer); @@ -110,6 +111,7 @@ public class MapWithAIActionTest { assertNull(notification); MapWithAILayer layer = MapWithAIDataUtils.getLayer(true); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> MapWithAIDataUtils.getLayer(false) != null); notification = MapWithAIAction.createMessageDialog(); assertNotNull(notification); DataSource source = new DataSource(new Bounds(38.8876078, -77.012173, 38.892087, -77.0059234), "test area"); 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 e08ca60..de7662a 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java @@ -22,6 +22,7 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; +import org.awaitility.Awaitility; import org.awaitility.Durations; import org.junit.After; import org.junit.Before; @@ -138,6 +139,7 @@ public class MapWithAILayerTest { assertNull(mapWithAILayer, "There should be no MapWithAI layer yet"); mapWithAILayer = MapWithAIDataUtils.getLayer(true); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> MapWithAIDataUtils.getLayer(false) != null); assertEquals(MapWithAILayer.class, mapWithAILayer.getClass(), "The MapWithAI layer should be of the MapWithAILayer.class"); @@ -201,6 +203,7 @@ public class MapWithAILayerTest { @Test public void testGetMenuEntries() { Layer layer = MapWithAIDataUtils.getLayer(true); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> MapWithAIDataUtils.getLayer(false) != null); Action[] actions = layer.getMenuEntries(); assertTrue(actions.length > 0); assertEquals(ContinuousDownloadAction.class, layer.getMenuEntries()[actions.length - 3].getClass()); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java index ae28fc7..60590cd 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java @@ -7,9 +7,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.awaitility.Awaitility; +import org.awaitility.Durations; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.UndoRedoHandler; import org.openstreetmap.josm.data.coor.LatLon; @@ -71,6 +74,11 @@ public class MapWithAIMoveActionTest { assertNull(osmLayer.getDataSet().getPrimitiveById(way1), "way1 should have been removed from the OSM layer"); } + @Test + public void testMoveEmptyAction() { + Assertions.assertDoesNotThrow(() -> moveAction.actionPerformed(null)); + } + @Test public void testConflationDupeKeyRemoval() { mapWithAIData.unlock(); @@ -80,6 +88,7 @@ public class MapWithAIMoveActionTest { final DataSet ds = osmLayer.getDataSet(); moveAction.actionPerformed(null); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> ds.getPrimitiveById(way1) != null); assertTrue(((Way) ds.getPrimitiveById(way1)).lastNode().equals(((Way) ds.getPrimitiveById(way2)).lastNode()), "The duplicate node should have been replaced"); assertFalse(((Way) ds.getPrimitiveById(way2)).lastNode().hasKey(DuplicateCommand.KEY), @@ -88,6 +97,8 @@ public class MapWithAIMoveActionTest { "The dupe key should no longer exist"); UndoRedoHandler.getInstance().undo(); + Awaitility.await().atMost(Durations.ONE_SECOND) + .until(() -> !((Way) ds.getPrimitiveById(way2)).lastNode().hasKey(DuplicateCommand.KEY)); assertFalse(way2.lastNode().hasKey(DuplicateCommand.KEY), "The dupe key should no longer exist"); assertTrue(way1.lastNode().hasKey(DuplicateCommand.KEY), "The dupe key should no longer exist"); } @@ -101,12 +112,14 @@ public class MapWithAIMoveActionTest { mapWithAIData.addSelected(way1); moveAction.actionPerformed(null); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> way1.isDeleted()); assertFalse(way2.lastNode().hasKey(ConnectedCommand.KEY), "The conn key should have been removed"); assertFalse(way2.firstNode().hasKey(ConnectedCommand.KEY), "The conn key should have been removed"); assertFalse(way2.getNode(1).hasKey(ConnectedCommand.KEY), "The conn key should have been removed"); assertTrue(way1.isDeleted(), "way1 should be deleted when added"); UndoRedoHandler.getInstance().undo(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> !way1.isDeleted() && !way1.lastNode().isDeleted()); assertFalse(way2.lastNode().hasKey(ConnectedCommand.KEY), "The conn key shouldn't exist"); assertTrue(way1.lastNode().hasKey(ConnectedCommand.KEY), "The conn key should exist"); assertFalse(way1.lastNode().isDeleted(), "way1 should no longer be deleted"); 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 4addf28..e2b6568 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIRemoteControlTest.java @@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.openstreetmap.josm.tools.I18n.tr; +import org.awaitility.Awaitility; import org.awaitility.Durations; import org.junit.After; import org.junit.Before; @@ -94,6 +95,9 @@ public class MapWithAIRemoteControlTest { public void testNominalRequest() throws Exception { newHandler("https://localhost?url=" + Utils.encodeUrl(MapWithAILayerInfo.instance.getLayers().get(0).getUrl())) .handle(); + Awaitility.await().atMost(Durations.ONE_SECOND) + .until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); + assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); assertTrue(MapWithAIDataUtils.getLayer(false).getDataSet().getDataSourceBounds().isEmpty()); @@ -103,6 +107,8 @@ public class MapWithAIRemoteControlTest { public void testTemporaryUrl() throws Exception { final String badUrl = "https://bad.url"; newHandler("https://localhost?url=" + Utils.encodeUrl(badUrl)).handle(); + Awaitility.await().atMost(Durations.ONE_SECOND) + .until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() @@ -122,6 +128,9 @@ public class MapWithAIRemoteControlTest { final Integer maxObj = 1; newHandler("http://127.0.0.1:8111/mapwithai?bbox=" + getTestBBox().toStringCSV(",") + "&max_obj=" + maxObj.toString()).handle(); + Awaitility.await().atMost(Durations.ONE_SECOND) + .until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); + assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); assertEquals(maxObj.intValue(), MapWithAIPreferenceHelper.getMaximumAddition()); @@ -138,6 +147,9 @@ public class MapWithAIRemoteControlTest { public void testBBox() throws Exception { BBox temp = getTestBBox(); newHandler("http://127.0.0.1:8111/mapwithai?bbox={bbox}".replace("{bbox}", temp.toStringCSV(","))).handle(); + Awaitility.await().atMost(Durations.ONE_SECOND) + .until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); + assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); await().atMost(Durations.TEN_SECONDS) 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 351464b..c541750 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java @@ -13,6 +13,8 @@ import java.io.File; import java.util.Arrays; import java.util.Collections; +import org.awaitility.Awaitility; +import org.awaitility.Durations; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -91,6 +93,7 @@ public class MapWithAIAddComandTest { assertTrue(way3.isDeleted(), "The way should be deleted"); command.undoCommand(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> ds2.getPrimitiveById(way3) == null); assertNull(ds2.getPrimitiveById(way3), "DataSet should no longer contain object"); assertFalse(ds1.getPrimitiveById(way3).isDeleted(), "The way should no longer be deleted in its original DataSet"); @@ -143,11 +146,13 @@ public class MapWithAIAddComandTest { MapWithAIAddCommand command = new MapWithAIAddCommand(mapWithAIData, osmData, mapWithAIData.getSelected()); command.executeCommand(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> mapWithAIData.allNonDeletedPrimitives().isEmpty()); assertEquals(6, osmData.allNonDeletedPrimitives().size(), "All primitives should now be in osmData"); assertTrue(mapWithAIData.allNonDeletedPrimitives().isEmpty(), "There should be no remaining non-deleted primitives"); command.undoCommand(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> osmData.allNonDeletedPrimitives().size() == 3); assertEquals(3, osmData.allNonDeletedPrimitives().size(), "The DataSet should be in its original state"); assertEquals(3, mapWithAIData.allNonDeletedPrimitives().size(), "The DataSet should be in its original state"); @@ -155,11 +160,13 @@ public class MapWithAIAddComandTest { way1.lastNode().put(dupe); command = new MapWithAIAddCommand(mapWithAIData, osmData, mapWithAIData.getSelected()); command.executeCommand(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> osmData.allNonDeletedPrimitives().size() == 5); assertEquals(5, osmData.allNonDeletedPrimitives().size(), "All primitives should now be in osmData"); assertTrue(mapWithAIData.allNonDeletedPrimitives().isEmpty(), "There should be no remaining non-deleted primitives"); command.undoCommand(); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> osmData.allNonDeletedPrimitives().size() == 3); assertEquals(3, osmData.allNonDeletedPrimitives().size(), "The DataSet should be in its original state"); assertEquals(3, mapWithAIData.allNonDeletedPrimitives().size(), "The DataSet should be in its original state"); } @@ -213,6 +220,12 @@ public class MapWithAIAddComandTest { mapWithAIData.addSelected(way); MapWithAIAddCommand command = new MapWithAIAddCommand(mapWithAIData, osmData, mapWithAIData.getSelected()); command.executeCommand(); + Awaitility.await().atMost(Durations.FIVE_SECONDS) + .until(() -> osmData.getPrimitiveById(176232378, OsmPrimitiveType.NODE) != null); + Awaitility.await().atMost(Durations.FIVE_SECONDS) + .until(() -> osmData.getPrimitiveById(176220609, OsmPrimitiveType.NODE) != null); + Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> osmData.getPrimitiveById(way) != null); + assertNotNull(osmData.getPrimitiveById(176232378, OsmPrimitiveType.NODE)); assertNotNull(osmData.getPrimitiveById(176220609, OsmPrimitiveType.NODE)); assertNotNull(osmData.getPrimitiveById(way)); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/MapWithAILayerInfoTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/MapWithAILayerInfoTest.java index ff429a1..5afbce0 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/MapWithAILayerInfoTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/MapWithAILayerInfoTest.java @@ -16,16 +16,20 @@ import com.github.tomakehurst.wiremock.WireMockServer; */ public class MapWithAILayerInfoTest { public static void setupMapWithAILayerInfo(WireMockServer wireMock) { - resetMapWithAILayerInfo(); - MapWithAILayerInfo.instance.getLayers().stream() - .forEach(i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMock, i.getUrl()))); - MapWithAILayerInfo.instance.save(); + synchronized (MapWithAILayerInfoTest.class) { + resetMapWithAILayerInfo(); + MapWithAILayerInfo.instance.getLayers().stream() + .forEach(i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMock, i.getUrl()))); + MapWithAILayerInfo.instance.save(); + } } public static void resetMapWithAILayerInfo() { - MapWithAILayerInfo.instance.clear(); - MapWithAILayerInfo.instance.getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry) - .forEach(MapWithAILayerInfo.instance::add); - MapWithAILayerInfo.instance.save(); + synchronized (MapWithAILayerInfoTest.class) { + MapWithAILayerInfo.instance.clear(); + MapWithAILayerInfo.instance.getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry) + .forEach(MapWithAILayerInfo.instance::add); + MapWithAILayerInfo.instance.save(); + } } }