Skip to content

Commit ddcc2c6

Browse files
committed
Add WKTReader parse error tests
1 parent 575e022 commit ddcc2c6

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

modules/core/src/main/java/org/locationtech/jts/io/WKTReader.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,29 @@
137137
* <b>Z</b>|<b> Z</b>|<b>M</b>|<b> M</b>|<b>ZM</b>|<b> ZM</b>
138138
*
139139
* </pre></blockquote>
140-
*
140+
*
141+
* <h3>Examples</h3>
142+
* <pre>
143+
* POINT (0 0)
144+
* POINT EMPTY
145+
* LINESTRING (0 0, 0 1, 1 2)
146+
* LINESTRING EMPTY
147+
* POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
148+
* POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))
149+
* POLYGON EMPTY
150+
* MULTIPOINT ((0 0), (1 1))
151+
* MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))
152+
* MULTIPOLYGON (((1 1, 1 3, 3 3, 3 1, 1 1)), ((4 3, 6 3, 6 1, 4 1, 4 3)))
153+
* GEOMETRYCOLLECTION (MULTIPOINT((0 0), (1 1)), POINT(3 4), LINESTRING(2 3, 3 4))
154+
*
155+
* POINTZ (0 0 0)
156+
* POINT Z (0 0 0)
157+
* POINT Z EMPTY
158+
* POINTM (0 0 0)
159+
* POINT M (0 0 0)
160+
* POINTZM (0 0 0 0)
161+
* POINT ZM (0 0 0 0)
162+
* </pre>
141163
*
142164
*@version 1.7
143165
* @see WKTWriter

modules/core/src/test/java/org/locationtech/jts/io/WKTReaderParseErrorTest.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public void testBadPlusSign() throws IOException
6565
readWithParseException("POINT ( +1e+01 1X02)");
6666
}
6767

68+
public void testBadNumber() throws IOException
69+
{
70+
readWithParseException("POINT (0x 0)");
71+
readWithParseException("POINT (0e 0)");
72+
readWithParseException("POINT (0.. 0)");
73+
}
74+
6875
public void testBadCharsInType() throws IOException
6976
{
7077
readWithParseException("POINTABC ( 0 0 )");
@@ -74,7 +81,7 @@ public void testBadCharsInType() throws IOException
7481
readWithParseException("MULTIPOINTABC (( 0 0 ), ( 0 0 ))");
7582
readWithParseException("MULTILINESTRINGABC (( 0 0, 1 1 ), ( 0 0, 1 1 ))");
7683
readWithParseException("MULTIPOLYGONABC ((( 0 0, 1 1, 2 2, 0 0 )), (( 0 0, 1 1, 2 2, 0 0 )))");
77-
readWithParseException("GEOMETRYCOLLECTIONABC (POINT( 0 0 ), LINESTRING( 0 0, 1 1))");
84+
readWithParseException("GEOMETRYCOLLECTIONABC (POINT( 0 0 ), LINESTRING( 0 0, 1 1))");
7885
}
7986

8087
public void testBadCharsInTypeZ() throws IOException
@@ -99,6 +106,63 @@ public void testBadCharsInTypeZM() throws IOException
99106
readWithParseException("LINESTRINGABCZM ( 0 0 0 0, 1 1 1 1 )");
100107
}
101108

109+
public void testBadType() throws IOException
110+
{
111+
readWithParseException("POIN (0 0)");
112+
readWithParseException("POIN T(0 0)");
113+
readWithParseException("P OINT (0 0)");
114+
readWithParseException("POINtt (0 0)");
115+
readWithParseException("POINTzz (0 0)");
116+
readWithParseException("POINTabc (0 0)");
117+
readWithParseException("POINTxy (0 0)");
118+
readWithParseException("POINT XY (0 0)");
119+
readWithParseException("POINT XY EMPT");
120+
readWithParseException("POINT XY EMPT Y");
121+
readWithParseException("POINT XY EMPTYY");
122+
123+
//-- not an error, since parser stops after correct parse
124+
//checkParseError("POINT EMPTY Z");
125+
}
126+
127+
public void testBadDimension() throws IOException
128+
{
129+
readWithParseException("POINTZZ (0 0 0)");
130+
readWithParseException("POINT ZZ (0 0 0)");
131+
readWithParseException("POINT ZZM (0 0 0)");
132+
133+
readWithParseException("POINT Z M (0 0 0 0)");
134+
readWithParseException("POINTZ M (0 0 0 0)");
135+
readWithParseException("POINT MZ (0 0 0 0)");
136+
readWithParseException("POINTMZ (0 0 0 0)");
137+
readWithParseException("POINTZ ZM (0 0 0 0)");
138+
readWithParseException("POINT ZMc (0 0 0 0)");
139+
140+
//-- not errors; perhaps should be?
141+
//checkParseErrorZ("POINTZ Z (0 0 0)");
142+
//checkParseErrorZM("POINTZM Z (0 0 0 0)");
143+
}
144+
145+
public void testMissingOrdinates() throws IOException
146+
{
147+
readWithParseException("POINT (0)");
148+
readWithParseException("LINESTRING (0, 1 1)");
149+
}
150+
151+
public void testMissingComponents() throws IOException
152+
{
153+
readWithParseException("MULTILINESTRING (0 0)");
154+
readWithParseException("MULTILINESTRING ()");
155+
readWithParseException("GEOMETRYCOLLECTION ()");
156+
readWithParseException("GEOMETRYCOLLECTION");
157+
}
158+
159+
public void testEmptyComponents() throws ParseException, IOException {
160+
readWithInvalidException("POLYGON( EMPTY, (1 1,2 2,1 2,1 1))");
161+
162+
//-- empty rings are valid
163+
//checkInvalidError("POLYGON( (1 1,2 2,1 2,1 1), EMPTY)");
164+
}
165+
102166
private void readWithParseException(String wkt)
103167
throws IOException
104168
{
@@ -112,5 +176,18 @@ private void readWithParseException(String wkt)
112176
}
113177
assertTrue(threwParseEx);
114178
}
179+
180+
private void readWithInvalidException(String wkt)
181+
throws IOException, ParseException
182+
{
183+
try {
184+
rdr.read(wkt);
185+
}
186+
catch (IllegalArgumentException ex) {
187+
//System.out.println(ex.getMessage());
188+
return;
189+
}
190+
fail();
191+
}
115192
}
116193

0 commit comments

Comments
 (0)