Fix #23062: NPE in StreetAddressTest#realVisit

Signed-off-by: Taylor Smock <tsmock@meta.com>
pull/24/head
Taylor Smock 2023-07-17 06:36:19 -06:00
rodzic 5a8033b9b0
commit 3412935eaf
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 233BB2E466604E27
2 zmienionych plików z 32 dodań i 21 usunięć

Wyświetl plik

@ -78,8 +78,10 @@ public class StreetAddressTest extends Test {
@Override @Override
public void visit(Node node) { public void visit(Node node) {
if (node.isLatLonKnown()) {
realVisit(node); realVisit(node);
} }
}
@Override @Override
public void endTest() { public void endTest() {
@ -164,7 +166,7 @@ public class StreetAddressTest extends Test {
} }
} }
private static Collection<String> getWayNames(Way way) { private static Collection<String> getWayNames(IPrimitive way) {
return way.getInterestingTags().entrySet().stream() return way.getInterestingTags().entrySet().stream()
.filter(e -> (e.getKey().contains("name") || e.getKey().contains("ref")) .filter(e -> (e.getKey().contains("name") || e.getKey().contains("ref"))
&& !e.getKey().contains("tiger")) && !e.getKey().contains("tiger"))
@ -204,24 +206,25 @@ public class StreetAddressTest extends Test {
* way. * way.
*/ */
static Pair<Way, Double> distanceToWay(Way way, OsmPrimitive prim) { static Pair<Way, Double> distanceToWay(Way way, OsmPrimitive prim) {
final Node[] nodes; final ILatLon[] nodes;
if (prim instanceof Node) { if (prim instanceof Node) {
nodes = new Node[] { (Node) prim }; nodes = new ILatLon[] { (Node) prim };
} else if (prim instanceof Way) { } else if (prim instanceof Way) {
nodes = ((Way) prim).getNodes().toArray(new Node[0]); nodes = ((Way) prim).getNodes().toArray(new ILatLon[0]);
} else if (prim instanceof Relation) { } else if (prim instanceof Relation) {
nodes = ((Relation) prim).getMemberPrimitives().stream().filter(p -> p instanceof Node || p instanceof Way) nodes = ((Relation) prim).getMemberPrimitives().stream()
.flatMap(p -> p instanceof Node ? Stream.of((Node) p) : ((Way) p).getNodes().stream()) .filter(p -> p instanceof ILatLon || p instanceof Way)
.toArray(Node[]::new); .flatMap(p -> p instanceof ILatLon ? Stream.of((ILatLon) p) : ((Way) p).getNodes().stream())
.toArray(ILatLon[]::new);
} else { } else {
throw new IllegalArgumentException("Unknown primitive type: " + prim.getClass()); throw new IllegalArgumentException("Unknown primitive type: " + prim.getClass());
} }
double dist = Double.NaN; double dist = Double.NaN;
List<Node> wayNodes = way.getNodes(); List<? extends INode> wayNodes = way.getNodes();
for (int i = 0; i < wayNodes.size() - 1; i++) { for (int i = 0; i < wayNodes.size() - 1; i++) {
final Node a = wayNodes.get(i); final ILatLon a = wayNodes.get(i);
final Node b = wayNodes.get(i + 1); final ILatLon b = wayNodes.get(i + 1);
for (Node node : nodes) { for (ILatLon node : nodes) {
double tDist = getSegmentNodeDistSq(a, b, node); double tDist = getSegmentNodeDistSq(a, b, node);
if (Double.isNaN(dist) || (!Double.isNaN(tDist) && tDist < dist)) { if (Double.isNaN(dist) || (!Double.isNaN(tDist) && tDist < dist)) {
dist = tDist; dist = tDist;

Wyświetl plik

@ -1,6 +1,7 @@
// License: GPL. For details, see LICENSE file. // License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.data.validation.tests; package org.openstreetmap.josm.plugins.mapwithai.data.validation.tests;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
@ -10,7 +11,6 @@ import java.lang.reflect.Method;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.TestUtils;
import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.AbstractPrimitive; import org.openstreetmap.josm.data.osm.AbstractPrimitive;
@ -19,19 +19,15 @@ import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node; import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.Way; import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.data.validation.OsmValidator; import org.openstreetmap.josm.data.validation.OsmValidator;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences; import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.annotations.Projection;
import org.openstreetmap.josm.tools.Pair; import org.openstreetmap.josm.tools.Pair;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; // Needed due to OsmValidator static initialization initializing tests which in turn need preferences
@BasicPreferences
@BasicPreferences // Needed due to OsmValidator static initialization initializing tests which in @Projection
// turn need preferences
class StreetAddressTestTest { class StreetAddressTestTest {
private final static String ADDR_STREET = "addr:street"; private final static String ADDR_STREET = "addr:street";
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
static JOSMTestRules test = new JOSMTestRules().projection();
@BeforeAll @BeforeAll
static void setup() { static void setup() {
@ -130,4 +126,16 @@ class StreetAddressTestTest {
assertTrue(BBox.bboxesAreFunctionallyEqual(bbox, new BBox(-0.01, -0.01, 0.01, 0.01), 0.0)); assertTrue(BBox.bboxesAreFunctionallyEqual(bbox, new BBox(-0.01, -0.01, 0.01, 0.01), 0.0));
} }
/**
* Non-regression test for
* <a href="https://josm.openstreetmap.de/ticket/23062">#23062</a>
*/
@Test
void testNonRegression23062() {
final StreetAddressTest test = new StreetAddressTest();
final Node node = new Node();
node.put(ADDR_STREET, "Test Road 1");
assertDoesNotThrow(() -> test.visit(node));
assertTrue(test.getErrors().isEmpty());
}
} }