1 22 23 package testsuite.simple; 24 25 import java.sql.Connection ; 26 import java.sql.Statement ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import testsuite.BaseTestCase; 36 37 public class CharsetTests extends BaseTestCase { 38 39 public CharsetTests(String name) { 40 super(name); 41 } 43 44 public static void main(String [] args) { 45 junit.textui.TestRunner.run(CharsetTests.class); 46 } 47 48 public void testCP932Backport() throws Exception { 49 if (versionMeetsMinimum(4, 1, 12)) { 50 if (versionMeetsMinimum(5, 0)) { 51 if (!versionMeetsMinimum(5, 0, 3)) { 52 return; 53 } 54 } 55 56 Properties props = new Properties (); 57 props.put("useUnicode", "true"); 58 props.put("characterEncoding", "WINDOWS-31J"); 59 getConnectionWithProps(props).close(); 60 } 61 } 62 63 public void testNECExtendedCharsByEUCJPSolaris() throws Exception { 64 if (versionMeetsMinimum(5, 0, 5)) { 65 char necExtendedChar = 0x3231; String necExtendedCharString = String.valueOf(necExtendedChar); 68 69 Properties props = new Properties (); 70 props.put("user", ""); 71 props.put("password", ""); 72 props.put("useUnicode", "true"); 73 props.put("characterEncoding", "EUC_JP_Solaris"); 74 75 Connection conn2 = getConnectionWithProps(props); 76 Statement 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 110 public static final char[] SJIS_CHARS = new char[] { 0xFF71, 0x65E5, 0x8868, 0x2016 }; 123 124 129 private static final char[] CP932_CHARS = new char[] { 0xFF71, 0x65E5, 0x3231, 0x67BB, 0x6D6F, 0x8868, 0x2225 }; 149 150 154 public static final char[] UJIS_CHARS = new char[] { 0xFF71, 0x65E5, 0x7B5D, 0x301C }; 166 167 171 public static final char[] EUCJPMS_CHARS = new char[] { 0xFF71, 0x65E5, 0x7B5D, 0x3231, 0xFF5E }; 185 186 public void testInsertCharStatement() throws Exception { 187 if (versionMeetsMinimum(4, 1, 12)) { 188 Map testDataMap = new HashMap (); 189 190 List charsetList = new ArrayList (); 191 192 Map connectionMap = new HashMap (); 193 194 Map connectionWithResultMap = new HashMap (); 195 196 Map statementMap = new HashMap (); 197 198 Map statementWithResultMap = new HashMap (); 199 200 Map javaToMysqlCharsetMap = new HashMap (); 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 javaToMysqlCharsetMap.put("EUC_JP", "ujis"); 227 228 charsetList.add("EUC_JP_Solaris"); 229 testDataMap.put("EUC_JP_Solaris", EUCJPMS_CHARS); 230 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 charsetIterator = charsetList.iterator(); 240 241 while (charsetIterator.hasNext()) { 242 String charset = (String ) charsetIterator.next(); 243 Properties props = new Properties (); 244 props.put("user", ""); 245 props.put("password", ""); 246 props.put("useUnicode", "true"); 247 props.put("characterEncoding", charset); 248 Connection 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 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 charset = (String ) charsetIterator.next(); 263 264 String mysqlCharset = (String ) javaToMysqlCharsetMap 265 .get(charset); 266 Statement stmt2 = (Statement ) statementMap.get(charset 267 .toLowerCase(Locale.ENGLISH)); 268 String query1 = "DROP TABLE IF EXISTS t1"; 269 String 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 query3 = "INSERT INTO t1 values(" + i + ", '" 276 + testData[i] + "')"; 277 stmt2.executeUpdate(query3); 278 String query4 = "SELECT c2 FROM t1 WHERE c1 = " + i; 279 this.rs = stmt2.executeQuery(query4); 280 this.rs.next(); 281 String value = rs.getString(1); 282 283 assertEquals("For character set " + charset + "/ " 284 + mysqlCharset, String.valueOf(testData[i]), value); 285 } 286 String query5 = "DROP TABLE t1"; 287 stmt2.executeUpdate(query5); 288 } 289 } 290 } 291 292 } 293 | Popular Tags |