More tests for MergeDuplicateWays

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-11-20 09:41:45 -07:00
rodzic 6ff8c134d3
commit 5a5c8867b2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 88 dodań i 45 usunięć

Wyświetl plik

@ -62,10 +62,10 @@ public class MergeDuplicateWays extends Command {
@Override
public boolean executeCommand() {
if (commands.isEmpty() || command == null) {
if (way1 == null && way2 == null) {
if (commands.isEmpty() || (command == null)) {
if ((way1 == null) && (way2 == null)) {
filterDataSet(getAffectedDataSet(), commands);
} else if (way1 != null && way2 == null) {
} else if ((way1 != null) && (way2 == null)) {
checkForDuplicateWays(way1, commands);
} else if (way1 == null) {
checkForDuplicateWays(way2, commands);
@ -76,11 +76,11 @@ public class MergeDuplicateWays extends Command {
tCommand.executeCommand();
}
}
List<Command> realCommands = commands.stream().filter(Objects::nonNull).distinct()
final List<Command> realCommands = commands.stream().filter(Objects::nonNull).distinct()
.collect(Collectors.toList());
commands.clear();
commands.addAll(realCommands);
if (!commands.isEmpty() && commands.size() != 1) {
if (!commands.isEmpty() && (commands.size() != 1)) {
command = new SequenceCommand(getDescriptionText(), commands);
} else if (commands.size() == 1) {
command = commands.get(0);
@ -99,9 +99,8 @@ public class MergeDuplicateWays extends Command {
}
public static void filterDataSet(DataSet dataSet, List<Command> commands) {
final List<Way> ways = new ArrayList<>(
dataSet.getWays().parallelStream().filter(prim -> !prim.isIncomplete() && !prim.isDeleted())
.collect(Collectors.toList()));
final List<Way> ways = new ArrayList<>(dataSet.getWays().parallelStream()
.filter(prim -> !prim.isIncomplete() && !prim.isDeleted()).collect(Collectors.toList()));
for (int i = 0; i < ways.size(); i++) {
final Way way1 = ways.get(i);
final Collection<Way> nearbyWays = dataSet.searchWays(way1.getBBox()).parallelStream()
@ -163,10 +162,12 @@ public class MergeDuplicateWays extends Command {
if (compressed.parallelStream().anyMatch(entry -> entry.a.b.isDeleted() || entry.b.b.isDeleted())) {
Logging.error("Bad node");
}
if (compressed.size() > 1
if ((compressed.size() > 1)
&& duplicateEntrySet.parallelStream().noneMatch(entry -> entry.getValue().size() > 1)) {
List<Integer> initial = compressed.stream().map(entry -> entry.a.a).sorted().collect(Collectors.toList());
List<Integer> after = compressed.stream().map(entry -> entry.b.a).sorted().collect(Collectors.toList());
final List<Integer> initial = compressed.stream().map(entry -> entry.a.a).sorted()
.collect(Collectors.toList());
final List<Integer> after = compressed.stream().map(entry -> entry.b.a).sorted()
.collect(Collectors.toList());
if (sorted(initial) && sorted(after)) {
returnCommand = mergeWays(way1, way2, compressed);
}
@ -187,7 +188,7 @@ public class MergeDuplicateWays extends Command {
public static Command mergeWays(Way way1, Way way2,
Set<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> compressed) {
Command command = null;
if (compressed.size() > 1 || (way1.hasKey(ORIG_ID) && way1.get(ORIG_ID).equals(way2.get(ORIG_ID)))) {
if ((compressed.size() > 1) || (way1.hasKey(ORIG_ID) && way1.get(ORIG_ID).equals(way2.get(ORIG_ID)))) {
Set<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> realSet = new LinkedHashSet<>(compressed);
final boolean sameDirection = checkDirection(realSet);
final List<Node> way2Nodes = way2.getNodes();
@ -269,9 +270,6 @@ public class MergeDuplicateWays extends Command {
final boolean way1Forward = first.a.a < second.a.a;
final boolean way2Forward = first.b.a < second.b.a;
returnValue = way1Forward == way2Forward;
} else if (compressed.size() == 1) {
final Pair<Pair<Integer, Node>, Pair<Integer, Node>> first = iterator.next();
returnValue = (first.a.a == 0 && first.b.a != 0) || (first.a.a != 0 && first.b.a == 0);
}
return returnValue;
}
@ -288,7 +286,7 @@ public class MergeDuplicateWays extends Command {
Integer last = collection.get(0);
for (int i = 1; i < collection.size(); i++) {
final Integer next = collection.get(i);
if (next - last != 1) {
if ((next - last) != 1) {
returnValue = false;
break;
}
@ -312,8 +310,8 @@ public class MergeDuplicateWays extends Command {
for (int k = 0; k < way2.getNodesCount(); k++) {
final Node possDupeNode = way2.getNode(k);
if (origNode.equals(possDupeNode)
|| origNode.getCoor().greatCircleDistance(possDupeNode.getCoor()) < MapWithAIPreferenceHelper
.getMaxNodeDistance()) {
|| (origNode.getCoor().greatCircleDistance(possDupeNode.getCoor()) < MapWithAIPreferenceHelper
.getMaxNodeDistance())) {
final Pair<Integer, Node> origNodePair = new Pair<>(j, origNode);
final Map<Integer, Node> dupeNodeMap = duplicateNodes.getOrDefault(origNodePair, new HashMap<>());
dupeNodeMap.put(k, possDupeNode);

Wyświetl plik

@ -1,15 +1,18 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.backend;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@ -24,7 +27,7 @@ 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.MergeDuplicateWays;
import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnable;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Pair;
@ -39,11 +42,11 @@ public class MergeDuplicateWaysTest {
public JOSMTestRules test = new JOSMTestRules().projection();
/**
* Test method for {@link GetDataRunnable#removeCommonTags(DataSet)}.
* Test method for {@link MergeDuplicateWays#removeCommonTags(DataSet)}.
*/
@Test
public void testRemoveCommonTags() {
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);
assertEquals(1, ds1.allPrimitives().stream().mapToInt(prim -> prim.getKeys().size()).sum());
GetDataRunnable.removeCommonTags(ds1);
@ -51,13 +54,13 @@ public class MergeDuplicateWaysTest {
}
/**
* Test method for {@link GetDataRunnable#filterDataSet(DataSet)}.
* Test method for {@link MergeDuplicateWays#filterDataSet(DataSet)}.
*/
@Test
public void testFilterDataSet() {
DataSet ds1 = new DataSet();
Way way1 = TestUtils.newWay("", new Node(new LatLon(0, 1)), new Node(new LatLon(1, 2)));
Way way2 = TestUtils.newWay("", new Node(new LatLon(1, 1)), new Node(new LatLon(1, 2)),
final DataSet ds1 = new DataSet();
final Way way1 = TestUtils.newWay("", new Node(new LatLon(0, 1)), new Node(new LatLon(1, 2)));
final Way way2 = TestUtils.newWay("", new Node(new LatLon(1, 1)), new Node(new LatLon(1, 2)),
new Node(new LatLon(2, 2)));
way1.getNodes().forEach(ds1::addPrimitive);
way2.getNodes().forEach(ds1::addPrimitive);
@ -70,26 +73,26 @@ public class MergeDuplicateWaysTest {
way1.getNodes().forEach(node -> assertFalse(way2.containsNode(node)));
way2.getNodes().forEach(node -> assertFalse(way1.containsNode(node)));
Node tNode = new Node(new LatLon(1, 1));
final Node tNode = new Node(new LatLon(1, 1));
ds1.addPrimitive(tNode);
way1.addNode(1, tNode);
new MergeDuplicateWays(ds1).executeCommand();
assertNotSame(way1.isDeleted(), way2.isDeleted());
Way tWay = way1.isDeleted() ? way2 : way1;
final Way tWay = way1.isDeleted() ? way2 : way1;
assertEquals(4, tWay.getNodesCount());
}
/**
* Test method for {@link GetDataRunnable#mergeWays(Way, Way, Set)}.
* Test method for {@link MergeDuplicateWays#mergeWays(Way, Way, Set)}.
*/
@Test
public void testMergeWays() {
int undoRedoTries = 10;
final int undoRedoTries = 10;
Way way1 = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
Way way2 = TestUtils.newWay("", new Node(new LatLon(1, 1)), new Node(new LatLon(1, 2)));
Set<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> set = new LinkedHashSet<>();
final Set<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> set = new LinkedHashSet<>();
set.add(new Pair<>(new Pair<>(1, way1.lastNode()), new Pair<>(0, way2.firstNode())));
DataSet ds = new DataSet();
way1.getNodes().forEach(ds::addPrimitive);
@ -104,7 +107,7 @@ public class MergeDuplicateWaysTest {
assertEquals(2, way1.getNodesCount());
// Test with two nodes in common
Node tNode = new Node(new LatLon(0, 0));
final Node tNode = new Node(new LatLon(0, 0));
ds.addPrimitive(tNode);
way2.addNode(0, tNode);
set.clear(); // we can't use the last pair added
@ -132,7 +135,7 @@ public class MergeDuplicateWaysTest {
way2.getNodes().forEach(ds::addPrimitive);
ds.addPrimitive(way2);
ds.addPrimitive(way1);
List<Node> way2Nodes = way2.getNodes();
final List<Node> way2Nodes = way2.getNodes();
Collections.reverse(way2Nodes);
way2.setNodes(way2Nodes);
set.clear();
@ -165,7 +168,7 @@ public class MergeDuplicateWaysTest {
set.clear();
set.add(new Pair<>(new Pair<>(0, way1.firstNode()), new Pair<>(2, way2.getNode(2))));
set.add(new Pair<>(new Pair<>(1, way1.lastNode()), new Pair<>(3, way2.getNode(3))));
List<Node> currentWay2Nodes = way2.getNodes();
final List<Node> currentWay2Nodes = way2.getNodes();
command = MergeDuplicateWays.mergeWays(way1, way2, set);
for (int i = 0; i < undoRedoTries; i++) {
@ -180,18 +183,22 @@ public class MergeDuplicateWaysTest {
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));
}
/**
* Test method for {@link GetDataRunnable#checkDirection(Set)}.
* Test method for {@link MergeDuplicateWays#checkDirection(Set)}.
*/
@Test
public void testCheckDirection() {
LinkedHashSet<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> set = new LinkedHashSet<>();
Pair<Pair<Integer, Node>, Pair<Integer, Node>> pair1 = new Pair<>(new Pair<>(0, new Node(new LatLon(0, 0))),
new Pair<>(0, new Node(new LatLon(0, 0))));
Pair<Pair<Integer, Node>, Pair<Integer, Node>> pair2 = new Pair<>(new Pair<>(1, new Node(new LatLon(1, 0))),
new Pair<>(1, new Node(new LatLon(1, 0))));
final LinkedHashSet<Pair<Pair<Integer, Node>, Pair<Integer, Node>>> set = new LinkedHashSet<>();
final Pair<Pair<Integer, Node>, Pair<Integer, Node>> pair1 = new Pair<>(
new Pair<>(0, new Node(new LatLon(0, 0))), new Pair<>(0, new Node(new LatLon(0, 0))));
final Pair<Pair<Integer, Node>, Pair<Integer, Node>> pair2 = new Pair<>(
new Pair<>(1, new Node(new LatLon(1, 0))), new Pair<>(1, new Node(new LatLon(1, 0))));
set.add(pair1);
set.add(pair2);
@ -208,10 +215,16 @@ public class MergeDuplicateWaysTest {
pair1.b.a = pair1.b.a + 3;
assertFalse(MergeDuplicateWays.checkDirection(set));
pair1.b.a = pair1.b.a - 2;
assertThrows(NullPointerException.class, () -> MergeDuplicateWays.checkDirection(null));
assertFalse(MergeDuplicateWays.checkDirection(Collections.emptySet()));
set.remove(pair1);
assertFalse(MergeDuplicateWays.checkDirection(set));
}
/**
* Test method for {@link GetDataRunnable#sorted(List)}.
* Test method for {@link MergeDuplicateWays#sorted(List)}.
*/
@Test
public void testSorted() {
@ -227,12 +240,12 @@ public class MergeDuplicateWaysTest {
}
/**
* Test method for {@link GetDataRunnable#getDuplicateNodes(Way, Way)}.
* Test method for {@link MergeDuplicateWays#getDuplicateNodes(Way, Way)}.
*/
@Test
public void testGetDuplicateNodes() {
Way way1 = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
Way way2 = TestUtils.newWay("", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1)));
final Way way1 = 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);
assertEquals(2, duplicateNodes.size());
@ -258,4 +271,36 @@ public class MergeDuplicateWaysTest {
assertEquals(2, duplicateNodes.values().stream().flatMap(col -> col.keySet().stream()).count());
}
/**
* Test method for {@link MergeDuplicateWays#getDescriptionText}
*/
@Test
public void testGetDescriptionText() {
final Command command = new MergeDuplicateWays(new DataSet());
assertNotNull(command.getDescriptionText());
assertFalse(command.getDescriptionText().isEmpty());
}
/**
* Test method for {@link MergeDuplicateWays#nodeInCompressed}
*/
@Test
public void testNodeInCompressed() {
final Node testNode = new Node();
assertThrows(NullPointerException.class, () -> MergeDuplicateWays.nodeInCompressed(testNode, null));
assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, Collections.emptySet()));
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())));
assertSame(testNode, MergeDuplicateWays.nodeInCompressed(testNode, testSet));
Pair<Pair<Integer, Node>, Pair<Integer, Node>> matchPair = new Pair<>(new Pair<>(2, testNode),
new Pair<>(1, new Node()));
testSet.add(matchPair);
assertSame(matchPair.b.b, MergeDuplicateWays.nodeInCompressed(testNode, testSet));
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));
}
}