Source Code Cross Referenced for TestBasicDataSource.java in  » Database-JDBC-Connection-Pool » Connection-Pool-DBCP » org » apache » commons » dbcp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » Connection Pool DBCP » org.apache.commons.dbcp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.dbcp;
019:
020:        import java.io.IOException;
021:        import java.sql.Connection;
022:        import java.sql.SQLException;
023:
024:        import junit.framework.Test;
025:        import junit.framework.TestSuite;
026:
027:        /**
028:         * TestSuite for BasicDataSource
029:         * 
030:         * @author Dirk Verbeeck
031:         * @version $Revision: 522304 $ $Date: 2007-03-25 10:21:23 -0700 (Sun, 25 Mar 2007) $
032:         */
033:        public class TestBasicDataSource extends TestConnectionPool {
034:            public TestBasicDataSource(String testName) {
035:                super (testName);
036:            }
037:
038:            public static Test suite() {
039:                return new TestSuite(TestBasicDataSource.class);
040:            }
041:
042:            protected Connection getConnection() throws Exception {
043:                return ds.getConnection();
044:            }
045:
046:            protected BasicDataSource ds = null;
047:            private static String CATALOG = "test catalog";
048:
049:            public void setUp() throws Exception {
050:                super .setUp();
051:                ds = new BasicDataSource();
052:                ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver");
053:                ds.setUrl("jdbc:apache:commons:testdriver");
054:                ds.setMaxActive(getMaxActive());
055:                ds.setMaxWait(getMaxWait());
056:                ds.setDefaultAutoCommit(true);
057:                ds.setDefaultReadOnly(false);
058:                ds
059:                        .setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
060:                ds.setDefaultCatalog(CATALOG);
061:                ds.setUsername("username");
062:                ds.setPassword("password");
063:                ds.setValidationQuery("SELECT DUMMY FROM DUAL");
064:            }
065:
066:            public void tearDown() throws Exception {
067:                super .tearDown();
068:                ds.close();
069:                ds = null;
070:            }
071:
072:            public void testTransactionIsolationBehavior() throws Exception {
073:                Connection conn = getConnection();
074:                assertTrue(conn != null);
075:                assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn
076:                        .getTransactionIsolation());
077:                conn
078:                        .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
079:                conn.close();
080:
081:                Connection conn2 = getConnection();
082:                assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn2
083:                        .getTransactionIsolation());
084:
085:                Connection conn3 = getConnection();
086:                assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn3
087:                        .getTransactionIsolation());
088:
089:                conn2.close();
090:
091:                conn3.close();
092:            }
093:
094:            public void testPooling() throws Exception {
095:                // this also needs access to the undelying connection
096:                ds.setAccessToUnderlyingConnectionAllowed(true);
097:                super .testPooling();
098:            }
099:
100:            public void testNoAccessToUnderlyingConnectionAllowed()
101:                    throws Exception {
102:                // default: false
103:                assertEquals(false, ds.isAccessToUnderlyingConnectionAllowed());
104:
105:                Connection conn = getConnection();
106:                Connection dconn = ((DelegatingConnection) conn).getDelegate();
107:                assertNull(dconn);
108:
109:                dconn = ((DelegatingConnection) conn).getInnermostDelegate();
110:                assertNull(dconn);
111:            }
112:
113:            public void testAccessToUnderlyingConnectionAllowed()
114:                    throws Exception {
115:                ds.setAccessToUnderlyingConnectionAllowed(true);
116:                assertEquals(true, ds.isAccessToUnderlyingConnectionAllowed());
117:
118:                Connection conn = getConnection();
119:                Connection dconn = ((DelegatingConnection) conn).getDelegate();
120:                assertNotNull(dconn);
121:
122:                dconn = ((DelegatingConnection) conn).getInnermostDelegate();
123:                assertNotNull(dconn);
124:
125:                assertTrue(dconn instanceof  TesterConnection);
126:            }
127:
128:            public void testEmptyValidationQuery() throws Exception {
129:                assertNotNull(ds.getValidationQuery());
130:
131:                ds.setValidationQuery("");
132:                assertNull(ds.getValidationQuery());
133:
134:                ds.setValidationQuery("   ");
135:                assertNull(ds.getValidationQuery());
136:            }
137:
138:            public void testInvalidValidationQuery() {
139:                try {
140:                    ds.setValidationQuery("invalid");
141:                    ds.getConnection();
142:                    fail("expected SQLException");
143:                } catch (SQLException e) {
144:                    if (e.toString().indexOf("invalid") < 0) {
145:                        fail("expected detailed error message");
146:                    }
147:                }
148:            }
149:
150:            public void testSetValidationTestProperties() {
151:                // defaults
152:                assertEquals(true, ds.getTestOnBorrow());
153:                assertEquals(false, ds.getTestOnReturn());
154:                assertEquals(false, ds.getTestWhileIdle());
155:
156:                ds.setTestOnBorrow(true);
157:                ds.setTestOnReturn(true);
158:                ds.setTestWhileIdle(true);
159:                assertEquals(true, ds.getTestOnBorrow());
160:                assertEquals(true, ds.getTestOnReturn());
161:                assertEquals(true, ds.getTestWhileIdle());
162:
163:                ds.setTestOnBorrow(false);
164:                ds.setTestOnReturn(false);
165:                ds.setTestWhileIdle(false);
166:                assertEquals(false, ds.getTestOnBorrow());
167:                assertEquals(false, ds.getTestOnReturn());
168:                assertEquals(false, ds.getTestWhileIdle());
169:            }
170:
171:            public void testNoValidationQuery() throws Exception {
172:                ds.setTestOnBorrow(true);
173:                ds.setTestOnReturn(true);
174:                ds.setTestWhileIdle(true);
175:                ds.setValidationQuery("");
176:
177:                Connection conn = ds.getConnection();
178:                conn.close();
179:
180:                assertEquals(false, ds.getTestOnBorrow());
181:                assertEquals(false, ds.getTestOnReturn());
182:                assertEquals(false, ds.getTestWhileIdle());
183:            }
184:
185:            public void testDefaultCatalog() throws Exception {
186:                Connection[] c = new Connection[getMaxActive()];
187:                for (int i = 0; i < c.length; i++) {
188:                    c[i] = getConnection();
189:                    assertTrue(c[i] != null);
190:                    assertEquals(CATALOG, c[i].getCatalog());
191:                }
192:
193:                for (int i = 0; i < c.length; i++) {
194:                    c[i].setCatalog("error");
195:                    c[i].close();
196:                }
197:
198:                for (int i = 0; i < c.length; i++) {
199:                    c[i] = getConnection();
200:                    assertTrue(c[i] != null);
201:                    assertEquals(CATALOG, c[i].getCatalog());
202:                }
203:
204:                for (int i = 0; i < c.length; i++) {
205:                    c[i].close();
206:                }
207:            }
208:
209:            public void testSetAutoCommitTrueOnClose() throws Exception {
210:                ds.setAccessToUnderlyingConnectionAllowed(true);
211:                ds.setDefaultAutoCommit(false);
212:
213:                Connection conn = getConnection();
214:                assertNotNull(conn);
215:                assertEquals(false, conn.getAutoCommit());
216:
217:                Connection dconn = ((DelegatingConnection) conn)
218:                        .getInnermostDelegate();
219:                assertNotNull(dconn);
220:                assertEquals(false, dconn.getAutoCommit());
221:
222:                conn.close();
223:
224:                assertEquals(true, dconn.getAutoCommit());
225:            }
226:
227:            public void testInitialSize() throws Exception {
228:                ds.setMaxActive(20);
229:                ds.setMaxIdle(20);
230:                ds.setInitialSize(10);
231:
232:                Connection conn = getConnection();
233:                assertNotNull(conn);
234:                conn.close();
235:
236:                assertEquals(0, ds.getNumActive());
237:                assertEquals(10, ds.getNumIdle());
238:            }
239:
240:            // Bugzilla Bug 28251:  Returning dead database connections to BasicDataSource
241:            // isClosed() failure blocks returning a connection to the pool 
242:            public void testIsClosedFailure() throws SQLException {
243:                ds.setAccessToUnderlyingConnectionAllowed(true);
244:                Connection conn = ds.getConnection();
245:                assertNotNull(conn);
246:                assertEquals(1, ds.getNumActive());
247:
248:                // set an IO failure causing the isClosed mathod to fail
249:                TesterConnection tconn = (TesterConnection) ((DelegatingConnection) conn)
250:                        .getInnermostDelegate();
251:                tconn.setFailure(new IOException("network error"));
252:
253:                try {
254:                    conn.close();
255:                    fail("Expected SQLException");
256:                } catch (SQLException ex) {
257:                }
258:
259:                assertEquals(0, ds.getNumActive());
260:            }
261:
262:            /** 
263:             * Bugzilla Bug 29054: 
264:             * The BasicDataSource.setTestOnReturn(boolean) is not carried through to 
265:             * the GenericObjectPool variable _testOnReturn.
266:             */
267:            public void testPropertyTestOnReturn() throws Exception {
268:                ds.setValidationQuery("select 1 from dual");
269:                ds.setTestOnBorrow(false);
270:                ds.setTestWhileIdle(false);
271:                ds.setTestOnReturn(true);
272:
273:                Connection conn = ds.getConnection();
274:                assertNotNull(conn);
275:
276:                assertEquals(false, ds.connectionPool.getTestOnBorrow());
277:                assertEquals(false, ds.connectionPool.getTestWhileIdle());
278:                assertEquals(true, ds.connectionPool.getTestOnReturn());
279:            }
280:
281:            /**
282:             * Bugzilla Bug 29055: AutoCommit and ReadOnly
283:             * The DaffodilDB driver throws an SQLException if 
284:             * trying to commit or rollback a readOnly connection. 
285:             */
286:            public void testRollbackReadOnly() throws Exception {
287:                ds.setDefaultReadOnly(true);
288:                ds.setDefaultAutoCommit(false);
289:
290:                Connection conn = ds.getConnection();
291:                assertNotNull(conn);
292:                conn.close();
293:            }
294:
295:            /**
296:             * Bugzilla Bug 29832: Broken behaviour for BasicDataSource.setMaxActive(0)
297:             * MaxActive == 0 should throw SQLException on getConnection.
298:             * Results from Bug 29863 in commons-pool.
299:             */
300:            public void testMaxActiveZero() throws Exception {
301:                ds.setMaxActive(0);
302:
303:                try {
304:                    Connection conn = ds.getConnection();
305:                    assertNotNull(conn);
306:                    fail("SQLException expected");
307:
308:                } catch (SQLException e) {
309:                    // test OK
310:                }
311:            }
312:
313:            /**
314:             * JIRA DBCP-93: If an SQLException occurs after the GenericObjectPool is
315:             * initialized in createDataSource, the evictor task is not cleaned up.
316:             */
317:            public void testCreateDataSourceCleanupThreads() throws Exception {
318:                ds.close();
319:                ds = null;
320:                ds = new BasicDataSource();
321:                ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver");
322:                ds.setUrl("jdbc:apache:commons:testdriver");
323:                ds.setMaxActive(getMaxActive());
324:                ds.setMaxWait(getMaxWait());
325:                ds.setDefaultAutoCommit(true);
326:                ds.setDefaultReadOnly(false);
327:                ds
328:                        .setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
329:                ds.setDefaultCatalog(CATALOG);
330:                ds.setUsername("username");
331:                // Set timeBetweenEvictionRuns > 0, so evictor is created
332:                ds.setTimeBetweenEvictionRunsMillis(100);
333:                // Make password incorrect, so createDataSource will throw
334:                ds.setPassword("wrong");
335:                ds.setValidationQuery("SELECT DUMMY FROM DUAL");
336:                int threadCount = Thread.activeCount();
337:                for (int i = 0; i < 10; i++) {
338:                    try {
339:                        Connection con = ds.getConnection();
340:                    } catch (SQLException ex) {
341:                        // ignore
342:                    }
343:                }
344:                // Allow one extra thread for JRockit compatibility
345:                assertTrue(Thread.activeCount() <= threadCount + 1);
346:            }
347:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.