|
| 1 | +package example; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.sql.*; |
| 5 | +import java.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 | +public class Unicode { |
| 23 | + |
| 24 | + /** |
| 25 | + * The url for the database to connect to. |
| 26 | + */ |
| 27 | + private String url; |
| 28 | + |
| 29 | + /** |
| 30 | + * The user to connect as. |
| 31 | + */ |
| 32 | + private String user; |
| 33 | + |
| 34 | + /** |
| 35 | + * The password to connect with. |
| 36 | + */ |
| 37 | + private String password; |
| 38 | + |
| 39 | + private static void usage() { |
| 40 | + log("usage: example.Unicode <url> <user> <password>"); |
| 41 | + } |
| 42 | + |
| 43 | + private static void log(String message) { |
| 44 | + System.err.println(message); |
| 45 | + } |
| 46 | + |
| 47 | + private static void log(String message, Exception e) { |
| 48 | + System.err.println(message); |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public Unicode(String url, String user, String password) { |
| 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 | + private Connection getConnection() throws SQLException, |
| 63 | + ClassNotFoundException { |
| 64 | + Class.forName("org.postgresql.Driver"); |
| 65 | + Properties info = new Properties(); |
| 66 | + info.put("user", user); |
| 67 | + info.put("password", password); |
| 68 | + info.put("charSet", "utf-8"); |
| 69 | + return DriverManager.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 | + private String getSqlSafeUnicodeBlock(int blockNum) { |
| 77 | + if (blockNum < 0 || blockNum > 255) |
| 78 | + throw new IllegalArgumentException("blockNum must be from 0 to " |
| 79 | + + "255: " + blockNum); |
| 80 | + StringBuffer sb = new StringBuffer(256); |
| 81 | + int blockFirst = blockNum * 256; |
| 82 | + int blockLast = blockFirst + 256; |
| 83 | + for (int i = blockFirst; i < blockLast; i++) { |
| 84 | + char c = (char) i; |
| 85 | + if (c == '\0' || c == '\'' || c == '\\') |
| 86 | + continue; |
| 87 | + sb.append(c); |
| 88 | + } |
| 89 | + return sb.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 | + private boolean isValidUnicodeBlock(int blockNum) { |
| 100 | + if (blockNum >= 0xd8 && blockNum <= 0xdb) |
| 101 | + return false; |
| 102 | + else |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Report incorrect block retrieval. |
| 108 | + */ |
| 109 | + private void reportRetrievalError(int blockNum, String block, |
| 110 | + String retrieved) { |
| 111 | + String message = "Block " + blockNum + " returned incorrectly: "; |
| 112 | + int i = 0; |
| 113 | + for (i = 0; i < block.length(); i++) { |
| 114 | + if (i >= retrieved.length()) { |
| 115 | + message += "too short"; |
| 116 | + break; |
| 117 | + } else if (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 | + public void runTest() { |
| 135 | + Connection connection = null; |
| 136 | + Statement statement = null; |
| 137 | + int blockNum = 0; |
| 138 | + final int CREATE = 0; |
| 139 | + final int INSERT = 1; |
| 140 | + final int SELECT = 2; |
| 141 | + final int LIKE = 3; |
| 142 | + int mode = 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 | + String block = 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 | + String block = getSqlSafeUnicodeBlock(blockNum); |
| 162 | + ResultSet rs = 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 | + String retrieved = 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 | + String block = getSqlSafeUnicodeBlock(blockNum); |
| 179 | + String likeString = "%" + |
| 180 | + block.substring(2, block.length() - 3) + "%" ; |
| 181 | + ResultSet rs = 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 (SQLException sqle) { |
| 189 | + switch (mode) { |
| 190 | + case CREATE: |
| 191 | + log("Exception creating database", sqle); |
| 192 | + break; |
| 193 | + case INSERT: |
| 194 | + log("Exception inserting block " + blockNum, sqle); |
| 195 | + break; |
| 196 | + case SELECT: |
| 197 | + log("Exception selecting block " + blockNum, sqle); |
| 198 | + break; |
| 199 | + case LIKE: |
| 200 | + log("Exception doing LIKE on block " + blockNum, sqle); |
| 201 | + break; |
| 202 | + default: |
| 203 | + log("Exception", sqle); |
| 204 | + break; |
| 205 | + } |
| 206 | + } catch (ClassNotFoundException cnfe) { |
| 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 (SQLException sqle) { |
| 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 (Exception sqle) { |
| 227 | + log("*** ERROR: unable to delete test table " |
| 228 | + + "test_unicode; must be deleted manually", sqle); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + public static void main(String [] args) { |
| 234 | + if (args.length != 3) { |
| 235 | + usage(); |
| 236 | + System.exit(1); |
| 237 | + } |
| 238 | + new Unicode(args[0], args[1], args[2]).runTest(); |
| 239 | + } |
| 240 | +} |
0 commit comments