commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r821742 - in /commons/proper/dbcp/trunk: ./ src/java/org/apache/commons/dbcp/cpdsadapter/ src/test/org/apache/commons/dbcp/ src/test/org/apache/commons/dbcp/cpdsadapter/ xdocs/
Date Mon, 05 Oct 2009 10:30:19 GMT
Author: psteitz
Date: Mon Oct  5 10:30:18 2009
New Revision: 821742

URL: http://svn.apache.org/viewvc?rev=821742&view=rev
Log:
Added connectionProperties property to DriverAdapterCPDS.  JIRA: DBCP-276.

Added:
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java
Modified:
    commons/proper/dbcp/trunk/pom.xml
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/DriverAdapterCPDS.java
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java
    commons/proper/dbcp/trunk/xdocs/changes.xml

Modified: commons/proper/dbcp/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/pom.xml?rev=821742&r1=821741&r2=821742&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/pom.xml (original)
+++ commons/proper/dbcp/trunk/pom.xml Mon Oct  5 10:30:18 2009
@@ -271,6 +271,8 @@
                 <include>org/apache/commons/dbcp/TestPoolingDataSource.java</include>
                 <include>org/apache/commons/dbcp/TestJndi.java</include>
                 
+                <include>org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java</include>
+                
                 <include>org/apache/commons/dbcp/datasources/TestFactory.java</include>
                 <include>org/apache/commons/dbcp/datasources/TestCPDSConnectionFactory.java</include>
                 <include>org/apache/commons/dbcp/datasources/TestKeyedCPDSConnectionFactory.java</include>

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/DriverAdapterCPDS.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/DriverAdapterCPDS.java?rev=821742&r1=821741&r2=821742&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/DriverAdapterCPDS.java
(original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/cpdsadapter/DriverAdapterCPDS.java
Mon Oct  5 10:30:18 2009
@@ -18,8 +18,9 @@
 package org.apache.commons.dbcp.cpdsadapter;
 
 import java.util.Hashtable;
-import  java.io.PrintWriter;
-import  java.io.Serializable;
+import java.util.Properties;
+import java.io.PrintWriter;
+import java.io.Serializable;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import javax.sql.PooledConnection;
@@ -118,6 +119,9 @@
 
     /** Whether or not getConnection has been called */
     private volatile boolean getConnectionCalled = false;
+    
+    /** Connection properties passed to JDBC Driver */
+    private Properties connectionProperties = null;
 
     static {
         // Attempt to prevent deadlocks - see DBCP - 272
@@ -180,15 +184,29 @@
         // Workaround for buggy WebLogic 5.1 classloader - ignore the
         // exception upon first invocation.
         try {
-            return new PooledConnectionImpl(
-                    DriverManager.getConnection(getUrl(), username, password), 
-                    stmtPool );
+            if (connectionProperties != null) {
+                connectionProperties.put("user", username);
+                connectionProperties.put("password", password);
+                return new PooledConnectionImpl(
+                        DriverManager.getConnection(getUrl(), connectionProperties), 
+                        stmtPool); 
+            } else {
+                return new PooledConnectionImpl(
+                        DriverManager.getConnection(getUrl(), username, password), 
+                        stmtPool ); 
+            }
         }
         catch (ClassCircularityError e)
         {
-            return new PooledConnectionImpl(
-                    DriverManager.getConnection(getUrl(), username, password), 
-                    stmtPool );
+            if (connectionProperties != null) {
+                return new PooledConnectionImpl(
+                        DriverManager.getConnection(getUrl(), connectionProperties), 
+                        stmtPool); 
+            } else {
+                return new PooledConnectionImpl(
+                        DriverManager.getConnection(getUrl(), username, password), 
+                        stmtPool ); 
+            }
         }
     }
 
@@ -326,6 +344,42 @@
     // Properties
     
     /**
+     * Get the connection properties passed to the JDBC driver.
+     * 
+     * @return the JDBC connection properties used when creating connections.
+     * @since 1.3
+     */
+    public Properties getConnectionProperties() {
+        return connectionProperties;
+    }
+    
+    /**
+     * <p>Set the connection properties passed to the JDBC driver.</p>
+     * 
+     * <p>If <code>props</code> contains "user" and/or "password"
+     * properties, the corresponding instance properties are set. If these
+     * properties are not present, they are filled in using
+     * {@link #getUser()}, {@link #getPassword()} when {@link #getPooledConnection()}
+     * is called, or using the actual parameters to the method call when 
+     * {@link #getPooledConnection(String, String)} is called. Calls to
+     * {@link #setUser(String)} or {@link #setPassword(String)} overwrite the values
+     * of these properties if <code>connectionProperties</code> is not null.</p>
+     * 
+     * @param props Connection properties to use when creating new connections.
+     * @since 1.3
+     */
+    public void setConnectionProperties(Properties props) {
+        assertInitializationAllowed();
+        connectionProperties = props;
+        if (connectionProperties.containsKey("user")) {
+            setUser(connectionProperties.getProperty("user"));
+        }
+        if (connectionProperties.containsKey("password")) {
+            setPassword(connectionProperties.getProperty("password"));
+        }
+    }
+    
+    /**
      * Get the value of description.  This property is here for use by
      * the code which will deploy this datasource.  It is not used
      * internally.
@@ -362,6 +416,9 @@
     public void setPassword(String v) {
         assertInitializationAllowed();
         this.password = v;
+        if (connectionProperties != null) {
+            connectionProperties.setProperty("password", v);
+        }
     }
 
     /**
@@ -396,6 +453,9 @@
     public void setUser(String v) {
         assertInitializationAllowed();
         this.user = v;
+        if (connectionProperties != null) {
+            connectionProperties.setProperty("user", v);
+        }
     }
 
     /**

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java?rev=821742&r1=821741&r2=821742&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java (original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java Mon Oct  5 10:30:18
2009
@@ -20,6 +20,7 @@
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
+import org.apache.commons.dbcp.cpdsadapter.TestDriverAdapterCPDS;
 import org.apache.commons.dbcp.datasources.TestFactory;
 import org.apache.commons.dbcp.datasources.TestInstanceKeyDataSource;
 import org.apache.commons.dbcp.datasources.TestKeyedCPDSConnectionFactory;
@@ -60,6 +61,8 @@
         suite.addTest(TestPoolingDataSource.suite());
         suite.addTest(TestPStmtPooling.suite());
         suite.addTest(TestPStmtPoolingBasicDataSource.suite());
+        // o.a.c.dbcp.cpdsadapter
+        suite.addTest(TestDriverAdapterCPDS.suite());
         // o.a.c.dbcp.datasources
         suite.addTest(TestFactory.suite());
         suite.addTest(TestInstanceKeyDataSource.suite());

Added: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java?rev=821742&view=auto
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java
(added)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/cpdsadapter/TestDriverAdapterCPDS.java
Mon Oct  5 10:30:18 2009
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.dbcp.cpdsadapter;
+
+import java.util.Properties;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.dbcp.SQLNestedException;
+
+/**
+ * Tests for DriverAdapterCPDS
+ * 
+ * @version $Revision:$ $Date:$
+ */
+public class TestDriverAdapterCPDS extends TestCase {
+    public TestDriverAdapterCPDS(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestDriverAdapterCPDS.class);
+    }
+
+    private DriverAdapterCPDS pcds;
+
+    public void setUp() throws Exception {
+        pcds = new DriverAdapterCPDS();
+        pcds.setDriver("org.apache.commons.dbcp.TesterDriver");
+        pcds.setUrl("jdbc:apache:commons:testdriver");
+        pcds.setUser("foo");
+        pcds.setPassword("bar");
+        pcds.setPoolPreparedStatements(false);
+    }
+
+    /**
+     * JIRA: DBCP-245
+     */
+    public void testIncorrectPassword() throws Exception 
+    {
+        pcds.getPooledConnection("u2", "p2").close();
+        try {
+            // Use bad password
+            pcds.getPooledConnection("u1", "zlsafjk").close();
+            fail("Able to retrieve connection with incorrect password");
+        } catch (SQLException e1) {
+            // should fail
+
+        }
+        
+        // Use good password
+        pcds.getPooledConnection("u1", "p1").close();
+        try {
+            pcds.getPooledConnection("u1", "x").close();
+            fail("Able to retrieve connection with incorrect password");
+        }
+        catch (SQLException e) {
+            if (!e.getMessage().startsWith("x is not the correct password")) {
+                throw e;
+            }
+            // else the exception was expected
+        }
+        
+        // Make sure we can still use our good password.
+        pcds.getPooledConnection("u1", "p1").close();
+    }
+
+
+    public void testSimple() throws Exception {
+        Connection conn = pcds.getPooledConnection().getConnection();
+        assertTrue(null != conn);
+        PreparedStatement stmt = conn.prepareStatement("select * from dual");
+        assertTrue(null != stmt);
+        ResultSet rset = stmt.executeQuery();
+        assertTrue(null != rset);
+        assertTrue(rset.next());
+        rset.close();
+        stmt.close();
+        conn.close();
+    }
+
+    public void testSimpleWithUsername() throws Exception {
+        Connection conn = pcds.getPooledConnection("u1", "p1").getConnection();
+        assertTrue(null != conn);
+        PreparedStatement stmt = conn.prepareStatement("select * from dual");
+        assertTrue(null != stmt);
+        ResultSet rset = stmt.executeQuery();
+        assertTrue(null != rset);
+        assertTrue(rset.next());
+        rset.close();
+        stmt.close();
+        conn.close();
+    }
+
+    public void testClosingWithUserName() 
+        throws Exception {
+        Connection[] c = new Connection[pcds.getMaxActive()];
+        // open the maximum connections
+        for (int i=0; i<c.length; i++) {
+            c[i] = pcds.getPooledConnection("u1", "p1").getConnection();
+        }
+
+        // close one of the connections
+        c[0].close();
+        assertTrue(c[0].isClosed());
+        // get a new connection
+        c[0] = pcds.getPooledConnection("u1", "p1").getConnection();
+
+        for (int i=0; i<c.length; i++) {
+            c[i].close();
+        }
+
+        // open the maximum connections
+        for (int i=0; i<c.length; i++) {
+            c[i] = pcds.getPooledConnection("u1", "p1").getConnection();
+        }
+        for (int i=0; i<c.length; i++) {
+            c[i].close();
+        }
+    }
+    
+    public void testSetProperties() throws Exception {
+        // Set user property to bad value
+        pcds.setUser("bad");
+        // Supply correct value in connection properties
+        // This will overwrite field value
+        Properties properties = new Properties();
+        properties.put("user", "foo");
+        pcds.setConnectionProperties(properties);
+        pcds.getPooledConnection().close();
+        assertEquals("foo", pcds.getUser());
+        // Put bad password into properties
+        properties.put("password", "bad");
+        // This does not change local field
+        assertEquals("bar", pcds.getPassword());
+        // Supply correct password in getPooledConnection
+        // Call will succeed and overwrite property
+        pcds.getPooledConnection("foo", "bar").close();
+        assertEquals("bar", pcds.getConnectionProperties().getProperty("password"));    

+    }
+}

Modified: commons/proper/dbcp/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/xdocs/changes.xml?rev=821742&r1=821741&r2=821742&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/xdocs/changes.xml (original)
+++ commons/proper/dbcp/trunk/xdocs/changes.xml Mon Oct  5 10:30:18 2009
@@ -42,6 +42,9 @@
      new features as well as bug fixes and instrumentation.  Some bug fixes
      will change semantics (e.g. connection close will become idempotent).
      The minimum JDK level will be increased to 1.4">
+      <action dev="psteitz" type="update" issue="DBCP-276">
+        Added connectionProperties property to DriverAdapterCPDS.
+      </action>
       <action dev="psteitz" type="update" issue="DBCP-226">
         Added a validationQueryTimeout configuration parameter to BasicDataSource
         allowing the user to specify a timeout value (in seconds) for connection



Mime
View raw message