1 16 17 package org.springframework.jdbc.support; 18 19 import java.sql.Connection ; 20 import java.sql.DatabaseMetaData ; 21 import java.sql.SQLException ; 22 import java.util.Arrays ; 23 24 import javax.sql.DataSource ; 25 26 import junit.framework.TestCase; 27 import org.easymock.MockControl; 28 29 import org.springframework.core.io.ClassPathResource; 30 import org.springframework.core.io.Resource; 31 import org.springframework.jdbc.datasource.AbstractDataSource; 32 33 39 public class SQLErrorCodesFactoryTests extends TestCase { 40 41 44 public void testDefaultInstanceWithNoSuchDatabase() { 45 SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("xx"); 46 assertTrue(sec.getBadSqlGrammarCodes().length == 0); 47 assertTrue(sec.getDataIntegrityViolationCodes().length == 0); 48 } 49 50 53 public void testDefaultInstanceWithOracle() { 54 SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle"); 55 assertIsOracle(sec); 56 } 57 58 private void assertIsOracle(SQLErrorCodes sec) { 59 assertTrue(sec.getBadSqlGrammarCodes().length > 0); 60 assertTrue(sec.getDataIntegrityViolationCodes().length > 0); 61 assertTrue(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942") >= 0); 63 assertFalse(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42") >= 0); 65 } 66 67 private void assertIsHsql(SQLErrorCodes sec) { 68 assertTrue(sec.getBadSqlGrammarCodes().length > 0); 69 assertTrue(sec.getDataIntegrityViolationCodes().length > 0); 70 assertTrue(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-22") >= 0); 72 assertFalse(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-9") >= 0); 74 } 75 76 private void assertIsDB2(SQLErrorCodes sec) { 77 assertTrue(sec.getBadSqlGrammarCodes().length > 0); 78 assertTrue(sec.getDataIntegrityViolationCodes().length > 0); 79 80 assertFalse(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942") >= 0); 81 assertTrue(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-204") >= 0); 83 } 84 85 public void testLookupOrder() { 86 class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { 87 private int lookups = 0; 88 protected Resource loadResource(String path) { 89 ++lookups; 90 if (lookups == 1) { 91 assertEquals(SQLErrorCodesFactory.SQL_ERROR_CODE_DEFAULT_PATH, path); 92 return null; 93 } 94 else { 95 assertEquals(2, lookups); 97 assertEquals(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH, path); 98 return null; 99 } 100 } 101 } 102 103 TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); 105 assertTrue(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length == 0); 106 assertTrue(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes().length == 0); 107 } 108 109 112 public void testFindUserDefinedCodes() { 113 class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { 114 protected Resource loadResource(String path) { 115 if (SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH.equals(path)) { 116 return new ClassPathResource("test-error-codes.xml", SQLErrorCodesFactoryTests.class); 117 } 118 return null; 119 } 120 } 121 122 TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); 124 assertTrue(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length == 0); 125 assertEquals(2, sf.getErrorCodes("Oracle").getBadSqlGrammarCodes().length); 126 assertEquals("1", sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[0]); 127 assertEquals("2", sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[1]); 128 } 129 130 public void testInvalidUserDefinedCodeFormat() { 131 class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { 132 protected Resource loadResource(String path) { 133 if (SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH.equals(path)) { 134 return new ClassPathResource("SQLExceptionTranslator.class", SQLErrorCodesFactoryTests.class); 136 } 137 return null; 138 } 139 } 140 141 TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); 143 assertTrue(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length == 0); 144 assertEquals(0, sf.getErrorCodes("Oracle").getBadSqlGrammarCodes().length); 145 } 146 147 150 public void testFindCustomCodes() { 151 class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { 152 protected Resource loadResource(String path) { 153 if (SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH.equals(path)) { 154 return new ClassPathResource("custom-error-codes.xml", SQLErrorCodesFactoryTests.class); 155 } 156 return null; 157 } 158 } 159 160 TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); 162 assertEquals(1, sf.getErrorCodes("Oracle").getCustomTranslations().length); 163 CustomSQLErrorCodesTranslation translation = 164 (CustomSQLErrorCodesTranslation) sf.getErrorCodes("Oracle").getCustomTranslations()[0]; 165 assertEquals(CustomErrorCodeException.class, translation.getExceptionClass()); 166 assertEquals(1, translation.getErrorCodes().length); 167 } 168 169 public void testDataSourceWithNullMetadata() throws Exception { 170 171 MockControl ctrlConnection = MockControl.createControl(Connection .class); 172 Connection mockConnection = (Connection ) ctrlConnection.getMock(); 173 mockConnection.getMetaData(); 174 ctrlConnection.setReturnValue(null); 175 mockConnection.close(); 176 ctrlConnection.setVoidCallable(); 177 ctrlConnection.replay(); 178 179 MockControl ctrlDataSource = MockControl.createControl(DataSource .class); 180 DataSource mockDataSource = (DataSource ) ctrlDataSource.getMock(); 181 mockDataSource.getConnection(); 182 ctrlDataSource.setDefaultReturnValue(mockConnection); 183 ctrlDataSource.replay(); 184 185 SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(mockDataSource); 186 assertIsEmpty(sec); 187 188 ctrlConnection.verify(); 189 ctrlDataSource.verify(); 190 } 191 192 public void testGetFromDataSourceWithSQLException() throws Exception { 193 194 SQLException expectedSQLException = new SQLException (); 195 196 MockControl ctrlDataSource = MockControl.createControl(DataSource .class); 197 DataSource mockDataSource = (DataSource ) ctrlDataSource.getMock(); 198 mockDataSource.getConnection(); 199 ctrlDataSource.setThrowable(expectedSQLException); 200 ctrlDataSource.replay(); 201 202 SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(mockDataSource); 203 assertIsEmpty(sec); 204 205 ctrlDataSource.verify(); 206 } 207 208 private void assertIsEmpty(SQLErrorCodes sec) { 209 assertEquals(0, sec.getBadSqlGrammarCodes().length); 211 assertEquals(0, sec.getDataIntegrityViolationCodes().length); 212 } 213 214 private SQLErrorCodes getErrorCodesFromDataSourceWithGivenMetadata(String productName, SQLErrorCodesFactory factory) throws Exception { 215 MockControl mdControl = MockControl.createControl(DatabaseMetaData .class); 216 DatabaseMetaData md = (DatabaseMetaData ) mdControl.getMock(); 217 md.getDatabaseProductName(); 218 mdControl.setReturnValue(productName); 219 mdControl.replay(); 220 221 MockControl ctrlConnection = MockControl.createControl(Connection .class); 222 Connection mockConnection = (Connection ) ctrlConnection.getMock(); 223 mockConnection.getMetaData(); 224 ctrlConnection.setReturnValue(md); 225 mockConnection.close(); 226 ctrlConnection.setVoidCallable(); 227 ctrlConnection.replay(); 228 229 235 DataSource mockDataSource = new SpringMockDataSource(productName, mockConnection); 241 242 SQLErrorCodesFactory secf = null; 243 if (factory != null) 244 secf = factory; 245 else 246 secf = SQLErrorCodesFactory.getInstance(); 247 248 SQLErrorCodes sec = secf.getErrorCodes(mockDataSource); 249 250 mdControl.verify(); 251 ctrlConnection.verify(); 252 254 return sec; 255 } 256 257 260 private class SpringMockDataSource extends AbstractDataSource { 261 String productName; 262 Connection conn; 263 private SpringMockDataSource(String productName, Connection conn) { 264 this.productName = productName; 265 this.conn = conn; 266 } 267 public Connection getConnection() { 268 return conn; 269 } 270 public Connection getConnection(String u, String p) { 271 return null; 272 } 273 } 274 275 public void testOracleRecognizedFromMetadata() throws Exception { 276 SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("Oracle", null); 277 assertIsOracle(sec); 278 } 279 280 public void testHsqlRecognizedFromMetadata() throws Exception { 281 SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("HSQL Database Engine", null); 282 assertIsHsql(sec); 283 } 284 285 public void testDB2RecognizedFromMetadata() throws Exception { 286 SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("DB2", null); 287 assertIsDB2(sec); 288 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB2/", null); 289 assertIsDB2(sec); 290 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB-2", null); 291 assertIsEmpty(sec); 292 } 293 294 297 public void testWildCardNameRecognized() throws Exception { 298 class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory { 299 protected Resource loadResource(String path) { 300 if (SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH.equals(path)) { 301 return new ClassPathResource("wildcard-error-codes.xml", SQLErrorCodesFactoryTests.class); 302 } 303 return null; 304 } 305 } 306 307 WildcardSQLErrorCodesFactory factory = new WildcardSQLErrorCodesFactory(); 308 SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("DB2", factory); 309 assertIsDB2(sec); 310 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB2 UDB for Xxxxx", factory); 311 assertIsDB2(sec); 312 313 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB3", factory); 314 assertIsDB2(sec); 315 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB3/", factory); 316 assertIsDB2(sec); 317 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB3", factory); 318 assertIsDB2(sec); 319 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB3", factory); 320 assertIsDB2(sec); 321 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB3/", factory); 322 assertIsDB2(sec); 323 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB-3", factory); 324 assertIsEmpty(sec); 325 326 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB1", factory); 327 assertIsDB2(sec); 328 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB1/", factory); 329 assertIsDB2(sec); 330 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB1", factory); 331 assertIsEmpty(sec); 332 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB1/", factory); 333 assertIsEmpty(sec); 334 335 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB0", factory); 336 assertIsDB2(sec); 337 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB0", factory); 338 assertIsDB2(sec); 339 sec = getErrorCodesFromDataSourceWithGivenMetadata("DB0/", factory); 340 assertIsEmpty(sec); 341 sec = getErrorCodesFromDataSourceWithGivenMetadata("/DB0/", factory); 342 assertIsEmpty(sec); 343 } 344 345 } 346 | Popular Tags |