View Javadoc
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.dbcp2;
19  
20  import java.sql.Connection;
21  import java.sql.Driver;
22  import java.sql.SQLException;
23  import java.util.Properties;
24  
25  /**
26   * Dummy {@link ConnectionFactory} for testing purpose.
27   */
28  public class TesterConnectionFactory implements ConnectionFactory {
29  
30      private final String connectionString;
31      private final Driver driver;
32      private final Properties properties;
33  
34      /**
35       * Constructs a connection factory for a given Driver.
36       *
37       * @param driver        The Driver.
38       * @param connectString The connection string.
39       * @param properties    The connection properties.
40       */
41      public TesterConnectionFactory(final Driver driver, final String connectString, final Properties properties) {
42          this.driver = driver;
43          this.connectionString = connectString;
44          this.properties = properties;
45      }
46  
47      @Override
48      public Connection createConnection() throws SQLException {
49          final Connection conn = driver.connect(connectionString, properties);
50          doSomething(conn);
51          return conn;
52      }
53  
54      private void doSomething(final Connection conn) {
55          // do something
56      }
57  
58      /**
59       * @return The connection String.
60       */
61      public String getConnectionString() {
62          return connectionString;
63      }
64  
65      /**
66       * @return The Driver.
67       */
68      public Driver getDriver() {
69          return driver;
70      }
71  
72      /**
73       * @return The Properties.
74       */
75      public Properties getProperties() {
76          return properties;
77      }
78  
79      @Override
80      public String toString() {
81          return this.getClass().getName() + " [" + driver + ";" + connectionString + ";" + properties + "]";
82      }
83  }