2
Copyright (C) 2002-2007 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of version 2 of the GNU General Public License as
6
published by the Free Software Foundation.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
package testsuite.simple;
28
import java.sql.Connection;
29
import java.sql.SQLException;
30
import java.util.Iterator;
31
import java.util.Properties;
33
import com.mysql.jdbc.CharsetMapping;
35
import testsuite.BaseTestCase;
37
public class ResultSetTest extends BaseTestCase {
39
public ResultSetTest(String name) {
44
* Runs all test cases in this test suite
48
public static void main(String[] args) {
49
junit.textui.TestRunner.run(ResultSetTest.class);
52
public void testPadding() throws Exception {
53
if (!versionMeetsMinimum(4, 1, 0)) {
57
Connection paddedConn = null;
61
Iterator charsetNames = CharsetMapping.STATIC_CHARSET_TO_NUM_BYTES_MAP
63
StringBuffer columns = new StringBuffer();
64
StringBuffer emptyBuf = new StringBuffer();
65
StringBuffer abcBuf = new StringBuffer();
66
StringBuffer repeatBuf = new StringBuffer();
67
StringBuffer selectBuf = new StringBuffer();
71
while (charsetNames.hasNext()) {
72
String charsetName = charsetNames.next().toString();
74
if (charsetName.equalsIgnoreCase("LATIN7")
75
|| charsetName.equalsIgnoreCase("BINARY")) {
76
continue; // no mapping in Java
83
repeatBuf.append(",");
84
selectBuf.append(",");
87
emptyBuf.append("''");
88
abcBuf.append("'abc'");
89
repeatBuf.append("REPEAT('b', " + numChars + ")");
91
columns.append("field_");
92
columns.append(charsetName);
94
columns.append(" CHAR(");
95
columns.append(numChars);
96
columns.append(") CHARACTER SET ");
97
columns.append(charsetName);
99
selectBuf.append("field_");
100
selectBuf.append(charsetName);
105
createTable("testPadding", "(" + columns.toString() + ", ord INT)");
107
this.stmt.executeUpdate("INSERT INTO testPadding VALUES ("
108
+ emptyBuf.toString() + ", 1), (" + abcBuf.toString()
109
+ ", 2), (" + repeatBuf.toString() + ", 3)");
112
Properties props = new Properties();
113
props.setProperty("padCharsWithSpace", "true");
115
paddedConn = getConnectionWithProps(props);
117
testPaddingForConnection(paddedConn, numChars, selectBuf);
119
props.setProperty("useDynamicCharsetInfo", "true");
121
paddedConn = getConnectionWithProps(props);
123
testPaddingForConnection(paddedConn, numChars, selectBuf);
125
closeMemberJDBCResources();
127
if (paddedConn != null) {
133
private void testPaddingForConnection(Connection paddedConn, int numChars,
134
StringBuffer selectBuf) throws SQLException {
136
String query = "SELECT " + selectBuf.toString()
137
+ " FROM testPadding ORDER by ord";
139
this.rs = paddedConn.createStatement().executeQuery(query);
140
int numCols = this.rs.getMetaData().getColumnCount();
142
while (this.rs.next()) {
143
for (int i = 0; i < numCols; i++) {
144
assertEquals("For column '"
145
+ this.rs.getMetaData().getColumnName(i + 1)
147
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
148
.getMetaData()).getColumnCharacterSet(i + 1),
149
numChars, this.rs.getString(i + 1).length());
153
this.rs = ((com.mysql.jdbc.Connection) paddedConn)
154
.clientPrepareStatement(query).executeQuery();
156
while (this.rs.next()) {
157
for (int i = 0; i < numCols; i++) {
158
assertEquals("For column '"
159
+ this.rs.getMetaData().getColumnName(i + 1)
161
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
162
.getMetaData()).getColumnCharacterSet(i + 1),
163
numChars, this.rs.getString(i + 1).length());
167
if (versionMeetsMinimum(4, 1)) {
168
this.rs = ((com.mysql.jdbc.Connection) paddedConn).serverPrepareStatement(
169
query).executeQuery();
171
while (this.rs.next()) {
172
for (int i = 0; i < numCols; i++) {
173
assertEquals("For column '"
174
+ this.rs.getMetaData().getColumnName(i + 1)
176
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
178
.getColumnCharacterSet(i + 1), numChars,
179
this.rs.getString(i + 1).length());
184
this.rs = this.stmt.executeQuery(query);
186
while (this.rs.next()) {
187
for (int i = 0; i < numCols; i++) {
188
if (this.rs.getRow() != 3) {
189
assertTrue("For column '"
190
+ this.rs.getMetaData().getColumnName(i + 1)
192
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
194
.getColumnCharacterSet(i + 1),
195
numChars != this.rs.getString(i + 1).length());
197
assertEquals("For column '"
198
+ this.rs.getMetaData().getColumnName(i + 1)
200
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
202
.getColumnCharacterSet(i + 1), numChars,
203
this.rs.getString(i + 1).length());
208
this.rs = ((com.mysql.jdbc.Connection) this.conn)
209
.clientPrepareStatement(query).executeQuery();
211
while (this.rs.next()) {
212
for (int i = 0; i < numCols; i++) {
213
if (this.rs.getRow() != 3) {
214
assertTrue("For column '"
215
+ this.rs.getMetaData().getColumnName(i + 1)
217
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
219
.getColumnCharacterSet(i + 1),
220
numChars != this.rs.getString(i + 1).length());
222
assertEquals("For column '"
223
+ this.rs.getMetaData().getColumnName(i + 1)
225
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
227
.getColumnCharacterSet(i + 1), numChars,
228
this.rs.getString(i + 1).length());
233
if (versionMeetsMinimum(4, 1)) {
234
this.rs = ((com.mysql.jdbc.Connection) this.conn).serverPrepareStatement(
235
query).executeQuery();
237
while (this.rs.next()) {
238
for (int i = 0; i < numCols; i++) {
239
if (this.rs.getRow() != 3) {
240
assertTrue("For column '"
241
+ this.rs.getMetaData().getColumnName(i + 1)
243
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
245
.getColumnCharacterSet(i + 1),
246
numChars != this.rs.getString(i + 1).length());
248
assertEquals("For column '"
249
+ this.rs.getMetaData().getColumnName(i + 1)
251
+ ((com.mysql.jdbc.ResultSetMetaData) this.rs
253
.getColumnCharacterSet(i + 1),
254
numChars, this.rs.getString(i + 1).length());