From 5aefb58eb67d5ec303a9318f9d0a99324369566e Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 16 Dec 2019 09:46:07 -0700 Subject: [PATCH] Fix some PMD issues in tests Signed-off-by: Taylor Smock --- .../mapwithai/MapWithAIPluginTest.java | 33 ++-- .../mapwithai/MapWithAIPreferencesTest.java | 31 ++-- .../MapWithAIURLPreferenceTableTest.java | 43 ++--- .../plugins/mapwithai/UpdateProdTest.java | 6 +- .../commands/MergeDuplicateWaysTest.java | 152 ++++++++++-------- 5 files changed, 150 insertions(+), 115 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java index 2c55996..39c9bf6 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java @@ -2,6 +2,8 @@ package org.openstreetmap.josm.plugins.mapwithai; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -13,7 +15,6 @@ import javax.swing.JMenu; 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; @@ -66,7 +67,8 @@ public class MapWithAIPluginTest { @Test public void testGetPreferenceSetting() { plugin = new MapWithAIPlugin(info); - Assert.assertTrue(plugin.getPreferenceSetting() instanceof MapWithAIPreferences); + assertTrue(plugin.getPreferenceSetting() instanceof MapWithAIPreferences, + "We didn't get the expected Preference class"); } /** @@ -78,27 +80,34 @@ public class MapWithAIPluginTest { final JMenu dataMenu = MainApplication.getMenu().dataMenu; final int dataMenuSize = dataMenu.getMenuComponentCount(); plugin = new MapWithAIPlugin(info); - Assert.assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount()); - Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() - .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount(), "Menu items were not added"); + assertEquals(1, + MapPaintStyles.getStyles().getStyleSources().parallelStream() + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count(), + "The paint style was not added"); for (boolean existed : Arrays.asList(false, true)) { // false, true order is important plugin = new MapWithAIPlugin(info); Config.getPref().putBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS, existed); plugin.destroy(); - Assert.assertEquals(dataMenuSize, dataMenu.getMenuComponentCount()); + assertEquals(dataMenuSize, dataMenu.getMenuComponentCount(), + "Menu items were added after they were already added"); Awaitility.await().atMost(Durations.FIVE_SECONDS) .until(() -> existed == MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); - Assert.assertEquals(Config.getPref().getBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS) ? 1 : 0, + assertEquals(Config.getPref().getBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS) ? 1 : 0, MapPaintStyles.getStyles().getStyleSources().parallelStream() - .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count(), + "The paint style was added multiple times"); } for (int i = 0; i < 3; i++) { plugin = new MapWithAIPlugin(info); - Assert.assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount()); - Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() - .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); + assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount(), + "The menu items were added multiple times"); + assertEquals(1, + MapPaintStyles.getStyles().getStyleSources().parallelStream() + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count(), + "The paint style was added multiple times"); } } @@ -108,7 +117,7 @@ public class MapWithAIPluginTest { @Test public void testGetVersionInfo() { plugin = new MapWithAIPlugin(info); // needs to be called for version info to be initialized. - Assert.assertEquals(VERSION, MapWithAIPlugin.getVersionInfo()); + assertEquals(VERSION, MapWithAIPlugin.getVersionInfo(), "We didn't get the expected version"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java index f874013..0da86ee 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPreferencesTest.java @@ -1,6 +1,10 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapwithai; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import javax.swing.SpinnerNumberModel; import org.junit.Assert; @@ -43,32 +47,39 @@ public class MapWithAIPreferencesTest { preferences.addGui(pane); - Assert.assertEquals(tabs + 1, pane.getPluginPreference().getTabPane().getTabCount()); - Assert.assertEquals(pane.getPluginPreference(), preferences.getTabPreferenceSetting(pane)); + assertEquals(tabs + 1, pane.getPluginPreference().getTabPane().getTabCount(), "Preferences wasn't added"); + assertEquals(pane.getPluginPreference(), preferences.getTabPreferenceSetting(pane), + "The expected parent of the settings panel was different"); final boolean switchLayers = MapWithAIPreferenceHelper.isSwitchLayers(); - Assert.assertEquals(switchLayers, preferences.getSwitchLayerCheckBox().isSelected()); + assertEquals(switchLayers, preferences.getSwitchLayerCheckBox().isSelected(), + "The default for switching layers is true"); preferences.ok(); - Assert.assertEquals(switchLayers, MapWithAIPreferenceHelper.isSwitchLayers()); + assertEquals(switchLayers, MapWithAIPreferenceHelper.isSwitchLayers(), + "The default for switching layers is true"); preferences.getSwitchLayerCheckBox().setSelected(!switchLayers); - Assert.assertNotEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers()); + assertNotEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers(), "OK hasn't been selected yet"); preferences.ok(); - Assert.assertEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers()); + assertEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers(), + "We deselected switchLayers, so it should be off"); final Object tmp = preferences.getMaximumAdditionSpinner().getModel(); SpinnerNumberModel spinnerModel = null; if (tmp instanceof SpinnerNumberModel) { spinnerModel = (SpinnerNumberModel) tmp; } - Assert.assertNotNull(spinnerModel); + assertNotNull(spinnerModel, "The spinner model should be a SpinnerNumberModel"); final Number currentNumber = MapWithAIPreferenceHelper.getMaximumAddition(); - Assert.assertEquals(currentNumber.intValue(), spinnerModel.getNumber().intValue()); + assertEquals(currentNumber.intValue(), spinnerModel.getNumber().intValue(), + "The default additions should be the current setting"); spinnerModel.setValue(currentNumber.intValue() + 3); - Assert.assertNotEquals(spinnerModel.getNumber().intValue(), MapWithAIPreferenceHelper.getMaximumAddition()); + assertNotEquals(spinnerModel.getNumber().intValue(), MapWithAIPreferenceHelper.getMaximumAddition(), + "We've increased the max add by three, but have not selected OK, so it should still be the default"); preferences.ok(); - Assert.assertEquals(spinnerModel.getNumber().intValue(), MapWithAIPreferenceHelper.getMaximumAddition()); + assertEquals(spinnerModel.getNumber().intValue(), MapWithAIPreferenceHelper.getMaximumAddition(), + "OK has been selected, so the max adds have been updated"); } /** diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIURLPreferenceTableTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIURLPreferenceTableTest.java index ae16cd8..34476ec 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIURLPreferenceTableTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIURLPreferenceTableTest.java @@ -34,20 +34,20 @@ public class MapWithAIURLPreferenceTableTest { public void testMapWithAIURLPreferenceTable() { List dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); - assertEquals(4, table.getModel().getColumnCount()); - assertEquals(1, table.getModel().getRowCount()); - assertFalse(dataUrls.isEmpty()); - assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), - table.getModel().getValueAt(0, 0)); + assertEquals(4, table.getModel().getColumnCount(), "There should be four columns"); + assertEquals(1, table.getModel().getRowCount(), "There is only one entry"); + assertFalse(dataUrls.isEmpty(), "The backing list should not be empty"); + assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0), + "The backing map and the table should have the same entries"); dataUrls.add(0, new DataUrl("no-source", "no-url", true)); table.fireDataChanged(); - assertEquals(4, table.getModel().getColumnCount()); - assertEquals(2, table.getModel().getRowCount()); - assertFalse(dataUrls.isEmpty()); - assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), - table.getModel().getValueAt(0, 0)); + assertEquals(4, table.getModel().getColumnCount(), "The column count should not change"); + assertEquals(2, table.getModel().getRowCount(), "An additional DataUrl was added"); + assertFalse(dataUrls.isEmpty(), "The backing list should not be empty"); + assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0), + "The backing map and table should have the same entries"); } /** @@ -61,10 +61,10 @@ public class MapWithAIURLPreferenceTableTest { map.put("timeout", Integer.toString(50)); map.put("maxnodedistance", Double.toString(1.2)); Map objectifiedMap = MapWithAIURLPreferenceTable.objectify(map); - assertTrue(objectifiedMap.get("source") instanceof String); - assertTrue(objectifiedMap.get("enabled") instanceof Boolean); - assertTrue(objectifiedMap.get("timeout") instanceof Integer); - assertTrue(objectifiedMap.get("maxnodedistance") instanceof Double); + assertTrue(objectifiedMap.get("source") instanceof String, "Source should be a string"); + assertTrue(objectifiedMap.get("enabled") instanceof Boolean, "Enabled should be a boolean"); + assertTrue(objectifiedMap.get("timeout") instanceof Integer, "Timeout should be an integer"); + assertTrue(objectifiedMap.get("maxnodedistance") instanceof Double, "Maxnodedistance should be a double"); } /** @@ -75,8 +75,9 @@ public class MapWithAIURLPreferenceTableTest { List dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); table.addRowSelectionInterval(0, 0); - assertTrue(table.getSelectedItems().parallelStream().allMatch(dataUrls::contains)); - assertEquals(1, table.getSelectedItems().size()); + assertTrue(table.getSelectedItems().parallelStream().allMatch(dataUrls::contains), + "All selected objects should be in dataUrls"); + assertEquals(1, table.getSelectedItems().size(), "There should only be one selected item"); } /** @@ -88,7 +89,8 @@ public class MapWithAIURLPreferenceTableTest { List dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); for (int i = 0; i < dataUrls.get(0).getDataList().size(); i++) { - assertEquals(dataUrls.get(0).getDataList().get(i).getClass(), table.getModel().getColumnClass(i)); + assertEquals(dataUrls.get(0).getDataList().get(i).getClass(), table.getModel().getColumnClass(i), + "The classes should match"); } } @@ -103,9 +105,10 @@ public class MapWithAIURLPreferenceTableTest { DataUrl initial = DataUrl.emptyData(); // Don't need to clone the "current" first entry dataUrls.add(initial); table.setValueAt("New Source", 0, 0); - assertEquals("New Source", dataUrls.get(0).getDataList().get(0)); - assertEquals(2, dataUrls.size()); + assertEquals("New Source", dataUrls.get(0).getDataList().get(0), + "The source should be set and passed through to the dataUrls"); + assertEquals(2, dataUrls.size(), "There should be two entries still"); table.setValueAt("", 0, 0); - assertSame(initial, dataUrls.get(0)); + assertSame(initial, dataUrls.get(0), "The \"initial\" dataUrl should be sorted to be first"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java index a45c011..bc33377 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/UpdateProdTest.java @@ -30,10 +30,10 @@ public class UpdateProdTest { String intKey = "message.".concat(MapWithAIPlugin.NAME.concat(".ignore_next_version")).concat(".value"); // "message.MapWithAI.ignore_next_version.value"; Config.getPref().putBoolean(booleanKey, false); Config.getPref().putInt(intKey, JOptionPane.YES_OPTION); - assertTrue(UpdateProd.doProd(Integer.MAX_VALUE)); + assertTrue(UpdateProd.doProd(Integer.MAX_VALUE), "An update is required"); Config.getPref().putInt(intKey, JOptionPane.NO_OPTION); - assertTrue(UpdateProd.doProd(Integer.MAX_VALUE)); - assertFalse(UpdateProd.doProd(0)); + assertTrue(UpdateProd.doProd(Integer.MAX_VALUE), "An update is required"); + assertFalse(UpdateProd.doProd(0), "An update is not required"); } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWaysTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWaysTest.java index 7c13ef1..522fa31 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWaysTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWaysTest.java @@ -1,14 +1,14 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapwithai.commands; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +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.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import java.util.Collections; @@ -48,9 +48,11 @@ public class MergeDuplicateWaysTest { public void testRemoveCommonTags() { final DataSet ds1 = new DataSet(TestUtils.newNode("orig_id=2222 highway=secondary")); GetDataRunnable.removeCommonTags(ds1); - assertEquals(1, ds1.allPrimitives().stream().mapToInt(prim -> prim.getKeys().size()).sum()); + assertEquals(1, ds1.allPrimitives().stream().mapToInt(prim -> prim.getKeys().size()).sum(), + "orig_id should be removed"); GetDataRunnable.removeCommonTags(ds1); - assertEquals(1, ds1.allPrimitives().stream().mapToInt(prim -> prim.getKeys().size()).sum()); + assertEquals(1, ds1.allPrimitives().stream().mapToInt(prim -> prim.getKeys().size()).sum(), + "No other tags should be removed"); } /** @@ -68,8 +70,8 @@ public class MergeDuplicateWaysTest { ds1.addPrimitive(way2); new MergeDuplicateWays(ds1).executeCommand(); - assertFalse(way1.isDeleted()); - assertFalse(way2.isDeleted()); + assertFalse(way1.isDeleted(), "way1 should not yet be deleted"); + assertFalse(way2.isDeleted(), "way2 should not yet be deleted"); way1.getNodes().forEach(node -> assertFalse(way2.containsNode(node))); way2.getNodes().forEach(node -> assertFalse(way1.containsNode(node))); @@ -78,9 +80,9 @@ public class MergeDuplicateWaysTest { way1.addNode(1, tNode); new MergeDuplicateWays(ds1).executeCommand(); - assertNotSame(way1.isDeleted(), way2.isDeleted()); + assertNotSame(way1.isDeleted(), way2.isDeleted(), "Either way1 or way2 should be delted, but not both"); final Way tWay = way1.isDeleted() ? way2 : way1; - assertEquals(4, tWay.getNodesCount()); + assertEquals(4, tWay.getNodesCount(), "The undeleted way should have the missing node(s) from the other way"); } /** @@ -101,10 +103,11 @@ public class MergeDuplicateWaysTest { ds.addPrimitive(way1); // Test with one node in common - assertNull(MergeDuplicateWays.mergeWays(way1, way2, set)); - assertFalse(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(2, way1.getNodesCount()); + assertNull(MergeDuplicateWays.mergeWays(way1, way2, set), + "We cannot merge the ways, since there is insufficient overlap"); + assertFalse(way2.isDeleted(), "way2 should not be deleted"); + assertFalse(way1.isDeleted(), "way1 should not be deleted"); + assertEquals(2, way1.getNodesCount(), "way1 nodes should not have been modified"); // Test with two nodes in common final Node tNode = new Node(new LatLon(0, 0)); @@ -115,14 +118,7 @@ public class MergeDuplicateWaysTest { set.add(new Pair<>(new Pair<>(1, way1.lastNode()), new Pair<>(1, way2.getNode(1)))); Command command = MergeDuplicateWays.mergeWays(way1, way2, set); for (int i = 0; i < undoRedoTries; i++) { - command.executeCommand(); - assertTrue(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(3, way1.getNodesCount()); - command.undoCommand(); - assertFalse(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(2, way1.getNodesCount()); + checkCommand(command, way1, way2, 3); } command.executeCommand(); @@ -144,15 +140,7 @@ public class MergeDuplicateWaysTest { command = MergeDuplicateWays.mergeWays(way1, way2, set); for (int i = 0; i < undoRedoTries; i++) { - command.executeCommand(); - assertTrue(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(3, way1.getNodesCount()); - - command.undoCommand(); - assertFalse(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(2, way1.getNodesCount()); + checkCommand(command, way1, way2, 3); } // Test that nodes on both sides get added @@ -172,21 +160,34 @@ public class MergeDuplicateWaysTest { command = MergeDuplicateWays.mergeWays(way1, way2, set); for (int i = 0; i < undoRedoTries; i++) { + checkCommand(command, way1, way2, 4); command.executeCommand(); - assertTrue(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(4, way1.getNodesCount()); - assertEquals(currentWay2Nodes.get(0), way1.firstNode()); - assertEquals(currentWay2Nodes.get(1), way1.getNode(1)); + assertEquals(currentWay2Nodes.get(0), way1.firstNode(), + "The first node of the way1 should be the first node of way2"); + assertEquals(currentWay2Nodes.get(1), way1.getNode(1), + "The second node of way1 should be the second node of way2"); command.undoCommand(); - assertFalse(way2.isDeleted()); - assertFalse(way1.isDeleted()); - assertEquals(2, way1.getNodesCount()); } - assertThrows(NullPointerException.class, () -> MergeDuplicateWays.mergeWays(null, null, null)); - assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), Collections.emptySet())); - assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), set)); + assertThrows(NullPointerException.class, () -> MergeDuplicateWays.mergeWays(null, null, null), + "Throw NPE if any argument is null"); + assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), Collections.emptySet()), + "We should return null if there is no overlap"); + assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), set), + "We should return null if there is no overlap"); + } + + private static void checkCommand(Command command, Way way1, Way way2, int expectedNodes) { + int way1InitialNodes = way1.getNodesCount(); + command.executeCommand(); + assertTrue(way2.isDeleted(), "way2 should be deleted"); + assertFalse(way1.isDeleted(), "way1 should not be deleted"); + assertEquals(expectedNodes, way1.getNodesCount(), "way1 should have an additional node"); + + command.undoCommand(); + assertFalse(way2.isDeleted(), "way2 should not be deleted"); + assertFalse(way1.isDeleted(), "way1 should not be deleted"); + assertEquals(way1InitialNodes, way1.getNodesCount(), "way1 should not have an additional node"); } /** @@ -202,25 +203,28 @@ public class MergeDuplicateWaysTest { set.add(pair1); set.add(pair2); - assertTrue(MergeDuplicateWays.checkDirection(set)); + assertTrue(MergeDuplicateWays.checkDirection(set), "The direction is the same"); pair1.a.a = pair1.a.a - 1; - assertTrue(MergeDuplicateWays.checkDirection(set)); + assertTrue(MergeDuplicateWays.checkDirection(set), "The direction is the same"); pair1.a.a = pair1.a.a + 3; - assertFalse(MergeDuplicateWays.checkDirection(set)); + assertFalse(MergeDuplicateWays.checkDirection(set), "The direction is not the same"); pair1.a.a = pair1.a.a - 2; - assertTrue(MergeDuplicateWays.checkDirection(set)); + assertTrue(MergeDuplicateWays.checkDirection(set), "The direction is the same"); pair1.b.a = pair1.b.a - 1; - assertTrue(MergeDuplicateWays.checkDirection(set)); + assertTrue(MergeDuplicateWays.checkDirection(set), "The direction is the same"); pair1.b.a = pair1.b.a + 3; - assertFalse(MergeDuplicateWays.checkDirection(set)); + assertFalse(MergeDuplicateWays.checkDirection(set), "The direction is not the same"); pair1.b.a = pair1.b.a - 2; - assertThrows(NullPointerException.class, () -> MergeDuplicateWays.checkDirection(null)); + assertThrows(NullPointerException.class, () -> MergeDuplicateWays.checkDirection(null), + "Throw an NPE if the argument is null"); - assertFalse(MergeDuplicateWays.checkDirection(Collections.emptySet())); + assertFalse(MergeDuplicateWays.checkDirection(Collections.emptySet()), + "If there are no pairs of nodes, the direction is not the same"); set.remove(pair1); - assertFalse(MergeDuplicateWays.checkDirection(set)); + assertFalse(MergeDuplicateWays.checkDirection(set), + "If there is only one set of pairs, then the direction is not the same"); } /** @@ -229,14 +233,14 @@ public class MergeDuplicateWaysTest { @Test public void testSorted() { List integerList = Arrays.asList(1, 2, 3, 4, 6, 7, 8, 9, 5); - assertFalse(MergeDuplicateWays.sorted(integerList)); + assertFalse(MergeDuplicateWays.sorted(integerList), "The list is not yet sorted"); integerList = integerList.stream().sorted().collect(Collectors.toList()); - assertTrue(MergeDuplicateWays.sorted(integerList)); + assertTrue(MergeDuplicateWays.sorted(integerList), "The list is sorted"); integerList.remove(3); - assertFalse(MergeDuplicateWays.sorted(integerList)); + assertFalse(MergeDuplicateWays.sorted(integerList), "The list is not sorted"); integerList = Arrays.asList(1); - assertTrue(MergeDuplicateWays.sorted(integerList)); + assertTrue(MergeDuplicateWays.sorted(integerList), "The list is sorted"); } /** @@ -248,27 +252,30 @@ public class MergeDuplicateWaysTest { final Way way2 = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); Map, Map> duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); - assertEquals(2, duplicateNodes.size()); + assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs"); assertEquals(2, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); way2.addNode(new Node(new LatLon(0, 0))); duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); - assertEquals(2, duplicateNodes.size()); - assertEquals(3, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); + assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs"); + assertEquals(3, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count(), + "There are three nodes that should be in the list"); way2.addNode(way2.firstNode()); duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); - assertEquals(2, duplicateNodes.size()); - assertEquals(4, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); + assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs"); + assertEquals(4, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count(), + "There should be four nodes in the duplicate values list"); way2.setNodes(way2.getNodes().stream().limit(2).collect(Collectors.toList())); way2.addNode(new Node(new LatLon(2, 2))); duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); - assertEquals(2, duplicateNodes.size()); - assertEquals(2, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); + assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs"); + assertEquals(2, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count(), + "There should only be two duplicate nodes"); } /** @@ -277,8 +284,8 @@ public class MergeDuplicateWaysTest { @Test public void testGetDescriptionText() { final Command command = new MergeDuplicateWays(new DataSet()); - assertNotNull(command.getDescriptionText()); - assertFalse(command.getDescriptionText().isEmpty()); + assertNotNull(command.getDescriptionText(), "The description should not be null"); + assertFalse(command.getDescriptionText().isEmpty(), "The description should not be empty"); } /** @@ -287,20 +294,25 @@ public class MergeDuplicateWaysTest { @Test public void testNodeInCompressed() { final Node testNode = new Node(); - assertThrows(NullPointerException.class, () -> MergeDuplicateWays.nodeInCompressed(testNode, null)); - assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, Collections.emptySet())); + assertThrows(NullPointerException.class, () -> MergeDuplicateWays.nodeInCompressed(testNode, null), + "Throw an NPE if the compressed collection is null"); + assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, Collections.emptySet()), + "If a node is not in the set, it should be returned"); final Set, Pair>> testSet = new HashSet<>(); testSet.add(new Pair<>(new Pair<>(1, new Node()), new Pair<>(2, new Node()))); - assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, testSet)); + assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, testSet), + "If a node is not in the set, it shoudl be returned"); Pair, Pair> matchPair = new Pair<>(new Pair<>(2, testNode), new Pair<>(1, new Node())); testSet.add(matchPair); - assertSame(matchPair.b.b, MergeDuplicateWays.nodeInCompressed(testNode, testSet)); + assertSame(matchPair.b.b, MergeDuplicateWays.nodeInCompressed(testNode, testSet), + "If a node has a pairing, then the paired node should be returned"); testSet.remove(matchPair); matchPair = new Pair<>(new Pair<>(2, new Node()), new Pair<>(1, testNode)); testSet.add(matchPair); - assertSame(matchPair.a.b, MergeDuplicateWays.nodeInCompressed(testNode, testSet)); + assertSame(matchPair.a.b, MergeDuplicateWays.nodeInCompressed(testNode, testSet), + "If a node has a pairing, then the paired node should be returned"); } }