KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > CharsetTests


1 /*
2     Copyright (C) 2005 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 package testsuite.simple;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import testsuite.BaseTestCase;
36
37 public class CharsetTests extends BaseTestCase {
38
39     public CharsetTests(String JavaDoc name) {
40         super(name);
41         // TODO Auto-generated constructor stub
42
}
43
44     public static void main(String JavaDoc[] args) {
45         junit.textui.TestRunner.run(CharsetTests.class);
46     }
47
48     public void testCP932Backport() throws Exception JavaDoc {
49         if (versionMeetsMinimum(4, 1, 12)) {
50             if (versionMeetsMinimum(5, 0)) {
51                 if (!versionMeetsMinimum(5, 0, 3)) {
52                     return;
53                 }
54             }
55
56             Properties JavaDoc props = new Properties JavaDoc();
57             props.put("useUnicode", "true");
58             props.put("characterEncoding", "WINDOWS-31J");
59             getConnectionWithProps(props).close();
60         }
61     }
62
63     public void testNECExtendedCharsByEUCJPSolaris() throws Exception JavaDoc {
64         if (versionMeetsMinimum(5, 0, 5)) {
65             char necExtendedChar = 0x3231; // 0x878A of WINDOWS-31J, NEC
66
// special(row13).
67
String JavaDoc necExtendedCharString = String.valueOf(necExtendedChar);
68
69             Properties JavaDoc props = new Properties JavaDoc();
70             props.put("user", "");
71             props.put("password", "");
72             props.put("useUnicode", "true");
73             props.put("characterEncoding", "EUC_JP_Solaris");
74
75             Connection JavaDoc conn2 = getConnectionWithProps(props);
76             Statement JavaDoc stmt2 = conn2.createStatement();
77
78             stmt2.executeUpdate("DROP TABLE IF EXISTS t_eucjpms");
79             createTable("t_eucjpms", "(c1 char(1))"
80                     + " default character set = eucjpms");
81             stmt2.executeUpdate("INSERT INTO t_eucjpms VALUES ('"
82                     + necExtendedCharString + "')");
83             this.rs = stmt2.executeQuery("SELECT c1 FROM t_eucjpms");
84             this.rs.next();
85             assertEquals(necExtendedCharString, this.rs.getString("c1"));
86
87             this.rs.close();
88             stmt2.close();
89             conn2.close();
90
91             props.put("characterSetResults", "EUC_JP_Solaris");
92             conn2 = getConnectionWithProps(props);
93             stmt2 = conn.createStatement();
94
95             this.rs = stmt2.executeQuery("SELECT c1 FROM t_eucjpms");
96             this.rs.next();
97             assertEquals(necExtendedCharString, rs.getString("c1"));
98
99             stmt2.executeUpdate("DROP TABLE t_eucjpms");
100             this.rs.close();
101             stmt2.close();
102             conn2.close();
103         }
104     }
105
106     /**
107      * Test data of sjis. sjis consists of ASCII, JIS-Roman, JISX0201 and
108      * JISX0208.
109      */

110     public static final char[] SJIS_CHARS = new char[] { 0xFF71, // halfwidth
111
// katakana
112
// letter A,
113
// 0xB100 of
114
// SJIS, one
115
// of
116
// JISX0201.
117
0x65E5, // CJK unified ideograph, 0x93FA of SJIS, one of JISX0208.
118
0x8868, // CJK unified ideograph, 0x955C of SJIS, one of '5c'
119
// character.
120
0x2016 // 0x8161 of SJIS/WINDOWS-31J, converted to differently
121
// to/from ucs2
122
};
123
124     /**
125      * Test data of cp932. WINDOWS-31J consists of ASCII, JIS-Roman, JISX0201,
126      * JISX0208, NEC special characters(row13), NEC selected IBM special
127      * characters, and IBM special characters.
128      */

129     private static final char[] CP932_CHARS = new char[] { 0xFF71, // halfwidth
130
// katakana
131
// letter A,
132
// 0xB100 of
133
// WINDOWS-31J,
134
// one of
135
// JISX0201.
136
0x65E5, // CJK unified ideograph, 0x93FA of WINDOWS-31J, one of
137
// JISX0208.
138
0x3231, // parenthesized ideograph stok, 0x878B of WINDOWS-31J, one
139
// of NEC special characters(row13).
140
0x67BB, // CJK unified ideograph, 0xEDC6 of WINDOWS-31J, one of NEC
141
// selected IBM special characters.
142
0x6D6F, // CJK unified ideograph, 0xFAFC of WINDOWS-31J, one of IBM
143
// special characters.
144
0x8868, // one of CJK unified ideograph, 0x955C of WINDOWS-31J, one
145
// of '5c' characters.
146
0x2225 // 0x8161 of SJIS/WINDOWS-31J, converted to differently
147
// to/from ucs2
148
};
149
150     /**
151      * Test data of ujis. ujis consists of ASCII, JIS-Roman, JISX0201, JISX0208,
152      * JISX0212.
153      */

154     public static final char[] UJIS_CHARS = new char[] { 0xFF71, // halfwidth
155
// katakana
156
// letter A,
157
// 0x8EB1 of
158
// ujis, one
159
// of
160
// JISX0201.
161
0x65E5, // CJK unified ideograph, 0xC6FC of ujis, one of JISX0208.
162
0x7B5D, // CJK unified ideograph, 0xE4B882 of ujis, one of JISX0212
163
0x301C // wave dash, 0xA1C1 of ujis, convertion rule is different
164
// from ujis
165
};
166
167     /**
168      * Test data of eucjpms. ujis consists of ASCII, JIS-Roman, JISX0201,
169      * JISX0208, JISX0212, NEC special characters(row13)
170      */

171     public static final char[] EUCJPMS_CHARS = new char[] { 0xFF71, // halfwidth
172
// katakana
173
// letter A,
174
// 0x8EB1 of
175
// ujis, one
176
// of
177
// JISX0201.
178
0x65E5, // CJK unified ideograph, 0xC6FC of ujis, one of JISX0208.
179
0x7B5D, // CJK unified ideograph, 0xE4B882 of ujis, one of JISX0212
180
0x3231, // parenthesized ideograph stok, 0x878A of WINDOWS-31J, one
181
// of NEC special characters(row13).
182
0xFF5E // wave dash, 0xA1C1 of eucjpms, convertion rule is
183
// different from ujis
184
};
185
186     public void testInsertCharStatement() throws Exception JavaDoc {
187         if (versionMeetsMinimum(4, 1, 12)) {
188             Map JavaDoc testDataMap = new HashMap JavaDoc();
189
190             List JavaDoc charsetList = new ArrayList JavaDoc();
191
192             Map JavaDoc connectionMap = new HashMap JavaDoc();
193
194             Map JavaDoc connectionWithResultMap = new HashMap JavaDoc();
195
196             Map JavaDoc statementMap = new HashMap JavaDoc();
197
198             Map JavaDoc statementWithResultMap = new HashMap JavaDoc();
199
200             Map JavaDoc javaToMysqlCharsetMap = new HashMap JavaDoc();
201
202             charsetList.add("SJIS");
203             testDataMap.put("SJIS", SJIS_CHARS);
204             javaToMysqlCharsetMap.put("SJIS", "sjis");
205
206             charsetList.add("Shift_JIS");
207             testDataMap.put("Shift_JIS", SJIS_CHARS);
208             javaToMysqlCharsetMap.put("Shift_JIS", "sjis");
209
210             charsetList.add("CP943");
211             testDataMap.put("CP943", SJIS_CHARS);
212             javaToMysqlCharsetMap.put("CP943", "sjis");
213
214             if (versionMeetsMinimum(5, 0, 3)) {
215                 charsetList.add("WINDOWS-31J");
216                 testDataMap.put("WINDOWS-31J", CP932_CHARS);
217                 javaToMysqlCharsetMap.put("WINDOWS-31J", "cp932");
218
219                 charsetList.add("MS932");
220                 testDataMap.put("MS932", CP932_CHARS);
221                 javaToMysqlCharsetMap.put("MS932", "cp932");
222
223                 charsetList.add("EUC_JP");
224                 testDataMap.put("EUC_JP", UJIS_CHARS);
225                 // testDataHexMap.put("EUC_JP", UJIS_CHARS_HEX);
226
javaToMysqlCharsetMap.put("EUC_JP", "ujis");
227
228                 charsetList.add("EUC_JP_Solaris");
229                 testDataMap.put("EUC_JP_Solaris", EUCJPMS_CHARS);
230                 // testDataHexMap.put("EUC_JP_Solaris", EUCJPMS_CHARS_HEX);
231
javaToMysqlCharsetMap.put("EUC_JP_Solaris", "eucjpms");
232
233             } else {
234                 charsetList.add("EUC_JP");
235                 testDataMap.put("EUC_JP", UJIS_CHARS);
236                 javaToMysqlCharsetMap.put("EUC_JP", "ujis");
237             }
238
239             Iterator JavaDoc charsetIterator = charsetList.iterator();
240
241             while (charsetIterator.hasNext()) {
242                 String JavaDoc charset = (String JavaDoc) charsetIterator.next();
243                 Properties JavaDoc props = new Properties JavaDoc();
244                 props.put("user", "");
245                 props.put("password", "");
246                 props.put("useUnicode", "true");
247                 props.put("characterEncoding", charset);
248                 Connection JavaDoc conn2 = getConnectionWithProps(props);
249                 connectionMap.put(charset.toLowerCase(Locale.ENGLISH), conn2);
250                 statementMap.put(charset.toLowerCase(Locale.ENGLISH), conn2
251                         .createStatement());
252
253                 props.put("characterSetResult", charset);
254                 Connection JavaDoc connWithResult = getConnectionWithProps(props);
255                 connectionWithResultMap.put(charset, connWithResult);
256                 statementWithResultMap.put(charset, connWithResult
257                         .createStatement());
258             }
259
260             charsetIterator = charsetList.iterator();
261             while (charsetIterator.hasNext()) {
262                 String JavaDoc charset = (String JavaDoc) charsetIterator.next();
263
264                 String JavaDoc mysqlCharset = (String JavaDoc) javaToMysqlCharsetMap
265                         .get(charset);
266                 Statement JavaDoc stmt2 = (Statement JavaDoc) statementMap.get(charset
267                         .toLowerCase(Locale.ENGLISH));
268                 String JavaDoc query1 = "DROP TABLE IF EXISTS t1";
269                 String JavaDoc query2 = "CREATE TABLE t1 (c1 int, c2 char(1)) "
270                         + "DEFAULT CHARACTER SET = " + mysqlCharset;
271                 stmt2.executeUpdate(query1);
272                 stmt2.executeUpdate(query2);
273                 char[] testData = (char[]) testDataMap.get(charset);
274                 for (int i = 0; i < testData.length; i++) {
275                     String JavaDoc query3 = "INSERT INTO t1 values(" + i + ", '"
276                             + testData[i] + "')";
277                     stmt2.executeUpdate(query3);
278                     String JavaDoc query4 = "SELECT c2 FROM t1 WHERE c1 = " + i;
279                     this.rs = stmt2.executeQuery(query4);
280                     this.rs.next();
281                     String JavaDoc value = rs.getString(1);
282
283                     assertEquals("For character set " + charset + "/ "
284                             + mysqlCharset, String.valueOf(testData[i]), value);
285                 }
286                 String JavaDoc query5 = "DROP TABLE t1";
287                 stmt2.executeUpdate(query5);
288             }
289         }
290     }
291
292 }
293
Popular Tags