From e58f941fbbc8a21e5bc077a21497625d1d1ad6d1 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 16 Dec 2019 11:19:34 -0700 Subject: [PATCH] Move to Assertions from Assert and add comments to asserts Signed-off-by: Taylor Smock --- .../mapwithai/MapWithAIPreferencesTest.java | 4 +- .../DetectTaskingManagerUtilsTest.java | 38 +++--- .../backend/GetDataRunnableTest.java | 6 +- .../backend/MapWithAIAvailabilityTest.java | 39 +++--- .../backend/MapWithAIDataUtilsTest.java | 99 +++++++-------- .../mapwithai/backend/MapWithAILayerTest.java | 63 ++++++---- .../backend/MapWithAIMoveActionTest.java | 42 ++++--- .../backend/MapWithAIUploadHookTest.java | 46 +++---- .../commands/AddNodeToWayCommandTest.java | 19 +-- .../CreateConnectionsCommandTest.java | 117 ++++++++++-------- .../commands/DeletePrimitivesCommandTest.java | 37 +++--- .../commands/MapWithAIAddComandTest.java | 78 ++++++------ 12 files changed, 312 insertions(+), 276 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java index 0da86ee..4d9ffb0 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java @@ -2,12 +2,12 @@ package org.openstreetmap.josm.plugins.mapwithai; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import javax.swing.SpinnerNumberModel; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -87,6 +87,6 @@ public class MapWithAIPreferencesTest { */ @Test public void testIsExpert() { - Assert.assertFalse(preferences.isExpert()); + assertFalse(preferences.isExpert(), "This is not an expert only preference panel"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DetectTaskingManagerUtilsTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DetectTaskingManagerUtilsTest.java index 0d1f881..48b86d6 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DetectTaskingManagerUtilsTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/DetectTaskingManagerUtilsTest.java @@ -1,7 +1,12 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapwithai.backend; -import org.junit.Assert; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.Rule; import org.junit.Test; import org.openstreetmap.josm.data.coor.LatLon; @@ -10,7 +15,6 @@ import org.openstreetmap.josm.data.gpx.WayPoint; import org.openstreetmap.josm.data.osm.BBox; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.layer.GpxLayer; -import org.openstreetmap.josm.plugins.mapwithai.backend.DetectTaskingManagerUtils; import org.openstreetmap.josm.testutils.JOSMTestRules; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -28,48 +32,50 @@ public class DetectTaskingManagerUtilsTest { @Test public void testHasTaskingManagerLayer() { - Assert.assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer()); + assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer(), "No TM layer exists yet"); MainApplication.getLayerManager().addLayer(new GpxLayer(new GpxData())); - Assert.assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer()); + assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer(), "No TM layer exists yet"); MainApplication.getLayerManager().addLayer(new GpxLayer(new GpxData(), LAYER_NAME)); - Assert.assertTrue(DetectTaskingManagerUtils.hasTaskingManagerLayer()); + assertTrue(DetectTaskingManagerUtils.hasTaskingManagerLayer(), "A TM layer exists yet"); } @Test public void testGetTaskingManagerLayer() { - Assert.assertNull(DetectTaskingManagerUtils.getTaskingManagerLayer()); + assertNull(DetectTaskingManagerUtils.getTaskingManagerLayer(), "No TM layer exists yet"); final GpxLayer layer = new GpxLayer(new GpxData(), LAYER_NAME); MainApplication.getLayerManager().addLayer(layer); - Assert.assertSame(layer, DetectTaskingManagerUtils.getTaskingManagerLayer()); + assertSame(layer, DetectTaskingManagerUtils.getTaskingManagerLayer(), "The TM layer was added"); } @Test public void testGetTaskingManagerBounds() { - Assert.assertFalse(DetectTaskingManagerUtils.getTaskingManagerBBox().isInWorld()); + assertFalse(DetectTaskingManagerUtils.getTaskingManagerBBox().isInWorld(), "No TM layer exists yet"); final GpxLayer layer = new GpxLayer(new GpxData(), LAYER_NAME); layer.data.addWaypoint(new WayPoint(new LatLon(0, 0))); MainApplication.getLayerManager().addLayer(layer); - Assert.assertEquals(0, DetectTaskingManagerUtils.getTaskingManagerBBox().height(), 0.000001); - Assert.assertEquals(0, DetectTaskingManagerUtils.getTaskingManagerBBox().width(), 0.000001); + assertEquals(0, DetectTaskingManagerUtils.getTaskingManagerBBox().height(), 0.000001, + "The TM layer only has one point"); + assertEquals(0, DetectTaskingManagerUtils.getTaskingManagerBBox().width(), 0.000001, + "The TM layer only has one point"); layer.data.addWaypoint(new WayPoint(new LatLon(1, 1))); final BBox bbox = DetectTaskingManagerUtils.getTaskingManagerBBox(); - Assert.assertTrue(bbox.isInWorld()); - Assert.assertTrue(bbox.getBottomRight().equalsEpsilon(new LatLon(0, 1))); - Assert.assertTrue(bbox.getTopLeft().equalsEpsilon(new LatLon(1, 0))); + assertTrue(bbox.isInWorld(), "A TM layer exists"); + assertTrue(bbox.getBottomRight().equalsEpsilon(new LatLon(0, 1)), "The bottom right should be at (0, 1)"); + assertTrue(bbox.getTopLeft().equalsEpsilon(new LatLon(1, 0)), "The top left should be at (1, 0)"); } @Test public void testCreateTaskingManagerGpxBounds() { - Assert.assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer()); + assertFalse(DetectTaskingManagerUtils.hasTaskingManagerLayer(), "No TM layer exists yet"); final BBox bbox = new BBox(0, 0, 1, 1); MainApplication.getLayerManager() .addLayer(new GpxLayer(DetectTaskingManagerUtils.createTaskingManagerGpxData(bbox), DetectTaskingManagerUtils.MAPWITHAI_CROP_AREA)); - Assert.assertTrue(DetectTaskingManagerUtils.hasTaskingManagerLayer()); - Assert.assertTrue(DetectTaskingManagerUtils.getTaskingManagerBBox().bounds(bbox)); + assertTrue(DetectTaskingManagerUtils.hasTaskingManagerLayer(), "A TM layer exists"); + assertTrue(DetectTaskingManagerUtils.getTaskingManagerBBox().bounds(bbox), "The TM layer should bound itself"); } } 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 b046aa4..0afd492 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnableTest.java @@ -2,12 +2,12 @@ package org.openstreetmap.josm.plugins.mapwithai.backend; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import java.util.Collections; 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 91cc280..398414d 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java @@ -3,10 +3,11 @@ package org.openstreetmap.josm.plugins.mapwithai.backend; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.awaitility.Durations; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -46,30 +47,32 @@ public class MapWithAIAvailabilityTest { @Test public void testHasDataBBox() { - Assert.assertFalse(instance.hasData(new BBox(0, 0, 0.1, 0.1))); - Assert.assertTrue(instance.hasData(new BBox(-99.9, 39.9, 100.1, 40.1))); + assertFalse(instance.hasData(new BBox(0, 0, 0.1, 0.1)), "There shouldn't be data in the ocean"); + assertTrue(instance.hasData(new BBox(-99.9, 39.9, 100.1, 40.1)), "There should be data in the US"); } @Test public void testHasDataLatLon() { - Assert.assertFalse(instance.hasData(new LatLon(0, 0))); - Assert.assertTrue(instance.hasData(new LatLon(40, -100))); - Assert.assertTrue(instance.hasData(new LatLon(45.424722, -75.695))); - Assert.assertTrue(instance.hasData(new LatLon(19.433333, -99.133333))); + assertFalse(instance.hasData(new LatLon(0, 0)), "There should not be data in the ocean"); + assertTrue(instance.hasData(new LatLon(40, -100)), "There should be data in the US"); + assertTrue(instance.hasData(new LatLon(45.424722, -75.695)), "There should be data in Canada"); + assertTrue(instance.hasData(new LatLon(19.433333, -99.133333)), "There should be data in Mexico"); } @Test public void testgetDataLatLon() { - Assert.assertTrue(DataAvailability.getDataTypes(new LatLon(0, 0)).isEmpty()); - Assert.assertTrue(DataAvailability.getDataTypes(new LatLon(40, -100)).getOrDefault("highway", false)); - Assert.assertTrue(DataAvailability.getDataTypes(new LatLon(40, -100)).getOrDefault("building", false)); - Assert.assertFalse( - DataAvailability.getDataTypes(new LatLon(45.424722, -75.695)).getOrDefault("highway", false)); - Assert.assertTrue( - DataAvailability.getDataTypes(new LatLon(45.424722, -75.695)).getOrDefault("building", false)); - Assert.assertTrue( - DataAvailability.getDataTypes(new LatLon(19.433333, -99.133333)).getOrDefault("highway", false)); - Assert.assertFalse( - DataAvailability.getDataTypes(new LatLon(19.433333, -99.133333)).getOrDefault("building", false)); + assertTrue(DataAvailability.getDataTypes(new LatLon(0, 0)).isEmpty(), "There should not be data in the ocean"); + assertTrue(DataAvailability.getDataTypes(new LatLon(40, -100)).getOrDefault("highway", false), + "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"); + 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), + "Mexico does not yet have building data"); } } 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 5cf8c64..0c3a3a8 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtilsTest.java @@ -2,8 +2,12 @@ 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.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -14,7 +18,6 @@ import java.util.stream.Collectors; import org.awaitility.Awaitility; import org.awaitility.Durations; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -78,7 +81,7 @@ public class MapWithAIDataUtilsTest { public void testGetData() { final BBox testBBox = getTestBBox(); final DataSet ds = new DataSet(MapWithAIDataUtils.getData(testBBox)); - Assert.assertEquals(1, ds.getWays().size()); + assertEquals(1, ds.getWays().size(), "There should only be one way in the testBBox"); } /** @@ -95,10 +98,10 @@ public class MapWithAIDataUtilsTest { final DataSet originalData = MapWithAIDataUtils.getData(testBBox); MainApplication.getLayerManager().addLayer(gpx); final DataSet ds = MapWithAIDataUtils.getData(testBBox); - Assert.assertEquals(1, ds.getWays().size()); - Assert.assertEquals(3, ds.getNodes().size()); - Assert.assertEquals(1, originalData.getWays().size()); - Assert.assertEquals(4, originalData.getNodes().size()); + assertEquals(1, ds.getWays().size(), "There should only be one way in the cropped testBBox"); + assertEquals(3, ds.getNodes().size(), "There should be three nodes in the cropped testBBox"); + assertEquals(1, originalData.getWays().size(), "There should be one way in the testBBox"); + assertEquals(4, originalData.getNodes().size(), "There should be four nodes in the testBBox"); } @Test @@ -108,9 +111,9 @@ public class MapWithAIDataUtilsTest { final DataSet ds = new DataSet(way1.firstNode(), way1.lastNode(), way1); final String source = "random source"; - Assert.assertNull(way1.get("source")); + assertNull(way1.get("source"), "The source for the data should not be null"); MapWithAIDataUtils.addSourceTags(ds, "highway", source); - Assert.assertEquals(source, way1.get("source")); + assertEquals(source, way1.get("source"), "The source for the data should be the specified source"); } public static BBox getTestBBox() { @@ -125,9 +128,8 @@ public class MapWithAIDataUtilsTest { final Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(0, 0.1))); final Collection collection = new TreeSet<>(); - Assert.assertEquals(0, collection.size()); MapWithAIDataUtils.addPrimitivesToCollection(collection, Collections.singletonList(way1)); - Assert.assertEquals(3, collection.size()); + assertEquals(3, collection.size(), "The way has two child primitives, for a total of three primitives"); } @Test @@ -140,9 +142,9 @@ public class MapWithAIDataUtilsTest { } ds1.addPrimitive(way1); - Assert.assertEquals(3, ds1.allPrimitives().size()); + assertEquals(3, ds1.allPrimitives().size(), "The DataSet should have three primitives"); MapWithAIDataUtils.removePrimitivesFromDataSet(Collections.singleton(way1)); - Assert.assertEquals(0, ds1.allPrimitives().size()); + assertEquals(0, ds1.allPrimitives().size(), "All of the primitives should have been removed from the DataSet"); } @Test @@ -152,64 +154,53 @@ public class MapWithAIDataUtilsTest { .until(() -> !MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); List paintStyles = MapPaintStyles.getStyles().getStyleSources(); int initialSize = paintStyles.size(); - Assert.assertEquals(initialSize, paintStyles.size()); - MapWithAIDataUtils.addMapWithAIPaintStyles(); - paintStyles = MapPaintStyles.getStyles().getStyleSources(); - Assert.assertEquals(initialSize + 1, paintStyles.size()); - MapWithAIDataUtils.addMapWithAIPaintStyles(); - paintStyles = MapPaintStyles.getStyles().getStyleSources(); - Assert.assertEquals(initialSize + 1, paintStyles.size()); - MapWithAIDataUtils.addMapWithAIPaintStyles(); + for (int i = 0; i < 10; i++) { + MapWithAIDataUtils.addMapWithAIPaintStyles(); + paintStyles = MapPaintStyles.getStyles().getStyleSources(); + assertEquals(initialSize + 1, paintStyles.size(), + "The paintstyle should have been added, but only one of it"); + } } @Test public void testMapWithAIURLPreferences() { final String fakeUrl = "https://fake.url"; - Assert.assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() - .noneMatch(map -> fakeUrl.equals(map.get("url")))); + assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream().noneMatch( + map -> fakeUrl.equals(map.get("url"))), "fakeUrl shouldn't be in the current MapWithAI urls"); MapWithAIPreferenceHelper.setMapWithAIUrl("Fake", fakeUrl, true, true); - Assert.assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() - .anyMatch(map -> fakeUrl.equals(map.get("url")))); + assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() + .anyMatch(map -> fakeUrl.equals(map.get("url"))), "fakeUrl should have been added"); final List> urls = new ArrayList<>(MapWithAIPreferenceHelper.getMapWithAIURLs()); - Assert.assertEquals(2, urls.size()); + assertEquals(2, urls.size(), "There should be two urls (fakeUrl and the default)"); MapWithAIPreferenceHelper.setMapWithAIUrl("MapWithAI", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API, true, true); - Assert.assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() - .anyMatch(map -> MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API.equals(map.get("url")))); + assertTrue( + MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() + .anyMatch(map -> MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API.equals(map.get("url"))), + "The default URL should exist"); MapWithAIPreferenceHelper.setMapWithAIUrl("Fake2", fakeUrl, true, true); - Assert.assertEquals(1, MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() - .filter(map -> fakeUrl.equals(map.get("url"))).count()); + assertEquals(1, MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() + .filter(map -> fakeUrl.equals(map.get("url"))).count(), "There should only be one fakeUrl"); MapWithAIPreferenceHelper.setMapWithAIURLs(urls.parallelStream() .filter(map -> !fakeUrl.equalsIgnoreCase(map.getOrDefault("url", ""))).collect(Collectors.toList())); - Assert.assertEquals(1, MapWithAIPreferenceHelper.getMapWithAIUrl().size()); - Assert.assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() + assertEquals(1, MapWithAIPreferenceHelper.getMapWithAIUrl().size(), + "The MapWithAI URLs should have been reset (essentially)"); + assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream() .anyMatch(map -> getDefaultMapWithAIAPIForTest(MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API) - .equals(map.get("url")))); + .equals(map.get("url"))), + "The MapWithAI URLs should have been reset"); } @Test public void testSplitBounds() { final BBox bbox = new BBox(0, 0, 0.0001, 0.0001); - List bboxes = MapWithAIDataUtils.reduceBBoxSize(bbox); - Assert.assertEquals(getExpectedNumberOfBBoxes(bbox), bboxes.size()); - checkInBBox(bbox, bboxes); - - bbox.add(0.001, 0.001); - bboxes = MapWithAIDataUtils.reduceBBoxSize(bbox); - Assert.assertEquals(getExpectedNumberOfBBoxes(bbox), bboxes.size()); - checkInBBox(bbox, bboxes); - - bbox.add(0.01, 0.01); - bboxes = MapWithAIDataUtils.reduceBBoxSize(bbox); - Assert.assertEquals(getExpectedNumberOfBBoxes(bbox), bboxes.size()); - checkInBBox(bbox, bboxes); - checkBBoxesConnect(bbox, bboxes); - - bbox.add(0.1, 0.1); - bboxes = MapWithAIDataUtils.reduceBBoxSize(bbox); - Assert.assertEquals(getExpectedNumberOfBBoxes(bbox), bboxes.size()); - checkInBBox(bbox, bboxes); - checkBBoxesConnect(bbox, bboxes); + for (Double i : Arrays.asList(0.0001, 0.001, 0.01, 0.1)) { + bbox.add(i, i); + List bboxes = MapWithAIDataUtils.reduceBBoxSize(bbox); + assertEquals(getExpectedNumberOfBBoxes(bbox), bboxes.size(), "The bbox should be appropriately reduced"); + checkInBBox(bbox, bboxes); + checkBBoxesConnect(bbox, bboxes); + } } private static int getExpectedNumberOfBBoxes(BBox bbox) { @@ -222,7 +213,7 @@ public class MapWithAIDataUtilsTest { private static void checkInBBox(BBox bbox, Collection bboxes) { for (final BBox tBBox : bboxes) { - Assert.assertTrue(bbox.bounds(tBBox)); + assertTrue(bbox.bounds(tBBox), "The bboxes should all be inside the original bbox"); } } @@ -240,7 +231,7 @@ public class MapWithAIDataUtilsTest { if (!bboxFoundConnections) { bboxFoundConnections = bboxCheckConnections(bbox1, originalBBox); } - Assert.assertTrue(bboxFoundConnections); + assertTrue(bboxFoundConnections, "The bbox should connect to other bboxes"); } } 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 4328d2d..ce223c5 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java @@ -3,6 +3,12 @@ package org.openstreetmap.josm.plugins.mapwithai.backend; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.openstreetmap.josm.tools.I18n.tr; import java.awt.Component; @@ -16,7 +22,6 @@ import javax.swing.JPanel; import org.awaitility.Durations; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -70,7 +75,7 @@ public class MapWithAILayerTest { @Test public void testGetSource() { - Assert.assertNull(layer.getChangesetSourceTag()); + assertNull(layer.getChangesetSourceTag(), "The source tag should be null"); DataSet to = new DataSet(); DataSet from = new DataSet(); Way way = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); @@ -79,23 +84,25 @@ public class MapWithAILayerTest { way.put(GetDataRunnable.MAPWITHAI_SOURCE_TAG_KEY, MapWithAIPlugin.NAME); MapWithAIAddCommand command = new MapWithAIAddCommand(from, to, Collections.singleton(way)); UndoRedoHandler.getInstance().add(command); - Assert.assertNotNull(layer.getChangesetSourceTag()); - Assert.assertFalse(layer.getChangesetSourceTag().trim().isEmpty()); - Assert.assertEquals(MapWithAIPlugin.NAME, layer.getChangesetSourceTag()); + assertNotNull(layer.getChangesetSourceTag(), "The source tag should not be null"); + assertFalse(layer.getChangesetSourceTag().trim().isEmpty(), "The source tag should not be an empty string"); + assertEquals(MapWithAIPlugin.NAME, layer.getChangesetSourceTag(), + "The source tag should be the plugin name (by default)"); } @Test public void testGetInfoComponent() { final Object tObject = layer.getInfoComponent(); - Assert.assertTrue(tObject instanceof JPanel); + assertTrue(tObject instanceof JPanel, "The info component should be a JPanel instead of a string"); JPanel jPanel = (JPanel) tObject; final List startComponents = Arrays.asList(jPanel.getComponents()); for (final Component comp : startComponents) { final JLabel label = (JLabel) comp; - Assert.assertFalse(label.getText().contains("URL")); - Assert.assertFalse(label.getText().contains("Maximum Additions")); - Assert.assertFalse(label.getText().contains("Switch Layers")); + assertFalse(label.getText().contains("URL"), "The layer doesn't have a custom URL"); + assertFalse(label.getText().contains("Maximum Additions"), "The layer doesn't have its own max additions"); + assertFalse(label.getText().contains("Switch Layers"), + "The layer doesn't have its own switchlayer boolean"); } layer.setMapWithAIUrl("bad_url"); @@ -108,11 +115,13 @@ public class MapWithAILayerTest { for (final Component comp : currentComponents) { final JLabel label = (JLabel) comp; if (label.getText().contains("URL")) { - Assert.assertEquals(tr("URL: {0}", "bad_url"), label.getText()); + assertEquals(tr("URL: {0}", "bad_url"), label.getText(), "The layer should have the bad_url set"); } else if (label.getText().contains("Maximum Additions")) { - Assert.assertEquals(tr("Maximum Additions: {0}", 0), label.getText()); + assertEquals(tr("Maximum Additions: {0}", 0), label.getText(), + "The layer should have max additions set"); } else if (label.getText().contains("Switch Layers")) { - Assert.assertEquals(tr("Switch Layers: {0}", false), label.getText()); + assertEquals(tr("Switch Layers: {0}", false), label.getText(), + "The layer should have switchlayers set"); } } } @@ -120,16 +129,16 @@ public class MapWithAILayerTest { @Test public void testGetLayer() { Layer mapWithAILayer = MapWithAIDataUtils.getLayer(false); - Assert.assertNull(mapWithAILayer); + assertNull(mapWithAILayer, "There should be no MapWithAI layer yet"); mapWithAILayer = MapWithAIDataUtils.getLayer(true); - Assert.assertEquals(MapWithAILayer.class, mapWithAILayer.getClass()); + assertEquals(MapWithAILayer.class, mapWithAILayer.getClass(), + "The MapWithAI layer should be of the MapWithAILayer.class"); - Layer tMapWithAI = MapWithAIDataUtils.getLayer(false); - Assert.assertSame(mapWithAILayer, tMapWithAI); - - tMapWithAI = MapWithAIDataUtils.getLayer(true); - Assert.assertSame(mapWithAILayer, tMapWithAI); + for (Boolean create : Arrays.asList(Boolean.FALSE, Boolean.TRUE)) { + Layer tMapWithAI = MapWithAIDataUtils.getLayer(create); + assertSame(mapWithAILayer, tMapWithAI, "getLayer should always return the same layer"); + } } @Test @@ -139,27 +148,31 @@ public class MapWithAILayerTest { MainApplication.getLayerManager().addLayer(osm); MapWithAIDataUtils.getMapWithAIData(mapWithAILayer, osm); - Assert.assertTrue(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty()); + assertTrue(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty(), "There should be no data source yet"); osm.getDataSet().addDataSource(new DataSource(new Bounds(0, 0, 0.001, 0.001), "random test")); osm.lock(); MapWithAIDataUtils.getMapWithAIData(mapWithAILayer); - Assert.assertTrue(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty()); + assertTrue(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty(), + "There should be no data due to the lock"); osm.unlock(); MapWithAIDataUtils.getMapWithAIData(mapWithAILayer); await().atMost(Durations.TEN_SECONDS).until(() -> !mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty()); - Assert.assertFalse(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty()); - Assert.assertEquals(1, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count()); + assertFalse(mapWithAILayer.getDataSet().getDataSourceBounds().isEmpty(), "There should be a data source"); + assertEquals(1, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count(), + "There should only be one data source"); osm.getDataSet().addDataSource(new DataSource(new Bounds(-0.001, -0.001, 0, 0), "random test")); MapWithAIDataUtils.getMapWithAIData(mapWithAILayer); await().atMost(Durations.TEN_SECONDS).until( () -> mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count() == 2); - Assert.assertEquals(2, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count()); + assertEquals(2, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count(), + "There should be two data sources"); MapWithAIDataUtils.getMapWithAIData(mapWithAILayer); - Assert.assertEquals(2, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count()); + assertEquals(2, mapWithAILayer.getDataSet().getDataSourceBounds().parallelStream().distinct().count(), + "There should be two data sources"); } } 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 496a388..eca3199 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveActionTest.java @@ -1,7 +1,12 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapwithai.backend; -import org.junit.Assert; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +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.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -54,10 +59,11 @@ public class MapWithAIMoveActionTest { public void testMoveAction() { mapWithAIData.addSelected(way1); moveAction.actionPerformed(null); - Assert.assertEquals(osmLayer, MainApplication.getLayerManager().getActiveLayer()); - Assert.assertNotNull(osmLayer.getDataSet().getPrimitiveById(way1)); + assertEquals(osmLayer, MainApplication.getLayerManager().getActiveLayer(), + "Current layer should be the OMS layer"); + assertNotNull(osmLayer.getDataSet().getPrimitiveById(way1), "way1 should have been added to the OSM layer"); UndoRedoHandler.getInstance().undo(); - Assert.assertNull(osmLayer.getDataSet().getPrimitiveById(way1)); + assertNull(osmLayer.getDataSet().getPrimitiveById(way1), "way1 should have been removed from the OSM layer"); } @Test @@ -69,14 +75,16 @@ public class MapWithAIMoveActionTest { final DataSet ds = osmLayer.getDataSet(); moveAction.actionPerformed(null); - Assert.assertTrue( - ((Way) ds.getPrimitiveById(way1)).lastNode().equals(((Way) ds.getPrimitiveById(way2)).lastNode())); - Assert.assertFalse(((Way) ds.getPrimitiveById(way2)).lastNode().hasKey(DuplicateCommand.DUPE_KEY)); - Assert.assertFalse(((Way) ds.getPrimitiveById(way1)).lastNode().hasKey(DuplicateCommand.DUPE_KEY)); + 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.DUPE_KEY), + "The dupe key should no longer exist"); + assertFalse(((Way) ds.getPrimitiveById(way1)).lastNode().hasKey(DuplicateCommand.DUPE_KEY), + "The dupe key should no longer exist"); UndoRedoHandler.getInstance().undo(); - Assert.assertFalse(way2.lastNode().hasKey(DuplicateCommand.DUPE_KEY)); - Assert.assertTrue(way1.lastNode().hasKey(DuplicateCommand.DUPE_KEY)); + assertFalse(way2.lastNode().hasKey(DuplicateCommand.DUPE_KEY), "The dupe key should no longer exist"); + assertTrue(way1.lastNode().hasKey(DuplicateCommand.DUPE_KEY), "The dupe key should no longer exist"); } @Test @@ -88,14 +96,14 @@ public class MapWithAIMoveActionTest { mapWithAIData.addSelected(way1); moveAction.actionPerformed(null); - Assert.assertFalse(way2.lastNode().hasKey(ConnectedCommand.CONN_KEY)); - Assert.assertFalse(way2.firstNode().hasKey(ConnectedCommand.CONN_KEY)); - Assert.assertFalse(way2.getNode(1).hasKey(ConnectedCommand.CONN_KEY)); - Assert.assertTrue(way1.lastNode().isDeleted()); + assertFalse(way2.lastNode().hasKey(ConnectedCommand.CONN_KEY), "The conn key should have been removed"); + assertFalse(way2.firstNode().hasKey(ConnectedCommand.CONN_KEY), "The conn key should have been removed"); + assertFalse(way2.getNode(1).hasKey(ConnectedCommand.CONN_KEY), "The conn key should have been removed"); + assertTrue(way1.lastNode().isDeleted(), "way1 should be deleted when added"); UndoRedoHandler.getInstance().undo(); - Assert.assertFalse(way2.lastNode().hasKey(ConnectedCommand.CONN_KEY)); - Assert.assertTrue(way1.lastNode().hasKey(ConnectedCommand.CONN_KEY)); - Assert.assertFalse(way1.lastNode().isDeleted()); + assertFalse(way2.lastNode().hasKey(ConnectedCommand.CONN_KEY), "The conn key shouldn't exist"); + assertTrue(way1.lastNode().hasKey(ConnectedCommand.CONN_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/MapWithAIUploadHookTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java index 322e495..c391ab9 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIUploadHookTest.java @@ -3,6 +3,9 @@ */ package org.openstreetmap.josm.plugins.mapwithai.backend; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -11,7 +14,6 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.openstreetmap.josm.TestUtils; @@ -64,19 +66,20 @@ public class MapWithAIUploadHookTest { aiLayer.getDataSet().addPrimitive(way2); Map tags = new TreeMap<>(); - Assert.assertTrue(tags.isEmpty()); hook.modifyChangesetTags(tags); - Assert.assertTrue(tags.isEmpty()); + assertTrue(tags.isEmpty(), "Tags should be empty due to no primitives being added"); aiLayer.getDataSet().setSelected(way1); MapWithAIMoveAction action = new MapWithAIMoveAction(); action.actionPerformed(null); hook.modifyChangesetTags(tags); - Assert.assertEquals(2, tags.size()); - Assert.assertEquals(Integer.toString(1), tags.get("mapwithai")); - Assert.assertTrue( - Arrays.asList(tags.get("mapwithai:options").split(";")).contains("version=".concat(info.localversion))); + assertEquals(2, tags.size(), "Tags should not be empty due to adding primitives"); + assertEquals(Integer.toString(1), tags.get("mapwithai"), + "mapwithai should equal 1, due to adding one primitive"); + assertTrue( + Arrays.asList(tags.get("mapwithai:options").split(";")).contains("version=".concat(info.localversion)), + "The version should be the localversion"); MapWithAIPreferenceHelper.setMapWithAIUrl("False URL", "false-url", true, true); @@ -86,21 +89,21 @@ public class MapWithAIUploadHookTest { action.actionPerformed(null); hook.modifyChangesetTags(tags); - Assert.assertEquals(Integer.toString(2), tags.get("mapwithai")); - Assert.assertEquals(2, tags.size()); + assertEquals(Integer.toString(2), tags.get("mapwithai"), "Two objects have been added"); + assertEquals(2, tags.size(), "Tags should not be empty due to adding primitives"); List split = Arrays.asList(tags.get("mapwithai:options").split(";")); - Assert.assertEquals(2, split.size()); - Assert.assertTrue(split.contains("version=".concat(info.localversion))); - Assert.assertTrue(split.contains("url=false-url")); + assertEquals(2, split.size(), "There should be another option in mapwithai:options"); + assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version"); + assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag"); MapWithAIPreferenceHelper.setMaximumAddition(20, false); tags.clear(); hook.modifyChangesetTags(tags); split = Arrays.asList(tags.get("mapwithai:options").split(";")); - Assert.assertEquals(3, split.size()); - Assert.assertTrue(split.contains("version=".concat(info.localversion))); - Assert.assertTrue(split.contains("url=false-url")); - Assert.assertTrue(split.contains("maxadd=20")); + assertEquals(3, split.size(), "There should be three ; in mapwithai:options"); + assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version"); + assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag"); + assertTrue(split.contains("maxadd=20"), "The maxadd should be 20"); BBox tBBox = new BBox(1, 0, 0, 1); MainApplication.getLayerManager() @@ -110,10 +113,11 @@ public class MapWithAIUploadHookTest { tags.clear(); hook.modifyChangesetTags(tags); split = Arrays.asList(tags.get("mapwithai:options").split(";")); - Assert.assertEquals(4, split.size()); - Assert.assertTrue(split.contains("version=".concat(info.localversion))); - Assert.assertTrue(split.contains("url=false-url")); - Assert.assertTrue(split.contains("maxadd=20")); - Assert.assertTrue(split.contains("task=".concat(tBBox.toStringCSV(",")))); + assertEquals(4, split.size(), "There should be four ; in mapwithai:options"); + assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version"); + assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag"); + assertTrue(split.contains("maxadd=20"), "The maxadd should be 20"); + assertTrue(split.contains("task=".concat(tBBox.toStringCSV(","))), + "There should be a task in the mapwithai:options"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/AddNodeToWayCommandTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/AddNodeToWayCommandTest.java index cfab40d..1d92112 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/AddNodeToWayCommandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/AddNodeToWayCommandTest.java @@ -2,11 +2,12 @@ package org.openstreetmap.josm.plugins.mapwithai.commands; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -39,23 +40,23 @@ public class AddNodeToWayCommandTest { @Test public void testAddNodeToWay() { command.executeCommand(); - Assert.assertEquals(3, way.getNodesCount()); + assertEquals(3, way.getNodesCount(), "A node should have been added to the way"); command.undoCommand(); - Assert.assertEquals(2, way.getNodesCount()); + assertEquals(2, way.getNodesCount(), "The way should no longer be modified"); command = new AddNodeToWayCommand(toAdd, way, way.lastNode(), way.firstNode()); command.executeCommand(); - Assert.assertEquals(3, way.getNodesCount()); + assertEquals(3, way.getNodesCount(), "A node should have been added to the way"); command.undoCommand(); - Assert.assertEquals(2, way.getNodesCount()); + assertEquals(2, way.getNodesCount(), "The way should no longer be modified"); } @Test public void testDescription() { - Assert.assertNotNull(command.getDescriptionText()); + assertNotNull(command.getDescriptionText(), "The command should have a description"); } @Test @@ -64,9 +65,9 @@ public class AddNodeToWayCommandTest { final List modified = new ArrayList<>(); final List deleted = new ArrayList<>(); command.fillModifiedData(modified, deleted, added); - Assert.assertTrue(deleted.isEmpty()); - Assert.assertTrue(added.isEmpty()); - Assert.assertEquals(2, modified.size()); + assertTrue(deleted.isEmpty(), "Nothing should have been deleted"); + assertTrue(added.isEmpty(), "Nothing should have been added"); + assertEquals(2, modified.size(), "The way should have been modified, node is included in count"); } @Test 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 3bca9c0..8966aa0 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/CreateConnectionsCommandTest.java @@ -2,13 +2,17 @@ package org.openstreetmap.josm.plugins.mapwithai.commands; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +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 java.util.ArrayList; import java.util.Collection; import java.util.Collections; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -74,42 +78,43 @@ public class CreateConnectionsCommandTest { Collections.singleton(node3)); createConnections.executeCommand(); - Assert.assertFalse(dataSet.isModified()); + assertFalse(dataSet.isModified(), "DataSet shouldn't be modified yet"); createConnections.undoCommand(); - Assert.assertFalse(dataSet.isModified()); + assertFalse(dataSet.isModified(), "DataSet shouldn't be modified yet"); node3.put(ConnectedCommand.CONN_KEY, "w" + way.getUniqueId() + ",n" + node1.getUniqueId() + ",n" + node2.getUniqueId()); createConnections = new CreateConnectionsCommand(dataSet, Collections.singleton(node3)); createConnections.executeCommand(); - Assert.assertTrue(dataSet.isModified()); - Assert.assertEquals(3, way.getNodesCount()); - Assert.assertFalse(node3.hasKey(ConnectedCommand.CONN_KEY)); + assertTrue(dataSet.isModified(), "DataSet should be modified"); + assertEquals(3, way.getNodesCount(), "The way should have three nodes"); + assertFalse(node3.hasKey(ConnectedCommand.CONN_KEY), "There should be no conn key"); createConnections.fillModifiedData(modified, deleted, added); - Assert.assertEquals(3, modified.size()); // 3 since we remove the key from the node - Assert.assertTrue(deleted.isEmpty()); - Assert.assertTrue(added.isEmpty()); + assertEquals(3, modified.size(), + "There should be three modifications (the connecting way, the node, and the node again for key removal)"); + assertTrue(deleted.isEmpty(), "Nothing has been deleted"); + assertTrue(added.isEmpty(), "Nothing has been added"); createConnections.undoCommand(); - Assert.assertFalse(dataSet.isModified()); - Assert.assertEquals(2, way.getNodesCount()); - Assert.assertTrue(node3.hasKey(ConnectedCommand.CONN_KEY)); + assertFalse(dataSet.isModified(), "DataSet is no longer modified"); + assertEquals(2, way.getNodesCount(), "The way should have two nodes again"); + assertTrue(node3.hasKey(ConnectedCommand.CONN_KEY), "The conn key should exist again"); dupe.put(DuplicateCommand.DUPE_KEY, "n" + node1.getUniqueId()); createConnections = new CreateConnectionsCommand(dataSet, Collections.singleton(dupe)); createConnections.executeCommand(); - Assert.assertTrue(dataSet.isModified()); - Assert.assertEquals(2, way.getNodesCount()); - Assert.assertFalse(node1.hasKey(DuplicateCommand.DUPE_KEY)); + assertTrue(dataSet.isModified(), "The DataSet should be modified"); + assertEquals(2, way.getNodesCount(), "The way should have two nodes"); + assertFalse(node1.hasKey(DuplicateCommand.DUPE_KEY), "There should no longer be a dupe key"); modified.clear(); createConnections.fillModifiedData(modified, deleted, added); - Assert.assertEquals(2, modified.size()); - Assert.assertTrue(deleted.isEmpty()); - Assert.assertTrue(added.isEmpty()); + assertEquals(2, modified.size(), "We removed a node and modified a way"); + assertTrue(deleted.isEmpty(), "Nothing was truly deleted (the dupe node doesn't count)"); + assertTrue(added.isEmpty(), "Nothing was added"); createConnections.undoCommand(); - Assert.assertFalse(node1.hasKey(DuplicateCommand.DUPE_KEY)); - Assert.assertTrue(dupe.hasKey(DuplicateCommand.DUPE_KEY)); - Assert.assertFalse(dataSet.isModified()); - Assert.assertEquals(2, way.getNodesCount()); + assertFalse(node1.hasKey(DuplicateCommand.DUPE_KEY), "The original node should not have the dupe key"); + assertTrue(dupe.hasKey(DuplicateCommand.DUPE_KEY), "The dupe node should have the dupe key"); + assertFalse(dataSet.isModified(), "The DataSet is no longer modified"); + assertEquals(2, way.getNodesCount(), "The way still has two nodes"); } /** @@ -118,32 +123,32 @@ public class CreateConnectionsCommandTest { */ @Test public void testAddNodesToWay() { - final Node node1 = new Node(new LatLon(0, 0)); - final Node node2 = new Node(new LatLon(1, 0)); - final Node node3 = new Node(new LatLon(0.5, 0)); - final Way way = TestUtils.newWay("highway=residential", node1, node2); - new DataSet(node1, node2, node3, way); - Command addNodeToWayCommand = ConnectedCommand.addNodesToWay(node3, way, node1, node2); - Assert.assertEquals(2, way.getNodesCount()); + final Node wayNode1 = new Node(new LatLon(0, 0)); + final Node wayNode2 = new Node(new LatLon(1, 0)); + final Node toAddNode = new Node(new LatLon(0.5, 0)); + final Way way = TestUtils.newWay("highway=residential", wayNode1, wayNode2); + new DataSet(wayNode1, wayNode2, toAddNode, way); + Command addNodeToWayCommand = ConnectedCommand.addNodesToWay(toAddNode, way, wayNode1, wayNode2); + assertEquals(2, way.getNodesCount(), "The way should still have 2 nodes"); addNodeToWayCommand.executeCommand(); - Assert.assertEquals(3, way.getNodesCount()); + assertEquals(3, way.getNodesCount(), "The way should now have 3 nodes"); addNodeToWayCommand.undoCommand(); - Assert.assertEquals(2, way.getNodesCount()); + assertEquals(2, way.getNodesCount(), "The way should be back to having 2 nodes"); - node2.setCoor(new LatLon(1, 0.1)); - addNodeToWayCommand = ConnectedCommand.addNodesToWay(node3, way, node1, node2); - Assert.assertNull(addNodeToWayCommand); + wayNode2.setCoor(new LatLon(1, 0.1)); + addNodeToWayCommand = ConnectedCommand.addNodesToWay(toAddNode, way, wayNode1, wayNode2); + assertNull(addNodeToWayCommand, "There shouldn't be a command to for a node that is a fair distance away"); - node2.setCoor(new LatLon(1, 0.01)); - addNodeToWayCommand = ConnectedCommand.addNodesToWay(node3, way, node1, node2); - Assert.assertNull(addNodeToWayCommand); + wayNode2.setCoor(new LatLon(1, 0.01)); + addNodeToWayCommand = ConnectedCommand.addNodesToWay(toAddNode, way, wayNode1, wayNode2); + assertNull(addNodeToWayCommand, "There shouldn't be a command to for a node that is a fair distance away"); - node2.setCoor(new LatLon(1, 0.00008)); - addNodeToWayCommand = ConnectedCommand.addNodesToWay(node3, way, node1, node2); + wayNode2.setCoor(new LatLon(1, 0.00008)); + addNodeToWayCommand = ConnectedCommand.addNodesToWay(toAddNode, way, wayNode1, wayNode2); addNodeToWayCommand.executeCommand(); - Assert.assertEquals(3, way.getNodesCount()); + assertEquals(3, way.getNodesCount(), "The way should have 3 nodes now"); addNodeToWayCommand.undoCommand(); - Assert.assertEquals(2, way.getNodesCount()); + assertEquals(2, way.getNodesCount(), "The way should now have 2 nodes"); } /** @@ -156,12 +161,13 @@ public class CreateConnectionsCommandTest { new DataSet(node1, node2); final Command replaceNodeCommand = DuplicateCommand.replaceNode(node1, node2); replaceNodeCommand.executeCommand(); - Assert.assertTrue(node1.isDeleted()); + assertTrue(node1.isDeleted(), "The node should not exist anymore"); replaceNodeCommand.undoCommand(); - Assert.assertFalse(node1.isDeleted()); + assertFalse(node1.isDeleted(), "The node should exist again"); node2.setCoor(new LatLon(0.1, 0.1)); - Assert.assertNull(DuplicateCommand.replaceNode(node1, node2)); + assertNull(DuplicateCommand.replaceNode(node1, node2), + "There should not be a command for nodes with a large distance"); } /** @@ -175,12 +181,14 @@ public class CreateConnectionsCommandTest { Command replaceNodeCommand = CreateConnectionsCommand.createConnections(dataSet, Collections.singleton(node1)); replaceNodeCommand.executeCommand(); - Assert.assertEquals(1, dataSet.allNonDeletedPrimitives().size()); - Assert.assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE)); + assertEquals(1, dataSet.allNonDeletedPrimitives().size(), "There should be one primitive left"); + assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE), + "The OSM primitive should be the remaining primitive"); replaceNodeCommand.undoCommand(); - Assert.assertEquals(2, dataSet.allNonDeletedPrimitives().size()); // We don't roll back downloaded data - Assert.assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE)); + assertEquals(2, dataSet.allNonDeletedPrimitives().size(), "We don't roll back downloaded data"); + assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE), + "We don't roll back downloaded data"); node1.setCoor(new LatLon(39.067399, -108.5608433)); node1.put(DuplicateCommand.DUPE_KEY, "n6151680832"); @@ -189,12 +197,13 @@ public class CreateConnectionsCommandTest { replaceNodeCommand = CreateConnectionsCommand.createConnections(dataSet, Collections.singleton(node1)); replaceNodeCommand.executeCommand(); - Assert.assertEquals(2, dataSet.allNonDeletedPrimitives().size()); - Assert.assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE)); + assertEquals(2, dataSet.allNonDeletedPrimitives().size(), "The dupe node no longer matches with the OSM node"); + assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE), "The OSM node should still exist"); replaceNodeCommand.undoCommand(); - Assert.assertEquals(3, dataSet.allNonDeletedPrimitives().size()); // We don't roll back downloaded data - Assert.assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE)); + assertEquals(3, dataSet.allNonDeletedPrimitives().size(), "We don't roll back downloaded data"); + assertNotNull(dataSet.getPrimitiveById(6146500887L, OsmPrimitiveType.NODE), + "We don't roll back downloaded data"); } @@ -204,7 +213,7 @@ public class CreateConnectionsCommandTest { @Test public void testGetDescriptionText() { final String text = new CreateConnectionsCommand(new DataSet(), Collections.emptyList()).getDescriptionText(); - Assert.assertNotNull(text); - Assert.assertFalse(text.isEmpty()); + assertNotNull(text, "There should be a description for the command"); + assertFalse(text.isEmpty(), "The description should not be an empty string"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/DeletePrimitivesCommandTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/DeletePrimitivesCommandTest.java index d4cd5f1..e8012e7 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/DeletePrimitivesCommandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/DeletePrimitivesCommandTest.java @@ -1,9 +1,11 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapwithai.commands; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Collections; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.openstreetmap.josm.TestUtils; @@ -11,7 +13,6 @@ import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.data.osm.Node; import org.openstreetmap.josm.data.osm.Way; -import org.openstreetmap.josm.plugins.mapwithai.commands.DeletePrimitivesCommand; import org.openstreetmap.josm.testutils.JOSMTestRules; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -29,48 +30,46 @@ public class DeletePrimitivesCommandTest { way1.getNodes().forEach(node -> ds.addPrimitive(node)); ds.addPrimitive(way1); - Assert.assertTrue(ds.containsWay(way1)); - DeletePrimitivesCommand delete = new DeletePrimitivesCommand(ds, Collections.singleton(way1)); delete.executeCommand(); - Assert.assertTrue(way1.isDeleted()); - Assert.assertEquals(0, ds.allNonDeletedPrimitives().size()); + assertTrue(way1.isDeleted(), "The way should be deleted"); + assertEquals(0, ds.allNonDeletedPrimitives().size(), "There should be no non-deleted primitives"); delete.undoCommand(); - Assert.assertTrue(ds.containsWay(way1)); - Assert.assertEquals(3, ds.allNonDeletedPrimitives().size()); + assertTrue(ds.containsWay(way1), "The DataSet should contain way1"); + assertEquals(3, ds.allNonDeletedPrimitives().size(), "There should be three non-deleted primitives"); final Node tNode = new Node(new LatLon(0.1, 0.1)); ds.addPrimitive(tNode); - Assert.assertEquals(4, ds.allNonDeletedPrimitives().size()); delete.executeCommand(); - Assert.assertTrue(way1.isDeleted()); - Assert.assertEquals(1, ds.allNonDeletedPrimitives().size()); + assertTrue(way1.isDeleted(), "The way should be deleted"); + assertEquals(1, ds.allNonDeletedPrimitives().size(), "Non-relevant objects should not be affected"); delete.undoCommand(); - Assert.assertEquals(4, ds.allNonDeletedPrimitives().size()); + assertEquals(4, ds.allNonDeletedPrimitives().size(), "The DataSet should be as it was"); way1.firstNode().put("highway", "stop"); delete.executeCommand(); - Assert.assertTrue(way1.isDeleted()); - Assert.assertEquals(2, ds.allNonDeletedPrimitives().size()); + assertTrue(way1.isDeleted(), "The way should be deleted"); + assertEquals(2, ds.allNonDeletedPrimitives().size(), "Objects with their own keys should remain"); delete.undoCommand(); - Assert.assertTrue(way1.firstNode().hasKey("highway")); + assertTrue(way1.firstNode().hasKey("highway"), "Objects should not lose their keys"); delete = new DeletePrimitivesCommand(ds, Collections.singleton(way1), true); delete.executeCommand(); - Assert.assertTrue(way1.isDeleted()); - Assert.assertEquals(1, ds.allNonDeletedPrimitives().size()); + assertTrue(way1.isDeleted(), "The way should be deleted"); + assertEquals(1, ds.allNonDeletedPrimitives().size(), + "All nodes in the way not in another way should be removed"); delete.undoCommand(); - Assert.assertEquals(4, ds.allNonDeletedPrimitives().size()); + assertEquals(4, ds.allNonDeletedPrimitives().size(), "The DataSet should be as it was"); - Assert.assertTrue(way1.firstNode().hasKey("highway")); + assertTrue(way1.firstNode().hasKey("highway"), "Objects should not lose their keys"); } } 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 790d5fc..5196480 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MapWithAIAddComandTest.java @@ -2,13 +2,17 @@ package org.openstreetmap.josm.plugins.mapwithai.commands; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; 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 java.util.Arrays; import java.util.Collections; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -69,28 +73,27 @@ public class MapWithAIAddComandTest { } MapWithAIAddCommand command = new MapWithAIAddCommand(ds1, ds2, Arrays.asList(way1, way2)); command.executeCommand(); - Assert.assertNotNull(ds2.getPrimitiveById(way1)); - Assert.assertNotNull(ds2.getPrimitiveById(way1.firstNode())); - Assert.assertNotNull(ds2.getPrimitiveById(way1.lastNode())); - Assert.assertTrue(way1.isDeleted()); - Assert.assertNotNull(ds2.getPrimitiveById(way2)); - Assert.assertNotNull(ds2.getPrimitiveById(way2.firstNode())); - Assert.assertNotNull(ds2.getPrimitiveById(way2.lastNode())); - Assert.assertTrue(way2.isDeleted()); + for (Way way : Arrays.asList(way1, way2)) { + assertNotNull(ds2.getPrimitiveById(way), "DataSet should still contain object"); + assertNotNull(ds2.getPrimitiveById(way.firstNode()), "DataSet should still contain object"); + assertNotNull(ds2.getPrimitiveById(way.lastNode()), "DataSet should still contain object"); + assertTrue(way.isDeleted(), "The way should be deleted"); + } - Assert.assertNull(ds2.getPrimitiveById(way3)); + assertNull(ds2.getPrimitiveById(way3), "DataSet should not yet have way3"); command = new MapWithAIAddCommand(ds1, ds2, Arrays.asList(way3)); command.executeCommand(); - Assert.assertNotNull(ds2.getPrimitiveById(way3)); - Assert.assertNotNull(ds2.getPrimitiveById(way3.firstNode())); - Assert.assertNotNull(ds2.getPrimitiveById(way3.lastNode())); - Assert.assertTrue(way3.isDeleted()); + assertNotNull(ds2.getPrimitiveById(way3), "DataSet should still contain object"); + assertNotNull(ds2.getPrimitiveById(way3.firstNode()), "DataSet should still contain object"); + assertNotNull(ds2.getPrimitiveById(way3.lastNode()), "DataSet should still contain object"); + assertTrue(way3.isDeleted(), "The way should be deleted"); command.undoCommand(); - Assert.assertNull(ds2.getPrimitiveById(way3)); - Assert.assertNull(ds2.getPrimitiveById(way3.firstNode())); - Assert.assertNull(ds2.getPrimitiveById(way3.lastNode())); - Assert.assertFalse(ds1.getPrimitiveById(way3).isDeleted()); + assertNull(ds2.getPrimitiveById(way3), "DataSet should no longer contain object"); + assertNull(ds2.getPrimitiveById(way3.firstNode()), "DataSet should no longer contain object"); + assertNull(ds2.getPrimitiveById(way3.lastNode()), "DataSet should no longer contain object"); + assertFalse(ds1.getPrimitiveById(way3).isDeleted(), + "The way should no longer be deleted in its original DataSet"); } @Test @@ -109,8 +112,8 @@ public class MapWithAIAddComandTest { ds1.addPrimitive(way2); ds1.addPrimitive(way1); MapWithAIAddCommand.createConnections(ds1, Collections.singletonList(way2.firstNode())).executeCommand(); - Assert.assertEquals(3, way1.getNodesCount()); - Assert.assertFalse(way1.isFirstLastNode(way2.firstNode())); + assertEquals(3, way1.getNodesCount(), "The way should now have three nodes"); + assertFalse(way1.isFirstLastNode(way2.firstNode()), "The ways should be connected"); final Way way3 = TestUtils.newWay(HIGHWAY_RESIDENTIAL, new Node(new LatLon(0, 0)), new Node(new LatLon(-0.1, -0.1))); @@ -119,9 +122,9 @@ public class MapWithAIAddComandTest { ds1.addPrimitive(way3); final Node way3Node1 = way3.firstNode(); MapWithAIAddCommand.createConnections(ds1, Collections.singletonList(way3.firstNode())).executeCommand(); - Assert.assertNotEquals(way3Node1, way3.firstNode()); - Assert.assertEquals(way1.firstNode(), way3.firstNode()); - Assert.assertTrue(way3Node1.isDeleted()); + assertNotEquals(way3Node1, way3.firstNode(), "The original first node should no longer be the first node"); + assertEquals(way1.firstNode(), way3.firstNode(), "way1 and way3 should have the same first nodes"); + assertTrue(way3Node1.isDeleted(), "way3 should be deleted"); } @Test @@ -138,28 +141,27 @@ public class MapWithAIAddComandTest { mapWithAIData.addPrimitive(way1); mapWithAIData.setSelected(way1); - Assert.assertEquals(3, osmData.allNonDeletedPrimitives().size()); - Assert.assertEquals(3, mapWithAIData.allNonDeletedPrimitives().size()); - MapWithAIAddCommand command = new MapWithAIAddCommand(mapWithAIData, osmData, mapWithAIData.getSelected()); command.executeCommand(); - Assert.assertEquals(6, osmData.allNonDeletedPrimitives().size()); - Assert.assertTrue(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(); - Assert.assertEquals(3, osmData.allNonDeletedPrimitives().size()); - Assert.assertEquals(3, mapWithAIData.allNonDeletedPrimitives().size()); + 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"); final Tag dupe = new Tag("dupe", "n".concat(Long.toString(way2.lastNode().getUniqueId()))); way1.lastNode().put(dupe); command = new MapWithAIAddCommand(mapWithAIData, osmData, mapWithAIData.getSelected()); command.executeCommand(); - Assert.assertEquals(5, osmData.allNonDeletedPrimitives().size()); - Assert.assertTrue(mapWithAIData.allNonDeletedPrimitives().isEmpty()); + 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(); - Assert.assertEquals(3, osmData.allNonDeletedPrimitives().size()); - Assert.assertEquals(3, mapWithAIData.allNonDeletedPrimitives().size()); + 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"); } @Test @@ -178,12 +180,12 @@ public class MapWithAIAddComandTest { UndoRedoHandler.getInstance().add(new MoveCommand(tNode, LatLon.ZERO)); - Assert.assertTrue(UndoRedoHandler.getInstance().getRedoCommands().isEmpty()); - Assert.assertEquals(2, UndoRedoHandler.getInstance().getUndoCommands().size()); + assertTrue(UndoRedoHandler.getInstance().getRedoCommands().isEmpty(), "There shouldn't be any redo commands"); + assertEquals(2, UndoRedoHandler.getInstance().getUndoCommands().size(), "There should be two undo commands"); UndoRedoHandler.getInstance().undo(UndoRedoHandler.getInstance().getUndoCommands().size()); - Assert.assertTrue(UndoRedoHandler.getInstance().getUndoCommands().isEmpty()); - Assert.assertEquals(2, UndoRedoHandler.getInstance().getRedoCommands().size()); + assertTrue(UndoRedoHandler.getInstance().getUndoCommands().isEmpty(), "There should be no undo commands"); + assertEquals(2, UndoRedoHandler.getInstance().getRedoCommands().size(), "There should be two redo commands"); UndoRedoHandler.getInstance().redo(UndoRedoHandler.getInstance().getRedoCommands().size()); UndoRedoHandler.getInstance().undo(UndoRedoHandler.getInstance().getUndoCommands().size());