/
GridJdbcResultSetSelfTest.java
715 lines (554 loc) · 19.2 KB
/
GridJdbcResultSetSelfTest.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.jdbc;
import org.apache.ignite.cache.*;
import org.apache.ignite.cache.query.*;
import org.apache.ignite.configuration.*;
import org.apache.ignite.spi.discovery.tcp.*;
import org.apache.ignite.spi.discovery.tcp.ipfinder.*;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.testframework.*;
import org.apache.ignite.testframework.junits.common.*;
import java.io.*;
import java.math.*;
import java.net.*;
import java.sql.Date;
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import static org.apache.ignite.cache.GridCacheMode.*;
import static org.apache.ignite.cache.GridCacheWriteSynchronizationMode.*;
/**
* Result set test.
*/
@SuppressWarnings("FloatingPointEquality")
public class GridJdbcResultSetSelfTest extends GridCommonAbstractTest {
/** IP finder. */
private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
/** URL. */
private static final String URL = "jdbc:gridgain://127.0.0.1/";
/** SQL query. */
private static final String SQL =
"select id, boolVal, byteVal, shortVal, intVal, longVal, floatVal, " +
"doubleVal, bigVal, strVal, arrVal, dateVal, timeVal, tsVal, urlVal, f1, f2, f3, _val " +
"from TestObject where id = 1";
/** Statement. */
private Statement stmt;
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(gridName);
CacheConfiguration cache = defaultCacheConfiguration();
cache.setCacheMode(PARTITIONED);
cache.setBackups(1);
cache.setWriteSynchronizationMode(FULL_SYNC);
cfg.setCacheConfiguration(cache);
TcpDiscoverySpi disco = new TcpDiscoverySpi();
disco.setIpFinder(IP_FINDER);
cfg.setDiscoverySpi(disco);
cfg.setRestEnabled(true);
return cfg;
}
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
startGridsMultiThreaded(3);
GridCache<Integer, TestObject> cache = grid(0).cache(null);
assert cache != null;
TestObject o = createObjectWithData(1);
cache.put(1, o);
cache.put(2, new TestObject(2));
Class.forName("org.apache.ignite.jdbc.IgniteJdbcDriver");
}
/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
stopAllGrids();
}
/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
stmt = DriverManager.getConnection(URL).createStatement();
assert stmt != null;
assert !stmt.isClosed();
}
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
if (stmt != null) {
stmt.getConnection().close();
stmt.close();
assert stmt.isClosed();
}
}
/**
* @param id ID.
* @return Object.
* @throws MalformedURLException If URL in incorrect.
*/
@SuppressWarnings("deprecation")
private TestObject createObjectWithData(int id) throws MalformedURLException {
TestObject o = new TestObject(id);
o.boolVal = true;
o.byteVal = 1;
o.shortVal = 1;
o.intVal = 1;
o.longVal = 1L;
o.floatVal = 1.0f;
o.doubleVal = 1.0d;
o.bigVal = new BigDecimal(1);
o.strVal = "str";
o.arrVal = new byte[] {1};
o.dateVal = new Date(1, 1, 1);
o.timeVal = new Time(1, 1, 1);
o.tsVal = new Timestamp(1);
o.urlVal = new URL("http://abc.com/");
return o;
}
/**
* @throws Exception If failed.
*/
public void testBoolean() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getBoolean("boolVal");
assert rs.getBoolean(2);
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testByte() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getByte("byteVal") == 1;
assert rs.getByte(3) == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testShort() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getShort("shortVal") == 1;
assert rs.getShort(4) == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testInteger() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getInt("intVal") == 1;
assert rs.getInt(5) == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testLong() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getLong("longVal") == 1;
assert rs.getLong(6) == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testFloat() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getFloat("floatVal") == 1.0;
assert rs.getFloat(7) == 1.0;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testDouble() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getDouble("doubleVal") == 1.0;
assert rs.getDouble(8) == 1.0;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testBigDecimal() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getBigDecimal("bigVal").intValue() == 1;
assert rs.getBigDecimal(9).intValue() == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testString() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert "str".equals(rs.getString("strVal"));
assert "str".equals(rs.getString(10));
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testArray() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert Arrays.equals(rs.getBytes("arrVal"), new byte[] {1});
assert Arrays.equals(rs.getBytes(11), new byte[] {1});
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
@SuppressWarnings("deprecation")
public void testDate() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getDate("dateVal").equals(new Date(1, 1, 1));
assert rs.getDate(12).equals(new Date(1, 1, 1));
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
@SuppressWarnings("deprecation")
public void testTime() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getTime("timeVal").equals(new Time(1, 1, 1));
assert rs.getTime(13).equals(new Time(1, 1, 1));
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testTimestamp() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert rs.getTimestamp("tsVal").getTime() == 1;
assert rs.getTimestamp(14).getTime() == 1;
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testUrl() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
int cnt = 0;
while (rs.next()) {
if (cnt == 0) {
assert "http://abc.com/".equals(rs.getURL("urlVal").toString());
assert "http://abc.com/".equals(rs.getURL(15).toString());
}
cnt++;
}
assert cnt == 1;
}
/**
* @throws Exception If failed.
*/
public void testObject() throws Exception {
ResultSet rs = stmt.executeQuery(SQL);
TestObjectField f1 = new TestObjectField(100, "AAAA");
TestObjectField f2 = new TestObjectField(500, "BBBB");
TestObject o = createObjectWithData(1);
assertTrue(rs.next());
assertEquals(f1.toString(), rs.getObject("f1"));
assertEquals(f1.toString(), rs.getObject(16));
assertEquals(f2.toString(), rs.getObject("f2"));
assertEquals(f2.toString(), rs.getObject(17));
assertNull(rs.getObject("f3"));
assertTrue(rs.wasNull());
assertNull(rs.getObject(18));
assertTrue(rs.wasNull());
assertEquals(o.toString(), rs.getObject("_val"));
assertEquals(o.toString(), rs.getObject(19));
assertFalse(rs.next());
}
/**
* @throws Exception If failed.
*/
public void testNavigation() throws Exception {
ResultSet rs = stmt.executeQuery("select * from TestObject where id > 0");
assert rs.isBeforeFirst();
assert !rs.isAfterLast();
assert !rs.isFirst();
assert !rs.isLast();
assert rs.getRow() == 0;
assert rs.next();
assert !rs.isBeforeFirst();
assert !rs.isAfterLast();
assert rs.isFirst();
assert !rs.isLast();
assert rs.getRow() == 1;
assert rs.next();
assert !rs.isBeforeFirst();
assert !rs.isAfterLast();
assert !rs.isFirst();
assert rs.isLast();
assert rs.getRow() == 2;
assert !rs.next();
assert !rs.isBeforeFirst();
assert rs.isAfterLast();
assert !rs.isFirst();
assert !rs.isLast();
assert rs.getRow() == 0;
}
/**
* @throws Exception If failed.
*/
public void testFindColumn() throws Exception {
final ResultSet rs = stmt.executeQuery(SQL);
assert rs != null;
assert rs.next();
assert rs.findColumn("id") == 1;
GridTestUtils.assertThrows(
log,
new Callable<Object>() {
@Override public Object call() throws Exception {
rs.findColumn("wrong");
return null;
}
},
SQLException.class,
"Column not found: wrong"
);
}
/**
* Test object.
*/
@SuppressWarnings("UnusedDeclaration")
private static class TestObject implements Serializable {
/** */
@GridCacheQuerySqlField
private final int id;
/** */
@GridCacheQuerySqlField(index = false)
private Boolean boolVal;
/** */
@GridCacheQuerySqlField(index = false)
private Byte byteVal;
/** */
@GridCacheQuerySqlField(index = false)
private Short shortVal;
/** */
@GridCacheQuerySqlField(index = false)
private Integer intVal;
/** */
@GridCacheQuerySqlField(index = false)
private Long longVal;
/** */
@GridCacheQuerySqlField(index = false)
private Float floatVal;
/** */
@GridCacheQuerySqlField(index = false)
private Double doubleVal;
/** */
@GridCacheQuerySqlField(index = false)
private BigDecimal bigVal;
/** */
@GridCacheQuerySqlField(index = false)
private String strVal;
/** */
@GridCacheQuerySqlField(index = false)
private byte[] arrVal;
/** */
@GridCacheQuerySqlField(index = false)
private Date dateVal;
/** */
@GridCacheQuerySqlField(index = false)
private Time timeVal;
/** */
@GridCacheQuerySqlField(index = false)
private Timestamp tsVal;
/** */
@GridCacheQuerySqlField(index = false)
private URL urlVal;
/** */
@GridCacheQuerySqlField(index = false)
private TestObjectField f1 = new TestObjectField(100, "AAAA");
/** */
@GridCacheQuerySqlField(index = false)
private TestObjectField f2 = new TestObjectField(500, "BBBB");
/** */
@GridCacheQuerySqlField(index = false)
private TestObjectField f3;
/**
* @param id ID.
*/
private TestObject(int id) {
this.id = id;
}
/** {@inheritDoc} */
@Override public String toString() {
return S.toString(TestObject.class, this);
}
/** {@inheritDoc} */
@SuppressWarnings({"BigDecimalEquals", "EqualsHashCodeCalledOnUrl", "RedundantIfStatement"})
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestObject that = (TestObject)o;
if (id != that.id) return false;
if (!Arrays.equals(arrVal, that.arrVal)) return false;
if (bigVal != null ? !bigVal.equals(that.bigVal) : that.bigVal != null) return false;
if (boolVal != null ? !boolVal.equals(that.boolVal) : that.boolVal != null) return false;
if (byteVal != null ? !byteVal.equals(that.byteVal) : that.byteVal != null) return false;
if (dateVal != null ? !dateVal.equals(that.dateVal) : that.dateVal != null) return false;
if (doubleVal != null ? !doubleVal.equals(that.doubleVal) : that.doubleVal != null) return false;
if (f1 != null ? !f1.equals(that.f1) : that.f1 != null) return false;
if (f2 != null ? !f2.equals(that.f2) : that.f2 != null) return false;
if (f3 != null ? !f3.equals(that.f3) : that.f3 != null) return false;
if (floatVal != null ? !floatVal.equals(that.floatVal) : that.floatVal != null) return false;
if (intVal != null ? !intVal.equals(that.intVal) : that.intVal != null) return false;
if (longVal != null ? !longVal.equals(that.longVal) : that.longVal != null) return false;
if (shortVal != null ? !shortVal.equals(that.shortVal) : that.shortVal != null) return false;
if (strVal != null ? !strVal.equals(that.strVal) : that.strVal != null) return false;
if (timeVal != null ? !timeVal.equals(that.timeVal) : that.timeVal != null) return false;
if (tsVal != null ? !tsVal.equals(that.tsVal) : that.tsVal != null) return false;
if (urlVal != null ? !urlVal.equals(that.urlVal) : that.urlVal != null) return false;
return true;
}
/** {@inheritDoc} */
@SuppressWarnings("EqualsHashCodeCalledOnUrl")
@Override public int hashCode() {
int res = id;
res = 31 * res + (boolVal != null ? boolVal.hashCode() : 0);
res = 31 * res + (byteVal != null ? byteVal.hashCode() : 0);
res = 31 * res + (shortVal != null ? shortVal.hashCode() : 0);
res = 31 * res + (intVal != null ? intVal.hashCode() : 0);
res = 31 * res + (longVal != null ? longVal.hashCode() : 0);
res = 31 * res + (floatVal != null ? floatVal.hashCode() : 0);
res = 31 * res + (doubleVal != null ? doubleVal.hashCode() : 0);
res = 31 * res + (bigVal != null ? bigVal.hashCode() : 0);
res = 31 * res + (strVal != null ? strVal.hashCode() : 0);
res = 31 * res + (arrVal != null ? Arrays.hashCode(arrVal) : 0);
res = 31 * res + (dateVal != null ? dateVal.hashCode() : 0);
res = 31 * res + (timeVal != null ? timeVal.hashCode() : 0);
res = 31 * res + (tsVal != null ? tsVal.hashCode() : 0);
res = 31 * res + (urlVal != null ? urlVal.hashCode() : 0);
res = 31 * res + (f1 != null ? f1.hashCode() : 0);
res = 31 * res + (f2 != null ? f2.hashCode() : 0);
res = 31 * res + (f3 != null ? f3.hashCode() : 0);
return res;
}
}
/**
* Test object field.
*/
@SuppressWarnings("PackageVisibleField")
private static class TestObjectField implements Serializable {
/** */
final int a;
/** */
final String b;
/**
* @param a A.
* @param b B.
*/
private TestObjectField(int a, String b) {
this.a = a;
this.b = b;
}
/** {@inheritDoc} */
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestObjectField that = (TestObjectField)o;
return a == that.a && !(b != null ? !b.equals(that.b) : that.b != null);
}
/** {@inheritDoc} */
@Override public int hashCode() {
int res = a;
res = 31 * res + (b != null ? b.hashCode() : 0);
return res;
}
/** {@inheritDoc} */
@Override public String toString() {
return S.toString(TestObjectField.class, this);
}
}
}