Skip to content

Commit

Permalink
Add WKTReader parse error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed Feb 19, 2025
1 parent 575e022 commit ddcc2c6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,29 @@
* <b>Z</b>|<b> Z</b>|<b>M</b>|<b> M</b>|<b>ZM</b>|<b> ZM</b>
*
* </pre></blockquote>
*
*
* <h3>Examples</h3>
* <pre>
* POINT (0 0)
* POINT EMPTY
* LINESTRING (0 0, 0 1, 1 2)
* LINESTRING EMPTY
* POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
* POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))
* POLYGON EMPTY
* MULTIPOINT ((0 0), (1 1))
* MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))
* MULTIPOLYGON (((1 1, 1 3, 3 3, 3 1, 1 1)), ((4 3, 6 3, 6 1, 4 1, 4 3)))
* GEOMETRYCOLLECTION (MULTIPOINT((0 0), (1 1)), POINT(3 4), LINESTRING(2 3, 3 4))
*
* POINTZ (0 0 0)
* POINT Z (0 0 0)
* POINT Z EMPTY
* POINTM (0 0 0)
* POINT M (0 0 0)
* POINTZM (0 0 0 0)
* POINT ZM (0 0 0 0)
* </pre>
*
*@version 1.7
* @see WKTWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void testBadPlusSign() throws IOException
readWithParseException("POINT ( +1e+01 1X02)");
}

public void testBadNumber() throws IOException
{
readWithParseException("POINT (0x 0)");
readWithParseException("POINT (0e 0)");
readWithParseException("POINT (0.. 0)");
}

public void testBadCharsInType() throws IOException
{
readWithParseException("POINTABC ( 0 0 )");
Expand All @@ -74,7 +81,7 @@ public void testBadCharsInType() throws IOException
readWithParseException("MULTIPOINTABC (( 0 0 ), ( 0 0 ))");
readWithParseException("MULTILINESTRINGABC (( 0 0, 1 1 ), ( 0 0, 1 1 ))");
readWithParseException("MULTIPOLYGONABC ((( 0 0, 1 1, 2 2, 0 0 )), (( 0 0, 1 1, 2 2, 0 0 )))");
readWithParseException("GEOMETRYCOLLECTIONABC (POINT( 0 0 ), LINESTRING( 0 0, 1 1))");
readWithParseException("GEOMETRYCOLLECTIONABC (POINT( 0 0 ), LINESTRING( 0 0, 1 1))");
}

public void testBadCharsInTypeZ() throws IOException
Expand All @@ -99,6 +106,63 @@ public void testBadCharsInTypeZM() throws IOException
readWithParseException("LINESTRINGABCZM ( 0 0 0 0, 1 1 1 1 )");
}

public void testBadType() throws IOException
{
readWithParseException("POIN (0 0)");
readWithParseException("POIN T(0 0)");
readWithParseException("P OINT (0 0)");
readWithParseException("POINtt (0 0)");
readWithParseException("POINTzz (0 0)");
readWithParseException("POINTabc (0 0)");
readWithParseException("POINTxy (0 0)");
readWithParseException("POINT XY (0 0)");
readWithParseException("POINT XY EMPT");
readWithParseException("POINT XY EMPT Y");
readWithParseException("POINT XY EMPTYY");

//-- not an error, since parser stops after correct parse
//checkParseError("POINT EMPTY Z");
}

public void testBadDimension() throws IOException
{
readWithParseException("POINTZZ (0 0 0)");
readWithParseException("POINT ZZ (0 0 0)");
readWithParseException("POINT ZZM (0 0 0)");

readWithParseException("POINT Z M (0 0 0 0)");
readWithParseException("POINTZ M (0 0 0 0)");
readWithParseException("POINT MZ (0 0 0 0)");
readWithParseException("POINTMZ (0 0 0 0)");
readWithParseException("POINTZ ZM (0 0 0 0)");
readWithParseException("POINT ZMc (0 0 0 0)");

//-- not errors; perhaps should be?
//checkParseErrorZ("POINTZ Z (0 0 0)");
//checkParseErrorZM("POINTZM Z (0 0 0 0)");
}

public void testMissingOrdinates() throws IOException
{
readWithParseException("POINT (0)");
readWithParseException("LINESTRING (0, 1 1)");
}

public void testMissingComponents() throws IOException
{
readWithParseException("MULTILINESTRING (0 0)");
readWithParseException("MULTILINESTRING ()");
readWithParseException("GEOMETRYCOLLECTION ()");
readWithParseException("GEOMETRYCOLLECTION");
}

public void testEmptyComponents() throws ParseException, IOException {
readWithInvalidException("POLYGON( EMPTY, (1 1,2 2,1 2,1 1))");

//-- empty rings are valid
//checkInvalidError("POLYGON( (1 1,2 2,1 2,1 1), EMPTY)");
}

private void readWithParseException(String wkt)
throws IOException
{
Expand All @@ -112,5 +176,18 @@ private void readWithParseException(String wkt)
}
assertTrue(threwParseEx);
}

private void readWithInvalidException(String wkt)
throws IOException, ParseException
{
try {
rdr.read(wkt);
}
catch (IllegalArgumentException ex) {
//System.out.println(ex.getMessage());
return;
}
fail();
}
}

0 comments on commit ddcc2c6

Please sign in to comment.