-
Notifications
You must be signed in to change notification settings - Fork 219
/
MultiPolygonCodecTest.java
434 lines (403 loc) · 15.3 KB
/
MultiPolygonCodecTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab
package org.mariadb.jdbc.integration.codec;
import static org.junit.jupiter.api.Assertions.*;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mariadb.jdbc.Statement;
import org.mariadb.jdbc.client.result.CompleteResult;
import org.mariadb.jdbc.type.*;
public class MultiPolygonCodecTest extends CommonCodecTest {
public static org.mariadb.jdbc.Connection geoConn;
private final MultiPolygon ls1 =
new MultiPolygon(
new Polygon[] {
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(1, 1),
new Point(1, 5),
new Point(4, 9),
new Point(6, 9),
new Point(9, 3),
new Point(7, 2),
new Point(1, 1)
},
false),
}),
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(0, 0),
new Point(50, 0),
new Point(50, 50),
new Point(0, 50),
new Point(0, 0)
},
false),
new LineString(
new Point[] {
new Point(10, 10),
new Point(20, 10),
new Point(20, 20),
new Point(10, 20),
new Point(10, 10)
},
false)
})
});
private final MultiPolygon ls2 =
new MultiPolygon(
new Polygon[] {
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(1, 1),
new Point(1, 8),
new Point(4, 9),
new Point(6, 9),
new Point(9, 3),
new Point(7, 2),
new Point(1, 1)
},
false),
})
});
private final MultiPolygon ls3 =
new MultiPolygon(
new Polygon[] {
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(0, 0),
new Point(50, 0),
new Point(50, 50),
new Point(0, 50),
new Point(0, 0)
},
false),
new LineString(
new Point[] {
new Point(10, 10),
new Point(20, 10),
new Point(20, 20),
new Point(10, 20),
new Point(10, 10)
},
false)
})
});
@AfterAll
public static void drop() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("DROP TABLE IF EXISTS MultiPolygonCodec");
stmt.execute("DROP TABLE IF EXISTS MultiPolygonCodec2");
if (geoConn != null) geoConn.close();
}
@BeforeAll
public static void beforeAll2() throws Exception {
drop();
// xpand doesn't recognized MultiPolygon
Assumptions.assumeFalse(isXpand());
Statement stmt = sharedConn.createStatement();
stmt.execute(
"CREATE TABLE MultiPolygonCodec (t1 MultiPolygon, t2 MultiPolygon, t3 MultiPolygon, t4 MultiPolygon)");
stmt.execute(
"INSERT INTO MultiPolygonCodec VALUES "
+ "(ST_MPolyFromText('MULTIPOLYGON(((1 1, 1 5,4 9,6 9,9 3,7 2, 1 1)), ((0 0, 50 0,50 50,0 50,0 0), (10 10,20 10,20 20,10 20,10 10)))'), "
+ "ST_MPolyFromText('MULTIPOLYGON(((1 1, 1 8,4 9,6 9,9 3,7 2, 1 1)))'), "
+ "ST_MPolyFromText('MULTIPOLYGON(((0 0, 50 0,50 50,0 50,0 0), (10 10,20 10,20 20,10 20,10 10)))'), null)");
stmt.execute(
"CREATE TABLE MultiPolygonCodec2 (id int not null primary key auto_increment, t1 MultiPolygon)");
stmt.execute("FLUSH TABLES");
String binUrl =
mDefUrl + (mDefUrl.indexOf("?") > 0 ? "&" : "?") + "geometryDefaultType=default";
geoConn = (org.mariadb.jdbc.Connection) DriverManager.getConnection(binUrl);
}
private ResultSet get() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER
ResultSet rs =
stmt.executeQuery(
"select t1 as t1alias, t2 as t2alias, t3 as t3alias, t4 as t4alias from MultiPolygonCodec");
assertTrue(rs.next());
sharedConn.commit();
return rs;
}
private CompleteResult getPrepare(org.mariadb.jdbc.Connection con) throws SQLException {
java.sql.Statement stmt = con.createStatement();
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER
PreparedStatement preparedStatement =
con.prepareStatement(
"select t1 as t1alias, t2 as t2alias, t3 as t3alias, t4 as t4alias from MultiPolygonCodec"
+ " WHERE 1 > ?");
preparedStatement.closeOnCompletion();
preparedStatement.setInt(1, 0);
CompleteResult rs = (CompleteResult) preparedStatement.executeQuery();
assertTrue(rs.next());
con.commit();
return rs;
}
@Test
public void getObject() throws Exception {
getObject(get(), false);
}
@Test
public void getObjectPrepare() throws Exception {
getObject(getPrepare(sharedConn), false);
getObject(getPrepare(sharedConnBinary), false);
getObject(getPrepare(geoConn), true);
}
public void getObject(ResultSet rs, boolean defaultGeo) throws SQLException {
if (defaultGeo
&& isMariaDBServer()
&& minVersion(10, 5, 1)
&& !"maxscale".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv"))) {
assertEquals(ls1, rs.getObject(1));
assertFalse(rs.wasNull());
assertEquals(ls2, rs.getObject(2));
assertFalse(rs.wasNull());
assertEquals(ls3, rs.getObject(3));
assertFalse(rs.wasNull());
assertNull(rs.getObject(4));
assertTrue(rs.wasNull());
} else {
assertEquals(ls1, rs.getObject(1, MultiPolygon.class));
assertFalse(rs.wasNull());
assertEquals(ls2, rs.getObject(2, MultiPolygon.class));
assertFalse(rs.wasNull());
assertEquals(ls3, rs.getObject(3, MultiPolygon.class));
assertFalse(rs.wasNull());
assertNull(rs.getObject(4));
assertTrue(rs.wasNull());
}
}
@Test
public void getObjectType() throws Exception {
getObjectType(get());
}
@Test
public void getObjectTypePrepare() throws Exception {
getObjectType(getPrepare(sharedConn));
getObjectType(getPrepare(sharedConnBinary));
}
private static int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if (digit == -1) {
throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar);
}
return digit;
}
public static byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
public static byte[] decodeHexString(String hexString) {
if (hexString.length() % 2 == 1) {
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
}
return bytes;
}
public void getObjectType(ResultSet rs) throws Exception {
testErrObject(rs, Integer.class, 1);
testErrObject(rs, String.class, 1);
testErrObject(rs, Long.class, 1);
testErrObject(rs, Short.class, 1);
testErrObject(rs, BigDecimal.class, 1);
testErrObject(rs, BigInteger.class, 1);
testErrObject(rs, Double.class, 1);
testErrObject(rs, Float.class, 1);
testErrObject(rs, Byte.class, 1);
String hexa =
"0000000001060000000200000001030000000100000007000000000000000000F03F000000000000F03F000000000000F03F00000000000014400000000000001040000000000000224000000000000018400000000000002240000000000000224000000000000008400000000000001C400000000000000040000000000000F03F000000000000F03F010300000002000000050000000000000000000000000000000000000000000000000049400000000000000000000000000000494000000000000049400000000000000000000000000000494000000000000000000000000000000000050000000000000000002440000000000000244000000000000034400000000000002440000000000000344000000000000034400000000000002440000000000000344000000000000024400000000000002440";
testArrObject(rs, decodeHexString(hexa), 1);
testErrObject(rs, Boolean.class, 1);
testErrObject(rs, Clob.class, 1);
testErrObject(rs, NClob.class, 1);
testErrObject(rs, InputStream.class, 1);
testErrObject(rs, Reader.class, 1);
testErrObject(rs, java.util.Date.class, 1);
}
@Test
public void getMetaData() throws SQLException {
getMetaData(sharedConn, false);
try (org.mariadb.jdbc.Connection con = createCon("geometryDefaultType=default")) {
getMetaData(con, true);
}
}
private void getMetaData(org.mariadb.jdbc.Connection con, boolean geoDefault)
throws SQLException {
ResultSet rs = getPrepare(con);
ResultSetMetaData meta = rs.getMetaData();
if (isMariaDBServer()
&& minVersion(10, 5, 1)
&& !"maxscale".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv"))) {
assertEquals("MULTIPOLYGON", meta.getColumnTypeName(1));
} else {
assertEquals("GEOMETRY", meta.getColumnTypeName(1));
}
assertEquals(sharedConn.getCatalog(), meta.getCatalogName(1));
assertEquals(
geoDefault
? ((isMariaDBServer()
&& minVersion(10, 5, 1)
&& !"maxscale".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv")))
? MultiPolygon.class.getName()
: GeometryCollection.class.getName())
: "byte[]",
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
assertEquals(Types.VARBINARY, meta.getColumnType(1));
assertEquals(4, meta.getColumnCount());
assertEquals(0, meta.getScale(1));
assertEquals("", meta.getSchemaName(1));
}
@Test
public void sendParam() throws Exception {
sendParam(sharedConn);
sendParam(sharedConnBinary);
}
private void sendParam(Connection con) throws Exception {
java.sql.Statement stmt = con.createStatement();
stmt.execute("TRUNCATE TABLE MultiPolygonCodec2");
stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER
try (PreparedStatement prep =
con.prepareStatement("INSERT INTO MultiPolygonCodec2(t1) VALUES (?)")) {
prep.setObject(1, ls1);
prep.execute();
prep.setObject(1, (MultiPolygon) null);
prep.execute();
prep.setObject(1, ls2);
prep.addBatch();
prep.setObject(1, ls1);
prep.addBatch();
prep.executeBatch();
}
ResultSet rs =
con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)
.executeQuery("SELECT * FROM MultiPolygonCodec2");
assertTrue(rs.next());
assertEquals(ls1, rs.getObject(2, MultiPolygon.class));
rs.updateNull(2);
rs.updateRow();
assertNull(rs.getObject(2, MultiPolygon.class));
assertTrue(rs.next());
assertNull(rs.getObject(2, MultiPolygon.class));
rs.updateObject(2, ls2);
rs.updateRow();
assertEquals(ls2, rs.getObject(2, MultiPolygon.class));
assertTrue(rs.next());
assertEquals(ls2, rs.getObject(2, MultiPolygon.class));
assertTrue(rs.next());
assertEquals(ls1, rs.getObject(2, MultiPolygon.class));
con.commit();
}
@Test
public void equal() {
assertEquals(ls1, ls1);
MultiPolygon testPoly =
new MultiPolygon(
new Polygon[] {
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(1, 1),
new Point(1, 5),
new Point(4, 9),
new Point(6, 9),
new Point(9, 3),
new Point(7, 2),
new Point(1, 1)
},
false),
}),
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(0, 0),
new Point(50, 0),
new Point(50, 50),
new Point(0, 50),
new Point(0, 0)
},
false),
new LineString(
new Point[] {
new Point(10, 10),
new Point(20, 10),
new Point(20, 20),
new Point(10, 20),
new Point(10, 10)
},
false)
})
});
assertEquals(testPoly, ls1);
assertEquals(testPoly.hashCode(), ls1.hashCode());
assertFalse(ls1.equals(null));
assertFalse(ls1.equals(""));
assertNotEquals(
new MultiPolygon(
new Polygon[] {
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(1, 1),
new Point(1, 5),
new Point(4, 9),
new Point(6, 9),
new Point(9, 3),
new Point(7, 2),
new Point(1, 1)
},
false),
}),
new Polygon(
new LineString[] {
new LineString(
new Point[] {
new Point(0, 0),
new Point(50, 0),
new Point(50, 50),
new Point(0, 50),
new Point(0, 0)
},
false),
new LineString(
new Point[] {
new Point(10, 10),
new Point(20, 10),
new Point(20, 20),
new Point(10, 20),
new Point(10, 15)
},
false)
})
}),
ls1);
}
}