3
Copyright (C) 2002-2007 MySQL AB
7
This program is free software; you can redistribute it and/or modify
9
it under the terms of version 2 of the GNU General Public License as
11
published by the Free Software Foundation.
15
There are special exceptions to the terms and conditions of the GPL
17
as it is applied to this software. View the full text of the
19
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
21
software distribution.
25
This program is distributed in the hope that it will be useful,
27
but WITHOUT ANY WARRANTY; without even the implied warranty of
29
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
GNU General Public License for more details.
35
You should have received a copy of the GNU General Public License
37
along with this program; if not, write to the Free Software
39
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51
package testsuite.simple;
55
import java.sql.Connection;
57
import java.sql.SQLException;
59
import java.util.Iterator;
61
import java.util.Properties;
65
import com.mysql.jdbc.CharsetMapping;
69
import testsuite.BaseTestCase;
73
public class ResultSetTest extends BaseTestCase {
77
public ResultSetTest(String name) {
87
* Runs all test cases in this test suite
95
public static void main(String[] args) {
97
junit.textui.TestRunner.run(ResultSetTest.class);
103
public void testPadding() throws Exception {
105
if (!versionMeetsMinimum(4, 1, 0)) {
113
Connection paddedConn = null;
121
Iterator charsetNames = CharsetMapping.STATIC_CHARSET_TO_NUM_BYTES_MAP
123
.keySet().iterator();
125
StringBuffer columns = new StringBuffer();
127
StringBuffer emptyBuf = new StringBuffer();
129
StringBuffer abcBuf = new StringBuffer();
131
StringBuffer repeatBuf = new StringBuffer();
133
StringBuffer selectBuf = new StringBuffer();
141
while (charsetNames.hasNext()) {
143
String charsetName = charsetNames.next().toString();
147
if (charsetName.equalsIgnoreCase("LATIN7")
149
|| charsetName.equalsIgnoreCase("BINARY")) {
151
continue; // no mapping in Java
161
emptyBuf.append(",");
165
repeatBuf.append(",");
167
selectBuf.append(",");
173
emptyBuf.append("''");
175
abcBuf.append("'abc'");
177
repeatBuf.append("REPEAT('b', " + numChars + ")");
181
columns.append("field_");
183
columns.append(charsetName);
187
columns.append(" CHAR(");
189
columns.append(numChars);
191
columns.append(") CHARACTER SET ");
193
columns.append(charsetName);
197
selectBuf.append("field_");
199
selectBuf.append(charsetName);
209
createTable("testPadding", "(" + columns.toString() + ", ord INT)");
213
this.stmt.executeUpdate("INSERT INTO testPadding VALUES ("
215
+ emptyBuf.toString() + ", 1), (" + abcBuf.toString()
217
+ ", 2), (" + repeatBuf.toString() + ", 3)");
223
Properties props = new Properties();
225
props.setProperty("padCharsWithSpace", "true");
229
paddedConn = getConnectionWithProps(props);
233
testPaddingForConnection(paddedConn, numChars, selectBuf);
237
props.setProperty("useDynamicCharsetInfo", "true");
241
paddedConn = getConnectionWithProps(props);
245
testPaddingForConnection(paddedConn, numChars, selectBuf);
249
closeMemberJDBCResources();
253
if (paddedConn != null) {
265
private void testPaddingForConnection(Connection paddedConn, int numChars,
267
StringBuffer selectBuf) throws SQLException {
271
String query = "SELECT " + selectBuf.toString()
273
+ " FROM testPadding ORDER by ord";
277
this.rs = paddedConn.createStatement().executeQuery(query);
279
int numCols = this.rs.getMetaData().getColumnCount();
283
while (this.rs.next()) {
285
for (int i = 0; i < numCols; i++) {
287
assertEquals("For column '"
289
+ this.rs.getMetaData().getColumnName(i + 1)
293
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
295
.getMetaData()).getColumnCharacterSet(i + 1),
297
numChars, this.rs.getString(i + 1).length());
305
this.rs = ((com.mysql.jdbc.Connection) paddedConn)
307
.clientPrepareStatement(query).executeQuery();
311
while (this.rs.next()) {
313
for (int i = 0; i < numCols; i++) {
315
assertEquals("For column '"
317
+ this.rs.getMetaData().getColumnName(i + 1)
321
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
323
.getMetaData()).getColumnCharacterSet(i + 1),
325
numChars, this.rs.getString(i + 1).length());
333
if (versionMeetsMinimum(4, 1)) {
335
this.rs = ((com.mysql.jdbc.Connection) paddedConn).serverPrepare(
337
query).executeQuery();
341
while (this.rs.next()) {
343
for (int i = 0; i < numCols; i++) {
345
assertEquals("For column '"
347
+ this.rs.getMetaData().getColumnName(i + 1)
351
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
355
.getColumnCharacterSet(i + 1), numChars,
357
this.rs.getString(i + 1).length());
367
this.rs = this.stmt.executeQuery(query);
371
while (this.rs.next()) {
373
for (int i = 0; i < numCols; i++) {
375
if (this.rs.getRow() != 3) {
377
assertTrue("For column '"
379
+ this.rs.getMetaData().getColumnName(i + 1)
383
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
387
.getColumnCharacterSet(i + 1),
389
numChars != this.rs.getString(i + 1).length());
393
assertEquals("For column '"
395
+ this.rs.getMetaData().getColumnName(i + 1)
399
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
403
.getColumnCharacterSet(i + 1), numChars,
405
this.rs.getString(i + 1).length());
415
this.rs = ((com.mysql.jdbc.Connection) this.conn)
417
.clientPrepareStatement(query).executeQuery();
421
while (this.rs.next()) {
423
for (int i = 0; i < numCols; i++) {
425
if (this.rs.getRow() != 3) {
427
assertTrue("For column '"
429
+ this.rs.getMetaData().getColumnName(i + 1)
433
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
437
.getColumnCharacterSet(i + 1),
439
numChars != this.rs.getString(i + 1).length());
443
assertEquals("For column '"
445
+ this.rs.getMetaData().getColumnName(i + 1)
449
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
453
.getColumnCharacterSet(i + 1), numChars,
455
this.rs.getString(i + 1).length());
465
if (versionMeetsMinimum(4, 1)) {
467
this.rs = ((com.mysql.jdbc.Connection) this.conn).serverPrepare(
469
query).executeQuery();
473
while (this.rs.next()) {
475
for (int i = 0; i < numCols; i++) {
477
if (this.rs.getRow() != 3) {
479
assertTrue("For column '"
481
+ this.rs.getMetaData().getColumnName(i + 1)
485
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
489
.getColumnCharacterSet(i + 1),
491
numChars != this.rs.getString(i + 1).length());
495
assertEquals("For column '"
497
+ this.rs.getMetaData().getColumnName(i + 1)
501
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
505
.getColumnCharacterSet(i + 1),
507
numChars, this.rs.getString(i + 1).length());