source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/testsuite/simple/ResultSetTest.java @ 766

Last change on this file since 766 was 766, checked in by npipsl, 11 years ago
File size: 8.3 KB
Line 
1/*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
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
26package testsuite.simple;
27
28import java.io.UnsupportedEncodingException;
29import java.sql.Connection;
30import java.sql.SQLException;
31import java.util.HashMap;
32import java.util.Iterator;
33import java.util.Map;
34import java.util.Properties;
35
36import testsuite.BaseTestCase;
37
38import com.mysql.jdbc.CharsetMapping;
39import com.mysql.jdbc.ConnectionImpl;
40
41public class ResultSetTest extends BaseTestCase {
42
43        public ResultSetTest(String name) {
44                super(name);
45        }
46
47        /**
48         * Runs all test cases in this test suite
49         *
50         * @param args
51         */
52        public static void main(String[] args) {
53                junit.textui.TestRunner.run(ResultSetTest.class);
54        }
55
56        public void testPadding() throws Exception {
57                if (!versionMeetsMinimum(4, 1, 0)) {
58                        return;
59                }
60
61                Connection paddedConn = null;
62
63                int numChars = 32;
64
65                // build map of charsets supported by server
66                Map<String, Integer> charsetsMap = new HashMap<String, Integer>();
67                Iterator<Integer> collationIndexes = ((ConnectionImpl)this.conn).indexToJavaCharset.keySet().iterator();
68                while (collationIndexes.hasNext()) {
69                        Integer index = collationIndexes.next();
70                        String charsetName = ((ConnectionImpl)this.conn).indexToCustomMysqlCharset.get(index);
71                        if (charsetName == null) charsetName = CharsetMapping.STATIC_INDEX_TO_MYSQL_CHARSET_MAP.get(index);
72                        if (charsetName != null) charsetsMap.put(charsetName, index);
73                }
74               
75                Iterator<String> charsetNames = charsetsMap.keySet().iterator();
76                StringBuffer columns = new StringBuffer();
77                StringBuffer emptyBuf = new StringBuffer();
78                StringBuffer abcBuf = new StringBuffer();
79                StringBuffer repeatBuf = new StringBuffer();
80                StringBuffer selectBuf = new StringBuffer();
81
82                int counter = 0;
83
84                while (charsetNames.hasNext()) {
85                        String charsetName = charsetNames.next();
86
87                        if (charsetName.equalsIgnoreCase("LATIN7")
88                                        || charsetName.equalsIgnoreCase("BINARY")) {
89                                continue; // no mapping in Java
90                        }
91
92                        try {
93                                "".getBytes(charsetName);
94                        } catch (UnsupportedEncodingException uee) {
95                                continue; // not supported on this platform
96                        }
97
98                        if (counter != 0) {
99                                columns.append(",");
100                                emptyBuf.append(",");
101                                abcBuf.append(",");
102                                repeatBuf.append(",");
103                                selectBuf.append(",");
104                        }
105
106                        emptyBuf.append("''");
107                        abcBuf.append("'abc'");
108                        repeatBuf.append("REPEAT('b', " + numChars + ")");
109
110                        columns.append("field_");
111                        columns.append(charsetName);
112
113                        columns.append(" CHAR(");
114                        columns.append(numChars);
115                        columns.append(") CHARACTER SET ");
116                        columns.append(charsetName);
117
118                        selectBuf.append("field_");
119                        selectBuf.append(charsetName);
120
121                        counter++;
122                }
123
124                createTable("testPadding", "(" + columns.toString() + ", ord INT)");
125
126                this.stmt.executeUpdate("INSERT INTO testPadding VALUES ("
127                                + emptyBuf.toString() + ", 1), (" + abcBuf.toString()
128                                + ", 2), (" + repeatBuf.toString() + ", 3)");
129
130                try {
131                        Properties props = new Properties();
132                        props.setProperty("padCharsWithSpace", "true");
133
134                        paddedConn = getConnectionWithProps(props);
135
136                        testPaddingForConnection(paddedConn, numChars, selectBuf);
137
138                        props.setProperty("useDynamicCharsetInfo", "true");
139
140                        paddedConn = getConnectionWithProps(props);
141
142                        testPaddingForConnection(paddedConn, numChars, selectBuf);
143                } finally {
144                        if (paddedConn != null) {
145                                paddedConn.close();
146                        }
147                }
148        }
149
150        private void testPaddingForConnection(Connection paddedConn, int numChars,
151                        StringBuffer selectBuf) throws SQLException {
152
153                String query = "SELECT " + selectBuf.toString()
154                                + " FROM testPadding ORDER by ord";
155
156                this.rs = paddedConn.createStatement().executeQuery(query);
157                int numCols = this.rs.getMetaData().getColumnCount();
158
159                while (this.rs.next()) {
160                        for (int i = 0; i < numCols; i++) {
161                                assertEquals("For column '"
162                                                + this.rs.getMetaData().getColumnName(i + 1)
163                                                + "' of collation "
164                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
165                                                                .getMetaData()).getColumnCharacterSet(i + 1),
166                                                numChars, this.rs.getString(i + 1).length());
167                        }
168                }
169
170                this.rs = ((com.mysql.jdbc.Connection) paddedConn)
171                                .clientPrepareStatement(query).executeQuery();
172
173                while (this.rs.next()) {
174                        for (int i = 0; i < numCols; i++) {
175                                assertEquals("For column '"
176                                                + this.rs.getMetaData().getColumnName(i + 1)
177                                                + "' of collation "
178                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
179                                                                .getMetaData()).getColumnCharacterSet(i + 1),
180                                                numChars, this.rs.getString(i + 1).length());
181                        }
182                }
183
184                if (versionMeetsMinimum(4, 1)) {
185                        this.rs = ((com.mysql.jdbc.Connection) paddedConn).serverPrepareStatement(
186                                        query).executeQuery();
187
188                        while (this.rs.next()) {
189                                for (int i = 0; i < numCols; i++) {
190                                        assertEquals("For column '"
191                                                        + this.rs.getMetaData().getColumnName(i + 1)
192                                                        + "' of collation "
193                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
194                                                                        .getMetaData())
195                                                                        .getColumnCharacterSet(i + 1), numChars,
196                                                        this.rs.getString(i + 1).length());
197                                }
198                        }
199                }
200
201                this.rs = this.stmt.executeQuery(query);
202
203                while (this.rs.next()) {
204                        for (int i = 0; i < numCols; i++) {
205                                if (this.rs.getRow() != 3) {
206                                        assertTrue("For column '"
207                                                        + this.rs.getMetaData().getColumnName(i + 1)
208                                                        + "' of collation "
209                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
210                                                                        .getMetaData())
211                                                                        .getColumnCharacterSet(i + 1),
212                                                        numChars != this.rs.getString(i + 1).length());
213                                } else {
214                                        assertEquals("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), numChars,
220                                                        this.rs.getString(i + 1).length());
221                                }
222                        }
223                }
224
225                this.rs = ((com.mysql.jdbc.Connection) this.conn)
226                                .clientPrepareStatement(query).executeQuery();
227
228                while (this.rs.next()) {
229                        for (int i = 0; i < numCols; i++) {
230                                if (this.rs.getRow() != 3) {
231                                        assertTrue("For column '"
232                                                        + this.rs.getMetaData().getColumnName(i + 1)
233                                                        + "' of collation "
234                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
235                                                                        .getMetaData())
236                                                                        .getColumnCharacterSet(i + 1),
237                                                        numChars != this.rs.getString(i + 1).length());
238                                } else {
239                                        assertEquals("For column '"
240                                                        + this.rs.getMetaData().getColumnName(i + 1)
241                                                        + "' of collation "
242                                                        + ((com.mysql.jdbc.ResultSetMetaData) this.rs
243                                                                        .getMetaData())
244                                                                        .getColumnCharacterSet(i + 1), numChars,
245                                                        this.rs.getString(i + 1).length());
246                                }
247                        }
248                }
249
250                if (versionMeetsMinimum(4, 1)) {
251                        this.rs = ((com.mysql.jdbc.Connection) this.conn).serverPrepareStatement(
252                                        query).executeQuery();
253
254                        while (this.rs.next()) {
255                                for (int i = 0; i < numCols; i++) {
256                                        if (this.rs.getRow() != 3) {
257                                                assertTrue("For column '"
258                                                                + this.rs.getMetaData().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).length());
264                                        } else {
265                                                assertEquals("For column '"
266                                                                + this.rs.getMetaData().getColumnName(i + 1)
267                                                                + "' of collation "
268                                                                + ((com.mysql.jdbc.ResultSetMetaData) this.rs
269                                                                                .getMetaData())
270                                                                                .getColumnCharacterSet(i + 1),
271                                                                numChars, this.rs.getString(i + 1).length());
272                                        }
273                                }
274                        }
275                }
276        }
277}
Note: See TracBrowser for help on using the repository browser.