| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.data.validation.tests; |
| | 3 | |
| | 4 | import java.util.ArrayList; |
| | 5 | import java.util.List; |
| | 6 | |
| | 7 | import org.junit.Assert; |
| | 8 | import org.junit.BeforeClass; |
| | 9 | import org.junit.Test; |
| | 10 | import org.openstreetmap.josm.JOSMFixture; |
| | 11 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 12 | import org.openstreetmap.josm.data.osm.Node; |
| | 13 | import org.openstreetmap.josm.data.osm.OsmUtils; |
| | 14 | import org.openstreetmap.josm.data.osm.Way; |
| | 15 | |
| | 16 | /** |
| | 17 | * JUnit Test of Multipolygon validation test. |
| | 18 | */ |
| | 19 | public class SelfIntersectingWayTest { |
| | 20 | |
| | 21 | /** |
| | 22 | * Setup test. |
| | 23 | * @throws Exception if test cannot be initialized |
| | 24 | */ |
| | 25 | @BeforeClass |
| | 26 | public static void setUp() throws Exception { |
| | 27 | JOSMFixture.createUnitTestFixture().init(true); |
| | 28 | } |
| | 29 | |
| | 30 | private static List<Node> createNodes() { |
| | 31 | List<Node> nodes = new ArrayList<>(); |
| | 32 | for (int i = 0; i < 5; i++) { |
| | 33 | nodes.add(new Node(i+1)); |
| | 34 | } |
| | 35 | nodes.get(0).setCoor(new LatLon(34.2680878298, 133.56336369008)); |
| | 36 | nodes.get(1).setCoor(new LatLon(34.25096598132, 133.54891792012)); |
| | 37 | nodes.get(2).setCoor(new LatLon(34.24466741332, 133.56693544639)); |
| | 38 | nodes.get(3).setCoor(new LatLon(34.26815342405, 133.56066502976)); |
| | 39 | nodes.get(4).setCoor(new LatLon(34.26567411471, 133.56132705125)); |
| | 40 | return nodes; |
| | 41 | } |
| | 42 | |
| | 43 | |
| | 44 | /** |
| | 45 | * Self-Intersection at first (+ last) node of closed way |
| | 46 | */ |
| | 47 | @Test |
| | 48 | public void TestClosedWay() { |
| | 49 | List<Node> nodes = createNodes(); |
| | 50 | |
| | 51 | Way w = (Way) OsmUtils.createPrimitive("way "); |
| | 52 | List<Node> wayNodes = new ArrayList<>(); |
| | 53 | wayNodes.add(nodes.get(0)); |
| | 54 | wayNodes.add(nodes.get(1)); |
| | 55 | wayNodes.add(nodes.get(2)); |
| | 56 | wayNodes.add(nodes.get(0)); // problem |
| | 57 | wayNodes.add(nodes.get(3)); |
| | 58 | wayNodes.add(nodes.get(4)); |
| | 59 | wayNodes.add(nodes.get(0)); |
| | 60 | w.setNodes(wayNodes); |
| | 61 | SelfIntersectingWay test = new SelfIntersectingWay(); |
| | 62 | test.visit(w); |
| | 63 | Assert.assertEquals(1, test.getErrors().size()); |
| | 64 | Assert.assertTrue(test.getErrors().iterator().next().getHighlighted().contains(nodes.get(0))); |
| | 65 | } |
| | 66 | |
| | 67 | /** |
| | 68 | * Self-Intersection at first node of unclosed way |
| | 69 | */ |
| | 70 | @Test |
| | 71 | public void TestUnclosedWayFirst() { |
| | 72 | List<Node> nodes = createNodes(); |
| | 73 | |
| | 74 | Way w = (Way) OsmUtils.createPrimitive("way "); |
| | 75 | List<Node> wayNodes = new ArrayList<>(); |
| | 76 | wayNodes.add(nodes.get(0)); |
| | 77 | wayNodes.add(nodes.get(1)); |
| | 78 | wayNodes.add(nodes.get(2)); |
| | 79 | wayNodes.add(nodes.get(0)); // problem |
| | 80 | wayNodes.add(nodes.get(3)); |
| | 81 | wayNodes.add(nodes.get(4)); |
| | 82 | w.setNodes(wayNodes); |
| | 83 | SelfIntersectingWay test = new SelfIntersectingWay(); |
| | 84 | test.visit(w); |
| | 85 | Assert.assertEquals(1, test.getErrors().size()); |
| | 86 | Assert.assertTrue(test.getErrors().iterator().next().getHighlighted().contains(nodes.get(0))); |
| | 87 | } |
| | 88 | |
| | 89 | /** |
| | 90 | * Self-Intersection at first node of unclosed way |
| | 91 | */ |
| | 92 | @Test |
| | 93 | public void TestUnclosedWayLast() { |
| | 94 | List<Node> nodes = createNodes(); |
| | 95 | |
| | 96 | Way w = (Way) OsmUtils.createPrimitive("way "); |
| | 97 | List<Node> wayNodes = new ArrayList<>(); |
| | 98 | wayNodes.add(nodes.get(0)); |
| | 99 | wayNodes.add(nodes.get(1)); // problem |
| | 100 | wayNodes.add(nodes.get(2)); |
| | 101 | wayNodes.add(nodes.get(3)); |
| | 102 | wayNodes.add(nodes.get(4)); |
| | 103 | wayNodes.add(nodes.get(1)); |
| | 104 | w.setNodes(wayNodes); |
| | 105 | SelfIntersectingWay test = new SelfIntersectingWay(); |
| | 106 | test.visit(w); |
| | 107 | Assert.assertEquals(1, test.getErrors().size()); |
| | 108 | Assert.assertTrue(test.getErrors().iterator().next().getHighlighted().contains(nodes.get(1))); |
| | 109 | } |
| | 110 | |
| | 111 | /** |
| | 112 | * Self-Intersection at normal node (not first / last) |
| | 113 | */ |
| | 114 | @Test |
| | 115 | public void TestUnclosedWayNormal() { |
| | 116 | List<Node> nodes = createNodes(); |
| | 117 | |
| | 118 | Way w = (Way) OsmUtils.createPrimitive("way "); |
| | 119 | List<Node> wayNodes = new ArrayList<>(); |
| | 120 | wayNodes.add(nodes.get(0)); |
| | 121 | wayNodes.add(nodes.get(1)); |
| | 122 | wayNodes.add(nodes.get(2)); |
| | 123 | wayNodes.add(nodes.get(1)); // problem |
| | 124 | wayNodes.add(nodes.get(3)); |
| | 125 | wayNodes.add(nodes.get(4)); |
| | 126 | w.setNodes(wayNodes); |
| | 127 | SelfIntersectingWay test = new SelfIntersectingWay(); |
| | 128 | test.visit(w); |
| | 129 | Assert.assertEquals(1, test.getErrors().size()); |
| | 130 | Assert.assertTrue(test.getErrors().iterator().next().getHighlighted().contains(nodes.get(1))); |
| | 131 | } |
| | 132 | |
| | 133 | } |