Skip to content

Commit 164808e

Browse files
author
taylor.smock
committed
Fix #22600: NPE: cannot invoke "EastNorth.isValid" because "common" is null
git-svn-id: https://josm.openstreetmap.de/svn/trunk@18619 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent 1713f29 commit 164808e

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

src/org/openstreetmap/josm/data/validation/tests/SharpAngles.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ public void checkWayForSharpAngles(Way way) {
9191
}
9292

9393
private void checkAngle(Node node1, Node node2, Node node3, int i, Way way, boolean last) {
94-
if (node1 == null || node2 == null || node3 == null) return;
94+
if (node1 == null || !node1.isLatLonKnown()
95+
|| node2 == null || !node2.isLatLonKnown()
96+
|| node3 == null || !node3.isLatLonKnown()) {
97+
return;
98+
}
9599
EastNorth n1 = node1.getEastNorth();
96100
EastNorth n2 = node2.getEastNorth();
97101
EastNorth n3 = node3.getEastNorth();

test/unit/org/openstreetmap/josm/data/validation/tests/SharpAnglesTest.java

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
// License: GPL. For details, see LICENSE file.
22
package org.openstreetmap.josm.data.validation.tests;
33

4-
import org.junit.Assert;
4+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import java.util.Arrays;
8+
import java.util.stream.Stream;
9+
510
import org.junit.jupiter.api.BeforeEach;
611
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.MethodSource;
715
import org.openstreetmap.josm.JOSMFixture;
816
import org.openstreetmap.josm.TestUtils;
17+
import org.openstreetmap.josm.data.coor.EastNorth;
918
import org.openstreetmap.josm.data.coor.LatLon;
1019
import org.openstreetmap.josm.data.osm.Node;
1120
import org.openstreetmap.josm.data.osm.Way;
21+
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
1222

1323
/**
1424
* JUnit Test of the Sharp Angles validation test.
1525
*/
26+
@BasicPreferences
1627
class SharpAnglesTest {
1728
private SharpAngles angles;
1829

@@ -37,7 +48,7 @@ void testClosedLoopNoSharpAngles() {
3748
new Node(new LatLon(0.1, -0.2)), new Node(new LatLon(-0.1, -0.1)));
3849
way.addNode(way.firstNode());
3950
angles.visit(way);
40-
Assert.assertEquals(0, angles.getErrors().size());
51+
assertEquals(0, angles.getErrors().size());
4152
}
4253

4354
/**
@@ -51,7 +62,7 @@ void testClosedLoopSharpAngles() {
5162
way.addNode(way.firstNode());
5263
angles.setMaxLength(Double.MAX_VALUE);
5364
angles.visit(way);
54-
Assert.assertEquals(1, angles.getErrors().size());
65+
assertEquals(1, angles.getErrors().size());
5566
}
5667

5768
/**
@@ -66,7 +77,7 @@ void testMultipleSharpAngles() {
6677
new Node(new LatLon(0.005031826787678042, 0.0020116540789620915)));
6778
angles.setMaxLength(Double.MAX_VALUE);
6879
angles.visit(way);
69-
Assert.assertEquals(2, angles.getErrors().size());
80+
assertEquals(2, angles.getErrors().size());
7081
}
7182

7283
/**
@@ -78,7 +89,7 @@ void testNoSharpAngles() {
7889
new Node(new LatLon(0, 0)), new Node(new LatLon(0.1, 0.1)),
7990
new Node(new LatLon(0.2, 0.3)), new Node(new LatLon(0.3, 0.1)));
8091
angles.visit(way);
81-
Assert.assertEquals(0, angles.getErrors().size());
92+
assertEquals(0, angles.getErrors().size());
8293
}
8394

8495
/**
@@ -97,7 +108,7 @@ void testCheckBadAnglesFromSameNodeTwice() {
97108
new Node(new LatLon(52.8902482, 8.4307568)));
98109
way.addNode(way.firstNode());
99110
angles.visit(way);
100-
Assert.assertEquals(0, angles.getErrors().size());
111+
assertEquals(0, angles.getErrors().size());
101112
}
102113

103114
/**
@@ -110,22 +121,55 @@ void testIgnoredCases() {
110121
new Node(new LatLon(0, 0.01)));
111122
angles.setMaxLength(Double.MAX_VALUE);
112123
angles.visit(way);
113-
Assert.assertEquals(1, angles.getErrors().size());
124+
assertEquals(1, angles.getErrors().size());
114125
angles.getErrors().clear();
115126

116127
way.put("highway", "rest_area");
117128
angles.visit(way);
118-
Assert.assertEquals(0, angles.getErrors().size());
129+
assertEquals(0, angles.getErrors().size());
119130

120131
way.put("highway", "residential");
121132
angles.visit(way);
122-
Assert.assertEquals(1, angles.getErrors().size());
133+
assertEquals(1, angles.getErrors().size());
123134
angles.getErrors().clear();
124135
way.put("area", "yes");
125136
angles.visit(way);
126-
Assert.assertEquals(0, angles.getErrors().size());
137+
assertEquals(0, angles.getErrors().size());
127138
way.put("area", "no");
128139
angles.visit(way);
129-
Assert.assertEquals(1, angles.getErrors().size());
140+
assertEquals(1, angles.getErrors().size());
141+
}
142+
143+
static Stream<Arguments> testNonRegression22600() {
144+
return Stream.of(
145+
// All invalid nodes
146+
Arguments.of(new Node(), new Node(), new Node()),
147+
// Single valid nodes
148+
Arguments.of(new Node(LatLon.SOUTH_POLE), new Node(), new Node()),
149+
Arguments.of(new Node(), new Node(LatLon.ZERO), new Node()),
150+
Arguments.of(new Node(), new Node(), new Node(LatLon.NORTH_POLE)),
151+
// Two valid nodes
152+
Arguments.of(new Node(), new Node(LatLon.ZERO), new Node(LatLon.NORTH_POLE)),
153+
Arguments.of(new Node(LatLon.SOUTH_POLE), new Node(LatLon.ZERO), new Node()),
154+
Arguments.of(new Node(LatLon.SOUTH_POLE), new Node(), new Node(LatLon.NORTH_POLE)),
155+
// All valid nodes
156+
Arguments.of(new Node(LatLon.SOUTH_POLE), new Node(LatLon.ZERO), new Node(LatLon.NORTH_POLE))
157+
);
158+
}
159+
160+
/**
161+
* Non-regression test for #22600: NPE: Cannot invoke {@link EastNorth#isValid()} because "common" is null
162+
*/
163+
@ParameterizedTest
164+
@MethodSource
165+
void testNonRegression22600(Node node1, Node node2, Node node3) {
166+
Way invalidWay = new Way();
167+
invalidWay.put("highway", "trunk");
168+
invalidWay.setNodes(Arrays.asList(node1, node2, node3));
169+
assertDoesNotThrow(() -> this.angles.visit(invalidWay));
170+
for (Node node : invalidWay.getNodes()) {
171+
node.setCoor(LatLon.ZERO);
172+
assertDoesNotThrow(() -> this.angles.visit(invalidWay));
173+
}
130174
}
131175
}

0 commit comments

Comments
 (0)