|
| 1 | +packageexample; |
| 2 | + |
| 3 | +importjava.io.*; |
| 4 | +importjava.sql.*; |
| 5 | +importjava.util.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test inserting and extracting Unicode-encoded strings. |
| 9 | + * |
| 10 | + * Synopsis: |
| 11 | + * example.Unicode <url> <user> <password> |
| 12 | + * where <url> must specify an existing database to which <user> and |
| 13 | + * <password> give access and which has UNICODE as its encoding. |
| 14 | + * (To create a database with UNICODE encoding, you need to compile |
| 15 | + * postgres with "--enable-multibyte" and run createdb with the |
| 16 | + * flag "-E UNICODE".) |
| 17 | + * |
| 18 | + * This test only produces output on error. |
| 19 | + * |
| 20 | + * @author William Webber <william@live.com.au> |
| 21 | + */ |
| 22 | +publicclassUnicode { |
| 23 | + |
| 24 | +/** |
| 25 | + * The url for the database to connect to. |
| 26 | + */ |
| 27 | +privateStringurl; |
| 28 | + |
| 29 | +/** |
| 30 | + * The user to connect as. |
| 31 | + */ |
| 32 | +privateStringuser; |
| 33 | + |
| 34 | +/** |
| 35 | + * The password to connect with. |
| 36 | + */ |
| 37 | +privateStringpassword; |
| 38 | + |
| 39 | +privatestaticvoidusage() { |
| 40 | +log("usage: example.Unicode <url> <user> <password>"); |
| 41 | + } |
| 42 | + |
| 43 | +privatestaticvoidlog(Stringmessage) { |
| 44 | +System.err.println(message); |
| 45 | + } |
| 46 | + |
| 47 | +privatestaticvoidlog(Stringmessage,Exceptione) { |
| 48 | +System.err.println(message); |
| 49 | +e.printStackTrace(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +publicUnicode(Stringurl,Stringuser,Stringpassword) { |
| 54 | +this.url =url; |
| 55 | +this.user =user; |
| 56 | +this.password =password; |
| 57 | + } |
| 58 | + |
| 59 | +/** |
| 60 | + * Establish and return a connection to the database. |
| 61 | + */ |
| 62 | +privateConnectiongetConnection()throwsSQLException, |
| 63 | +ClassNotFoundException { |
| 64 | +Class.forName("org.postgresql.Driver"); |
| 65 | +Propertiesinfo =newProperties(); |
| 66 | +info.put("user",user); |
| 67 | +info.put("password",password); |
| 68 | +info.put("charSet","utf-8"); |
| 69 | +returnDriverManager.getConnection(url,info); |
| 70 | + } |
| 71 | + |
| 72 | +/** |
| 73 | + * Get string representing a block of 256 consecutive unicode characters. |
| 74 | + * We exclude the null character, "'", and "\". |
| 75 | + */ |
| 76 | +privateStringgetSqlSafeUnicodeBlock(intblockNum) { |
| 77 | +if (blockNum <0 ||blockNum >255) |
| 78 | +thrownewIllegalArgumentException("blockNum must be from 0 to " |
| 79 | + +"255: " +blockNum); |
| 80 | +StringBuffersb =newStringBuffer(256); |
| 81 | +intblockFirst =blockNum *256; |
| 82 | +intblockLast =blockFirst +256; |
| 83 | +for (inti =blockFirst;i <blockLast;i++) { |
| 84 | +charc = (char)i; |
| 85 | +if (c =='\0' ||c =='\'' ||c =='\\') |
| 86 | +continue; |
| 87 | +sb.append(c); |
| 88 | + } |
| 89 | +returnsb.toString(); |
| 90 | + } |
| 91 | + |
| 92 | +/** |
| 93 | + * Is the block a block of valid unicode values. |
| 94 | + * d800 to db7f is the "unassigned high surrogate" range. |
| 95 | + * db80 to dbff is the "private use" range. |
| 96 | + * These should not be used in actual Unicode strings; |
| 97 | + * at least, jdk1.2 will not convert them to utf-8. |
| 98 | + */ |
| 99 | +privatebooleanisValidUnicodeBlock(intblockNum) { |
| 100 | +if (blockNum >=0xd8 &&blockNum <=0xdb) |
| 101 | +returnfalse; |
| 102 | +else |
| 103 | +returntrue; |
| 104 | + } |
| 105 | + |
| 106 | +/** |
| 107 | + * Report incorrect block retrieval. |
| 108 | + */ |
| 109 | +privatevoidreportRetrievalError(intblockNum,Stringblock, |
| 110 | +Stringretrieved) { |
| 111 | +Stringmessage ="Block " +blockNum +" returned incorrectly: "; |
| 112 | +inti =0; |
| 113 | +for (i =0;i <block.length();i++) { |
| 114 | +if (i >=retrieved.length()) { |
| 115 | +message +="too short"; |
| 116 | +break; |
| 117 | + }elseif (retrieved.charAt(i) !=block.charAt(i)) { |
| 118 | +message += |
| 119 | +"first changed character at position " +i +", sent as 0x" |
| 120 | + +Integer.toHexString((int)block.charAt(i)) |
| 121 | + +", retrieved as 0x" |
| 122 | + +Integer.toHexString ((int)retrieved.charAt(i)); |
| 123 | +break; |
| 124 | + } |
| 125 | + } |
| 126 | +if (i >=block.length()) |
| 127 | +message +="too long"; |
| 128 | +log(message); |
| 129 | + } |
| 130 | + |
| 131 | +/** |
| 132 | + * Do the testing. |
| 133 | + */ |
| 134 | +publicvoidrunTest() { |
| 135 | +Connectionconnection =null; |
| 136 | +Statementstatement =null; |
| 137 | +intblockNum =0; |
| 138 | +finalintCREATE =0; |
| 139 | +finalintINSERT =1; |
| 140 | +finalintSELECT =2; |
| 141 | +finalintLIKE =3; |
| 142 | +intmode =CREATE; |
| 143 | +try { |
| 144 | +connection =getConnection(); |
| 145 | +statement =connection.createStatement(); |
| 146 | +statement.executeUpdate("CREATE TABLE test_unicode " |
| 147 | + +"( blockNum INT PRIMARY KEY, " |
| 148 | + +"block TEXT );"); |
| 149 | +mode =INSERT; |
| 150 | +for (blockNum =0;blockNum <256;blockNum++) { |
| 151 | +if (isValidUnicodeBlock(blockNum)) { |
| 152 | +Stringblock =getSqlSafeUnicodeBlock(blockNum); |
| 153 | +statement.executeUpdate |
| 154 | + ("INSERT INTO test_unicode VALUES ( " +blockNum |
| 155 | + +", '" +block +"');"); |
| 156 | + } |
| 157 | + } |
| 158 | +mode =SELECT; |
| 159 | +for (blockNum =0;blockNum <256;blockNum++) { |
| 160 | +if (isValidUnicodeBlock(blockNum)) { |
| 161 | +Stringblock =getSqlSafeUnicodeBlock(blockNum); |
| 162 | +ResultSetrs =statement.executeQuery |
| 163 | + ("SELECT block FROM test_unicode WHERE blockNum = " |
| 164 | + +blockNum +";"); |
| 165 | +if (!rs.next()) |
| 166 | +log("Could not retrieve block " +blockNum); |
| 167 | +else { |
| 168 | +Stringretrieved =rs.getString(1); |
| 169 | +if (!retrieved.equals(block)) { |
| 170 | +reportRetrievalError(blockNum,block,retrieved); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +mode =LIKE; |
| 176 | +for (blockNum =0;blockNum <256;blockNum++) { |
| 177 | +if (isValidUnicodeBlock(blockNum)) { |
| 178 | +Stringblock =getSqlSafeUnicodeBlock(blockNum); |
| 179 | +StringlikeString ="%" + |
| 180 | +block.substring(2,block.length() -3) +"%" ; |
| 181 | +ResultSetrs =statement.executeQuery |
| 182 | + ("SELECT blockNum FROM test_unicode WHERE block LIKE '" |
| 183 | + +likeString +"';"); |
| 184 | +if (!rs.next()) |
| 185 | +log("Could get block " +blockNum +" using LIKE"); |
| 186 | + } |
| 187 | + } |
| 188 | + }catch (SQLExceptionsqle) { |
| 189 | +switch (mode) { |
| 190 | +caseCREATE: |
| 191 | +log("Exception creating database",sqle); |
| 192 | +break; |
| 193 | +caseINSERT: |
| 194 | +log("Exception inserting block " +blockNum,sqle); |
| 195 | +break; |
| 196 | +caseSELECT: |
| 197 | +log("Exception selecting block " +blockNum,sqle); |
| 198 | +break; |
| 199 | +caseLIKE: |
| 200 | +log("Exception doing LIKE on block " +blockNum,sqle); |
| 201 | +break; |
| 202 | +default: |
| 203 | +log("Exception",sqle); |
| 204 | +break; |
| 205 | + } |
| 206 | + }catch (ClassNotFoundExceptioncnfe) { |
| 207 | +log("Unable to load driver",cnfe); |
| 208 | +return; |
| 209 | + } |
| 210 | +try { |
| 211 | +if (statement !=null) |
| 212 | +statement.close(); |
| 213 | +if (connection !=null) |
| 214 | +connection.close(); |
| 215 | + }catch (SQLExceptionsqle) { |
| 216 | +log("Exception closing connections",sqle); |
| 217 | + } |
| 218 | +if (mode >CREATE) { |
| 219 | +// If the backend gets what it regards as garbage on a connection, |
| 220 | +// that connection may become unusable. To be safe, we create |
| 221 | +// a fresh connection to delete the table. |
| 222 | +try { |
| 223 | +connection =getConnection(); |
| 224 | +statement =connection.createStatement(); |
| 225 | +statement.executeUpdate("DROP TABLE test_unicode;"); |
| 226 | + }catch (Exceptionsqle) { |
| 227 | +log("*** ERROR: unable to delete test table " |
| 228 | + +"test_unicode; must be deleted manually",sqle); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | +publicstaticvoidmain(String []args) { |
| 234 | +if (args.length !=3) { |
| 235 | +usage(); |
| 236 | +System.exit(1); |
| 237 | + } |
| 238 | +newUnicode(args[0],args[1],args[2]).runTest(); |
| 239 | + } |
| 240 | +} |