Fix some PMD issues in tests

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-12-16 09:46:07 -07:00
rodzic 31c60293f6
commit 5aefb58eb6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
5 zmienionych plików z 150 dodań i 115 usunięć

Wyświetl plik

@ -2,6 +2,8 @@
package org.openstreetmap.josm.plugins.mapwithai; package org.openstreetmap.josm.plugins.mapwithai;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; 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.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
@ -13,7 +15,6 @@ import javax.swing.JMenu;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.awaitility.Durations; import org.awaitility.Durations;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -66,7 +67,8 @@ public class MapWithAIPluginTest {
@Test @Test
public void testGetPreferenceSetting() { public void testGetPreferenceSetting() {
plugin = new MapWithAIPlugin(info); 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 JMenu dataMenu = MainApplication.getMenu().dataMenu;
final int dataMenuSize = dataMenu.getMenuComponentCount(); final int dataMenuSize = dataMenu.getMenuComponentCount();
plugin = new MapWithAIPlugin(info); plugin = new MapWithAIPlugin(info);
Assert.assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount()); assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount(), "Menu items were not added");
Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() assertEquals(1,
.filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); 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 for (boolean existed : Arrays.asList(false, true)) { // false, true order is important
plugin = new MapWithAIPlugin(info); plugin = new MapWithAIPlugin(info);
Config.getPref().putBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS, existed); Config.getPref().putBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS, existed);
plugin.destroy(); 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) Awaitility.await().atMost(Durations.FIVE_SECONDS)
.until(() -> existed == MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); .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() 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++) { for (int i = 0; i < 3; i++) {
plugin = new MapWithAIPlugin(info); plugin = new MapWithAIPlugin(info);
Assert.assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount()); assertEquals(dataMenuSize + addedMenuItems, dataMenu.getMenuComponentCount(),
Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() "The menu items were added multiple times");
.filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); 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 @Test
public void testGetVersionInfo() { public void testGetVersionInfo() {
plugin = new MapWithAIPlugin(info); // needs to be called for version info to be initialized. 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");
} }
} }

Wyświetl plik

@ -1,6 +1,10 @@
// License: GPL. For details, see LICENSE file. // License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai; 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 javax.swing.SpinnerNumberModel;
import org.junit.Assert; import org.junit.Assert;
@ -43,32 +47,39 @@ public class MapWithAIPreferencesTest {
preferences.addGui(pane); preferences.addGui(pane);
Assert.assertEquals(tabs + 1, pane.getPluginPreference().getTabPane().getTabCount()); assertEquals(tabs + 1, pane.getPluginPreference().getTabPane().getTabCount(), "Preferences wasn't added");
Assert.assertEquals(pane.getPluginPreference(), preferences.getTabPreferenceSetting(pane)); assertEquals(pane.getPluginPreference(), preferences.getTabPreferenceSetting(pane),
"The expected parent of the settings panel was different");
final boolean switchLayers = MapWithAIPreferenceHelper.isSwitchLayers(); 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(); preferences.ok();
Assert.assertEquals(switchLayers, MapWithAIPreferenceHelper.isSwitchLayers()); assertEquals(switchLayers, MapWithAIPreferenceHelper.isSwitchLayers(),
"The default for switching layers is true");
preferences.getSwitchLayerCheckBox().setSelected(!switchLayers); preferences.getSwitchLayerCheckBox().setSelected(!switchLayers);
Assert.assertNotEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers()); assertNotEquals(!switchLayers, MapWithAIPreferenceHelper.isSwitchLayers(), "OK hasn't been selected yet");
preferences.ok(); 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(); final Object tmp = preferences.getMaximumAdditionSpinner().getModel();
SpinnerNumberModel spinnerModel = null; SpinnerNumberModel spinnerModel = null;
if (tmp instanceof SpinnerNumberModel) { if (tmp instanceof SpinnerNumberModel) {
spinnerModel = (SpinnerNumberModel) tmp; spinnerModel = (SpinnerNumberModel) tmp;
} }
Assert.assertNotNull(spinnerModel); assertNotNull(spinnerModel, "The spinner model should be a SpinnerNumberModel");
final Number currentNumber = MapWithAIPreferenceHelper.getMaximumAddition(); 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); 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(); 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");
} }
/** /**

Wyświetl plik

@ -34,20 +34,20 @@ public class MapWithAIURLPreferenceTableTest {
public void testMapWithAIURLPreferenceTable() { public void testMapWithAIURLPreferenceTable() {
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
assertEquals(4, table.getModel().getColumnCount()); assertEquals(4, table.getModel().getColumnCount(), "There should be four columns");
assertEquals(1, table.getModel().getRowCount()); assertEquals(1, table.getModel().getRowCount(), "There is only one entry");
assertFalse(dataUrls.isEmpty()); assertFalse(dataUrls.isEmpty(), "The backing list should not be empty");
assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0),
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)); dataUrls.add(0, new DataUrl("no-source", "no-url", true));
table.fireDataChanged(); table.fireDataChanged();
assertEquals(4, table.getModel().getColumnCount()); assertEquals(4, table.getModel().getColumnCount(), "The column count should not change");
assertEquals(2, table.getModel().getRowCount()); assertEquals(2, table.getModel().getRowCount(), "An additional DataUrl was added");
assertFalse(dataUrls.isEmpty()); assertFalse(dataUrls.isEmpty(), "The backing list should not be empty");
assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0),
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("timeout", Integer.toString(50));
map.put("maxnodedistance", Double.toString(1.2)); map.put("maxnodedistance", Double.toString(1.2));
Map<String, Object> objectifiedMap = MapWithAIURLPreferenceTable.objectify(map); Map<String, Object> objectifiedMap = MapWithAIURLPreferenceTable.objectify(map);
assertTrue(objectifiedMap.get("source") instanceof String); assertTrue(objectifiedMap.get("source") instanceof String, "Source should be a string");
assertTrue(objectifiedMap.get("enabled") instanceof Boolean); assertTrue(objectifiedMap.get("enabled") instanceof Boolean, "Enabled should be a boolean");
assertTrue(objectifiedMap.get("timeout") instanceof Integer); assertTrue(objectifiedMap.get("timeout") instanceof Integer, "Timeout should be an integer");
assertTrue(objectifiedMap.get("maxnodedistance") instanceof Double); assertTrue(objectifiedMap.get("maxnodedistance") instanceof Double, "Maxnodedistance should be a double");
} }
/** /**
@ -75,8 +75,9 @@ public class MapWithAIURLPreferenceTableTest {
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
table.addRowSelectionInterval(0, 0); table.addRowSelectionInterval(0, 0);
assertTrue(table.getSelectedItems().parallelStream().allMatch(dataUrls::contains)); assertTrue(table.getSelectedItems().parallelStream().allMatch(dataUrls::contains),
assertEquals(1, table.getSelectedItems().size()); "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<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData())); List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls); MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
for (int i = 0; i < dataUrls.get(0).getDataList().size(); i++) { 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 DataUrl initial = DataUrl.emptyData(); // Don't need to clone the "current" first entry
dataUrls.add(initial); dataUrls.add(initial);
table.setValueAt("New Source", 0, 0); table.setValueAt("New Source", 0, 0);
assertEquals("New Source", dataUrls.get(0).getDataList().get(0)); assertEquals("New Source", dataUrls.get(0).getDataList().get(0),
assertEquals(2, dataUrls.size()); "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); table.setValueAt("", 0, 0);
assertSame(initial, dataUrls.get(0)); assertSame(initial, dataUrls.get(0), "The \"initial\" dataUrl should be sorted to be first");
} }
} }

Wyświetl plik

@ -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"; 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().putBoolean(booleanKey, false);
Config.getPref().putInt(intKey, JOptionPane.YES_OPTION); 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); Config.getPref().putInt(intKey, JOptionPane.NO_OPTION);
assertTrue(UpdateProd.doProd(Integer.MAX_VALUE)); assertTrue(UpdateProd.doProd(Integer.MAX_VALUE), "An update is required");
assertFalse(UpdateProd.doProd(0)); assertFalse(UpdateProd.doProd(0), "An update is not required");
} }
} }

Wyświetl plik

@ -1,14 +1,14 @@
// License: GPL. For details, see LICENSE file. // License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.commands; package org.openstreetmap.josm.plugins.mapwithai.commands;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.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.assertNotNull; 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.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -48,9 +48,11 @@ public class MergeDuplicateWaysTest {
public void testRemoveCommonTags() { public void testRemoveCommonTags() {
final DataSet ds1 = new DataSet(TestUtils.newNode("orig_id=2222 highway=secondary")); final DataSet ds1 = new DataSet(TestUtils.newNode("orig_id=2222 highway=secondary"));
GetDataRunnable.removeCommonTags(ds1); 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); 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); ds1.addPrimitive(way2);
new MergeDuplicateWays(ds1).executeCommand(); new MergeDuplicateWays(ds1).executeCommand();
assertFalse(way1.isDeleted()); assertFalse(way1.isDeleted(), "way1 should not yet be deleted");
assertFalse(way2.isDeleted()); assertFalse(way2.isDeleted(), "way2 should not yet be deleted");
way1.getNodes().forEach(node -> assertFalse(way2.containsNode(node))); way1.getNodes().forEach(node -> assertFalse(way2.containsNode(node)));
way2.getNodes().forEach(node -> assertFalse(way1.containsNode(node))); way2.getNodes().forEach(node -> assertFalse(way1.containsNode(node)));
@ -78,9 +80,9 @@ public class MergeDuplicateWaysTest {
way1.addNode(1, tNode); way1.addNode(1, tNode);
new MergeDuplicateWays(ds1).executeCommand(); 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; 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); ds.addPrimitive(way1);
// Test with one node in common // Test with one node in common
assertNull(MergeDuplicateWays.mergeWays(way1, way2, set)); assertNull(MergeDuplicateWays.mergeWays(way1, way2, set),
assertFalse(way2.isDeleted()); "We cannot merge the ways, since there is insufficient overlap");
assertFalse(way1.isDeleted()); assertFalse(way2.isDeleted(), "way2 should not be deleted");
assertEquals(2, way1.getNodesCount()); 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 // Test with two nodes in common
final Node tNode = new Node(new LatLon(0, 0)); 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)))); set.add(new Pair<>(new Pair<>(1, way1.lastNode()), new Pair<>(1, way2.getNode(1))));
Command command = MergeDuplicateWays.mergeWays(way1, way2, set); Command command = MergeDuplicateWays.mergeWays(way1, way2, set);
for (int i = 0; i < undoRedoTries; i++) { for (int i = 0; i < undoRedoTries; i++) {
command.executeCommand(); checkCommand(command, way1, way2, 3);
assertTrue(way2.isDeleted());
assertFalse(way1.isDeleted());
assertEquals(3, way1.getNodesCount());
command.undoCommand();
assertFalse(way2.isDeleted());
assertFalse(way1.isDeleted());
assertEquals(2, way1.getNodesCount());
} }
command.executeCommand(); command.executeCommand();
@ -144,15 +140,7 @@ public class MergeDuplicateWaysTest {
command = MergeDuplicateWays.mergeWays(way1, way2, set); command = MergeDuplicateWays.mergeWays(way1, way2, set);
for (int i = 0; i < undoRedoTries; i++) { for (int i = 0; i < undoRedoTries; i++) {
command.executeCommand(); checkCommand(command, way1, way2, 3);
assertTrue(way2.isDeleted());
assertFalse(way1.isDeleted());
assertEquals(3, way1.getNodesCount());
command.undoCommand();
assertFalse(way2.isDeleted());
assertFalse(way1.isDeleted());
assertEquals(2, way1.getNodesCount());
} }
// Test that nodes on both sides get added // Test that nodes on both sides get added
@ -172,21 +160,34 @@ public class MergeDuplicateWaysTest {
command = MergeDuplicateWays.mergeWays(way1, way2, set); command = MergeDuplicateWays.mergeWays(way1, way2, set);
for (int i = 0; i < undoRedoTries; i++) { for (int i = 0; i < undoRedoTries; i++) {
checkCommand(command, way1, way2, 4);
command.executeCommand(); command.executeCommand();
assertTrue(way2.isDeleted()); assertEquals(currentWay2Nodes.get(0), way1.firstNode(),
assertFalse(way1.isDeleted()); "The first node of the way1 should be the first node of way2");
assertEquals(4, way1.getNodesCount()); assertEquals(currentWay2Nodes.get(1), way1.getNode(1),
assertEquals(currentWay2Nodes.get(0), way1.firstNode()); "The second node of way1 should be the second node of way2");
assertEquals(currentWay2Nodes.get(1), way1.getNode(1));
command.undoCommand(); command.undoCommand();
assertFalse(way2.isDeleted());
assertFalse(way1.isDeleted());
assertEquals(2, way1.getNodesCount());
} }
assertThrows(NullPointerException.class, () -> MergeDuplicateWays.mergeWays(null, null, null)); assertThrows(NullPointerException.class, () -> MergeDuplicateWays.mergeWays(null, null, null),
assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), Collections.emptySet())); "Throw NPE if any argument is null");
assertNull(MergeDuplicateWays.mergeWays(new Way(), new Way(), set)); 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(pair1);
set.add(pair2); set.add(pair2);
assertTrue(MergeDuplicateWays.checkDirection(set)); assertTrue(MergeDuplicateWays.checkDirection(set), "The direction is the same");
pair1.a.a = pair1.a.a - 1; 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; 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; 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; 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; 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; 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); 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 @Test
public void testSorted() { public void testSorted() {
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 6, 7, 8, 9, 5); List<Integer> 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()); integerList = integerList.stream().sorted().collect(Collectors.toList());
assertTrue(MergeDuplicateWays.sorted(integerList)); assertTrue(MergeDuplicateWays.sorted(integerList), "The list is sorted");
integerList.remove(3); integerList.remove(3);
assertFalse(MergeDuplicateWays.sorted(integerList)); assertFalse(MergeDuplicateWays.sorted(integerList), "The list is not sorted");
integerList = Arrays.asList(1); 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))); final Way way2 = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
Map<Pair<Integer, Node>, Map<Integer, Node>> duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); Map<Pair<Integer, Node>, Map<Integer, Node>> 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()); assertEquals(2, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count());
way2.addNode(new Node(new LatLon(0, 0))); way2.addNode(new Node(new LatLon(0, 0)));
duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2);
assertEquals(2, duplicateNodes.size()); assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs");
assertEquals(3, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); 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()); way2.addNode(way2.firstNode());
duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2);
assertEquals(2, duplicateNodes.size()); assertEquals(2, duplicateNodes.size(), "There should be two duplicate pairs");
assertEquals(4, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count()); 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.setNodes(way2.getNodes().stream().limit(2).collect(Collectors.toList()));
way2.addNode(new Node(new LatLon(2, 2))); way2.addNode(new Node(new LatLon(2, 2)));
duplicateNodes = MergeDuplicateWays.getDuplicateNodes(way1, way2); 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()); 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 @Test
public void testGetDescriptionText() { public void testGetDescriptionText() {
final Command command = new MergeDuplicateWays(new DataSet()); final Command command = new MergeDuplicateWays(new DataSet());
assertNotNull(command.getDescriptionText()); assertNotNull(command.getDescriptionText(), "The description should not be null");
assertFalse(command.getDescriptionText().isEmpty()); assertFalse(command.getDescriptionText().isEmpty(), "The description should not be empty");
} }
/** /**
@ -287,20 +294,25 @@ public class MergeDuplicateWaysTest {
@Test @Test
public void testNodeInCompressed() { public void testNodeInCompressed() {
final Node testNode = new Node(); final Node testNode = new Node();
assertThrows(NullPointerException.class, () -> MergeDuplicateWays.nodeInCompressed(testNode, null)); assertThrows(NullPointerException.class, () -> MergeDuplicateWays.nodeInCompressed(testNode, null),
assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, Collections.emptySet())); "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<Pair<Integer, Node>, Pair<Integer, Node>>> testSet = new HashSet<>(); final Set<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> testSet = new HashSet<>();
testSet.add(new Pair<>(new Pair<>(1, new Node()), new Pair<>(2, new Node()))); 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<Integer, Node>, Pair<Integer, Node>> matchPair = new Pair<>(new Pair<>(2, testNode), Pair<Pair<Integer, Node>, Pair<Integer, Node>> matchPair = new Pair<>(new Pair<>(2, testNode),
new Pair<>(1, new Node())); new Pair<>(1, new Node()));
testSet.add(matchPair); 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); testSet.remove(matchPair);
matchPair = new Pair<>(new Pair<>(2, new Node()), new Pair<>(1, testNode)); matchPair = new Pair<>(new Pair<>(2, new Node()), new Pair<>(1, testNode));
testSet.add(matchPair); 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");
} }
} }