1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp;
19  
20  import java.sql.Connection;
21  import java.util.Properties;
22  
23  import javax.sql.DataSource;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  /**
30   * TestSuite for BasicDataSourceFactory
31   * 
32   * @author Dirk Verbeeck
33   * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
34   */
35  public class TestBasicDataSourceFactory extends TestCase {
36      public TestBasicDataSourceFactory(String testName) {
37          super(testName);
38      }
39  
40      public static Test suite() {
41          return new TestSuite(TestBasicDataSourceFactory.class);
42      }
43      
44      public void testNoProperties() throws Exception {
45          Properties properties = new Properties();
46          DataSource ds = BasicDataSourceFactory.createDataSource(properties);
47          
48          assertNotNull(ds);
49          assertTrue(ds instanceof BasicDataSource);
50      }
51  
52      public void testProperties() throws Exception {
53          Properties properties = new Properties();
54          properties.setProperty("driverClassName", "org.apache.commons.dbcp.TesterDriver");
55          properties.setProperty("url", "jdbc:apache:commons:testdriver");
56          properties.setProperty("maxActive", "10");
57          properties.setProperty("maxIdle", "8");
58          properties.setProperty("minIdle", "0");
59          properties.setProperty("maxWait", "500");
60          properties.setProperty("initialSize", "5");
61          properties.setProperty("defaultAutoCommit", "true");
62          properties.setProperty("defaultReadOnly", "false");
63          properties.setProperty("defaultTransactionIsolation", "READ_COMMITTED");
64          properties.setProperty("defaultCatalog", "test");
65          properties.setProperty("testOnBorrow", "true");
66          properties.setProperty("testOnReturn", "false");
67          properties.setProperty("username", "username");
68          properties.setProperty("password", "password");
69          properties.setProperty("validationQuery", "SELECT DUMMY FROM DUAL");
70          properties.setProperty("validationQueryTimeout", "100");
71          properties.setProperty("connectionInitSqls", "SELECT 1;SELECT 2");
72          properties.setProperty("timeBetweenEvictionRunsMillis", "1000");
73          properties.setProperty("minEvictableIdleTimeMillis", "2000");
74          properties.setProperty("numTestsPerEvictionRun", "2");
75          properties.setProperty("testWhileIdle", "true");
76          properties.setProperty("accessToUnderlyingConnectionAllowed", "true");
77          properties.setProperty("removeAbandoned", "true");
78          properties.setProperty("removeAbandonedTimeout", "3000");
79          properties.setProperty("logAbandoned", "true");
80          properties.setProperty("poolPreparedStatements", "true");
81          properties.setProperty("maxOpenPreparedStatements", "10");
82  
83          BasicDataSource ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);
84          
85          assertEquals("org.apache.commons.dbcp.TesterDriver", ds.getDriverClassName());
86          assertEquals("jdbc:apache:commons:testdriver", ds.getUrl());
87          assertEquals(10, ds.getMaxActive());
88          assertEquals(8, ds.getMaxIdle());
89          assertEquals(0, ds.getMinIdle());
90          assertEquals(500, ds.getMaxWait());
91          assertEquals(5, ds.getInitialSize());
92          assertEquals(5, ds.getNumIdle());
93          assertEquals(true, ds.getDefaultAutoCommit());
94          assertEquals(false, ds.getDefaultReadOnly());
95          assertEquals(Connection.TRANSACTION_READ_COMMITTED, ds.getDefaultTransactionIsolation());
96          assertEquals("test", ds.getDefaultCatalog());
97          assertEquals(true, ds.getTestOnBorrow());
98          assertEquals(false, ds.getTestOnReturn());
99          assertEquals("username", ds.getUsername());
100         assertEquals("password", ds.getPassword());
101         assertEquals("SELECT DUMMY FROM DUAL", ds.getValidationQuery());
102         assertEquals(100, ds.getValidationQueryTimeout());
103         assertEquals(2, ds.connectionInitSqls.size());
104         assertEquals("SELECT 1", ds.connectionInitSqls.get(0));
105         assertEquals("SELECT 2", ds.connectionInitSqls.get(1));
106         assertEquals(1000, ds.getTimeBetweenEvictionRunsMillis());
107         assertEquals(2000, ds.getMinEvictableIdleTimeMillis());
108         assertEquals(2, ds.getNumTestsPerEvictionRun());
109         assertEquals(true, ds.getTestWhileIdle());
110         assertEquals(true, ds.isAccessToUnderlyingConnectionAllowed());
111         assertEquals(true, ds.getRemoveAbandoned());
112         assertEquals(3000, ds.getRemoveAbandonedTimeout());
113         assertEquals(true, ds.getLogAbandoned());
114         assertEquals(true, ds.isPoolPreparedStatements());
115         assertEquals(10, ds.getMaxOpenPreparedStatements());
116     }
117 }