~ubuntu-branches/ubuntu/oneiric/mysql-connector-java/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2007-11-30 10:34:13 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20071130103413-mxm4lpfrr4fnjbuj
Tags: 5.1.5+dfsg-1
* New upstream release. Closes: #450718.
* Add Homepage field to debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2002-2007 MySQL AB
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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
 
21
 
 
22
 
 
23
 
 
24
 */
 
25
 
 
26
package testsuite.simple;
 
27
 
 
28
import java.sql.Connection;
 
29
import java.sql.SQLException;
 
30
import java.util.Iterator;
 
31
import java.util.Properties;
 
32
 
 
33
import com.mysql.jdbc.CharsetMapping;
 
34
 
 
35
import testsuite.BaseTestCase;
 
36
 
 
37
public class ResultSetTest extends BaseTestCase {
 
38
 
 
39
        public ResultSetTest(String name) {
 
40
                super(name);
 
41
        }
 
42
 
 
43
        /**
 
44
         * Runs all test cases in this test suite
 
45
         * 
 
46
         * @param args
 
47
         */
 
48
        public static void main(String[] args) {
 
49
                junit.textui.TestRunner.run(ResultSetTest.class);
 
50
        }
 
51
 
 
52
        public void testPadding() throws Exception {
 
53
                if (!versionMeetsMinimum(4, 1, 0)) {
 
54
                        return;
 
55
                }
 
56
 
 
57
                Connection paddedConn = null;
 
58
 
 
59
                int numChars = 32;
 
60
 
 
61
                Iterator charsetNames = CharsetMapping.STATIC_CHARSET_TO_NUM_BYTES_MAP
 
62
                                .keySet().iterator();
 
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();
 
68
 
 
69
                int counter = 0;
 
70
 
 
71
                while (charsetNames.hasNext()) {
 
72
                        String charsetName = charsetNames.next().toString();
 
73
 
 
74
                        if (charsetName.equalsIgnoreCase("LATIN7")
 
75
                                        || charsetName.equalsIgnoreCase("BINARY")) {
 
76
                                continue; // no mapping in Java
 
77
                        }
 
78
 
 
79
                        if (counter != 0) {
 
80
                                columns.append(",");
 
81
                                emptyBuf.append(",");
 
82
                                abcBuf.append(",");
 
83
                                repeatBuf.append(",");
 
84
                                selectBuf.append(",");
 
85
                        }
 
86
 
 
87
                        emptyBuf.append("''");
 
88
                        abcBuf.append("'abc'");
 
89
                        repeatBuf.append("REPEAT('b', " + numChars + ")");
 
90
 
 
91
                        columns.append("field_");
 
92
                        columns.append(charsetName);
 
93
 
 
94
                        columns.append(" CHAR(");
 
95
                        columns.append(numChars);
 
96
                        columns.append(") CHARACTER SET ");
 
97
                        columns.append(charsetName);
 
98
 
 
99
                        selectBuf.append("field_");
 
100
                        selectBuf.append(charsetName);
 
101
 
 
102
                        counter++;
 
103
                }
 
104
 
 
105
                createTable("testPadding", "(" + columns.toString() + ", ord INT)");
 
106
 
 
107
                this.stmt.executeUpdate("INSERT INTO testPadding VALUES ("
 
108
                                + emptyBuf.toString() + ", 1), (" + abcBuf.toString()
 
109
                                + ", 2), (" + repeatBuf.toString() + ", 3)");
 
110
 
 
111
                try {
 
112
                        Properties props = new Properties();
 
113
                        props.setProperty("padCharsWithSpace", "true");
 
114
 
 
115
                        paddedConn = getConnectionWithProps(props);
 
116
 
 
117
                        testPaddingForConnection(paddedConn, numChars, selectBuf);
 
118
 
 
119
                        props.setProperty("useDynamicCharsetInfo", "true");
 
120
 
 
121
                        paddedConn = getConnectionWithProps(props);
 
122
 
 
123
                        testPaddingForConnection(paddedConn, numChars, selectBuf);
 
124
                } finally {
 
125
                        closeMemberJDBCResources();
 
126
 
 
127
                        if (paddedConn != null) {
 
128
                                paddedConn.close();
 
129
                        }
 
130
                }
 
131
        }
 
132
 
 
133
        private void testPaddingForConnection(Connection paddedConn, int numChars,
 
134
                        StringBuffer selectBuf) throws SQLException {
 
135
 
 
136
                String query = "SELECT " + selectBuf.toString()
 
137
                                + " FROM testPadding ORDER by ord";
 
138
 
 
139
                this.rs = paddedConn.createStatement().executeQuery(query);
 
140
                int numCols = this.rs.getMetaData().getColumnCount();
 
141
 
 
142
                while (this.rs.next()) {
 
143
                        for (int i = 0; i < numCols; i++) {
 
144
                                assertEquals("For column '"
 
145
                                                + this.rs.getMetaData().getColumnName(i + 1)
 
146
                                                + "' of collation "
 
147
                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
148
                                                                .getMetaData()).getColumnCharacterSet(i + 1),
 
149
                                                numChars, this.rs.getString(i + 1).length());
 
150
                        }
 
151
                }
 
152
 
 
153
                this.rs = ((com.mysql.jdbc.Connection) paddedConn)
 
154
                                .clientPrepareStatement(query).executeQuery();
 
155
 
 
156
                while (this.rs.next()) {
 
157
                        for (int i = 0; i < numCols; i++) {
 
158
                                assertEquals("For column '"
 
159
                                                + this.rs.getMetaData().getColumnName(i + 1)
 
160
                                                + "' of collation "
 
161
                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
162
                                                                .getMetaData()).getColumnCharacterSet(i + 1),
 
163
                                                numChars, this.rs.getString(i + 1).length());
 
164
                        }
 
165
                }
 
166
 
 
167
                if (versionMeetsMinimum(4, 1)) {
 
168
                        this.rs = ((com.mysql.jdbc.Connection) paddedConn).serverPrepareStatement(
 
169
                                        query).executeQuery();
 
170
 
 
171
                        while (this.rs.next()) {
 
172
                                for (int i = 0; i < numCols; i++) {
 
173
                                        assertEquals("For column '"
 
174
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
175
                                                        + "' of collation "
 
176
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
177
                                                                        .getMetaData())
 
178
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
179
                                                        this.rs.getString(i + 1).length());
 
180
                                }
 
181
                        }
 
182
                }
 
183
 
 
184
                this.rs = this.stmt.executeQuery(query);
 
185
 
 
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)
 
191
                                                        + "' of collation "
 
192
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
193
                                                                        .getMetaData())
 
194
                                                                        .getColumnCharacterSet(i + 1),
 
195
                                                        numChars != this.rs.getString(i + 1).length());
 
196
                                } else {
 
197
                                        assertEquals("For column '"
 
198
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
199
                                                        + "' of collation "
 
200
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
201
                                                                        .getMetaData())
 
202
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
203
                                                        this.rs.getString(i + 1).length());
 
204
                                }
 
205
                        }
 
206
                }
 
207
 
 
208
                this.rs = ((com.mysql.jdbc.Connection) this.conn)
 
209
                                .clientPrepareStatement(query).executeQuery();
 
210
 
 
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)
 
216
                                                        + "' of collation "
 
217
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
218
                                                                        .getMetaData())
 
219
                                                                        .getColumnCharacterSet(i + 1),
 
220
                                                        numChars != this.rs.getString(i + 1).length());
 
221
                                } else {
 
222
                                        assertEquals("For column '"
 
223
                                                        + this.rs.getMetaData().getColumnName(i + 1)
 
224
                                                        + "' of collation "
 
225
                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
226
                                                                        .getMetaData())
 
227
                                                                        .getColumnCharacterSet(i + 1), numChars,
 
228
                                                        this.rs.getString(i + 1).length());
 
229
                                }
 
230
                        }
 
231
                }
 
232
 
 
233
                if (versionMeetsMinimum(4, 1)) {
 
234
                        this.rs = ((com.mysql.jdbc.Connection) this.conn).serverPrepareStatement(
 
235
                                        query).executeQuery();
 
236
 
 
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)
 
242
                                                                + "' of collation "
 
243
                                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
244
                                                                                .getMetaData())
 
245
                                                                                .getColumnCharacterSet(i + 1),
 
246
                                                                numChars != this.rs.getString(i + 1).length());
 
247
                                        } else {
 
248
                                                assertEquals("For column '"
 
249
                                                                + this.rs.getMetaData().getColumnName(i + 1)
 
250
                                                                + "' of collation "
 
251
                                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
 
252
                                                                                .getMetaData())
 
253
                                                                                .getColumnCharacterSet(i + 1),
 
254
                                                                numChars, this.rs.getString(i + 1).length());
 
255
                                        }
 
256
                                }
 
257
                        }
 
258
                }
 
259
        }
 
260
}