KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > ConnectionTest


1 /*
2  Copyright (C) 2002-2004 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 package testsuite.simple;
26
27 import testsuite.BaseTestCase;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.DatabaseMetaData JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.ResultSetMetaData JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Savepoint JavaDoc;
39 import java.sql.Statement JavaDoc;
40
41 import java.util.Properties JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 import com.mysql.jdbc.ConnectionPropertiesTransform;
45 import com.mysql.jdbc.NonRegisteringDriver;
46 import com.mysql.jdbc.SQLError;
47 import com.mysql.jdbc.StringUtils;
48 import com.mysql.jdbc.log.StandardLogger;
49
50 /**
51  * Tests java.sql.Connection functionality ConnectionTest.java,v 1.1 2002/12/06
52  * 22:01:05 mmatthew Exp
53  *
54  * @author Mark Matthews
55  */

56 public class ConnectionTest extends BaseTestCase {
57     /**
58      * Constructor for ConnectionTest.
59      *
60      * @param name
61      * the name of the test to run
62      */

63     public ConnectionTest(String JavaDoc name) {
64         super(name);
65     }
66
67     /**
68      * Runs all test cases in this test suite
69      *
70      * @param args
71      */

72     public static void main(String JavaDoc[] args) {
73         junit.textui.TestRunner.run(ConnectionTest.class);
74     }
75
76     /**
77      * Tests catalog functionality
78      *
79      * @throws Exception
80      * if an error occurs
81      */

82     public void testCatalog() throws Exception JavaDoc {
83         String JavaDoc currentCatalog = this.conn.getCatalog();
84         this.conn.setCatalog(currentCatalog);
85         assertTrue(currentCatalog.equals(this.conn.getCatalog()));
86     }
87
88     /**
89      * Tests a cluster connection for failover, requires a two-node cluster URL
90      * specfied in com.mysql.jdbc.testsuite.ClusterUrl system proeprty.
91      *
92      * @throws Exception
93      * DOCUMENT ME!
94      */

95     public void testClusterConnection() throws Exception JavaDoc {
96         String JavaDoc url = System.getProperty("com.mysql.jdbc.testsuite.ClusterUrl");
97
98         if ((url != null) && (url.length() > 0)) {
99             Object JavaDoc versionNumObj = getSingleValueWithQuery("SHOW VARIABLES LIKE 'version'");
100
101             if ((versionNumObj != null)
102                     && (versionNumObj.toString().indexOf("cluster") != -1)) {
103                 Connection JavaDoc clusterConn = null;
104                 Statement JavaDoc clusterStmt = null;
105
106                 try {
107                     clusterConn = new NonRegisteringDriver().connect(url, null);
108
109                     clusterStmt = clusterConn.createStatement();
110                     clusterStmt
111                             .executeQuery("DROP TABLE IF EXISTS testClusterConn");
112                     clusterStmt
113                             .executeQuery("CREATE TABLE testClusterConn (field1 INT) TYPE=ndbcluster");
114                     clusterStmt
115                             .executeQuery("INSERT INTO testClusterConn VALUES (1)");
116
117                     clusterConn.setAutoCommit(false);
118
119                     clusterStmt.executeQuery("SELECT * FROM testClusterConn");
120                     clusterStmt
121                             .executeUpdate("UPDATE testClusterConn SET field1=4");
122
123                     // Kill the connection
124
String JavaDoc connectionId = getSingleValueWithQuery(
125                             "SELECT CONNECTION_ID()").toString();
126
127                     System.out
128                             .println("Please kill the MySQL server now and press return...");
129                     System.in.read();
130
131                     System.out.println("Waiting for TCP/IP timeout...");
132                     Thread.sleep(10);
133
134                     System.out.println("Attempting auto reconnect");
135
136                     try {
137                         clusterConn.setAutoCommit(true);
138                         clusterConn.setAutoCommit(false);
139                     } catch (SQLException JavaDoc sqlEx) {
140                         System.out.println(sqlEx);
141                     }
142
143                     //
144
// Test that this 'new' connection is not read-only
145
//
146
clusterStmt
147                             .executeUpdate("UPDATE testClusterConn SET field1=5");
148
149                     ResultSet JavaDoc rs = clusterStmt
150                             .executeQuery("SELECT * FROM testClusterConn WHERE field1=5");
151
152                     assertTrue("One row should be returned", rs.next());
153                 } finally {
154                     if (clusterStmt != null) {
155                         clusterStmt
156                                 .executeQuery("DROP TABLE IF EXISTS testClusterConn");
157                         clusterStmt.close();
158                     }
159
160                     if (clusterConn != null) {
161                         clusterConn.close();
162                     }
163                 }
164             }
165         }
166     }
167
168     /**
169      * DOCUMENT ME!
170      *
171      * @throws Exception
172      * DOCUMENT ME!
173      */

174     public void testDeadlockDetection() throws Exception JavaDoc {
175         try {
176             this.rs = this.stmt
177                     .executeQuery("SHOW VARIABLES LIKE 'innodb_lock_wait_timeout'");
178             this.rs.next();
179
180             int timeoutSecs = this.rs.getInt(2);
181
182             this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
183             this.stmt
184                     .executeUpdate("CREATE TABLE t1 (id INTEGER, x INTEGER) TYPE=INNODB");
185             this.stmt.executeUpdate("INSERT INTO t1 VALUES(0, 0)");
186             this.conn.setAutoCommit(false);
187             this.conn.createStatement().executeQuery(
188                     "SELECT * FROM t1 WHERE id=0 FOR UPDATE");
189
190             Connection JavaDoc deadlockConn = getConnectionWithProps(new Properties JavaDoc());
191             deadlockConn.setAutoCommit(false);
192
193             // The following query should hang because con1 is locking the page
194
deadlockConn.createStatement().executeUpdate(
195                     "UPDATE t1 SET x=2 WHERE id=0");
196             deadlockConn.commit();
197
198             Thread.sleep(timeoutSecs * 2 * 1000);
199         } catch (SQLException JavaDoc sqlEx) {
200             System.out
201                     .println("Caught SQLException due to deadlock/lock timeout");
202             System.out.println("SQLState: " + sqlEx.getSQLState());
203             System.out.println("Vendor error: " + sqlEx.getErrorCode());
204             System.out.println("Message: " + sqlEx.getMessage());
205
206             //
207
// Check whether the driver thinks it really is deadlock...
208
//
209
assertTrue(SQLError.SQL_STATE_DEADLOCK.equals(sqlEx.getSQLState()));
210             assertTrue(sqlEx.getErrorCode() == 1205);
211         } finally {
212             this.conn.setAutoCommit(true);
213             this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
214         }
215     }
216
217     /**
218      * DOCUMENT ME!
219      *
220      * @throws Exception
221      * DOCUMENT ME!
222      */

223     public void testCharsets() throws Exception JavaDoc {
224         if (versionMeetsMinimum(4, 1)) {
225             try {
226                 Properties JavaDoc props = new Properties JavaDoc();
227                 props.setProperty("useUnicode", "true");
228                 props.setProperty("characterEncoding", "UTF-8");
229
230                 Connection JavaDoc utfConn = getConnectionWithProps(props);
231
232                 this.stmt = utfConn.createStatement();
233
234                 this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
235                 // this.stmt.executeUpdate("SET CHARACTER SET latin1");
236

237                 this.stmt.executeUpdate("CREATE TABLE t1 ("
238                         + "comment CHAR(32) ASCII NOT NULL,"
239                         + "koi8_ru_f CHAR(32) CHARACTER SET koi8r NOT NULL"
240                         + ") CHARSET=latin5");
241
242                 this.stmt
243                         .executeUpdate("ALTER TABLE t1 CHANGE comment comment CHAR(32) CHARACTER SET latin2 NOT NULL");
244                 this.stmt
245                         .executeUpdate("ALTER TABLE t1 ADD latin5_f CHAR(32) NOT NULL");
246                 this.stmt.executeUpdate("ALTER TABLE t1 CHARSET=latin2");
247                 this.stmt
248                         .executeUpdate("ALTER TABLE t1 ADD latin2_f CHAR(32) NOT NULL");
249                 this.stmt
250                         .executeUpdate("ALTER TABLE t1 DROP latin2_f, DROP latin5_f");
251
252                 this.stmt
253                         .executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('a','LAT SMALL A')");
254                 /*
255                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
256                  * VALUES ('b','LAT SMALL B')"); this.stmt.executeUpdate("INSERT
257                  * INTO t1 (koi8_ru_f,comment) VALUES ('c','LAT SMALL C')");
258                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
259                  * VALUES ('d','LAT SMALL D')"); this.stmt.executeUpdate("INSERT
260                  * INTO t1 (koi8_ru_f,comment) VALUES ('e','LAT SMALL E')");
261                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
262                  * VALUES ('f','LAT SMALL F')"); this.stmt.executeUpdate("INSERT
263                  * INTO t1 (koi8_ru_f,comment) VALUES ('g','LAT SMALL G')");
264                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
265                  * VALUES ('h','LAT SMALL H')"); this.stmt.executeUpdate("INSERT
266                  * INTO t1 (koi8_ru_f,comment) VALUES ('i','LAT SMALL I')");
267                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
268                  * VALUES ('j','LAT SMALL J')"); this.stmt.executeUpdate("INSERT
269                  * INTO t1 (koi8_ru_f,comment) VALUES ('k','LAT SMALL K')");
270                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
271                  * VALUES ('l','LAT SMALL L')"); this.stmt.executeUpdate("INSERT
272                  * INTO t1 (koi8_ru_f,comment) VALUES ('m','LAT SMALL M')");
273                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
274                  * VALUES ('n','LAT SMALL N')"); this.stmt.executeUpdate("INSERT
275                  * INTO t1 (koi8_ru_f,comment) VALUES ('o','LAT SMALL O')");
276                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
277                  * VALUES ('p','LAT SMALL P')"); this.stmt.executeUpdate("INSERT
278                  * INTO t1 (koi8_ru_f,comment) VALUES ('q','LAT SMALL Q')");
279                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
280                  * VALUES ('r','LAT SMALL R')"); this.stmt.executeUpdate("INSERT
281                  * INTO t1 (koi8_ru_f,comment) VALUES ('s','LAT SMALL S')");
282                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
283                  * VALUES ('t','LAT SMALL T')"); this.stmt.executeUpdate("INSERT
284                  * INTO t1 (koi8_ru_f,comment) VALUES ('u','LAT SMALL U')");
285                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
286                  * VALUES ('v','LAT SMALL V')"); this.stmt.executeUpdate("INSERT
287                  * INTO t1 (koi8_ru_f,comment) VALUES ('w','LAT SMALL W')");
288                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
289                  * VALUES ('x','LAT SMALL X')"); this.stmt.executeUpdate("INSERT
290                  * INTO t1 (koi8_ru_f,comment) VALUES ('y','LAT SMALL Y')");
291                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
292                  * VALUES ('z','LAT SMALL Z')"); this.stmt.executeUpdate("INSERT
293                  * INTO t1 (koi8_ru_f,comment) VALUES ('A','LAT CAPIT A')");
294                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
295                  * VALUES ('B','LAT CAPIT B')"); this.stmt.executeUpdate("INSERT
296                  * INTO t1 (koi8_ru_f,comment) VALUES ('C','LAT CAPIT C')");
297                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
298                  * VALUES ('D','LAT CAPIT D')"); this.stmt.executeUpdate("INSERT
299                  * INTO t1 (koi8_ru_f,comment) VALUES ('E','LAT CAPIT E')");
300                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
301                  * VALUES ('F','LAT CAPIT F')"); this.stmt.executeUpdate("INSERT
302                  * INTO t1 (koi8_ru_f,comment) VALUES ('G','LAT CAPIT G')");
303                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
304                  * VALUES ('H','LAT CAPIT H')"); this.stmt.executeUpdate("INSERT
305                  * INTO t1 (koi8_ru_f,comment) VALUES ('I','LAT CAPIT I')");
306                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
307                  * VALUES ('J','LAT CAPIT J')"); this.stmt.executeUpdate("INSERT
308                  * INTO t1 (koi8_ru_f,comment) VALUES ('K','LAT CAPIT K')");
309                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
310                  * VALUES ('L','LAT CAPIT L')"); this.stmt.executeUpdate("INSERT
311                  * INTO t1 (koi8_ru_f,comment) VALUES ('M','LAT CAPIT M')");
312                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
313                  * VALUES ('N','LAT CAPIT N')"); this.stmt.executeUpdate("INSERT
314                  * INTO t1 (koi8_ru_f,comment) VALUES ('O','LAT CAPIT O')");
315                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
316                  * VALUES ('P','LAT CAPIT P')"); this.stmt.executeUpdate("INSERT
317                  * INTO t1 (koi8_ru_f,comment) VALUES ('Q','LAT CAPIT Q')");
318                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
319                  * VALUES ('R','LAT CAPIT R')"); this.stmt.executeUpdate("INSERT
320                  * INTO t1 (koi8_ru_f,comment) VALUES ('S','LAT CAPIT S')");
321                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
322                  * VALUES ('T','LAT CAPIT T')"); this.stmt.executeUpdate("INSERT
323                  * INTO t1 (koi8_ru_f,comment) VALUES ('U','LAT CAPIT U')");
324                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
325                  * VALUES ('V','LAT CAPIT V')"); this.stmt.executeUpdate("INSERT
326                  * INTO t1 (koi8_ru_f,comment) VALUES ('W','LAT CAPIT W')");
327                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
328                  * VALUES ('X','LAT CAPIT X')"); this.stmt.executeUpdate("INSERT
329                  * INTO t1 (koi8_ru_f,comment) VALUES ('Y','LAT CAPIT Y')");
330                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
331                  * VALUES ('Z','LAT CAPIT Z')");
332                  */

333
334                 String JavaDoc cyrillicSmallA = "\u0430";
335                 this.stmt
336                         .executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('"
337                                 + cyrillicSmallA + "','CYR SMALL A')");
338
339                 /*
340                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
341                  * VALUES (_koi8r'�','CYR SMALL BE')");
342                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
343                  * VALUES (_koi8r'�','CYR SMALL VE')");
344                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
345                  * VALUES (_koi8r'�','CYR SMALL GE')");
346                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
347                  * VALUES (_koi8r'�','CYR SMALL DE')");
348                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
349                  * VALUES (_koi8r'�','CYR SMALL IE')");
350                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
351                  * VALUES (_koi8r'�','CYR SMALL IO')");
352                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
353                  * VALUES (_koi8r'�','CYR SMALL ZHE')");
354                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
355                  * VALUES (_koi8r'�','CYR SMALL ZE')");
356                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
357                  * VALUES (_koi8r'�','CYR SMALL I')");
358                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
359                  * VALUES (_koi8r'�','CYR SMALL KA')");
360                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
361                  * VALUES (_koi8r'�','CYR SMALL EL')");
362                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
363                  * VALUES (_koi8r'�','CYR SMALL EM')");
364                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
365                  * VALUES (_koi8r'�','CYR SMALL EN')");
366                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
367                  * VALUES (_koi8r'�','CYR SMALL O')");
368                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
369                  * VALUES (_koi8r'�','CYR SMALL PE')");
370                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
371                  * VALUES (_koi8r'�','CYR SMALL ER')");
372                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
373                  * VALUES (_koi8r'�','CYR SMALL ES')");
374                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
375                  * VALUES (_koi8r'�','CYR SMALL TE')");
376                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
377                  * VALUES (_koi8r'�','CYR SMALL U')");
378                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
379                  * VALUES (_koi8r'�','CYR SMALL EF')");
380                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
381                  * VALUES (_koi8r'�','CYR SMALL HA')");
382                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
383                  * VALUES (_koi8r'�','CYR SMALL TSE')");
384                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
385                  * VALUES (_koi8r'�','CYR SMALL CHE')");
386                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
387                  * VALUES (_koi8r'�','CYR SMALL SHA')");
388                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
389                  * VALUES (_koi8r'�','CYR SMALL SCHA')");
390                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
391                  * VALUES (_koi8r'�','CYR SMALL HARD SIGN')");
392                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
393                  * VALUES (_koi8r'�','CYR SMALL YERU')");
394                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
395                  * VALUES (_koi8r'�','CYR SMALL SOFT SIGN')");
396                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
397                  * VALUES (_koi8r'�','CYR SMALL E')");
398                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
399                  * VALUES (_koi8r'�','CYR SMALL YU')");
400                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
401                  * VALUES (_koi8r'�','CYR SMALL YA')");
402                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
403                  * VALUES (_koi8r'�','CYR CAPIT A')");
404                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
405                  * VALUES (_koi8r'�','CYR CAPIT BE')");
406                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
407                  * VALUES (_koi8r'�','CYR CAPIT VE')");
408                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
409                  * VALUES (_koi8r'�','CYR CAPIT GE')");
410                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
411                  * VALUES (_koi8r'�','CYR CAPIT DE')");
412                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
413                  * VALUES (_koi8r'�','CYR CAPIT IE')");
414                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
415                  * VALUES (_koi8r'�','CYR CAPIT IO')");
416                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
417                  * VALUES (_koi8r'�','CYR CAPIT ZHE')");
418                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
419                  * VALUES (_koi8r'�','CYR CAPIT ZE')");
420                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
421                  * VALUES (_koi8r'�','CYR CAPIT I')");
422                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
423                  * VALUES (_koi8r'�','CYR CAPIT KA')");
424                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
425                  * VALUES (_koi8r'�','CYR CAPIT EL')");
426                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
427                  * VALUES (_koi8r'�','CYR CAPIT EM')");
428                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
429                  * VALUES (_koi8r'�','CYR CAPIT EN')");
430                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
431                  * VALUES (_koi8r'�','CYR CAPIT O')");
432                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
433                  * VALUES (_koi8r'�','CYR CAPIT PE')");
434                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
435                  * VALUES (_koi8r'�','CYR CAPIT ER')");
436                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
437                  * VALUES (_koi8r'�','CYR CAPIT ES')");
438                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
439                  * VALUES (_koi8r'�','CYR CAPIT TE')");
440                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
441                  * VALUES (_koi8r'�','CYR CAPIT U')");
442                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
443                  * VALUES (_koi8r'�','CYR CAPIT EF')");
444                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
445                  * VALUES (_koi8r'�','CYR CAPIT HA')");
446                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
447                  * VALUES (_koi8r'�','CYR CAPIT TSE')");
448                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
449                  * VALUES (_koi8r'�','CYR CAPIT CHE')");
450                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
451                  * VALUES (_koi8r'�','CYR CAPIT SHA')");
452                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
453                  * VALUES (_koi8r'�','CYR CAPIT SCHA')");
454                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
455                  * VALUES (_koi8r'�','CYR CAPIT HARD SIGN')");
456                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
457                  * VALUES (_koi8r'�','CYR CAPIT YERU')");
458                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
459                  * VALUES (_koi8r'�','CYR CAPIT SOFT SIGN')");
460                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
461                  * VALUES (_koi8r'�','CYR CAPIT E')");
462                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
463                  * VALUES (_koi8r'�','CYR CAPIT YU')");
464                  * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)
465                  * VALUES (_koi8r'�','CYR CAPIT YA')");
466                  */

467
468                 this.stmt
469                         .executeUpdate("ALTER TABLE t1 ADD utf8_f CHAR(32) CHARACTER SET utf8 NOT NULL");
470                 this.stmt
471                         .executeUpdate("UPDATE t1 SET utf8_f=CONVERT(koi8_ru_f USING utf8)");
472                 this.stmt.executeUpdate("SET CHARACTER SET koi8r");
473                 // this.stmt.executeUpdate("SET CHARACTER SET UTF8");
474
this.rs = this.stmt.executeQuery("SELECT * FROM t1");
475
476                 ResultSetMetaData JavaDoc rsmd = this.rs.getMetaData();
477
478                 int numColumns = rsmd.getColumnCount();
479
480                 for (int i = 0; i < numColumns; i++) {
481                     System.out.print(rsmd.getColumnName(i + 1));
482                     System.out.print("\t\t");
483                 }
484
485                 System.out.println();
486
487                 while (this.rs.next()) {
488                     System.out.println(this.rs.getString(1) + "\t\t"
489                             + this.rs.getString(2) + "\t\t"
490                             + this.rs.getString(3));
491
492                     if (this.rs.getString(1).equals("CYR SMALL A")) {
493                         this.rs.getString(2);
494                     }
495                 }
496
497                 System.out.println();
498
499                 this.stmt.executeUpdate("SET NAMES utf8");
500                 this.rs = this.stmt.executeQuery("SELECT _koi8r 0xC1;");
501
502                 rsmd = this.rs.getMetaData();
503
504                 numColumns = rsmd.getColumnCount();
505
506                 for (int i = 0; i < numColumns; i++) {
507                     System.out.print(rsmd.getColumnName(i + 1));
508                     System.out.print("\t\t");
509                 }
510
511                 System.out.println();
512
513                 while (this.rs.next()) {
514                     System.out.println(this.rs.getString(1).equals("\u0430")
515                             + "\t\t");
516                     System.out
517                             .println(new String JavaDoc(this.rs.getBytes(1), "KOI8_R"));
518
519                 }
520
521                 char[] c = new char[] { 0xd0b0 };
522
523                 System.out.println(new String JavaDoc(c));
524                 System.out.println("\u0430");
525             } finally {
526                 // this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
527
}
528         }
529     }
530
531     /**
532      * Tests isolation level functionality
533      *
534      * @throws Exception
535      * if an error occurs
536      */

537     public void testIsolationLevel() throws Exception JavaDoc {
538         if (versionMeetsMinimum(4, 0)) {
539             String JavaDoc[] isoLevelNames = new String JavaDoc[] {
540                     "Connection.TRANSACTION_NONE",
541                     "Connection.TRANSACTION_READ_COMMITTED",
542                     "Connection.TRANSACTION_READ_UNCOMMITTED",
543                     "Connection.TRANSACTION_REPEATABLE_READ",
544                     "Connection.TRANSACTION_SERIALIZABLE" };
545
546             int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE,
547                     Connection.TRANSACTION_READ_COMMITTED,
548                     Connection.TRANSACTION_READ_UNCOMMITTED,
549                     Connection.TRANSACTION_REPEATABLE_READ,
550                     Connection.TRANSACTION_SERIALIZABLE };
551
552             DatabaseMetaData JavaDoc dbmd = this.conn.getMetaData();
553
554             for (int i = 0; i < isolationLevels.length; i++) {
555                 if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) {
556                     this.conn.setTransactionIsolation(isolationLevels[i]);
557
558                     assertTrue(
559                             "Transaction isolation level that was set ("
560                                     + isoLevelNames[i]
561                                     + ") was not returned, nor was a more restrictive isolation level used by the server",
562                             this.conn.getTransactionIsolation() == isolationLevels[i]
563                                     || this.conn.getTransactionIsolation() > isolationLevels[i]);
564                 }
565             }
566         }
567     }
568
569     /**
570      * Tests the savepoint functionality in MySQL.
571      *
572      * @throws Exception
573      * if an error occurs.
574      */

575     public void testSavepoint() throws Exception JavaDoc {
576         DatabaseMetaData JavaDoc dbmd = this.conn.getMetaData();
577
578         if (dbmd.supportsSavepoints()) {
579             System.out.println("Testing SAVEPOINTs");
580
581             try {
582                 this.conn.setAutoCommit(true);
583
584                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints");
585                 this.stmt
586                         .executeUpdate("CREATE TABLE testSavepoints (field1 int) TYPE=InnoDB");
587
588                 // Try with named save points
589
this.conn.setAutoCommit(false);
590                 this.stmt
591                         .executeUpdate("INSERT INTO testSavepoints VALUES (1)");
592
593                 Savepoint JavaDoc afterInsert = this.conn.setSavepoint("afterInsert");
594                 this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
595
596                 Savepoint JavaDoc afterUpdate = this.conn.setSavepoint("afterUpdate");
597                 this.stmt.executeUpdate("DELETE FROM testSavepoints");
598
599                 assertTrue("Row count should be 0",
600                         getRowCount("testSavepoints") == 0);
601                 this.conn.rollback(afterUpdate);
602                 assertTrue("Row count should be 1",
603                         getRowCount("testSavepoints") == 1);
604                 assertTrue("Value should be 2", "2".equals(getSingleValue(
605                         "testSavepoints", "field1", null).toString()));
606                 this.conn.rollback(afterInsert);
607                 assertTrue("Value should be 1", "1".equals(getSingleValue(
608                         "testSavepoints", "field1", null).toString()));
609                 this.conn.rollback();
610                 assertTrue("Row count should be 0",
611                         getRowCount("testSavepoints") == 0);
612
613                 // Try with 'anonymous' save points
614
this.conn.rollback();
615
616                 this.stmt
617                         .executeUpdate("INSERT INTO testSavepoints VALUES (1)");
618                 afterInsert = this.conn.setSavepoint();
619                 this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
620                 afterUpdate = this.conn.setSavepoint();
621                 this.stmt.executeUpdate("DELETE FROM testSavepoints");
622
623                 assertTrue("Row count should be 0",
624                         getRowCount("testSavepoints") == 0);
625                 this.conn.rollback(afterUpdate);
626                 assertTrue("Row count should be 1",
627                         getRowCount("testSavepoints") == 1);
628                 assertTrue("Value should be 2", "2".equals(getSingleValue(
629                         "testSavepoints", "field1", null).toString()));
630                 this.conn.rollback(afterInsert);
631                 assertTrue("Value should be 1", "1".equals(getSingleValue(
632                         "testSavepoints", "field1", null).toString()));
633                 this.conn.rollback();
634
635                 this.conn.releaseSavepoint(this.conn.setSavepoint());
636             } finally {
637                 this.conn.setAutoCommit(true);
638                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints");
639             }
640         } else {
641             System.out.println("MySQL version does not support SAVEPOINTs");
642         }
643     }
644
645     /**
646      * Tests the ability to set the connection collation via properties.
647      *
648      * @throws Exception
649      * if an error occurs or the test fails
650      */

651     public void testNonStandardConnectionCollation() throws Exception JavaDoc {
652         if (versionMeetsMinimum(4, 1)) {
653             String JavaDoc collationToSet = "utf8_bin";
654             String JavaDoc characterSet = "utf8";
655
656             Properties JavaDoc props = new Properties JavaDoc();
657             props.setProperty("connectionCollation", collationToSet);
658             props.setProperty("characterEncoding", characterSet);
659
660             Connection JavaDoc collConn = null;
661             Statement JavaDoc collStmt = null;
662             ResultSet JavaDoc collRs = null;
663
664             try {
665                 collConn = getConnectionWithProps(props);
666
667                 collStmt = collConn.createStatement();
668
669                 collRs = collStmt
670                         .executeQuery("SHOW VARIABLES LIKE 'collation_connection'");
671
672                 assertTrue(collRs.next());
673                 assertTrue(collationToSet.equalsIgnoreCase(collRs.getString(2)));
674             } finally {
675                 if (collConn != null) {
676                     collConn.close();
677                 }
678             }
679         }
680     }
681
682     public void testDumpQueriesOnException() throws Exception JavaDoc {
683         Properties JavaDoc props = new Properties JavaDoc();
684         props.setProperty("dumpQueriesOnException", "true");
685         String JavaDoc bogusSQL = "SELECT 1 TO BAZ";
686         Connection JavaDoc dumpConn = getConnectionWithProps(props);
687
688         try {
689             dumpConn.createStatement().executeQuery(bogusSQL);
690         } catch (SQLException JavaDoc sqlEx) {
691             assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1);
692         }
693
694         try {
695             ((com.mysql.jdbc.Connection) dumpConn).clientPrepareStatement(
696                     bogusSQL).executeQuery();
697         } catch (SQLException JavaDoc sqlEx) {
698             assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1);
699         }
700
701         try {
702             this.stmt
703                     .executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException");
704             this.stmt
705                     .executeUpdate("CREATE TABLE testDumpQueriesOnException (field1 int UNIQUE)");
706             this.stmt
707                     .executeUpdate("INSERT INTO testDumpQueriesOnException VALUES (1)");
708
709             PreparedStatement JavaDoc pStmt = dumpConn
710                     .prepareStatement("INSERT INTO testDumpQueriesOnException VALUES (?)");
711             pStmt.setInt(1, 1);
712             pStmt.executeUpdate();
713         } catch (SQLException JavaDoc sqlEx) {
714             assertTrue(sqlEx.getMessage().indexOf(
715                     "INSERT INTO testDumpQueriesOnException") != -1);
716         } finally {
717             this.stmt
718                     .executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException");
719         }
720
721         try {
722             dumpConn.prepareStatement(bogusSQL);
723         } catch (SQLException JavaDoc sqlEx) {
724             assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1);
725         }
726     }
727
728     /**
729      * Tests functionality of the ConnectionPropertiesTransform interface.
730      *
731      * @throws Exception
732      * if the test fails.
733      */

734     public void testConnectionPropertiesTransform() throws Exception JavaDoc {
735         String JavaDoc transformClassName = SimpleTransformer.class.getName();
736
737         Properties JavaDoc props = new Properties JavaDoc();
738
739         props.setProperty(NonRegisteringDriver.PROPERTIES_TRANSFORM_KEY,
740                 transformClassName);
741
742         NonRegisteringDriver driver = new NonRegisteringDriver();
743
744         Properties JavaDoc transformedProps = driver
745                 .parseURL(BaseTestCase.dbUrl, props);
746
747         assertTrue("albequerque".equals(transformedProps
748                 .getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY)));
749     }
750
751     /**
752      * Tests functionality of using URLs in 'LOAD DATA LOCAL INFILE' statements.
753      *
754      * @throws Exception
755      * if the test fails.
756      */

757     public void testLocalInfileWithUrl() throws Exception JavaDoc {
758         File JavaDoc infile = File.createTempFile("foo", "txt");
759         infile.deleteOnExit();
760         String JavaDoc url = infile.toURL().toExternalForm();
761         FileWriter JavaDoc output = new FileWriter JavaDoc(infile);
762         output.write("Test");
763         output.flush();
764         output.close();
765
766         try {
767             this.stmt
768                     .executeUpdate("DROP TABLE IF EXISTS testLocalInfileWithUrl");
769             this.stmt
770                     .executeUpdate("CREATE TABLE testLocalInfileWithUrl (field1 LONGTEXT)");
771
772             Properties JavaDoc props = new Properties JavaDoc();
773             props.setProperty("allowUrlInLocalInfile", "true");
774
775             Connection JavaDoc loadConn = getConnectionWithProps(props);
776             Statement JavaDoc loadStmt = loadConn.createStatement();
777
778             try {
779                 loadStmt.executeQuery("LOAD DATA LOCAL INFILE '" + url
780                         + "' INTO TABLE testLocalInfileWithUrl");
781             } catch (SQLException JavaDoc sqlEx) {
782                 sqlEx.printStackTrace();
783
784                 throw sqlEx;
785             }
786
787             this.rs = this.stmt
788                     .executeQuery("SELECT * FROM testLocalInfileWithUrl");
789             assertTrue(this.rs.next());
790             assertTrue("Test".equals(this.rs.getString(1)));
791             int count = this.stmt
792                     .executeUpdate("DELETE FROM testLocalInfileWithUrl");
793             assertTrue(count == 1);
794
795             StringBuffer JavaDoc escapedPath = new StringBuffer JavaDoc();
796             String JavaDoc path = infile.getCanonicalPath();
797
798             for (int i = 0; i < path.length(); i++) {
799                 char c = path.charAt(i);
800
801                 if (c == '\\') {
802                     escapedPath.append('\\');
803                 }
804
805                 escapedPath.append(c);
806             }
807
808             loadStmt.executeQuery("LOAD DATA LOCAL INFILE '"
809                     + escapedPath.toString()
810                     + "' INTO TABLE testLocalInfileWithUrl");
811             this.rs = this.stmt
812                     .executeQuery("SELECT * FROM testLocalInfileWithUrl");
813             assertTrue(this.rs.next());
814             assertTrue("Test".equals(this.rs.getString(1)));
815
816             try {
817                 loadStmt
818                         .executeQuery("LOAD DATA LOCAL INFILE 'foo:///' INTO TABLE testLocalInfileWithUrl");
819             } catch (SQLException JavaDoc sqlEx) {
820                 assertTrue(sqlEx.getMessage() != null);
821                 assertTrue(sqlEx.getMessage().indexOf("FileNotFoundException") != -1);
822             }
823
824         } finally {
825             this.stmt
826                     .executeUpdate("DROP TABLE IF EXISTS testLocalInfileWithUrl");
827         }
828     }
829
830     public void testServerConfigurationCache() throws Exception JavaDoc {
831         Properties JavaDoc props = new Properties JavaDoc();
832
833         props.setProperty("cacheServerConfiguration", "true");
834         props.setProperty("profileSQL", "true");
835         props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");
836
837         Connection JavaDoc conn1 = getConnectionWithProps(props);
838
839         StandardLogger.saveLogsToBuffer();
840
841         Connection JavaDoc conn2 = getConnectionWithProps(props);
842
843         assertTrue("Configuration wasn't cached", StandardLogger.bufferedLog
844                 .toString().indexOf("SHOW VARIABLES") == -1);
845
846         if (versionMeetsMinimum(4, 1)) {
847             assertTrue("Configuration wasn't cached",
848                     StandardLogger.bufferedLog.toString().indexOf(
849                             "SHOW COLLATION") == -1);
850
851         }
852     }
853
854     /**
855      * Tests whether or not the configuration 'useLocalSessionState' actually
856      * prevents non-needed 'set autocommit=', 'set session transaction isolation
857      * ...' and 'show variables like tx_isolation' queries.
858      *
859      * @throws Exception
860      * if the test fails.
861      */

862     public void testUseLocalSessionState() throws Exception JavaDoc {
863         Properties JavaDoc props = new Properties JavaDoc();
864
865         props.setProperty("useLocalSessionState", "true");
866         props.setProperty("profileSQL", "true");
867         props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");
868
869         Connection JavaDoc conn1 = getConnectionWithProps(props);
870         conn1.setAutoCommit(true);
871         conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
872
873         StandardLogger.saveLogsToBuffer();
874         StandardLogger.bufferedLog.setLength(0);
875
876         conn1.setAutoCommit(true);
877         conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
878         conn1.getTransactionIsolation();
879
880         String JavaDoc logAsString = StandardLogger.bufferedLog.toString();
881
882         assertTrue(logAsString.indexOf("SET SESSION") == -1
883                 && logAsString.indexOf("SHOW VARIABLES LIKE 'tx_isolation'") == -1
884                 && logAsString.indexOf("SET autocommit=") == -1);
885
886     }
887
888     /**
889      * Tests whether re-connect with non-read-only connection can happen.
890      *
891      * @throws Exception
892      * if the test fails.
893      */

894     public void testFailoverConnection() throws Exception JavaDoc {
895
896         Properties JavaDoc props = new Properties JavaDoc();
897         props.setProperty("autoReconnect", "true");
898         props.setProperty("failOverReadOnly", "false");
899
900         // Re-build the connection information
901
int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2;
902         int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost);
903
904         String JavaDoc hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost,
905                 lastIndexOfHost);
906         System.out.println(hostPortPair);
907
908         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(hostPortPair, ":");
909
910         String JavaDoc host = null;
911         String JavaDoc port = null;
912
913         if (st.hasMoreTokens()) {
914             String JavaDoc possibleHostOrPort = st.nextToken();
915
916             if (Character.isDigit(possibleHostOrPort.charAt(0))) {
917                 port = possibleHostOrPort;
918                 host = "localhost";
919             } else {
920                 host = possibleHostOrPort;
921             }
922         }
923
924         if (host == null) {
925             host = "localhost";
926         }
927         
928         if (st.hasMoreTokens()) {
929             port = st.nextToken();
930         }
931
932         StringBuffer JavaDoc newHostBuf = new StringBuffer JavaDoc();
933         newHostBuf.append(host);
934         if (port != null) {
935             newHostBuf.append(":");
936             newHostBuf.append(port);
937         }
938         newHostBuf.append(",");
939         newHostBuf.append(host);
940         if (port != null) {
941             newHostBuf.append(":");
942             newHostBuf.append(port);
943         }
944
945         props
946                 .put(NonRegisteringDriver.HOST_PROPERTY_KEY, newHostBuf
947                         .toString());
948
949         Connection JavaDoc failoverConnection = null;
950
951         try {
952             failoverConnection = getConnectionWithProps(props);
953
954             String JavaDoc originalConnectionId = getSingleIndexedValueWithQuery(
955                     failoverConnection, 1, "SELECT connection_id()").toString();
956             System.out.println("Original Connection Id = "
957                     + originalConnectionId);
958
959             assertTrue("Connection should not be in READ_ONLY state",
960                     !failoverConnection.isReadOnly());
961
962             // Kill the connection
963
this.stmt.executeUpdate("KILL " + originalConnectionId);
964
965             // This takes a bit to occur
966

967             Thread.sleep(3000);
968
969             try {
970                 failoverConnection.createStatement().executeQuery("SELECT 1");
971                 fail("We expect an exception here, because the connection should be gone until the reconnect code picks it up again");
972             } catch (SQLException JavaDoc sqlEx) {
973                 ; // do-nothing
974
}
975
976             // Tickle re-connect
977

978             failoverConnection.setAutoCommit(true);
979
980             String JavaDoc newConnectionId = getSingleIndexedValueWithQuery(
981                     failoverConnection, 1, "SELECT connection_id()").toString();
982             System.out.println("new Connection Id = " + newConnectionId);
983
984             assertTrue(
985                     "We should have a new connection to the server in this case",
986                     !newConnectionId.equals(originalConnectionId));
987             assertTrue("Connection should not be read-only",
988                     !failoverConnection.isReadOnly());
989         } finally {
990             if (failoverConnection != null) {
991                 failoverConnection.close();
992             }
993         }
994
995     }
996
997     public void testCannedConfigs() throws Exception JavaDoc {
998         String JavaDoc url = "jdbc:mysql:///?useConfigs=clusterBase";
999
1000        Properties JavaDoc cannedProps = new NonRegisteringDriver().parseURL(url, null);
1001
1002        assertTrue("true".equals(cannedProps.getProperty("autoReconnect")));
1003        assertTrue("false".equals(cannedProps.getProperty("failOverReadOnly")));
1004        assertTrue("true".equals(cannedProps
1005                .getProperty("roundRobinLoadBalance")));
1006
1007        // this will fail, but we test that too
1008
url = "jdbc:mysql:///?useConfigs=clusterBase,clusterBase2";
1009
1010        try {
1011            cannedProps = new NonRegisteringDriver().parseURL(url, null);
1012            fail("should've bailed on that one!");
1013        } catch (SQLException JavaDoc sqlEx) {
1014            assertTrue(SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE
1015                    .equals(sqlEx.getSQLState()));
1016        }
1017    }
1018
1019    public void testUseOldUTF8Behavior() throws Exception JavaDoc {
1020
1021        Properties JavaDoc props = new Properties JavaDoc();
1022        props.setProperty("useOldUTF8Behavior", "true");
1023        props.setProperty("useUnicode", "true");
1024        props.setProperty("characterEncoding", "UTF-8");
1025        props.setProperty("logFactory", "com.mysql.jdbc.log.StandardLogger");
1026        props.setProperty("profileSQL", "true");
1027        StandardLogger.saveLogsToBuffer();
1028        StandardLogger.bufferedLog.setLength(0);
1029
1030        try {
1031            getConnectionWithProps(props);
1032
1033            assertTrue(StringUtils.indexOfIgnoreCase(StandardLogger.bufferedLog
1034                    .toString(), "SET NAMES utf8") == -1);
1035        } finally {
1036            StandardLogger.bufferedLog = null;
1037        }
1038    }
1039
1040    /**
1041     * Checks implementation of 'dontTrackOpenResources' property.
1042     *
1043     * @throws Exception
1044     * if the test fails.
1045     */

1046    public void testDontTrackOpenResources() throws Exception JavaDoc {
1047        Properties JavaDoc props = new Properties JavaDoc();
1048
1049        props.setProperty("dontTrackOpenResources", "true");
1050        Connection JavaDoc noTrackConn = null;
1051        Statement JavaDoc noTrackStatement = null;
1052        PreparedStatement JavaDoc noTrackPstmt = null;
1053        ResultSet JavaDoc rs2 = null;
1054
1055        try {
1056            noTrackConn = getConnectionWithProps(props);
1057            noTrackStatement = noTrackConn.createStatement();
1058            noTrackPstmt = noTrackConn.prepareStatement("SELECT 1");
1059            rs2 = noTrackPstmt.executeQuery();
1060            rs2.next();
1061
1062            this.rs = noTrackStatement.executeQuery("SELECT 1");
1063            this.rs.next();
1064
1065            noTrackConn.close();
1066
1067            // Under 'strict' JDBC requirements, these calls should fail
1068
// (and _do_ if dontTrackOpenResources == false)
1069

1070            this.rs.getString(1);
1071            rs2.getString(1);
1072        } finally {
1073            if (rs2 != null) {
1074                rs2.close();
1075            }
1076
1077            if (noTrackStatement != null) {
1078                noTrackStatement.close();
1079            }
1080
1081            if (noTrackConn != null & !noTrackConn.isClosed()) {
1082                noTrackConn.close();
1083            }
1084        }
1085    }
1086
1087    public void testPing() throws SQLException JavaDoc {
1088        Connection JavaDoc conn2 = getConnectionWithProps(null);
1089
1090        ((com.mysql.jdbc.Connection) conn2).ping();
1091        conn2.close();
1092
1093        try {
1094            ((com.mysql.jdbc.Connection) conn2).ping();
1095            fail("Should have failed with an exception");
1096        } catch (SQLException JavaDoc sqlEx) {
1097            // ignore for now
1098
}
1099
1100        //
1101
// This feature caused BUG#8975, so check for that too!
1102

1103        Properties JavaDoc props = new Properties JavaDoc();
1104        props.setProperty("autoReconnect", "true");
1105
1106        getConnectionWithProps(props);
1107    }
1108
1109    public void testSessionVariables() throws Exception JavaDoc {
1110        String JavaDoc getInitialMaxAllowedPacket = getMysqlVariable("max_allowed_packet");
1111
1112        int newMaxAllowedPacket = Integer.parseInt(getInitialMaxAllowedPacket) + 1024;
1113
1114        Properties JavaDoc props = new Properties JavaDoc();
1115        props.setProperty("sessionVariables", "max_allowed_packet="
1116                + newMaxAllowedPacket);
1117        props.setProperty("profileSQL", "true");
1118
1119        Connection JavaDoc varConn = getConnectionWithProps(props);
1120
1121        assertTrue(!getInitialMaxAllowedPacket.equals(getMysqlVariable(varConn,
1122                "max_allowed_packet")));
1123    }
1124
1125    /**
1126     * Tests setting profileSql on/off in the span of one connection.
1127     *
1128     * @throws Exception
1129     * if an error occurs.
1130     */

1131    public void testSetProfileSql() throws Exception JavaDoc {
1132        ((com.mysql.jdbc.Connection) this.conn).setProfileSql(false);
1133        stmt.executeQuery("SELECT 1");
1134        ((com.mysql.jdbc.Connection) this.conn).setProfileSql(true);
1135        stmt.executeQuery("SELECT 1");
1136    }
1137
1138    public void testCreateDatabaseIfNotExist() throws Exception JavaDoc {
1139        if (isAdminConnectionConfigured()) {
1140            Properties JavaDoc props = new Properties JavaDoc();
1141            props.setProperty("createDatabaseIfNotExist", "true");
1142            props.setProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY,
1143                    "testcreatedatabaseifnotexists");
1144
1145            Connection JavaDoc newConn = getAdminConnectionWithProps(props);
1146            newConn.createStatement().executeUpdate(
1147                    "DROP DATABASE testcreatedatabaseifnotexists");
1148        }
1149    }
1150}
1151
Popular Tags