001: /*
002: Copyright (C) 2002-2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025:
026: package testsuite.simple;
027:
028: import java.sql.Connection;
029: import java.sql.SQLException;
030: import java.util.Iterator;
031: import java.util.Properties;
032:
033: import com.mysql.jdbc.CharsetMapping;
034:
035: import testsuite.BaseTestCase;
036:
037: public class ResultSetTest extends BaseTestCase {
038:
039: public ResultSetTest(String name) {
040: super (name);
041: }
042:
043: /**
044: * Runs all test cases in this test suite
045: *
046: * @param args
047: */
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(ResultSetTest.class);
050: }
051:
052: public void testPadding() throws Exception {
053: if (!versionMeetsMinimum(4, 1, 0)) {
054: return;
055: }
056:
057: Connection paddedConn = null;
058:
059: int numChars = 32;
060:
061: Iterator charsetNames = CharsetMapping.STATIC_CHARSET_TO_NUM_BYTES_MAP
062: .keySet().iterator();
063: StringBuffer columns = new StringBuffer();
064: StringBuffer emptyBuf = new StringBuffer();
065: StringBuffer abcBuf = new StringBuffer();
066: StringBuffer repeatBuf = new StringBuffer();
067: StringBuffer selectBuf = new StringBuffer();
068:
069: int counter = 0;
070:
071: while (charsetNames.hasNext()) {
072: String charsetName = charsetNames.next().toString();
073:
074: if (charsetName.equalsIgnoreCase("LATIN7")
075: || charsetName.equalsIgnoreCase("BINARY")) {
076: continue; // no mapping in Java
077: }
078:
079: if (counter != 0) {
080: columns.append(",");
081: emptyBuf.append(",");
082: abcBuf.append(",");
083: repeatBuf.append(",");
084: selectBuf.append(",");
085: }
086:
087: emptyBuf.append("''");
088: abcBuf.append("'abc'");
089: repeatBuf.append("REPEAT('b', " + numChars + ")");
090:
091: columns.append("field_");
092: columns.append(charsetName);
093:
094: columns.append(" CHAR(");
095: columns.append(numChars);
096: columns.append(") CHARACTER SET ");
097: columns.append(charsetName);
098:
099: selectBuf.append("field_");
100: selectBuf.append(charsetName);
101:
102: counter++;
103: }
104:
105: createTable("testPadding", "(" + columns.toString()
106: + ", ord INT)");
107:
108: this .stmt.executeUpdate("INSERT INTO testPadding VALUES ("
109: + emptyBuf.toString() + ", 1), (" + abcBuf.toString()
110: + ", 2), (" + repeatBuf.toString() + ", 3)");
111:
112: try {
113: Properties props = new Properties();
114: props.setProperty("padCharsWithSpace", "true");
115:
116: paddedConn = getConnectionWithProps(props);
117:
118: testPaddingForConnection(paddedConn, numChars, selectBuf);
119:
120: props.setProperty("useDynamicCharsetInfo", "true");
121:
122: paddedConn = getConnectionWithProps(props);
123:
124: testPaddingForConnection(paddedConn, numChars, selectBuf);
125: } finally {
126: closeMemberJDBCResources();
127:
128: if (paddedConn != null) {
129: paddedConn.close();
130: }
131: }
132: }
133:
134: private void testPaddingForConnection(Connection paddedConn,
135: int numChars, StringBuffer selectBuf) throws SQLException {
136:
137: String query = "SELECT " + selectBuf.toString()
138: + " FROM testPadding ORDER by ord";
139:
140: this .rs = paddedConn.createStatement().executeQuery(query);
141: int numCols = this .rs.getMetaData().getColumnCount();
142:
143: while (this .rs.next()) {
144: for (int i = 0; i < numCols; i++) {
145: assertEquals("For column '"
146: + this .rs.getMetaData().getColumnName(i + 1)
147: + "' of collation "
148: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
149: .getMetaData())
150: .getColumnCharacterSet(i + 1),
151: numChars, this .rs.getString(i + 1).length());
152: }
153: }
154:
155: this .rs = ((com.mysql.jdbc.Connection) paddedConn)
156: .clientPrepareStatement(query).executeQuery();
157:
158: while (this .rs.next()) {
159: for (int i = 0; i < numCols; i++) {
160: assertEquals("For column '"
161: + this .rs.getMetaData().getColumnName(i + 1)
162: + "' of collation "
163: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
164: .getMetaData())
165: .getColumnCharacterSet(i + 1),
166: numChars, this .rs.getString(i + 1).length());
167: }
168: }
169:
170: if (versionMeetsMinimum(4, 1)) {
171: this .rs = ((com.mysql.jdbc.Connection) paddedConn)
172: .serverPrepareStatement(query).executeQuery();
173:
174: while (this .rs.next()) {
175: for (int i = 0; i < numCols; i++) {
176: assertEquals(
177: "For column '"
178: + this .rs.getMetaData()
179: .getColumnName(i + 1)
180: + "' of collation "
181: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
182: .getMetaData())
183: .getColumnCharacterSet(i + 1),
184: numChars, this .rs.getString(i + 1).length());
185: }
186: }
187: }
188:
189: this .rs = this .stmt.executeQuery(query);
190:
191: while (this .rs.next()) {
192: for (int i = 0; i < numCols; i++) {
193: if (this .rs.getRow() != 3) {
194: assertTrue(
195: "For column '"
196: + this .rs.getMetaData()
197: .getColumnName(i + 1)
198: + "' of collation "
199: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
200: .getMetaData())
201: .getColumnCharacterSet(i + 1),
202: numChars != this .rs.getString(i + 1)
203: .length());
204: } else {
205: assertEquals(
206: "For column '"
207: + this .rs.getMetaData()
208: .getColumnName(i + 1)
209: + "' of collation "
210: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
211: .getMetaData())
212: .getColumnCharacterSet(i + 1),
213: numChars, this .rs.getString(i + 1).length());
214: }
215: }
216: }
217:
218: this .rs = ((com.mysql.jdbc.Connection) this .conn)
219: .clientPrepareStatement(query).executeQuery();
220:
221: while (this .rs.next()) {
222: for (int i = 0; i < numCols; i++) {
223: if (this .rs.getRow() != 3) {
224: assertTrue(
225: "For column '"
226: + this .rs.getMetaData()
227: .getColumnName(i + 1)
228: + "' of collation "
229: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
230: .getMetaData())
231: .getColumnCharacterSet(i + 1),
232: numChars != this .rs.getString(i + 1)
233: .length());
234: } else {
235: assertEquals(
236: "For column '"
237: + this .rs.getMetaData()
238: .getColumnName(i + 1)
239: + "' of collation "
240: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
241: .getMetaData())
242: .getColumnCharacterSet(i + 1),
243: numChars, this .rs.getString(i + 1).length());
244: }
245: }
246: }
247:
248: if (versionMeetsMinimum(4, 1)) {
249: this .rs = ((com.mysql.jdbc.Connection) this .conn)
250: .serverPrepareStatement(query).executeQuery();
251:
252: while (this .rs.next()) {
253: for (int i = 0; i < numCols; i++) {
254: if (this .rs.getRow() != 3) {
255: assertTrue(
256: "For column '"
257: + this .rs.getMetaData()
258: .getColumnName(i + 1)
259: + "' of collation "
260: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
261: .getMetaData())
262: .getColumnCharacterSet(i + 1),
263: numChars != this .rs.getString(i + 1)
264: .length());
265: } else {
266: assertEquals(
267: "For column '"
268: + this .rs.getMetaData()
269: .getColumnName(i + 1)
270: + "' of collation "
271: + ((com.mysql.jdbc.ResultSetMetaData) this .rs
272: .getMetaData())
273: .getColumnCharacterSet(i + 1),
274: numChars, this .rs.getString(i + 1)
275: .length());
276: }
277: }
278: }
279: }
280: }
281: }
|