KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > StringRegressionTest


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.regression;
26
27 import com.mysql.jdbc.StringUtils;
28
29 import testsuite.BaseTestCase;
30 import testsuite.simple.BlobTest;
31
32 import java.sql.Connection JavaDoc;
33 import java.sql.DriverManager JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.Statement JavaDoc;
37
38 import java.util.Properties JavaDoc;
39
40 /**
41  * Tests for regressions of bugs in String handling in the driver.
42  *
43  * @author Mark Matthews
44  * @version StringRegressionTest.java,v 1.1 2002/11/04 14:58:25 mark_matthews
45  * Exp
46  */

47 public class StringRegressionTest extends BaseTestCase {
48     /**
49      * Creates a new StringTest object.
50      *
51      * @param name
52      * DOCUMENT ME!
53      */

54     public StringRegressionTest(String JavaDoc name) {
55         super(name);
56     }
57
58     /**
59      * Runs all test cases in this test suite
60      *
61      * @param args
62      */

63     public static void main(String JavaDoc[] args) {
64         junit.textui.TestRunner.run(StringRegressionTest.class);
65     }
66
67     /**
68      * Tests character conversion bug.
69      *
70      * @throws Exception
71      * if there is an internal error (which is a bug).
72      */

73     public void testAsciiCharConversion() throws Exception JavaDoc {
74         byte[] buf = new byte[10];
75         buf[0] = (byte) '?';
76         buf[1] = (byte) 'S';
77         buf[2] = (byte) 't';
78         buf[3] = (byte) 'a';
79         buf[4] = (byte) 't';
80         buf[5] = (byte) 'e';
81         buf[6] = (byte) '-';
82         buf[7] = (byte) 'b';
83         buf[8] = (byte) 'o';
84         buf[9] = (byte) 't';
85
86         String JavaDoc testString = "?State-bot";
87         String JavaDoc convertedString = StringUtils.toAsciiString(buf);
88
89         for (int i = 0; i < convertedString.length(); i++) {
90             System.out.println((byte) convertedString.charAt(i));
91         }
92
93         assertTrue("Converted string != test string", testString
94                 .equals(convertedString));
95     }
96
97     /**
98      * Tests fix for BUG#4010 -- GBK encoding getting escaped doubly when
99      * database default character set is GBK. Requires version older than 4.1.0
100      * and server set to default character set of 'gbk' to run.
101      *
102      * @throws Exception
103      * if the test fails.
104      */

105     public void testBug4010() throws Exception JavaDoc {
106         if (!versionMeetsMinimum(4, 1)) {
107             if ("GBK".equalsIgnoreCase(getMysqlVariable("character_set"))) {
108                 String JavaDoc origString = "\u603d";
109                 Properties JavaDoc props = new Properties JavaDoc();
110                 props.put("useUnicode", "true");
111                 props.put("characterEncoding", "GBK");
112
113                 Connection JavaDoc unicodeConn = getConnectionWithProps(props);
114                 Statement JavaDoc unicodeStmt = unicodeConn.createStatement();
115                 PreparedStatement JavaDoc unicodePstmt = unicodeConn
116                         .prepareStatement("INSERT INTO testBug4010 VALUES (?)");
117
118                 try {
119                     unicodeStmt
120                             .executeUpdate("DROP TABLE IF EXISTS testBug4010");
121                     unicodeStmt
122                             .executeUpdate("CREATE TABLE testBug4010 (field1 varchar(10))");
123
124                     unicodePstmt.setString(1, origString);
125                     unicodePstmt.executeUpdate();
126
127                     this.rs = unicodeStmt
128                             .executeQuery("SELECT * FROM testBug4010");
129                     assertTrue(this.rs.next());
130
131                     String JavaDoc stringFromDb = this.rs.getString(1);
132                     assertTrue("Retrieved string != sent string", origString
133                             .equals(stringFromDb));
134                 } finally {
135                     unicodeStmt
136                             .executeUpdate("DROP TABLE IF EXISTS testBug4010");
137                     unicodeStmt.close();
138                     unicodePstmt.close();
139                     unicodeConn.close();
140                 }
141             } else {
142                 System.err
143                         .println("WARN: Test not valid for servers not running GBK encoding");
144             }
145         } else {
146             System.err
147                     .println("WARN: Test not valid for MySQL version > 4.1.0, skipping");
148         }
149     }
150
151     /**
152      * Tests for regression of encoding forced by user, reported by Jive
153      * Software
154      *
155      * @throws Exception
156      * when encoding is not supported (which is a bug)
157      */

158     public void testEncodingRegression() throws Exception JavaDoc {
159         Properties JavaDoc props = new Properties JavaDoc();
160         props.put("characterEncoding", "UTF-8");
161         props.put("useUnicode", "true");
162         DriverManager.getConnection(dbUrl, props).close();
163     }
164
165     /**
166      * Tests fix for BUG#879
167      *
168      * @throws Exception
169      * if the bug resurfaces.
170      */

171     public void testEscapeSJISDoubleEscapeBug() throws Exception JavaDoc {
172         String JavaDoc testString = "'It\\'s a boy!'";
173
174         byte[] testStringAsBytes = testString.getBytes("SJIS");
175
176         byte[] escapedStringBytes = StringUtils.escapeEasternUnicodeByteStream(
177                 testStringAsBytes, testString, 0, testString.length());
178
179         String JavaDoc escapedString = new String JavaDoc(escapedStringBytes, "SJIS");
180
181         assertTrue(testString.equals(escapedString));
182
183         byte[] origByteStream = new byte[] { (byte) 0x95, (byte) 0x5c,
184                 (byte) 0x8e, (byte) 0x96, (byte) 0x5c, (byte) 0x62, (byte) 0x5c };
185
186         String JavaDoc origString = "\u955c\u8e96\u5c62\\";
187
188         byte[] newByteStream = StringUtils.escapeEasternUnicodeByteStream(
189                 origByteStream, origString, 0, origString.length());
190
191         assertTrue((newByteStream.length == (origByteStream.length + 2))
192                 && (newByteStream[1] == 0x5c) && (newByteStream[2] == 0x5c)
193                 && (newByteStream[5] == 0x5c) && (newByteStream[6] == 0x5c));
194
195         origByteStream = new byte[] { (byte) 0x8d, (byte) 0xb2, (byte) 0x93,
196                 (byte) 0x91, (byte) 0x81, (byte) 0x40, (byte) 0x8c, (byte) 0x5c };
197
198         testString = new String JavaDoc(origByteStream, "SJIS");
199
200         Properties JavaDoc connProps = new Properties JavaDoc();
201         connProps.put("useUnicode", "true");
202         connProps.put("characterEncoding", "sjis");
203
204         Connection JavaDoc sjisConn = getConnectionWithProps(connProps);
205         Statement JavaDoc sjisStmt = sjisConn.createStatement();
206
207         try {
208             sjisStmt.executeUpdate("DROP TABLE IF EXISTS doubleEscapeSJISTest");
209             sjisStmt
210                     .executeUpdate("CREATE TABLE doubleEscapeSJISTest (field1 BLOB)");
211
212             PreparedStatement JavaDoc sjisPStmt = sjisConn
213                     .prepareStatement("INSERT INTO doubleEscapeSJISTest VALUES (?)");
214             sjisPStmt.setString(1, testString);
215             sjisPStmt.executeUpdate();
216
217             this.rs = sjisStmt
218                     .executeQuery("SELECT * FROM doubleEscapeSJISTest");
219
220             this.rs.next();
221
222             String JavaDoc retrString = this.rs.getString(1);
223
224             System.out.println(retrString.equals(testString));
225         } finally {
226             // sjisStmt.executeUpdate("DROP TABLE IF EXISTS
227
// doubleEscapeSJISTest");
228
}
229     }
230
231     /**
232      * DOCUMENT ME!
233      *
234      * @throws Exception
235      * DOCUMENT ME!
236      */

237     public void testGreekUtf8411() throws Exception JavaDoc {
238         if (versionMeetsMinimum(4, 1)) {
239             try {
240                 Properties JavaDoc newProps = new Properties JavaDoc();
241                 newProps.put("useUnicode", "true");
242                 newProps.put("characterEncoding", "UTF-8");
243
244                 Connection JavaDoc utf8Conn = this.getConnectionWithProps(newProps);
245
246                 Statement JavaDoc utfStmt = utf8Conn.createStatement();
247
248                 utfStmt.executeUpdate("DROP TABLE IF EXISTS greekunicode");
249                 utfStmt
250                         .executeUpdate("CREATE TABLE greekunicode(ID INTEGER NOT NULL "
251                                 + " AUTO_INCREMENT,UpperCase VARCHAR (30),LowerCase VARCHAR (30),Accented "
252                                 + " VARCHAR (30),Special VARCHAR (30),PRIMARY KEY(ID)) TYPE = InnoDB, DEFAULT "
253                                 + "CHARACTER SET utf8");
254
255                 String JavaDoc upper = "\u0394\u930F\u039A\u0399\u039C\u0397";
256                 String JavaDoc lower = "\u03B4\u03BF\u03BA\u03B9\u03BC\u03B7";
257                 String JavaDoc accented = "\u03B4\u03CC\u03BA\u03AF\u03BC\u03AE";
258                 String JavaDoc special = "\u037E\u03C2\u03B0";
259
260                 utfStmt.executeUpdate("INSERT INTO greekunicode VALUES "
261                         + "('1','" + upper + "','" + lower + "','" + accented
262                         + "','" + special + "')");
263
264                 this.rs = utfStmt
265                         .executeQuery("SELECT UpperCase, LowerCase, Accented, Special from greekunicode");
266
267                 this.rs.next();
268
269                 assertTrue(upper.equals(this.rs.getString(1)));
270                 assertTrue(lower.equals(this.rs.getString(2)));
271                 assertTrue(accented.equals(this.rs.getString(3)));
272                 assertTrue(special.equals(this.rs.getString(4)));
273             } finally {
274                 this.stmt.executeUpdate("DROP TABLE IF EXISTS greekunicode");
275             }
276         }
277     }
278
279     /**
280      * Tests that 'latin1' character conversion works correctly.
281      *
282      * @throws Exception
283      * if any errors occur
284      */

285     public void testLatin1Encoding() throws Exception JavaDoc {
286         char[] latin1Charset = { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,
287                 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C,
288                 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014,
289                 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C,
290                 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023, 0x0024,
291                 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C,
292                 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034,
293                 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C,
294                 0x003D, 0x003E, 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044,
295                 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C,
296                 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054,
297                 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x005B, 0x005C,
298                 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062, 0x0063, 0x0064,
299                 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C,
300                 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
301                 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C,
302                 0x007D, 0x007E, 0x007F, 0x0080, 0x0081, 0x0082, 0x0083, 0x0084,
303                 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C,
304                 0x008D, 0x008E, 0x008F, 0x0090, 0x0091, 0x0092, 0x0093, 0x0094,
305                 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C,
306                 0x009D, 0x009E, 0x009F, 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4,
307                 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC,
308                 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4,
309                 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC,
310                 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4,
311                 0x00C5, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC,
312                 0x00CD, 0x00CE, 0x00CF, 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4,
313                 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC,
314                 0x00DD, 0x00DE, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4,
315                 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC,
316                 0x00ED, 0x00EE, 0x00EF, 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4,
317                 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC,
318                 0x00FD, 0x00FE, 0x00FF };
319
320         String JavaDoc latin1String = new String JavaDoc(latin1Charset);
321         PreparedStatement JavaDoc pStmt = null;
322
323         try {
324             this.stmt.executeUpdate("DROP TABLE IF EXISTS latin1RegressTest");
325             this.stmt
326                     .executeUpdate("CREATE TABLE latin1RegressTest (stringField TEXT)");
327
328             pStmt = this.conn
329                     .prepareStatement("INSERT INTO latin1RegressTest VALUES (?)");
330             pStmt.setString(1, latin1String);
331             pStmt.executeUpdate();
332
333             ((com.mysql.jdbc.Connection) this.conn).setTraceProtocol(true);
334
335             this.rs = this.stmt.executeQuery("SELECT * FROM latin1RegressTest");
336             ((com.mysql.jdbc.Connection) this.conn).setTraceProtocol(false);
337
338             this.rs.next();
339
340             String JavaDoc retrievedString = this.rs.getString(1);
341
342             System.out.println(latin1String);
343             System.out.println(retrievedString);
344
345             if (!retrievedString.equals(latin1String)) {
346                 int stringLength = Math.min(retrievedString.length(),
347                         latin1String.length());
348
349                 for (int i = 0; i < stringLength; i++) {
350                     char rChar = retrievedString.charAt(i);
351                     char origChar = latin1String.charAt(i);
352
353                     if ((rChar != '?') && (rChar != origChar)) {
354                         fail("characters differ at position "
355                                 + i
356                                 + "'"
357                                 + rChar
358                                 + "' retrieved from database, original char was '"
359                                 + origChar + "'");
360                     }
361                 }
362             }
363         } finally {
364             if (this.rs != null) {
365                 try {
366                     this.rs.close();
367                 } catch (Exception JavaDoc ex) {
368                     // ignore
369
}
370             }
371
372             if (pStmt != null) {
373                 try {
374                     pStmt.close();
375                 } catch (Exception JavaDoc ex) {
376                     // ignore
377
}
378             }
379
380             this.stmt.executeUpdate("DROP TABLE IF EXISTS latin1RegressTest");
381         }
382     }
383
384     /**
385      * Tests newline being treated correctly.
386      *
387      * @throws Exception
388      * if an error occurs
389      */

390     public void testNewlines() throws Exception JavaDoc {
391         String JavaDoc newlineStr = "Foo\nBar\n\rBaz";
392
393         this.stmt.executeUpdate("DROP TABLE IF EXISTS newlineRegressTest");
394         this.stmt
395                 .executeUpdate("CREATE TABLE newlineRegressTest (field1 MEDIUMTEXT)");
396
397         try {
398             this.stmt.executeUpdate("INSERT INTO newlineRegressTest VALUES ('"
399                     + newlineStr + "')");
400             this.pstmt = this.conn
401                     .prepareStatement("INSERT INTO newlineRegressTest VALUES (?)");
402             this.pstmt.setString(1, newlineStr);
403             this.pstmt.executeUpdate();
404
405             this.rs = this.stmt
406                     .executeQuery("SELECT * FROM newlineRegressTest");
407
408             while (this.rs.next()) {
409                 assertTrue(this.rs.getString(1).equals(newlineStr));
410             }
411         } finally {
412             this.stmt.executeUpdate("DROP TABLE IF EXISTS newlineRegressTest");
413         }
414     }
415
416     /**
417      * Tests that single-byte character conversion works correctly.
418      *
419      * @throws Exception
420      * if any errors occur
421      */

422     // TODO: Use Unicode Literal escapes for this, for now, this test is
423
// broken :(
424
/*
425      * public void testSingleByteConversion() throws Exception {
426      * testConversionForString("latin1", "��� ����");
427      * testConversionForString("latin1", "Kaarle ��nis Ilmari");
428      * testConversionForString("latin1", "������������������"); }
429      */

430
431     /**
432      * Tests that the 0x5c escaping works (we didn't use to have this).
433      *
434      * @throws Exception
435      * if an error occurs.
436      */

437     public void testSjis5c() throws Exception JavaDoc {
438         byte[] origByteStream = new byte[] { (byte) 0x95, (byte) 0x5c,
439                 (byte) 0x8e, (byte) 0x96 };
440
441         //
442
// Print the hex values of the string
443
//
444
StringBuffer JavaDoc bytesOut = new StringBuffer JavaDoc();
445
446         for (int i = 0; i < origByteStream.length; i++) {
447             bytesOut.append(Integer.toHexString(origByteStream[i] & 255));
448             bytesOut.append(" ");
449         }
450
451         System.out.println(bytesOut.toString());
452
453         String JavaDoc origString = new String JavaDoc(origByteStream, "SJIS");
454         byte[] newByteStream = StringUtils.getBytes(origString, "SJIS",
455                 "ISO8859_1 ", false);
456
457         //
458
// Print the hex values of the string (should have an extra 0x5c)
459
//
460
bytesOut = new StringBuffer JavaDoc();
461
462         for (int i = 0; i < newByteStream.length; i++) {
463             bytesOut.append(Integer.toHexString(newByteStream[i] & 255));
464             bytesOut.append(" ");
465         }
466
467         System.out.println(bytesOut.toString());
468
469         //
470
// Now, insert and retrieve the value from the database
471
//
472
Connection JavaDoc sjisConn = null;
473         Statement JavaDoc sjisStmt = null;
474
475         try {
476             Properties JavaDoc props = new Properties JavaDoc();
477             props.put("useUnicode", "true");
478             props.put("characterEncoding", "SJIS");
479             sjisConn = getConnectionWithProps(props);
480
481             sjisStmt = sjisConn.createStatement();
482
483             this.rs = sjisStmt
484                     .executeQuery("SHOW VARIABLES LIKE 'character_set%'");
485
486             while (this.rs.next()) {
487                 System.out.println(this.rs.getString(1) + " = "
488                         + this.rs.getString(2));
489             }
490
491             sjisStmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");
492
493             if (versionMeetsMinimum(4, 1)) {
494                 sjisStmt
495                         .executeUpdate("CREATE TABLE sjisTest (field1 char(50)) DEFAULT CHARACTER SET SJIS");
496             } else {
497                 sjisStmt
498                         .executeUpdate("CREATE TABLE sjisTest (field1 char(50))");
499             }
500
501             this.pstmt = sjisConn
502                     .prepareStatement("INSERT INTO sjisTest VALUES (?)");
503             this.pstmt.setString(1, origString);
504             this.pstmt.executeUpdate();
505
506             this.rs = sjisStmt.executeQuery("SELECT * FROM sjisTest");
507
508             while (this.rs.next()) {
509                 byte[] testValueAsBytes = this.rs.getBytes(1);
510
511                 bytesOut = new StringBuffer JavaDoc();
512
513                 for (int i = 0; i < testValueAsBytes.length; i++) {
514                     bytesOut.append(Integer
515                             .toHexString(testValueAsBytes[i] & 255));
516                     bytesOut.append(" ");
517                 }
518
519                 System.out.println("Value retrieved from database: "
520                         + bytesOut.toString());
521
522                 String JavaDoc testValue = this.rs.getString(1);
523
524                 assertTrue(testValue.equals(origString));
525             }
526         } finally {
527             this.stmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");
528         }
529     }
530
531     /**
532      * Tests that UTF-8 character conversion works correctly.
533      *
534      * @throws Exception
535      * if any errors occur
536      */

537     public void testUtf8Encoding() throws Exception JavaDoc {
538         Properties JavaDoc props = new Properties JavaDoc();
539         props.put("characterEncoding", "UTF8");
540         props.put("useUnicode", "true");
541         props.put("jdbcCompliantTruncation", "false");
542
543         Connection JavaDoc utfConn = DriverManager.getConnection(dbUrl, props);
544         testConversionForString("UTF8", utfConn, "\u043c\u0438\u0445\u0438");
545     }
546
547     /**
548      * DOCUMENT ME!
549      *
550      * @throws Exception
551      * ...
552      */

553     public void testUtf8Encoding2() throws Exception JavaDoc {
554         String JavaDoc field1 = "K��sel";
555         String JavaDoc field2 = "B�b";
556         byte[] field1AsBytes = field1.getBytes("utf-8");
557         byte[] field2AsBytes = field2.getBytes("utf-8");
558
559         Properties JavaDoc props = new Properties JavaDoc();
560         props.put("characterEncoding", "UTF8");
561         props.put("useUnicode", "true");
562
563         Connection JavaDoc utfConn = DriverManager.getConnection(dbUrl, props);
564         Statement JavaDoc utfStmt = utfConn.createStatement();
565
566         try {
567             utfStmt.executeUpdate("DROP TABLE IF EXISTS testUtf8");
568             utfStmt
569                     .executeUpdate("CREATE TABLE testUtf8 (field1 varchar(32), field2 varchar(32)) CHARACTER SET UTF8");
570             utfStmt.executeUpdate("INSERT INTO testUtf8 VALUES ('" + field1
571                     + "','" + field2 + "')");
572
573             PreparedStatement JavaDoc pStmt = utfConn
574                     .prepareStatement("INSERT INTO testUtf8 VALUES (?, ?)");
575             pStmt.setString(1, field1);
576             pStmt.setString(2, field2);
577             pStmt.executeUpdate();
578
579             ResultSet JavaDoc rs = utfStmt.executeQuery("SELECT * FROM testUtf8");
580             assertTrue(rs.next());
581
582             // Compare results stored using direct statement
583
// Compare to original string
584
assertTrue(field1.equals(rs.getString(1)));
585             assertTrue(field2.equals(rs.getString(2)));
586
587             // Compare byte-for-byte, ignoring encoding
588
assertTrue(bytesAreSame(field1AsBytes, rs.getBytes(1)));
589             assertTrue(bytesAreSame(field2AsBytes, rs.getBytes(2)));
590
591             assertTrue(rs.next());
592
593             // Compare to original string
594
assertTrue(field1.equals(rs.getString(1)));
595             assertTrue(field2.equals(rs.getString(2)));
596
597             // Compare byte-for-byte, ignoring encoding
598
assertTrue(bytesAreSame(field1AsBytes, rs.getBytes(1)));
599             assertTrue(bytesAreSame(field2AsBytes, rs.getBytes(2)));
600         } finally {
601             utfStmt.executeUpdate("DROP TABLE IF EXISTS testUtf8");
602         }
603     }
604
605     private boolean bytesAreSame(byte[] byte1, byte[] byte2) {
606         if (byte1.length != byte2.length) {
607             return false;
608         }
609
610         for (int i = 0; i < byte1.length; i++) {
611             if (byte1[i] != byte2[i]) {
612                 return false;
613             }
614         }
615
616         return true;
617     }
618
619     private void testConversionForString(String JavaDoc charsetName,
620             Connection JavaDoc convConn, String JavaDoc charsToTest) throws Exception JavaDoc {
621         PreparedStatement JavaDoc pStmt = null;
622
623         try {
624             this.stmt = convConn.createStatement();
625             this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest");
626             this.stmt
627                     .executeUpdate("CREATE TABLE charConvTest (field1 varchar(255))");
628             this.stmt.executeUpdate("INSERT INTO charConvTest VALUES ('"
629                     + charsToTest + "')");
630             this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest_"
631                     + charsetName);
632
633             if (!versionMeetsMinimum(4, 1)) {
634                 this.stmt.executeUpdate("CREATE TABLE charConvTest_"
635                         + charsetName + "(field1 CHAR(50))");
636             } else {
637                 this.stmt.executeUpdate("CREATE TABLE charConvTest_"
638                         + charsetName + "(field1 CHAR(50) CHARACTER SET "
639                         + charsetName + ")");
640             }
641
642             this.stmt.executeUpdate("INSERT INTO charConvTest_" + charsetName
643                     + " VALUES ('" + charsToTest + "')");
644             pStmt = convConn.prepareStatement("INSERT INTO charConvTest_"
645                     + charsetName + " VALUES (?)");
646             pStmt.setString(1, charsToTest);
647             pStmt.executeUpdate();
648             this.rs = this.stmt.executeQuery("SELECT * FROM charConvTest_"
649                     + charsetName);
650
651             boolean hadRows = false;
652
653             assertTrue(this.rs.next());
654
655             String JavaDoc testValue = this.rs.getString(1);
656             System.out.println(testValue);
657             assertTrue(testValue.equals(charsToTest));
658         } finally {
659             this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest_"
660                     + charsetName);
661         }
662     }
663
664     private void testConversionForString(String JavaDoc charsetName, String JavaDoc charsToTest)
665             throws Exception JavaDoc {
666         testConversionForString(charsetName, this.conn, charsToTest);
667     }
668
669     /**
670      * Tests fix for BUG#7601, '+' duplicated in fixDecimalExponent().
671      *
672      * @throws Exception
673      * if the test fails
674      */

675     public void testBug7601() throws Exception JavaDoc {
676         assertTrue("1.5E+7".equals(StringUtils.fixDecimalExponent("1.5E+7")));
677         assertTrue("1.5E-7".equals(StringUtils.fixDecimalExponent("1.5E-7")));
678         assertTrue("1.5E+7".equals(StringUtils.fixDecimalExponent("1.5E7")));
679     }
680 }
681
Popular Tags