~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/testsuite/simple/ResultSetTest.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 Copyright (C) 2002-2007 MySQL AB
 
4
 
 
5
 
 
6
 
 
7
 This program is free software; you can redistribute it and/or modify
 
8
 
 
9
 it under the terms of version 2 of the GNU General Public License as
 
10
 
 
11
 published by the Free Software Foundation.
 
12
 
 
13
 
 
14
 
 
15
 There are special exceptions to the terms and conditions of the GPL
 
16
 
 
17
 as it is applied to this software. View the full text of the
 
18
 
 
19
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
 
20
 
 
21
 software distribution.
 
22
 
 
23
 
 
24
 
 
25
 This program is distributed in the hope that it will be useful,
 
26
 
 
27
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
28
 
 
29
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
30
 
 
31
 GNU General Public License for more details.
 
32
 
 
33
 
 
34
 
 
35
 You should have received a copy of the GNU General Public License
 
36
 
 
37
 along with this program; if not, write to the Free Software
 
38
 
 
39
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
40
 
 
41
 
 
42
 
 
43
 
 
44
 
 
45
 
 
46
 
 
47
 */
 
48
 
 
49
 
 
50
 
 
51
package testsuite.simple;
 
52
 
 
53
 
 
54
 
 
55
import java.sql.Connection;
 
56
 
 
57
import java.sql.SQLException;
 
58
 
 
59
import java.util.Iterator;
 
60
 
 
61
import java.util.Properties;
 
62
 
 
63
 
 
64
 
 
65
import com.mysql.jdbc.CharsetMapping;
 
66
 
 
67
 
 
68
 
 
69
import testsuite.BaseTestCase;
 
70
 
 
71
 
 
72
 
 
73
public class ResultSetTest extends BaseTestCase {
 
74
 
 
75
 
 
76
 
 
77
        public ResultSetTest(String name) {
 
78
 
 
79
                super(name);
 
80
 
 
81
        }
 
82
 
 
83
 
 
84
 
 
85
        /**
 
86
 
 
87
         * Runs all test cases in this test suite
 
88
 
 
89
         * 
 
90
 
 
91
         * @param args
 
92
 
 
93
         */
 
94
 
 
95
        public static void main(String[] args) {
 
96
 
 
97
                junit.textui.TestRunner.run(ResultSetTest.class);
 
98
 
 
99
        }
 
100
 
 
101
 
 
102
 
 
103
        public void testPadding() throws Exception {
 
104
 
 
105
                if (!versionMeetsMinimum(4, 1, 0)) {
 
106
 
 
107
                        return;
 
108
 
 
109
                }
 
110
 
 
111
 
 
112
 
 
113
                Connection paddedConn = null;
 
114
 
 
115
 
 
116
 
 
117
                int numChars = 32;
 
118
 
 
119
 
 
120
 
 
121
                Iterator charsetNames = CharsetMapping.STATIC_CHARSET_TO_NUM_BYTES_MAP
 
122
 
 
123
                                .keySet().iterator();
 
124
 
 
125
                StringBuffer columns = new StringBuffer();
 
126
 
 
127
                StringBuffer emptyBuf = new StringBuffer();
 
128
 
 
129
                StringBuffer abcBuf = new StringBuffer();
 
130
 
 
131
                StringBuffer repeatBuf = new StringBuffer();
 
132
 
 
133
                StringBuffer selectBuf = new StringBuffer();
 
134
 
 
135
 
 
136
 
 
137
                int counter = 0;
 
138
 
 
139
 
 
140
 
 
141
                while (charsetNames.hasNext()) {
 
142
 
 
143
                        String charsetName = charsetNames.next().toString();
 
144
 
 
145
 
 
146
 
 
147
                        if (charsetName.equalsIgnoreCase("LATIN7")
 
148
 
 
149
                                        || charsetName.equalsIgnoreCase("BINARY")) {
 
150
 
 
151
                                continue; // no mapping in Java
 
152
 
 
153
                        }
 
154
 
 
155
 
 
156
 
 
157
                        if (counter != 0) {
 
158
 
 
159
                                columns.append(",");
 
160
 
 
161
                                emptyBuf.append(",");
 
162
 
 
163
                                abcBuf.append(",");
 
164
 
 
165
                                repeatBuf.append(",");
 
166
 
 
167
                                selectBuf.append(",");
 
168
 
 
169
                        }
 
170
 
 
171
 
 
172
 
 
173
                        emptyBuf.append("''");
 
174
 
 
175
                        abcBuf.append("'abc'");
 
176
 
 
177
                        repeatBuf.append("REPEAT('b', " + numChars + ")");
 
178
 
 
179
 
 
180
 
 
181
                        columns.append("field_");
 
182
 
 
183
                        columns.append(charsetName);
 
184
 
 
185
 
 
186
 
 
187
                        columns.append(" CHAR(");
 
188
 
 
189
                        columns.append(numChars);
 
190
 
 
191
                        columns.append(") CHARACTER SET ");
 
192
 
 
193
                        columns.append(charsetName);
 
194
 
 
195
 
 
196
 
 
197
                        selectBuf.append("field_");
 
198
 
 
199
                        selectBuf.append(charsetName);
 
200
 
 
201
 
 
202
 
 
203
                        counter++;
 
204
 
 
205
                }
 
206
 
 
207
 
 
208
 
 
209
                createTable("testPadding", "(" + columns.toString() + ", ord INT)");
 
210
 
 
211
 
 
212
 
 
213
                this.stmt.executeUpdate("INSERT INTO testPadding VALUES ("
 
214
 
 
215
                                + emptyBuf.toString() + ", 1), (" + abcBuf.toString()
 
216
 
 
217
                                + ", 2), (" + repeatBuf.toString() + ", 3)");
 
218
 
 
219
 
 
220
 
 
221
                try {
 
222
 
 
223
                        Properties props = new Properties();
 
224
 
 
225
                        props.setProperty("padCharsWithSpace", "true");
 
226
 
 
227
 
 
228
 
 
229
                        paddedConn = getConnectionWithProps(props);
 
230
 
 
231
 
 
232
 
 
233
                        testPaddingForConnection(paddedConn, numChars, selectBuf);
 
234
 
 
235
 
 
236
 
 
237
                        props.setProperty("useDynamicCharsetInfo", "true");
 
238
 
 
239
 
 
240
 
 
241
                        paddedConn = getConnectionWithProps(props);
 
242
 
 
243
 
 
244
 
 
245
                        testPaddingForConnection(paddedConn, numChars, selectBuf);
 
246
 
 
247
                } finally {
 
248
 
 
249
                        closeMemberJDBCResources();
 
250
 
 
251
 
 
252
 
 
253
                        if (paddedConn != null) {
 
254
 
 
255
                                paddedConn.close();
 
256
 
 
257
                        }
 
258
 
 
259
                }
 
260
 
 
261
        }
 
262
 
 
263
 
 
264
 
 
265
        private void testPaddingForConnection(Connection paddedConn, int numChars,
 
266
 
 
267
                        StringBuffer selectBuf) throws SQLException {
 
268
 
 
269
 
 
270
 
 
271
                String query = "SELECT " + selectBuf.toString()
 
272
 
 
273
                                + " FROM testPadding ORDER by ord";
 
274
 
 
275
 
 
276
 
 
277
                this.rs = paddedConn.createStatement().executeQuery(query);
 
278
 
 
279
                int numCols = this.rs.getMetaData().getColumnCount();
 
280
 
 
281
 
 
282
 
 
283
                while (this.rs.next()) {
 
284
 
 
285
                        for (int i = 0; i < numCols; i++) {
 
286
 
 
287
                                assertEquals("For column '"
 
288
 
 
289
                                                + this.rs.getMetaData().getColumnName(i + 1)
 
290
 
 
291
                                                + "' of collation "
 
292
 
 
293
                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
294
 
 
295
                                                                .getMetaData()).getColumnCharacterSet(i + 1),
 
296
 
 
297
                                                numChars, this.rs.getString(i + 1).length());
 
298
 
 
299
                        }
 
300
 
 
301
                }
 
302
 
 
303
 
 
304
 
 
305
                this.rs = ((com.mysql.jdbc.Connection) paddedConn)
 
306
 
 
307
                                .clientPrepareStatement(query).executeQuery();
 
308
 
 
309
 
 
310
 
 
311
                while (this.rs.next()) {
 
312
 
 
313
                        for (int i = 0; i < numCols; i++) {
 
314
 
 
315
                                assertEquals("For column '"
 
316
 
 
317
                                                + this.rs.getMetaData().getColumnName(i + 1)
 
318
 
 
319
                                                + "' of collation "
 
320
 
 
321
                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
322
 
 
323
                                                                .getMetaData()).getColumnCharacterSet(i + 1),
 
324
 
 
325
                                                numChars, this.rs.getString(i + 1).length());
 
326
 
 
327
                        }
 
328
 
 
329
                }
 
330
 
 
331
 
 
332
 
 
333
                if (versionMeetsMinimum(4, 1)) {
 
334
 
 
335
                        this.rs = ((com.mysql.jdbc.Connection) paddedConn).serverPrepare(
 
336
 
 
337
                                        query).executeQuery();
 
338
 
 
339
 
 
340
 
 
341
                        while (this.rs.next()) {
 
342
 
 
343
                                for (int i = 0; i < numCols; i++) {
 
344
 
 
345
                                        assertEquals("For column '"
 
346
 
 
347
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
348
 
 
349
                                                        + "' of collation "
 
350
 
 
351
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
352
 
 
353
                                                                        .getMetaData())
 
354
 
 
355
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
356
 
 
357
                                                        this.rs.getString(i + 1).length());
 
358
 
 
359
                                }
 
360
 
 
361
                        }
 
362
 
 
363
                }
 
364
 
 
365
 
 
366
 
 
367
                this.rs = this.stmt.executeQuery(query);
 
368
 
 
369
 
 
370
 
 
371
                while (this.rs.next()) {
 
372
 
 
373
                        for (int i = 0; i < numCols; i++) {
 
374
 
 
375
                                if (this.rs.getRow() != 3) {
 
376
 
 
377
                                        assertTrue("For column '"
 
378
 
 
379
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
380
 
 
381
                                                        + "' of collation "
 
382
 
 
383
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
384
 
 
385
                                                                        .getMetaData())
 
386
 
 
387
                                                                        .getColumnCharacterSet(i + 1),
 
388
 
 
389
                                                        numChars != this.rs.getString(i + 1).length());
 
390
 
 
391
                                } else {
 
392
 
 
393
                                        assertEquals("For column '"
 
394
 
 
395
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
396
 
 
397
                                                        + "' of collation "
 
398
 
 
399
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
400
 
 
401
                                                                        .getMetaData())
 
402
 
 
403
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
404
 
 
405
                                                        this.rs.getString(i + 1).length());
 
406
 
 
407
                                }
 
408
 
 
409
                        }
 
410
 
 
411
                }
 
412
 
 
413
 
 
414
 
 
415
                this.rs = ((com.mysql.jdbc.Connection) this.conn)
 
416
 
 
417
                                .clientPrepareStatement(query).executeQuery();
 
418
 
 
419
 
 
420
 
 
421
                while (this.rs.next()) {
 
422
 
 
423
                        for (int i = 0; i < numCols; i++) {
 
424
 
 
425
                                if (this.rs.getRow() != 3) {
 
426
 
 
427
                                        assertTrue("For column '"
 
428
 
 
429
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
430
 
 
431
                                                        + "' of collation "
 
432
 
 
433
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
434
 
 
435
                                                                        .getMetaData())
 
436
 
 
437
                                                                        .getColumnCharacterSet(i + 1),
 
438
 
 
439
                                                        numChars != this.rs.getString(i + 1).length());
 
440
 
 
441
                                } else {
 
442
 
 
443
                                        assertEquals("For column '"
 
444
 
 
445
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
446
 
 
447
                                                        + "' of collation "
 
448
 
 
449
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
450
 
 
451
                                                                        .getMetaData())
 
452
 
 
453
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
454
 
 
455
                                                        this.rs.getString(i + 1).length());
 
456
 
 
457
                                }
 
458
 
 
459
                        }
 
460
 
 
461
                }
 
462
 
 
463
 
 
464
 
 
465
                if (versionMeetsMinimum(4, 1)) {
 
466
 
 
467
                        this.rs = ((com.mysql.jdbc.Connection) this.conn).serverPrepare(
 
468
 
 
469
                                        query).executeQuery();
 
470
 
 
471
 
 
472
 
 
473
                        while (this.rs.next()) {
 
474
 
 
475
                                for (int i = 0; i < numCols; i++) {
 
476
 
 
477
                                        if (this.rs.getRow() != 3) {
 
478
 
 
479
                                                assertTrue("For column '"
 
480
 
 
481
                                                                + this.rs.getMetaData().getColumnName(i + 1)
 
482
 
 
483
                                                                + "' of collation "
 
484
 
 
485
                                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
486
 
 
487
                                                                                .getMetaData())
 
488
 
 
489
                                                                                .getColumnCharacterSet(i + 1),
 
490
 
 
491
                                                                numChars != this.rs.getString(i + 1).length());
 
492
 
 
493
                                        } else {
 
494
 
 
495
                                                assertEquals("For column '"
 
496
 
 
497
                                                                + this.rs.getMetaData().getColumnName(i + 1)
 
498
 
 
499
                                                                + "' of collation "
 
500
 
 
501
                                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
502
 
 
503
                                                                                .getMetaData())
 
504
 
 
505
                                                                                .getColumnCharacterSet(i + 1),
 
506
 
 
507
                                                                numChars, this.rs.getString(i + 1).length());
 
508
 
 
509
                                        }
 
510
 
 
511
                                }
 
512
 
 
513
                        }
 
514
 
 
515
                }
 
516
 
 
517
        }
 
518
 
 
519
}
 
520