Skip to content

Commit 6f2c67d

Browse files
authored
Merge pull request #86 from tobias-/empty_collections
Failing test cases for empty collections
2 parents a2b7329 + 3d59a11 commit 6f2c67d

File tree

6 files changed

+201
-14
lines changed

6 files changed

+201
-14
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 4
9+
indent_style = space
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
spaces_around_operators = true
13+
max_line_length = 130

postgis-jdbc-geometry/src/main/java/net/postgis/jdbc/geometry/ComposedGeom.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ public abstract class ComposedGeom extends Geometry {
4545
/* JDK 1.5 Serialization */
4646
private static final long serialVersionUID = 0x100;
4747

48-
public static final Geometry[] EMPTY = new Geometry[0];
49-
5048
/**
5149
* The Array containing the geometries
5250
*
5351
* This is only to be exposed by concrete subclasses, to retain type safety.
5452
*/
55-
protected Geometry[] subgeoms = EMPTY;
53+
protected Geometry[] subgeoms = createSubGeomArray(0);
5654

5755
/**
5856
* Constructs an instance with the specified type
@@ -104,6 +102,11 @@ protected ComposedGeom(int type, String value, boolean haveM) throws SQLExceptio
104102
return;
105103
}
106104

105+
if (value.equals("EMPTY")) {
106+
// Empty collection
107+
return;
108+
}
109+
107110
String valueNoParans = GeometryTokenizer.removeLeadingAndTrailingStrings(value, "(", ")");
108111
List<String> tokens = GeometryTokenizer.tokenize(valueNoParans, ',');
109112

postgis-jdbc-geometry/src/main/java/net/postgis/jdbc/geometry/GeometryBuilder.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ public static Geometry geomFromString(String value, BinaryParser bp, boolean hav
5757
Geometry result;
5858
if (value.startsWith("00") || value.startsWith("01")) {
5959
result = bp.parse(value);
60-
} else if (value.endsWith("EMPTY")) {
61-
// We have a standard conforming representation for an empty
62-
// geometry which is to be parsed as an empty GeometryCollection.
63-
result = new GeometryCollection();
6460
} else if (value.startsWith("MULTIPOLYGON")) {
6561
result = new MultiPolygon(value, haveM);
6662
} else if (value.startsWith("MULTILINESTRING")) {

postgis-jdbc-geometry/src/main/java/net/postgis/jdbc/geometry/Point.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ protected Point(String value, boolean haveM) throws SQLException {
181181
}
182182
String valueNoParans = GeometryTokenizer.removeLeadingAndTrailingStrings(value, "(", ")");
183183
List<String> tokens = GeometryTokenizer.tokenize(valueNoParans, ' ');
184+
if (tokens.size() == 1 && "EMPTY".equals(tokens.get(0))) {
185+
return;
186+
}
184187
try {
185188
x = Double.valueOf(tokens.get(0)).doubleValue();
186189
y = Double.valueOf(tokens.get(1)).doubleValue();

postgis-jdbc-geometry/src/test/java/net/postgis/jdbc/geometry/DatatypesTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
* You should have received a copy of the GNU Lesser General Public
2323
* License along with this library; if not, write to the Free Software
2424
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25-
*
25+
*
2626
*/
2727

2828
package net.postgis.jdbc.geometry;
2929

3030

31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertTrue;
33+
3134
import org.slf4j.Logger;
3235
import org.slf4j.LoggerFactory;
3336
import org.testng.annotations.Test;
@@ -106,4 +109,34 @@ public void testMultiLineString() throws SQLException {
106109
}
107110

108111

109-
}
112+
@Test
113+
public void testCreateAndGetPoints() {
114+
assertNotNull(new LineString().getPoints());
115+
assertNotNull(new MultiLineString().getLines());
116+
assertNotNull(new LinearRing(new Point[0]).getPoints());
117+
assertNotNull(new MultiPoint().getPoints());
118+
assertNotNull(new MultiPolygon().getPolygons());
119+
}
120+
121+
122+
@Test
123+
public void testParseEmpty() throws SQLException {
124+
assertTrue(GeometryBuilder.geomFromString("POINT EMPTY") instanceof Point);
125+
assertTrue(GeometryBuilder.geomFromString("POLYGON EMPTY") instanceof Polygon);
126+
assertTrue(GeometryBuilder.geomFromString("LINESTRING EMPTY") instanceof LineString);
127+
assertTrue(GeometryBuilder.geomFromString("MULTILINESTRING EMPTY") instanceof MultiLineString);
128+
assertTrue(GeometryBuilder.geomFromString("MULTIPOINT EMPTY") instanceof MultiPoint);
129+
assertTrue(GeometryBuilder.geomFromString("MULTIPOLYGON EMPTY") instanceof MultiPolygon);
130+
}
131+
132+
@Test
133+
public void testCreateEmpty() throws SQLException {
134+
new Point("POINT EMPTY");
135+
new Polygon("POLYGON EMPTY");
136+
new LineString("LINESTRING EMPTY");
137+
new MultiLineString("MULTILINESTRING EMPTY");
138+
new MultiPoint("MULTIPOINT EMPTY");
139+
new MultiPolygon("MULTIPOLYGON EMPTY");
140+
}
141+
142+
}

postgis-jdbc/src/test/java/net/postgis/jdbc/ServerTest.java

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
package net.postgis.jdbc;
2929

3030

31-
import net.postgis.tools.testutils.TestContainerController;
31+
import java.sql.Connection;
32+
import java.sql.DatabaseMetaData;
33+
import java.sql.DriverManager;
34+
import java.sql.PreparedStatement;
35+
import java.sql.ResultSet;
36+
import java.sql.SQLException;
37+
import java.sql.Statement;
38+
import java.util.UUID;
39+
3240
import org.slf4j.Logger;
3341
import org.slf4j.LoggerFactory;
3442
import org.testng.Assert;
@@ -37,8 +45,16 @@
3745
import org.testng.annotations.BeforeClass;
3846
import org.testng.annotations.Test;
3947

40-
import java.sql.*;
41-
import java.util.UUID;
48+
import net.postgis.jdbc.geometry.Geometry;
49+
import net.postgis.jdbc.geometry.GeometryBuilder;
50+
import net.postgis.jdbc.geometry.LineString;
51+
import net.postgis.jdbc.geometry.LinearRing;
52+
import net.postgis.jdbc.geometry.MultiLineString;
53+
import net.postgis.jdbc.geometry.MultiPoint;
54+
import net.postgis.jdbc.geometry.MultiPolygon;
55+
import net.postgis.jdbc.geometry.Point;
56+
import net.postgis.jdbc.geometry.Polygon;
57+
import net.postgis.tools.testutils.TestContainerController;
4258

4359

4460
public class ServerTest {
@@ -96,6 +112,126 @@ public void testServer() throws Exception {
96112

97113
}
98114

115+
@Test
116+
public void testColumnTypeSafetyNonEmpty1() throws SQLException {
117+
String tableName = "polygraph_" + UUID.randomUUID().toString().replace('-', '_');
118+
119+
String createSQL = "create table " + tableName + " (point geometry(point,4326), polygon geometry(polygon,4326), line_string geometry(linestring,4326), multi_line_string geometry(multilinestring,4326), multi_point geometry(multipoint,4326), multi_polygon geometry(multipolygon,4326));";
120+
String dropSQL = "drop table " + tableName;
121+
122+
statement.execute(createSQL);
123+
124+
final PreparedStatement prep = connection
125+
.prepareStatement("INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?)");
126+
prep.setObject(1, new PGgeometryLW(new Point("SRID=4326;POINT(2.8 1.7)")));
127+
prep.setObject(2, new PGgeometryLW(new Polygon("SRID=4326;POLYGON((2 2, 2 -2, -2 -2, -2 2, 2 2))")));
128+
prep.setObject(3, new PGgeometryLW(new LineString("SRID=4326;LINESTRING(0 0, 1 2)")));
129+
prep.setObject(4, new PGgeometryLW(new MultiLineString("SRID=4326;MULTILINESTRING((0 0, 1 2), (1 2, 3 -1))")));
130+
prep.setObject(5, new PGgeometryLW(new MultiPoint("SRID=4326;MULTIPOINT((2 3), (7 8))")));
131+
prep.setObject(6, new PGgeometryLW(new MultiPolygon("SRID=4326;MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))")));
132+
133+
prep.execute();
134+
statement.execute(dropSQL);
135+
}
136+
137+
@Test
138+
public void testColumnTypeSafetyNonEmpty2() throws SQLException {
139+
140+
String tableName = "polygraph_" + UUID.randomUUID().toString().replace('-', '_');
141+
142+
String createSQL = "create table " + tableName + " (point geometry(point,4326), polygon geometry(polygon,4326), line_string geometry(linestring,4326), multi_line_string geometry(multilinestring,4326), multi_point geometry(multipoint,4326), multi_polygon geometry(multipolygon,4326));";
143+
String dropSQL = "drop table " + tableName;
144+
145+
statement.execute(createSQL);
146+
147+
final PreparedStatement prep = connection
148+
.prepareStatement("INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?)");
149+
150+
prep.setObject(1, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;POINT(2.8 1.7)")));
151+
prep.setObject(2, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;POLYGON((2 2, 2 -2, -2 -2, -2 2, 2 2))")));
152+
prep.setObject(3, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;LINESTRING(0 0, 1 2)")));
153+
prep.setObject(4, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;MULTILINESTRING((0 0, 1 2), (1 2, 3 -1))")));
154+
prep.setObject(5, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;MULTIPOINT((2 3), (7 8))")));
155+
prep.setObject(6, new PGgeometryLW(
156+
GeometryBuilder.geomFromString("SRID=4326;MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))")
157+
));
158+
159+
prep.execute();
160+
statement.execute(dropSQL);
161+
}
162+
163+
164+
@Test
165+
public void testColumnTypeSafetyEmpty1() throws SQLException {
166+
String tableName = "polygraph_" + UUID.randomUUID().toString().replace('-', '_');
167+
168+
String createSQL = "create table " + tableName + " (point geometry(point,4326), polygon geometry(polygon,4326), line_string geometry(linestring,4326), multi_line_string geometry(multilinestring,4326), multi_point geometry(multipoint,4326), multi_polygon geometry(multipolygon,4326));";
169+
String dropSQL = "drop table " + tableName;
170+
171+
statement.execute(createSQL);
172+
173+
final PreparedStatement prep = connection
174+
.prepareStatement("INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?)");
175+
176+
prep.setObject(1, withSRID(new Point(), 4326));
177+
prep.setObject(2, withSRID(new Polygon(), 4326));
178+
prep.setObject(3, withSRID(new LineString(), 4326));
179+
prep.setObject(4, withSRID(new MultiLineString(), 4326));
180+
prep.setObject(5, withSRID(new MultiPoint(), 4326));
181+
prep.setObject(6, withSRID(new MultiPolygon(), 4326));
182+
183+
prep.execute();
184+
statement.execute(dropSQL);
185+
}
186+
187+
@Test
188+
public void testColumnTypeSafetyEmpty2() throws SQLException {
189+
190+
String tableName = "polygraph_" + UUID.randomUUID().toString().replace('-', '_');
191+
192+
String createSQL = "create table " + tableName + " (point geometry(point,4326), polygon geometry(polygon,4326), line_string geometry(linestring,4326), multi_line_string geometry(multilinestring,4326), multi_point geometry(multipoint,4326), multi_polygon geometry(multipolygon,4326));";
193+
String dropSQL = "drop table " + tableName;
194+
195+
statement.execute(createSQL);
196+
197+
final PreparedStatement prep = connection
198+
.prepareStatement("INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?)");
199+
200+
prep.setObject(1, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;POINT EMPTY")));
201+
prep.setObject(2, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;POLYGON EMPTY")));
202+
prep.setObject(3, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;LINESTRING EMPTY")));
203+
prep.setObject(4, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;MULTILINESTRING EMPTY")));
204+
prep.setObject(5, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;MULTIPOINT EMPTY")));
205+
prep.setObject(6, new PGgeometryLW(GeometryBuilder.geomFromString("SRID=4326;MULTIPOLYGON EMPTY")));
206+
207+
prep.execute();
208+
statement.execute(dropSQL);
209+
}
210+
211+
212+
@Test
213+
public void testColumnTypeSafetyEmpty3() throws SQLException {
214+
String tableName = "polygraph_" + UUID.randomUUID().toString().replace('-', '_');
215+
216+
String createSQL = "create table " + tableName + " (point geometry(point,4326), polygon geometry(polygon,4326), line_string geometry(linestring,4326), multi_line_string geometry(multilinestring,4326), multi_point geometry(multipoint,4326), multi_polygon geometry(multipolygon,4326));";
217+
String dropSQL = "drop table " + tableName;
218+
219+
statement.execute(createSQL);
220+
221+
final PreparedStatement prep = connection
222+
.prepareStatement("INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?)");
223+
224+
prep.setObject(1, withSRID(new Point(), 4326));
225+
prep.setObject(2, withSRID(new Polygon(new LinearRing[0]), 4326));
226+
prep.setObject(3, withSRID(new LineString(new Point[0]), 4326));
227+
prep.setObject(4, withSRID(new MultiLineString(new LineString[0]), 4326));
228+
prep.setObject(5, withSRID(new MultiPoint(new Point[0]), 4326));
229+
prep.setObject(6, withSRID(new MultiPolygon(new Polygon[0]), 4326));
230+
231+
prep.execute();
232+
statement.execute(dropSQL);
233+
}
234+
99235

100236
@BeforeClass
101237
public void initJdbcConnection(ITestContext ctx) throws Exception {
@@ -122,5 +258,8 @@ public void unallocateDatabaseResources() throws Exception {
122258
}
123259
}
124260

125-
126-
}
261+
private static PGgeometryLW withSRID(Geometry geo, int srid) {
262+
geo.setSrid(srid);
263+
return new PGgeometryLW(geo);
264+
}
265+
}

0 commit comments

Comments
 (0)