| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.data.validation.tests; |
| | 3 | |
| | 4 | import static org.junit.Assert.assertEquals; |
| | 5 | import static org.junit.Assert.assertFalse; |
| | 6 | import static org.junit.Assert.assertNull; |
| | 7 | import static org.junit.Assert.assertSame; |
| | 8 | import static org.junit.Assert.assertTrue; |
| | 9 | |
| | 10 | import java.util.Arrays; |
| | 11 | import java.util.Collection; |
| | 12 | import java.util.Collections; |
| | 13 | import java.util.HashSet; |
| | 14 | import java.util.Set; |
| | 15 | |
| | 16 | import org.junit.Rule; |
| | 17 | import org.junit.Test; |
| | 18 | import org.openstreetmap.josm.TestUtils; |
| | 19 | import org.openstreetmap.josm.data.Bounds; |
| | 20 | import org.openstreetmap.josm.data.DataSource; |
| | 21 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 22 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 23 | import org.openstreetmap.josm.data.osm.Node; |
| | 24 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 25 | import org.openstreetmap.josm.data.osm.Way; |
| | 26 | import org.openstreetmap.josm.spi.preferences.Config; |
| | 27 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 28 | |
| | 29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| | 30 | |
| | 31 | /** |
| | 32 | * Test class for {@link RoutingIslandsTest} |
| | 33 | * |
| | 34 | * @author Taylor Smock |
| | 35 | * @since xxx |
| | 36 | */ |
| | 37 | public class RoutingIslandsTestTest { |
| | 38 | /** |
| | 39 | * Setup test. |
| | 40 | */ |
| | 41 | @Rule |
| | 42 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| | 43 | public JOSMTestRules rule = new JOSMTestRules().projection().preferences(); |
| | 44 | |
| | 45 | /** |
| | 46 | * Test method for {@link RoutingIslandsTest#RoutingIslandsTest()} and the |
| | 47 | * testing apparatus |
| | 48 | */ |
| | 49 | @Test |
| | 50 | public void testRoutingIslandsTest() { |
| | 51 | RoutingIslandsTest test = new RoutingIslandsTest(); |
| | 52 | test.startTest(null); |
| | 53 | test.endTest(); |
| | 54 | assertTrue(test.getErrors().isEmpty()); |
| | 55 | |
| | 56 | DataSet ds = new DataSet(); |
| | 57 | |
| | 58 | Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); |
| | 59 | Way way2 = TestUtils.newWay("highway=residential", new Node(new LatLon(-1, 0)), way1.firstNode()); |
| | 60 | addToDataSet(ds, way1); |
| | 61 | addToDataSet(ds, way2); |
| | 62 | |
| | 63 | ds.addDataSource(new DataSource(new Bounds(0, 0, 1, 1), "openstreetmap.org")); |
| | 64 | |
| | 65 | test.clear(); |
| | 66 | test.startTest(null); |
| | 67 | test.visit(ds.allPrimitives()); |
| | 68 | test.endTest(); |
| | 69 | assertTrue(test.getErrors().isEmpty()); |
| | 70 | |
| | 71 | ds.addDataSource(new DataSource(new Bounds(-5, -5, 5, 5), "openstreetmap.org")); |
| | 72 | test.clear(); |
| | 73 | test.startTest(null); |
| | 74 | test.visit(ds.allPrimitives()); |
| | 75 | test.endTest(); |
| | 76 | assertEquals(1, test.getErrors().size()); |
| | 77 | assertEquals(2, test.getErrors().get(0).getPrimitives().size()); |
| | 78 | |
| | 79 | ds.clear(); |
| | 80 | way1 = TestUtils.newWay("highway=motorway oneway=yes", new Node(new LatLon(39.1156655, -108.5465434)), |
| | 81 | new Node(new LatLon(39.1157251, -108.5496874)), new Node(new LatLon(39.11592, -108.5566841))); |
| | 82 | way2 = TestUtils.newWay("highway=motorway oneway=yes", new Node(new LatLon(39.1157244, -108.55674)), |
| | 83 | new Node(new LatLon(39.1155548, -108.5496901)), new Node(new LatLon(39.1154827, -108.5462431))); |
| | 84 | addToDataSet(ds, way1); |
| | 85 | addToDataSet(ds, way2); |
| | 86 | ds.addDataSource( |
| | 87 | new DataSource(new Bounds(new LatLon(39.1161391, -108.5516083), new LatLon(39.1136949, -108.5489166)), |
| | 88 | "openstreetmap.org")); |
| | 89 | test.clear(); |
| | 90 | test.startTest(null); |
| | 91 | test.visit(ds.allPrimitives()); |
| | 92 | test.endTest(); |
| | 93 | assertTrue(test.getErrors().isEmpty()); |
| | 94 | Way way3 = TestUtils.newWay("highway=service", way1.getNode(1), way2.getNode(2)); |
| | 95 | addToDataSet(ds, way3); |
| | 96 | test.startTest(null); |
| | 97 | test.visit(ds.allPrimitives()); |
| | 98 | test.endTest(); |
| | 99 | assertTrue(test.getErrors().isEmpty()); |
| | 100 | } |
| | 101 | |
| | 102 | /** |
| | 103 | * Test roundabouts |
| | 104 | */ |
| | 105 | @Test |
| | 106 | public void testRoundabouts() { |
| | 107 | RoutingIslandsTest test = new RoutingIslandsTest(); |
| | 108 | Way roundabout = TestUtils.newWay("highway=residential junction=roundabout oneway=yes", |
| | 109 | new Node(new LatLon(39.119582, -108.5262686)), new Node(new LatLon(39.1196494, -108.5260935)), |
| | 110 | new Node(new LatLon(39.1197572, -108.5260784)), new Node(new LatLon(39.1197929, -108.526391)), |
| | 111 | new Node(new LatLon(39.1196595, -108.5264047))); |
| | 112 | roundabout.addNode(roundabout.firstNode()); // close it up |
| | 113 | DataSet ds = new DataSet(); |
| | 114 | addToDataSet(ds, roundabout); |
| | 115 | ds.addDataSource( |
| | 116 | new DataSource(new Bounds(new LatLon(39.1182025, -108.527574), new LatLon(39.1210588, -108.5251112)), |
| | 117 | "openstreetmap.org")); |
| | 118 | Way incomingFlare = TestUtils.newWay("highway=residential oneway=yes", |
| | 119 | new Node(new LatLon(39.1196377, -108.5257567)), roundabout.getNode(3)); |
| | 120 | addToDataSet(ds, incomingFlare); |
| | 121 | Way outgoingFlare = TestUtils.newWay("highway=residential oneway=yes", roundabout.getNode(2), |
| | 122 | incomingFlare.firstNode()); |
| | 123 | addToDataSet(ds, outgoingFlare); |
| | 124 | |
| | 125 | Way outgoingRoad = TestUtils.newWay("highway=residential", incomingFlare.firstNode(), |
| | 126 | new Node(new LatLon(39.1175184, -108.5219623))); |
| | 127 | addToDataSet(ds, outgoingRoad); |
| | 128 | |
| | 129 | test.startTest(null); |
| | 130 | test.visit(ds.allPrimitives()); |
| | 131 | test.endTest(); |
| | 132 | assertTrue(test.getErrors().isEmpty()); |
| | 133 | } |
| | 134 | |
| | 135 | private static void addToDataSet(DataSet ds, Way way) { |
| | 136 | way.getNodes().parallelStream().distinct().filter(node -> node.getDataSet() == null).forEach(ds::addPrimitive); |
| | 137 | if (way.getDataSet() == null) |
| | 138 | ds.addPrimitive(way); |
| | 139 | Long id = Math.max(ds.allPrimitives().parallelStream().mapToLong(prim -> prim.getId()).max().orElse(0L), 0L); |
| | 140 | for (OsmPrimitive osm : ds.allPrimitives()) { |
| | 141 | id++; |
| | 142 | osm.setOsmId(id, 1); |
| | 143 | } |
| | 144 | } |
| | 145 | |
| | 146 | /** |
| | 147 | * Test method for |
| | 148 | * {@link RoutingIslandsTest#checkForUnconnectedWays(Collection, Collection, String)}. |
| | 149 | */ |
| | 150 | @Test |
| | 151 | public void testCheckForUnconnectedWaysIncoming() { |
| | 152 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), Collections.emptySet(), null); |
| | 153 | Way way1 = TestUtils.newWay("highway=residential oneway=yes", new Node(new LatLon(0, 0)), |
| | 154 | new Node(new LatLon(1, 1))); |
| | 155 | Set<Way> incomingSet = new HashSet<>(); |
| | 156 | DataSet ds = new DataSet(); |
| | 157 | way1.getNodes().forEach(ds::addPrimitive); |
| | 158 | ds.addPrimitive(way1); |
| | 159 | incomingSet.add(way1); |
| | 160 | RoutingIslandsTest.checkForUnconnectedWays(incomingSet, Collections.emptySet(), null); |
| | 161 | assertEquals(1, incomingSet.size()); |
| | 162 | assertSame(way1, incomingSet.iterator().next()); |
| | 163 | |
| | 164 | Way way2 = TestUtils.newWay("highway=residential", way1.firstNode(), new Node(new LatLon(-1, -2))); |
| | 165 | way2.getNodes().parallelStream().filter(node -> node.getDataSet() == null).forEach(ds::addPrimitive); |
| | 166 | ds.addPrimitive(way2); |
| | 167 | |
| | 168 | RoutingIslandsTest.checkForUnconnectedWays(incomingSet, Collections.emptySet(), null); |
| | 169 | assertEquals(2, incomingSet.size()); |
| | 170 | assertTrue(incomingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2).contains(way))); |
| | 171 | |
| | 172 | Way way3 = TestUtils.newWay("highway=residential", way2.lastNode(), new Node(new LatLon(-2, -1))); |
| | 173 | way3.getNodes().parallelStream().filter(node -> node.getDataSet() == null).forEach(ds::addPrimitive); |
| | 174 | ds.addPrimitive(way3); |
| | 175 | |
| | 176 | incomingSet.clear(); |
| | 177 | incomingSet.add(way1); |
| | 178 | RoutingIslandsTest.checkForUnconnectedWays(incomingSet, Collections.emptySet(), null); |
| | 179 | assertEquals(3, incomingSet.size()); |
| | 180 | assertTrue(incomingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2, way3).contains(way))); |
| | 181 | |
| | 182 | Config.getPref().putInt("validator.routingislands.maxrecursion", 1); |
| | 183 | incomingSet.clear(); |
| | 184 | incomingSet.add(way1); |
| | 185 | RoutingIslandsTest.checkForUnconnectedWays(incomingSet, Collections.emptySet(), null); |
| | 186 | assertEquals(2, incomingSet.size()); |
| | 187 | assertTrue(incomingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2).contains(way))); |
| | 188 | } |
| | 189 | |
| | 190 | @Test |
| | 191 | public void testCheckForUnconnectedWaysOutgoing() { |
| | 192 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), Collections.emptySet(), null); |
| | 193 | Way way1 = TestUtils.newWay("highway=residential oneway=yes", new Node(new LatLon(0, 0)), |
| | 194 | new Node(new LatLon(1, 1))); |
| | 195 | Set<Way> outgoingSet = new HashSet<>(); |
| | 196 | DataSet ds = new DataSet(); |
| | 197 | way1.getNodes().forEach(ds::addPrimitive); |
| | 198 | ds.addPrimitive(way1); |
| | 199 | outgoingSet.add(way1); |
| | 200 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), outgoingSet, null); |
| | 201 | assertEquals(1, outgoingSet.size()); |
| | 202 | assertSame(way1, outgoingSet.iterator().next()); |
| | 203 | |
| | 204 | Way way2 = TestUtils.newWay("highway=residential", way1.firstNode(), new Node(new LatLon(-1, -2))); |
| | 205 | way2.getNodes().parallelStream().filter(node -> node.getDataSet() == null).forEach(ds::addPrimitive); |
| | 206 | ds.addPrimitive(way2); |
| | 207 | |
| | 208 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), outgoingSet, null); |
| | 209 | assertEquals(2, outgoingSet.size()); |
| | 210 | assertTrue(outgoingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2).contains(way))); |
| | 211 | |
| | 212 | Way way3 = TestUtils.newWay("highway=residential", way2.lastNode(), new Node(new LatLon(-2, -1))); |
| | 213 | way3.getNodes().parallelStream().filter(node -> node.getDataSet() == null).forEach(ds::addPrimitive); |
| | 214 | ds.addPrimitive(way3); |
| | 215 | |
| | 216 | outgoingSet.clear(); |
| | 217 | outgoingSet.add(way1); |
| | 218 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), outgoingSet, null); |
| | 219 | assertEquals(3, outgoingSet.size()); |
| | 220 | assertTrue(outgoingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2, way3).contains(way))); |
| | 221 | |
| | 222 | Config.getPref().putInt("validator.routingislands.maxrecursion", 1); |
| | 223 | outgoingSet.clear(); |
| | 224 | outgoingSet.add(way1); |
| | 225 | RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), outgoingSet, null); |
| | 226 | assertEquals(2, outgoingSet.size()); |
| | 227 | assertTrue(outgoingSet.parallelStream().allMatch(way -> Arrays.asList(way1, way2).contains(way))); |
| | 228 | } |
| | 229 | |
| | 230 | /** |
| | 231 | * Test method for {@link RoutingIslandsTest#outsideConnections(Node)}. |
| | 232 | */ |
| | 233 | @Test |
| | 234 | public void testOutsideConnections() { |
| | 235 | Node node = new Node(new LatLon(0, 0)); |
| | 236 | DataSet ds = new DataSet(node); |
| | 237 | ds.addDataSource(new DataSource(new Bounds(-0.1, -0.1, -0.01, -0.01), "Test bounds")); |
| | 238 | node.setOsmId(1, 1); |
| | 239 | assertTrue(RoutingIslandsTest.outsideConnections(node)); |
| | 240 | ds.addDataSource(new DataSource(new Bounds(-0.1, -0.1, 0.1, 0.1), "Test bounds")); |
| | 241 | assertFalse(RoutingIslandsTest.outsideConnections(node)); |
| | 242 | node.put("amenity", "parking_entrance"); |
| | 243 | assertTrue(RoutingIslandsTest.outsideConnections(node)); |
| | 244 | } |
| | 245 | |
| | 246 | /** |
| | 247 | * Test method for {@link RoutingIslandsTest#isOneway(Way, String)}. |
| | 248 | */ |
| | 249 | @Test |
| | 250 | public void testIsOneway() { |
| | 251 | Way way = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); |
| | 252 | assertEquals(Integer.valueOf(0), RoutingIslandsTest.isOneway(way, null)); |
| | 253 | assertEquals(Integer.valueOf(0), RoutingIslandsTest.isOneway(way, " ")); |
| | 254 | way.put("oneway", "yes"); |
| | 255 | assertEquals(Integer.valueOf(1), RoutingIslandsTest.isOneway(way, null)); |
| | 256 | assertEquals(Integer.valueOf(1), RoutingIslandsTest.isOneway(way, " ")); |
| | 257 | way.put("oneway", "-1"); |
| | 258 | assertEquals(Integer.valueOf(-1), RoutingIslandsTest.isOneway(way, null)); |
| | 259 | assertEquals(Integer.valueOf(-1), RoutingIslandsTest.isOneway(way, " ")); |
| | 260 | |
| | 261 | way.put("vehicle:forward", "yes"); |
| | 262 | assertEquals(Integer.valueOf(0), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 263 | way.put("vehicle:backward", "no"); |
| | 264 | assertEquals(Integer.valueOf(1), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 265 | way.put("vehicle:forward", "no"); |
| | 266 | assertNull(RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 267 | way.put("vehicle:backward", "yes"); |
| | 268 | assertEquals(Integer.valueOf(-1), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 269 | |
| | 270 | way.put("oneway", "yes"); |
| | 271 | way.remove("vehicle:backward"); |
| | 272 | way.remove("vehicle:forward"); |
| | 273 | assertEquals(Integer.valueOf(1), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 274 | way.remove("oneway"); |
| | 275 | assertEquals(Integer.valueOf(0), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 276 | |
| | 277 | way.put("oneway", "-1"); |
| | 278 | assertEquals(Integer.valueOf(-1), RoutingIslandsTest.isOneway(way, "vehicle")); |
| | 279 | } |
| | 280 | |
| | 281 | /** |
| | 282 | * Test method for {@link RoutingIslandsTest#firstNode(Way, String)}. |
| | 283 | */ |
| | 284 | @Test |
| | 285 | public void testFirstNode() { |
| | 286 | Way way = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); |
| | 287 | assertEquals(way.firstNode(), RoutingIslandsTest.firstNode(way, null)); |
| | 288 | way.put("oneway", "yes"); |
| | 289 | assertEquals(way.firstNode(), RoutingIslandsTest.firstNode(way, null)); |
| | 290 | way.put("oneway", "-1"); |
| | 291 | assertEquals(way.lastNode(), RoutingIslandsTest.firstNode(way, null)); |
| | 292 | |
| | 293 | way.put("vehicle:forward", "yes"); |
| | 294 | assertEquals(way.firstNode(), RoutingIslandsTest.firstNode(way, "vehicle")); |
| | 295 | way.put("vehicle:backward", "no"); |
| | 296 | assertEquals(way.firstNode(), RoutingIslandsTest.firstNode(way, "vehicle")); |
| | 297 | way.put("vehicle:forward", "no"); |
| | 298 | assertEquals(way.firstNode(), RoutingIslandsTest.firstNode(way, "vehicle")); |
| | 299 | way.put("vehicle:backward", "yes"); |
| | 300 | assertEquals(way.lastNode(), RoutingIslandsTest.firstNode(way, "vehicle")); |
| | 301 | } |
| | 302 | |
| | 303 | /** |
| | 304 | * Test method for {@link RoutingIslandsTest#lastNode(Way, String)}. |
| | 305 | */ |
| | 306 | @Test |
| | 307 | public void testLastNode() { |
| | 308 | Way way = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(1, 1))); |
| | 309 | assertEquals(way.lastNode(), RoutingIslandsTest.lastNode(way, null)); |
| | 310 | way.put("oneway", "yes"); |
| | 311 | assertEquals(way.lastNode(), RoutingIslandsTest.lastNode(way, null)); |
| | 312 | way.put("oneway", "-1"); |
| | 313 | assertEquals(way.firstNode(), RoutingIslandsTest.lastNode(way, null)); |
| | 314 | |
| | 315 | way.put("vehicle:forward", "yes"); |
| | 316 | assertEquals(way.lastNode(), RoutingIslandsTest.lastNode(way, "vehicle")); |
| | 317 | way.put("vehicle:backward", "no"); |
| | 318 | assertEquals(way.lastNode(), RoutingIslandsTest.lastNode(way, "vehicle")); |
| | 319 | way.put("vehicle:forward", "no"); |
| | 320 | assertEquals(way.lastNode(), RoutingIslandsTest.lastNode(way, "vehicle")); |
| | 321 | way.put("vehicle:backward", "yes"); |
| | 322 | assertEquals(way.firstNode(), RoutingIslandsTest.lastNode(way, "vehicle")); |
| | 323 | } |
| | 324 | |
| | 325 | } |