1 30 31 32 package org.hsqldb.test; 33 34 import junit.framework.TestCase; 35 import junit.framework.TestResult; 36 37 41 public class TestCollation extends TestBase { 42 43 java.sql.Statement statement; 44 java.sql.Connection connection; 45 org.hsqldb.Collation collation; 46 org.hsqldb.lib.Iterator collIterator; 47 org.hsqldb.lib.Iterator localeIterator; 48 49 50 public TestCollation(String name) { 51 52 super(name); 53 54 super.isNetwork = false; 55 } 56 57 protected void setUp() { 58 59 super.setUp(); 60 61 try { 62 connection = super.newConnection(); 63 statement = connection.createStatement(); 64 } catch (Exception e) {} 65 66 collation = new org.hsqldb.Collation(); 67 collIterator = collation.getCollationsIterator(); 68 localeIterator = collation.getLocalesIterator(); 69 } 70 71 protected void tearDown() { 72 73 try { 74 statement = connection.createStatement(); 75 76 statement.execute("SHUTDOWN"); 77 } catch (Exception e) {} 78 79 super.tearDown(); 80 } 81 82 85 public void verifyAvailability() { 86 87 try { 89 statement.execute( 90 getSetCollationStmt( 91 "ThisIsDefinatelyNoValidCollationIdentifier")); 92 fail("database did not reject invalid collation name"); 93 } catch (java.sql.SQLException e) {} 94 95 int count = 0; 97 98 while (collIterator.hasNext()) { 99 String collationName = (String ) collIterator.next(); 100 101 try { 102 statement.execute(getSetCollationStmt(collationName)); 103 } catch (java.sql.SQLException e) { 104 fail("could not set collation '" + collationName 105 + "'\n exception message: " + e.getMessage()); 106 } 107 108 ++count; 109 } 110 111 System.out.println("checked " + count 112 + " collations for availability."); 113 114 java.util.Locale [] availableLocales = 124 java.util.Locale.getAvailableLocales(); 125 org.hsqldb.lib.Set existenceCheck = new org.hsqldb.lib.HashSet(); 126 127 for (int i = 0; i < availableLocales.length; ++i) { 128 String availaleName = availableLocales[i].getLanguage(); 129 130 if (availableLocales[i].getCountry().length() > 0) { 131 availaleName += "-" + availableLocales[i].getCountry(); 132 } 133 134 existenceCheck.add(availaleName); 135 } 136 137 String notInstalled = ""; 138 int expected = 0, 139 failed = 0; 140 141 while (localeIterator.hasNext()) { 142 String localeName = (String ) localeIterator.next(); 143 144 ++expected; 145 146 if (!existenceCheck.contains(localeName)) { 147 if (notInstalled.length() > 0) { 148 notInstalled += "; "; 149 } 150 151 notInstalled += localeName; 152 153 ++failed; 154 } 155 } 156 157 if (notInstalled.length() > 0) { 158 fail("the following locales are not installed:\n " 159 + notInstalled + "\n (" + failed + " out of " + expected 160 + ")"); 161 } 162 } 163 164 167 public void verifyCollation() { 168 169 String failedCollations = ""; 170 String failMessage = ""; 171 172 while (collIterator.hasNext()) { 173 String collationName = (String ) collIterator.next(); 174 String message = checkSorting(collationName); 175 176 if (message.length() > 0) { 177 if (failedCollations.length() > 0) { 178 failedCollations += ", "; 179 } 180 181 failedCollations += collationName; 182 failMessage += message; 183 } 184 } 185 186 if (failedCollations.length() > 0) { 187 fail("test failed for following collations:\n" + failedCollations 188 + "\n" + failMessage); 189 } 190 } 191 192 195 protected final String getSetCollationStmt(String collationName) { 196 197 final String setCollationStmtPre = "SET DATABASE COLLATION \""; 198 final String setCollationStmtPost = "\""; 199 200 return setCollationStmtPre + collationName + setCollationStmtPost; 201 } 202 203 206 protected String checkSorting(String collationName) { 207 208 String prepareStmt = 209 "DROP TABLE WORDLIST IF EXISTS;" 210 + "CREATE TEXT TABLE WORDLIST ( ID INTEGER, WORD VARCHAR );" 211 + "SET TABLE WORDLIST SOURCE \"" + collationName 212 + ".csv;encoding=UTF-8\""; 213 String selectStmt = "SELECT ID, WORD FROM WORDLIST ORDER BY WORD"; 214 String returnMessage = ""; 215 216 try { 217 218 statement.execute(getSetCollationStmt(collationName)); 220 statement.execute(prepareStmt); 221 222 java.sql.ResultSet results = statement.executeQuery(selectStmt); 223 224 while (results.next()) { 225 int expectedPosition = results.getInt(1); 226 int foundPosition = results.getRow(); 227 228 if (expectedPosition != foundPosition) { 229 String word = results.getString(2); 230 231 return "testing collation '" + collationName 232 + "' failed\n" + " word : " + word 233 + "\n" + " expected position : " 234 + expectedPosition + "\n" 235 + " found position : " + foundPosition + "\n"; 236 } 237 } 238 } catch (java.sql.SQLException e) { 239 return "testing collation '" + collationName 240 + "' failed\n exception message: " + e.getMessage() 241 + "\n"; 242 } 243 244 return ""; 245 } 246 247 public static void main(String [] argv) { 248 249 TestResult result = new TestResult(); 250 TestCase availability = new TestCollation("verifyAvailability"); 251 252 availability.run(result); 253 254 TestCase sorting = new TestCollation("verifyCollation"); 255 256 sorting.run(result); 257 258 if (result.failureCount() != 0) { 259 System.err.println("TestCollation encountered errors:"); 260 261 java.util.Enumeration failures = result.failures(); 262 263 while (failures.hasMoreElements()) { 264 System.err.println(failures.nextElement().toString()); 265 } 266 } else { 267 System.out.println("TestCollation: all fine."); 268 } 269 } 270 } 271 | Popular Tags |