2828package 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+
3240import org .slf4j .Logger ;
3341import org .slf4j .LoggerFactory ;
3442import org .testng .Assert ;
3745import org .testng .annotations .BeforeClass ;
3846import 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
4460public 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