Create explicit tests for creating connections

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-10-02 10:34:48 -06:00
rodzic 4dbe59f7c3
commit 93f619499c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 176 dodań i 7 usunięć

Wyświetl plik

@ -18,6 +18,7 @@ import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.plugins.rapid.RapiDPlugin;
import org.openstreetmap.josm.tools.Geometry;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Utils;
@ -67,8 +68,11 @@ public class CreateConnectionsCommand extends Command {
for (int i = 0; i < primitiveConnections.length / 3; i++) {
if (primitiveConnections[i] instanceof Way && primitiveConnections[i + 1] instanceof Node
&& primitiveConnections[i + 2] instanceof Node) {
changedKeyList.add(addNodesToWay(node, (Way) primitiveConnections[i],
(Node) primitiveConnections[i + 1], (Node) primitiveConnections[i + 2]));
Command addNodesToWayCommand = addNodesToWay(node, (Way) primitiveConnections[i],
(Node) primitiveConnections[i + 1], (Node) primitiveConnections[i + 2]);
if (addNodesToWayCommand != null) {
changedKeyList.add(addNodesToWayCommand);
}
} else {
Logging.error("{0}: {1}, {2}: {3}, {4}: {5}", i, primitiveConnections[i].getClass(), i + 1,
primitiveConnections[i + 1].getClass(), i + 2, primitiveConnections[i + 2].getClass());
@ -83,7 +87,10 @@ public class CreateConnectionsCommand extends Command {
Logging.error("RapiD: dupe connection connected to more than one node? (dupe={0})",
node.get(DUPE_KEY));
}
changedKeyList.add(replaceNode(node, (Node) primitiveConnections[0]));
Command replaceCommand = replaceNode(node, (Node) primitiveConnections[0]);
if (replaceCommand != null) {
changedKeyList.add(replaceCommand);
}
}
}
if (!changedKeyList.isEmpty())
@ -126,11 +133,31 @@ public class CreateConnectionsCommand extends Command {
* @param second The second node in a waysegemnt
*/
public static Command addNodesToWay(Node toAddNode, Way way, Node first, Node second) {
return new AddNodeToWayCommand(toAddNode, way, first, second);
Command tCommand = null;
Way tWay = new Way();
tWay.addNode(first);
tWay.addNode(second);
double distance = Geometry.getDistanceWayNode(tWay, toAddNode);
if (distance < 5) {
tCommand = new AddNodeToWayCommand(toAddNode, way, first, second);
}
return tCommand;
}
/**
* Replace nodes that are in the same location
*
* @param original The original node (the one to replace)
* @param newNode The node that is replacing the original node
* @return A command that replaces the node, or null if they are not at the same
* location.
*/
public static Command replaceNode(Node original, Node newNode) {
return MergeNodesAction.mergeNodes(Arrays.asList(original), newNode, newNode);
Command tCommand = null;
if (original.getCoor().equalsEpsilon(newNode.getCoor())) {
tCommand = MergeNodesAction.mergeNodes(Arrays.asList(original), newNode, newNode);
}
return tCommand;
}
@Override
@ -141,7 +168,6 @@ public class CreateConnectionsCommand extends Command {
@Override
public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
Collection<OsmPrimitive> added) {
// Don't touch the collectioins, since we haven't modified anything -- the
// subcommands should do that.
command.fillModifiedData(modified, deleted, added);
}
}

Wyświetl plik

@ -0,0 +1,143 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.rapid.commands;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.TestUtils;
import org.openstreetmap.josm.command.Command;
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.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.testutils.JOSMTestRules;
/**
* Tests for {@link CreateConnections}
*
* @author Taylor Smock
*/
public class CreateConnectionsCommandTest {
@Rule
public JOSMTestRules test = new JOSMTestRules().projection();
/**
* Test method for
* {@link CreateConnectionsCommand#createConnections(DataSet, Collection)}.
*/
@Test
public void testCreateConnections() {
Node node1 = new Node(new LatLon(0, 0));
Node node2 = new Node(new LatLon(1, 0));
Node node3 = new Node(new LatLon(0.5, 0));
Node dupe = new Node(new LatLon(0, 0));
Way way = TestUtils.newWay("highway=residential", node1, node2);
Collection<OsmPrimitive> added = new ArrayList<>();
Collection<OsmPrimitive> modified = new ArrayList<>();
Collection<OsmPrimitive> deleted = new ArrayList<>();
DataSet dataSet = new DataSet(node1, node2, node3, dupe, way);
Command createConnections = new CreateConnectionsCommand(dataSet, Collections.singleton(node3));
createConnections.executeCommand();
Assert.assertFalse(dataSet.isModified());
createConnections.undoCommand();
Assert.assertFalse(dataSet.isModified());
node3.put(CreateConnectionsCommand.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());
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());
createConnections.undoCommand();
Assert.assertFalse(dataSet.isModified());
Assert.assertEquals(2, way.getNodesCount());
dupe.put(CreateConnectionsCommand.DUPE_KEY, "n" + node1.getUniqueId());
createConnections = new CreateConnectionsCommand(dataSet, Collections.singleton(dupe));
createConnections.executeCommand();
Assert.assertTrue(dataSet.isModified());
Assert.assertEquals(2, way.getNodesCount());
modified.clear();
createConnections.fillModifiedData(modified, deleted, added);
Assert.assertEquals(1, modified.size());
Assert.assertTrue(deleted.isEmpty());
Assert.assertTrue(added.isEmpty());
createConnections.undoCommand();
Assert.assertFalse(dataSet.isModified());
Assert.assertEquals(2, way.getNodesCount());
}
/**
* Test method for
* {@link CreateConnectionsCommand#addNodesToWay(Node, Node, Node, Node)}.
*/
@Test
public void testAddNodesToWay() {
Node node1 = new Node(new LatLon(0, 0));
Node node2 = new Node(new LatLon(1, 0));
Node node3 = new Node(new LatLon(0.5, 0));
Way way = TestUtils.newWay("highway=residential", node1, node2);
new DataSet(node1, node2, node3, way);
Command addNodeToWayCommand = CreateConnectionsCommand.addNodesToWay(node3, way, node1, node2);
Assert.assertEquals(2, way.getNodesCount());
addNodeToWayCommand.executeCommand();
Assert.assertEquals(3, way.getNodesCount());
addNodeToWayCommand.undoCommand();
Assert.assertEquals(2, way.getNodesCount());
node2.setCoor(new LatLon(1, 0.1));
addNodeToWayCommand = CreateConnectionsCommand.addNodesToWay(node3, way, node1, node2);
Assert.assertNull(addNodeToWayCommand);
node2.setCoor(new LatLon(1, 0.01));
addNodeToWayCommand = CreateConnectionsCommand.addNodesToWay(node3, way, node1, node2);
Assert.assertNull(addNodeToWayCommand);
node2.setCoor(new LatLon(1, 0.00008));
addNodeToWayCommand = CreateConnectionsCommand.addNodesToWay(node3, way, node1, node2);
addNodeToWayCommand.executeCommand();
Assert.assertEquals(3, way.getNodesCount());
addNodeToWayCommand.undoCommand();
Assert.assertEquals(2, way.getNodesCount());
}
/**
* Test method for {@link CreateConnectionsCommand#replaceNode(Node, Node)}.
*/
@Test
public void testReplaceNode() {
Node node1 = new Node(new LatLon(0, 0));
Node node2 = new Node(new LatLon(0, 0));
new DataSet(node1, node2);
Command replaceNodeCommand = CreateConnectionsCommand.replaceNode(node1, node2);
replaceNodeCommand.executeCommand();
Assert.assertTrue(node1.isDeleted());
replaceNodeCommand.undoCommand();
Assert.assertFalse(node1.isDeleted());
node2.setCoor(new LatLon(0.1, 0.1));
Assert.assertNull(CreateConnectionsCommand.replaceNode(node1, node2));
}
/**
* Test method for {@link CreateConnectionsCommand#getDescriptionText()}.
*/
@Test
public void testGetDescriptionText() {
String text = new CreateConnectionsCommand(new DataSet(), null).getDescriptionText();
Assert.assertNotNull(text);
Assert.assertFalse(text.isEmpty());
}
}