commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r558845 - in /jakarta/commons/proper/dbcp/trunk/src: java/org/apache/commons/dbcp/BasicDataSource.java test/org/apache/commons/dbcp/TestBasicDataSource.java
Date Mon, 23 Jul 2007 19:48:40 GMT
Author: dain
Date: Mon Jul 23 12:48:39 2007
New Revision: 558845

URL: http://svn.apache.org/viewvc?view=rev&rev=558845
Log:
DBCP-150 added setter for connectionProperties in BasicDataSource

Modified:
    jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
    jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java

Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java?view=diff&rev=558845&r1=558844&r2=558845
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
(original)
+++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
Mon Jul 23 12:48:39 2007
@@ -1093,6 +1093,40 @@
     }
 
     /**
+     * Sets the connection properties passed to driver.connect(...). 
+     *
+     * Format of the string must be [propertyName=property;]*
+     *
+     * NOTE - The "user" and "password" properties will be added
+     * explicitly, so they do not need to be included here.
+     *
+     * @param connectionProperties the connection properties used to
+     * create new connections
+     */
+    public void setConnectionProperties(String connectionProperties) {
+        if (connectionProperties == null) throw new NullPointerException("connectionProperties
is null");
+
+        String[] entries = connectionProperties.split(";");
+        Properties properties = new Properties();
+        for (int i = 0; i < entries.length; i++) {
+            String entry = entries[i];
+            if (entry.length() > 0) {
+                int index = entry.indexOf('=');
+                if (index > 0) {
+                    String name = entry.substring(0, index);
+                    String value = entry.substring(index + 1);
+                    properties.setProperty(name, value);
+                } else {
+                    // no value is empty string which is how java.util.Properties works
+                    properties.setProperty(entry, "");
+                }
+            }
+        }
+        this.connectionProperties = properties;
+        this.restartNeeded = true;
+    }
+
+    /**
      * Close and release all connections that are currently stored in the
      * connection pool associated with our data source.
      *

Modified: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java?view=diff&rev=558845&r1=558844&r2=558845
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java
(original)
+++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java
Mon Jul 23 12:48:39 2007
@@ -67,6 +67,57 @@
         ds.close();
         ds = null;
     }
+
+    public void testSetProperties() throws Exception {
+        // normal
+        ds.setConnectionProperties("name1=value1;name2=value2;name3=value3");
+        assertEquals(3, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+        assertEquals("value2", ds.connectionProperties.getProperty("name2"));
+        assertEquals("value3", ds.connectionProperties.getProperty("name3"));
+
+        // make sure all properties are replaced
+        ds.setConnectionProperties("name1=value1;name2=value2");
+        assertEquals(2, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+        assertEquals("value2", ds.connectionProperties.getProperty("name2"));
+        assertFalse(ds.connectionProperties.containsKey("name3"));
+
+        // no value is empty string
+        ds.setConnectionProperties("name1=value1;name2");
+        assertEquals(2, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+        assertEquals("", ds.connectionProperties.getProperty("name2"));
+
+        // no value (with equals) is empty string
+        ds.setConnectionProperties("name1=value1;name2=");
+        assertEquals(2, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+        assertEquals("", ds.connectionProperties.getProperty("name2"));
+
+        // single value
+        ds.setConnectionProperties("name1=value1");
+        assertEquals(1, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+
+        // single value with trailing ;
+        ds.setConnectionProperties("name1=value1;");
+        assertEquals(1, ds.connectionProperties.size());
+        assertEquals("value1", ds.connectionProperties.getProperty("name1"));
+
+        // single value wit no value
+        ds.setConnectionProperties("name1");
+        assertEquals(1, ds.connectionProperties.size());
+        assertEquals("", ds.connectionProperties.getProperty("name1"));
+
+        // null should throw a NullPointerException
+        try {
+            ds.setConnectionProperties(null);
+            fail("Expected NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
     
     public void testTransactionIsolationBehavior() throws Exception {
         Connection conn = getConnection();



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message