db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1032485 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/jdbcapi/DatabaseMetaDataTest.java junit/Decorator.java junit/DriverManagerConnector.java junit/JDBCDataSource.java junit/TestConfiguration.java
Date Mon, 08 Nov 2010 08:22:18 GMT
Author: kahatlen
Date: Mon Nov  8 08:22:17 2010
New Revision: 1032485

URL: http://svn.apache.org/viewvc?rev=1032485&view=rev
Log:
DERBY-4884 (partial) DatabasePropertyTestSetup cannot change static
properties in encrypted databases

Changed the decorator for encrypted databases to make the connection
attributes available in the code that opens a default connection. This
makes it possible to reboot an encrypted database with the helper
methods in the framework, which is what DatabasePropertyTestSetup
needs in order to change static properties.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Decorator.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java?rev=1032485&r1=1032484&r2=1032485&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java
Mon Nov  8 08:22:17 2010
@@ -707,10 +707,14 @@ public class DatabaseMetaDataTest extend
         
         assertFalse("getURL is supported!", JDBC.vmSupportsJSR169());
         assertTrue("getURL is supported!", JDBC.vmSupportsJDBC3());
-        
-        assertEquals("getURL match",
-                getTestConfiguration().getJDBCUrl(),
-                url);              
+
+        // DERBY-4886: Embedded returns the URL without connection attributes,
+        // client returns the URL with connection attributes.
+        TestConfiguration config = getTestConfiguration();
+        String expectedURL = usingEmbedded() ?
+            config.getJDBCUrl() : config.getJDBCUrlWithAttributes();
+
+        assertEquals("getURL match", expectedURL, url);
     }
     
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Decorator.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Decorator.java?rev=1032485&r1=1032484&r2=1032485&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Decorator.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Decorator.java Mon Nov
 8 08:22:17 2010
@@ -20,11 +20,9 @@
  */
 package org.apache.derbyTesting.junit;
 
-import java.sql.SQLException;
+import java.util.Properties;
 import java.util.Random;
 
-import javax.sql.DataSource;
-
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
@@ -43,7 +41,7 @@ public class Decorator {
     /**
      * Decorate a set of tests to use an encrypted
      * single use database. This is to run tests
-     * using encrpyption as a general test and
+     * using encryption as a general test and
      * not specific tests of how encryption is handled.
      * E.g. tests of setting various URL attributes
      * would be handled in a specific test.
@@ -53,8 +51,6 @@ public class Decorator {
      * <BR>
      * A boot password (phrase) is used with a random
      * set of characters and digits 16 characters long.
-     * <BR>
-     * The database is created during the setUp of the decorator.
      * 
      * @param test test to decorate
      * @return decorated tests
@@ -63,36 +59,18 @@ public class Decorator {
     {
         if (JDBC.vmSupportsJSR169())
             return new TestSuite("no encryption support");
-        
-        test = new BaseTestSetup(test) {
-            
-            /**
-             * Create an encrypted database using a
-             * JDBC data source.
-             */
-            protected void setUp() throws SQLException
-            {
-                String bootPhrase = getBootPhrase(16);
 
-                DataSource ds = JDBCDataSource.getDataSource();
-                               
-                JDBCDataSource.setBeanProperty(ds,
-                        "createDatabase", "create");
-                JDBCDataSource.setBeanProperty(ds,
-                        "connectionAttributes",
-                        "dataEncryption=true;bootPassword=" + bootPhrase);
-                
-                ds.getConnection().close();
-            }
-        };
-        
-        return TestConfiguration.singleUseDatabaseDecorator(test);
+        Properties attributes = new Properties();
+        attributes.setProperty("dataEncryption", "true");
+        attributes.setProperty("bootPassword", getBootPhrase(16));
+
+        return attributesDatabase(attributes, test);
     }
     
     /**
      * Decorate a set of tests to use an encrypted
      * single use database. This is to run tests
-     * using encrpyption as a general test and
+     * using encryption as a general test and
      * not specific tests of how encryption is handled.
      * E.g. tests of setting various URL attributes
      * would be handled in a specific test.
@@ -102,23 +80,18 @@ public class Decorator {
      * <BR>
      * A boot password (phrase) is used with a random
      * set of characters and digits 64 characters long.
-     * <BR>
-     * The database is created during the setUp of the decorator.
-
      * 
      * @param test test to decorate
      * @return decorated tests
      */
     public static Test encryptedDatabase(Test test, final String algorithm)
     {
-        String bootPhrase = getBootPhrase(64);
-        
-        String attributes =
-            "dataEncryption=true;bootPassword=" + bootPhrase +
-             ";encryptionAlgorithm=" + algorithm;
+        Properties attributes = new Properties();
+        attributes.setProperty("dataEncryption", "true");
+        attributes.setProperty("bootPassword", getBootPhrase(64));
+        attributes.setProperty("encryptionAlgorithm", algorithm);
 
         return attributesDatabase(attributes, test);
-        
     }
     
     private static String getBootPhrase(int length)
@@ -140,18 +113,17 @@ public class Decorator {
     /**
      * Decorate a set of tests to use an single
      * use database with TERRITORY_BASED collation
-     * set to the passed in locale. Database is created
-     * by the setUp method of the decorator.
+     * set to the passed in locale.
      * @param locale Locale used to set territory JDBC attribute. If null
      * then only collation=TERRITORY_BASED will be set.
      */
     public static Test territoryCollatedDatabase(Test test, final String locale)
     {
-        
-        String attributes = "collation=TERRITORY_BASED";
+        Properties attributes = new Properties();
+        attributes.setProperty("collation", "TERRITORY_BASED");
         
         if (locale != null)
-            attributes = attributes.concat(";territory=" + locale);
+            attributes.setProperty("territory", locale);
         
         return attributesDatabase(attributes, test);
     }
@@ -159,53 +131,36 @@ public class Decorator {
     /**
      * Decorate a set of tests to use an single
      * use database with TERRITORY_BASED:SECONDARY collation
-     * set to the passed in locale. Database is created
-     * by the setUp method of the decorator.
+     * set to the passed in locale.
      * @param locale Locale used to set territory JDBC attribute. If null
      * then only collation=TERRITORY_BASED:SECONDARY will be set.
      */
     public static Test territoryCollatedCaseInsensitiveDatabase(Test test, final String locale)
     {
-
-        String attributes = "collation=TERRITORY_BASED:SECONDARY";
+        Properties attributes = new Properties();
+        attributes.setProperty("collation", "TERRITORY_BASED:SECONDARY");
 
         if (locale != null)
-            attributes = attributes.concat(";territory=" + locale);
+            attributes.setProperty("territory", locale);
 
         return attributesDatabase(attributes, test);
     }
 
     /**
      * Decorate a test (or suite of tests) to use a single use database
-     * as the default database. The database is created by the setUp
-     * method of the decorator. The database will be created using
-     * a JDBC data source with createDatabase set to create and
-     * connectionAttributes set to the passed in attributes.
+     * as the default database with a specified set connection attributes.
      * 
-     * 
-     * @param attributes Value to set connectionAttributes to when creating
-     * the database.
+     * @param attributes properties to set in the connection URL or in the
+     * connectionAttributes of a data source when connecting to the database
      * @param test Test to decorate
      * @return Decorated test
      */
-    private static Test attributesDatabase(final String attributes, Test test)
+    private static Test attributesDatabase(
+            final Properties attributes, Test test)
     {
-        test = new BaseTestSetup(test) {
-            
-            /**
-             * Create a  database
-             * using a JDBC data source with connectionAttributes set.
-             */
-            protected void setUp() throws SQLException
-            {
-                DataSource ds = JDBCDataSource.getDataSource();
-                               
-                JDBCDataSource.setBeanProperty(ds,
-                        "createDatabase", "create");
-                JDBCDataSource.setBeanProperty(ds,
-                        "connectionAttributes", attributes);
-                                
-                ds.getConnection().close();
+        test = new ChangeConfigurationSetup(test) {
+            TestConfiguration getNewConfiguration(TestConfiguration old) {
+                return old.addConnectionAttributes(attributes);
             }
         };
         

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java?rev=1032485&r1=1032484&r2=1032485&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
Mon Nov  8 08:22:17 2010
@@ -72,8 +72,13 @@ public class DriverManagerConnector impl
             loadJDBCDriver();
         }
 
+        Properties connectionAttributes =
+                new Properties(config.getConnectionAttributes());
+        connectionAttributes.setProperty("user", user);
+        connectionAttributes.setProperty("password", password);
+
         try {
-            return DriverManager.getConnection(url, user, password);
+            return DriverManager.getConnection(url, connectionAttributes);
         } catch (SQLException e) {
             
             // Expected state for database not found.
@@ -88,9 +93,7 @@ public class DriverManagerConnector impl
             if (!expectedState.equals(e.getSQLState()))
                 throw e;
             
-            Properties attributes = new Properties();
-            attributes.setProperty("user", user);
-            attributes.setProperty("password", password);
+            Properties attributes = new Properties(connectionAttributes);
             attributes.setProperty("create", "true");
             return DriverManager.getConnection(url, attributes);
         }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java?rev=1032485&r1=1032484&r2=1032485&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java Mon
Nov  8 08:22:17 2010
@@ -126,7 +126,11 @@ public class JDBCDataSource {
         beanProperties.put("user", config.getUserName());
         beanProperties.put("password", config.getUserPassword());
 
-        
+        String attributes = config.getConnectionAttributesString();
+        if (attributes != null) {
+            beanProperties.put("connectionAttributes", attributes);
+        }
+
         return beanProperties;
     }
     

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?rev=1032485&r1=1032484&r2=1032485&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Mon Nov  8 08:22:17 2010
@@ -28,6 +28,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Properties;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.Hashtable;
 
 import junit.extensions.TestSetup;
@@ -1037,6 +1038,7 @@ public final class TestConfiguration {
         logicalDbMapping.putAll(copy.logicalDbMapping);
         this.userName = copy.userName;
         this.userPassword = copy.userPassword;
+        this.connectionAttributes = new Properties(copy.connectionAttributes);
 
         this.isVerbose = copy.isVerbose;
         this.doTrace = copy.doTrace;
@@ -1060,6 +1062,7 @@ public final class TestConfiguration {
         logicalDbMapping.putAll(copy.logicalDbMapping);
         this.userName = copy.userName;
         this.userPassword = copy.userPassword;
+        this.connectionAttributes = new Properties(copy.connectionAttributes);
 
         this.isVerbose = copy.isVerbose;
         this.doTrace = copy.doTrace;
@@ -1087,6 +1090,7 @@ public final class TestConfiguration {
         logicalDbMapping.putAll(copy.logicalDbMapping);
         this.userName = copy.userName;
         this.userPassword = copy.userPassword;
+        this.connectionAttributes = new Properties(copy.connectionAttributes);
 
         this.isVerbose = copy.isVerbose;
         this.doTrace = copy.doTrace;
@@ -1123,6 +1127,7 @@ public final class TestConfiguration {
         this.userPassword = password;
         this.passwordToken = passwordToken == null ?
                 copy.passwordToken : passwordToken;
+        this.connectionAttributes = new Properties(copy.connectionAttributes);
 
         this.isVerbose = copy.isVerbose;
         this.doTrace = copy.doTrace;
@@ -1186,6 +1191,7 @@ public final class TestConfiguration {
         
         this.userName = copy.userName;
         this.userPassword = copy.userPassword;
+        this.connectionAttributes = new Properties(copy.connectionAttributes);
 
         this.isVerbose = copy.isVerbose;
         this.doTrace = copy.doTrace;
@@ -1239,6 +1245,25 @@ public final class TestConfiguration {
     }
 
     /**
+     * Create a copy of this configuration with some additional connection
+     * attributes.
+     *
+     * @param attrs the extra connection attributes
+     * @return a copy of the configuration with extra attributes
+     */
+    TestConfiguration addConnectionAttributes(Properties attrs) {
+        TestConfiguration copy = new TestConfiguration(this);
+        Enumeration e = attrs.propertyNames();
+        while (e.hasMoreElements()) {
+            String key = (String) e.nextElement();
+            String val = attrs.getProperty(key);
+            copy.connectionAttributes.setProperty(key, val);
+        }
+        copy.initConnector(connector);
+        return copy;
+    }
+
+    /**
      * Get the system properties in a privileged block.
      *
      * @return the system properties.
@@ -1321,7 +1346,15 @@ public final class TestConfiguration {
     
     
     /**
+     * <p>
      * Return the jdbc url for connecting to the default database.
+     * </p>
+     *
+     * <p>
+     * The returned URL does not include the connection attributes. These must
+     * either be appended to the URL when connecting, or they must be passed
+     * as a {@code Properties} object to {@code DriverManager.getConnection()}.
+     * </p>
      *
      * @return JDBC url.
      */
@@ -1330,6 +1363,21 @@ public final class TestConfiguration {
     }
 
     /**
+     * Return the JDBC URL for connecting to the default database, including
+     * any connection attributes.
+     *
+     * @return JDBC URL with connection attributes
+     */
+    public String getJDBCUrlWithAttributes() {
+        String attrs = getConnectionAttributesString();
+        if (attrs == null) {
+            return url;
+        } else {
+            return url + ';' + attrs;
+        }
+    }
+
+    /**
      * Return the jdbc url for a connecting to the database.
      * 
      * @param databaseName name of database.
@@ -1377,6 +1425,52 @@ public final class TestConfiguration {
     }
 
     /**
+     * Return the connection attributes to use in this configuration. The
+     * attributes won't contain user name or password. Use
+     * {@link #getUserName()} or {@link #getUserPassword()} instead to
+     * retrieve those attributes.
+     *
+     * @return connection attributes (can be {@code null})
+     */
+    Properties getConnectionAttributes() {
+        return connectionAttributes;
+    }
+
+    /**
+     * Get a flat string representation of the connection attributes. To
+     * be used in the connectionAttributes property of a data source.
+     *
+     * @return all connection attributes concatenated ({@code null} if there
+     * are no attributes)
+     */
+    String getConnectionAttributesString() {
+        if (connectionAttributes == null) {
+            return null;
+        }
+
+        StringBuffer sb = new StringBuffer();
+        Enumeration e = connectionAttributes.propertyNames();
+        boolean first = true;
+        while (e.hasMoreElements()) {
+            if (!first) {
+                sb.append(';');
+            }
+            String key = (String) e.nextElement();
+            sb.append(key);
+            sb.append('=');
+            sb.append(connectionAttributes.getProperty(key));
+            first = false;
+        }
+
+        if (first) {
+            // No connection attributes.
+            return null;
+        }
+
+        return sb.toString();
+    }
+
+    /**
      * Return the host name for the network server.
      *
      * @return host name.
@@ -1743,7 +1837,13 @@ public final class TestConfiguration {
     private boolean isVerbose;
     private boolean doTrace;
     private String ssl;
-    
+
+    /**
+     * Extra connection attributes. Not for user name and password, use the
+     * fields {@link #userName} and {@link #userPassword} for those attributes.
+     */
+    private Properties connectionAttributes;
+
     /**
      * Password token used by the builtin authentication decorators.
      * Default simple scheme is the password is a function



Mime
View raw message