KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > SQLErrorCodesFactoryTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.support;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.DatabaseMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.Arrays JavaDoc;
23
24 import javax.sql.DataSource JavaDoc;
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 /**
34  * Tests for SQLErrorCodes loading.
35  *
36  * @author Rod Johnson
37  * @author Thomas Risberg
38  */

39 public class SQLErrorCodesFactoryTests extends TestCase {
40
41     /**
42      * Check that a default instance returns empty error codes for an unknown database.
43      */

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     /**
51      * Check that a known database produces recognizable codes.
52      */

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         // This had better be a Bad SQL Grammar code
62
assertTrue(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942") >= 0);
63         // This had better NOT be
64
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         // This had better be a Bad SQL Grammar code
71
assertTrue(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-22") >= 0);
72         // This had better NOT be
73
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         // This had better NOT be
82
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 JavaDoc path) {
89                 ++lookups;
90                 if (lookups == 1) {
91                     assertEquals(SQLErrorCodesFactory.SQL_ERROR_CODE_DEFAULT_PATH, path);
92                     return null;
93                 }
94                 else {
95                     // Should have only one more lookup
96
assertEquals(2, lookups);
97                     assertEquals(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH, path);
98                     return null;
99                 }
100             }
101         }
102         
103         // Should have failed to load without error
104
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
105         assertTrue(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length == 0);
106         assertTrue(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes().length == 0);
107     }
108     
109     /**
110      * Check that user defined error codes take precedence.
111      */

112     public void testFindUserDefinedCodes() {
113         class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
114             protected Resource loadResource(String JavaDoc 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         // Should have loaded without error
123
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 JavaDoc path) {
133                 if (SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH.equals(path)) {
134                     // Guaranteed to be on the classpath, but most certainly NOT XML
135
return new ClassPathResource("SQLExceptionTranslator.class", SQLErrorCodesFactoryTests.class);
136                 }
137                 return null;
138             }
139         }
140
141         // Should have failed to load without error
142
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
143         assertTrue(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length == 0);
144         assertEquals(0, sf.getErrorCodes("Oracle").getBadSqlGrammarCodes().length);
145     }
146     
147     /**
148      * Check that custom error codes take precedence.
149      */

150     public void testFindCustomCodes() {
151         class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
152             protected Resource loadResource(String JavaDoc 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         // Should have loaded without error
161
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 JavaDoc {
170         
171         MockControl ctrlConnection = MockControl.createControl(Connection JavaDoc.class);
172         Connection JavaDoc mockConnection = (Connection JavaDoc) 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 JavaDoc.class);
180         DataSource JavaDoc mockDataSource = (DataSource JavaDoc) 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 JavaDoc {
193         
194         SQLException JavaDoc expectedSQLException = new SQLException JavaDoc();
195
196         MockControl ctrlDataSource = MockControl.createControl(DataSource JavaDoc.class);
197         DataSource JavaDoc mockDataSource = (DataSource JavaDoc) 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         // Codes should be empty
210
assertEquals(0, sec.getBadSqlGrammarCodes().length);
211         assertEquals(0, sec.getDataIntegrityViolationCodes().length);
212     }
213
214     private SQLErrorCodes getErrorCodesFromDataSourceWithGivenMetadata(String JavaDoc productName, SQLErrorCodesFactory factory) throws Exception JavaDoc {
215         MockControl mdControl = MockControl.createControl(DatabaseMetaData JavaDoc.class);
216         DatabaseMetaData JavaDoc md = (DatabaseMetaData JavaDoc) mdControl.getMock();
217         md.getDatabaseProductName();
218         mdControl.setReturnValue(productName);
219         mdControl.replay();
220         
221         MockControl ctrlConnection = MockControl.createControl(Connection JavaDoc.class);
222         Connection JavaDoc mockConnection = (Connection JavaDoc) ctrlConnection.getMock();
223         mockConnection.getMetaData();
224         ctrlConnection.setReturnValue(md);
225         mockConnection.close();
226         ctrlConnection.setVoidCallable();
227         ctrlConnection.replay();
228
229         /*
230          * We can't use an EasyMock mock object here since we cache product name based on data source hashCode
231          * and EasyMock always seem to return the same hashCode regardles when/where/how it is created.
232          * This messes up the meta data calls - calling classes don't think they have to call since they
233          * erroneously think it is the same data source.
234          */

235         //MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
236
//DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
237
//mockDataSource.getConnection();
238
//ctrlDataSource.setDefaultReturnValue(mockConnection);
239
//ctrlDataSource.replay();
240
DataSource JavaDoc 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         //ctrlDataSource.verify();
253

254         return sec;
255     }
256     
257     /*
258      * Stand in for the EasyMock mock object - see comment above.
259      */

260     private class SpringMockDataSource extends AbstractDataSource {
261         String JavaDoc productName;
262         Connection JavaDoc conn;
263         private SpringMockDataSource(String JavaDoc productName, Connection JavaDoc conn) {
264             this.productName = productName;
265             this.conn = conn;
266         }
267         public Connection JavaDoc getConnection() {
268             return conn;
269         }
270         public Connection JavaDoc getConnection(String JavaDoc u, String JavaDoc p) {
271             return null;
272         }
273     }
274
275     public void testOracleRecognizedFromMetadata() throws Exception JavaDoc {
276         SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("Oracle", null);
277         assertIsOracle(sec);
278     }
279     
280     public void testHsqlRecognizedFromMetadata() throws Exception JavaDoc {
281         SQLErrorCodes sec = getErrorCodesFromDataSourceWithGivenMetadata("HSQL Database Engine", null);
282         assertIsHsql(sec);
283     }
284
285     public void testDB2RecognizedFromMetadata() throws Exception JavaDoc {
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     /**
295      * Check that wild card database name works.
296      */

297     public void testWildCardNameRecognized() throws Exception JavaDoc {
298         class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory {
299             protected Resource loadResource(String JavaDoc 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