db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r502358 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit: BaseJDBCTestCase.java ChangeUserSetup.java DatabasePropertyTestSetup.java TestConfiguration.java
Date Thu, 01 Feb 2007 21:08:56 GMT
Author: djd
Date: Thu Feb  1 13:08:54 2007
New Revision: 502358

URL: http://svn.apache.org/viewvc?view=rev&rev=502358
Log:
Add some utility decorators/code for the Junit infrastructure to enable easy use
of the default database with SQL authorization and a set of authenticated users. 

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeUserSetup.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?view=diff&rev=502358&r1=502357&r2=502358
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java Thu
Feb  1 13:08:54 2007
@@ -198,6 +198,48 @@
     }
     
     /**
+     * Open a connection to the current default database using the
+     * specified user name and password.
+     * <BR>
+     * This connection is not
+     * automaticaly closed on tearDown, the text fixture must
+     * ensure the connection is closed.
+     */
+    public Connection openDefaultConnection(String user, String password)
+    throws SQLException
+    {
+        Connection conn =  getTestConfiguration().openDefaultConnection(user,
+                password);
+        initializeConnection(conn);
+        return conn;        
+    }
+    
+    /**
+     * Open a connection to the current default database using the
+     * specified user name. The password is a function of
+     * the user name and the password token setup by the
+     * builtin authentication decorators.
+     * <BR>
+     * If the fixture is not wrapped in one of the decorators
+     * that setup BUILTIN authentication then the password
+     * is a function of the user name and the empty string
+     * as the password token. This mode is not recommended.
+     * 
+     * <BR>
+     * This connection is not
+     * automaticaly closed on tearDown, the text fixture must
+     * ensure the connection is closed.
+     * 
+     * @see DatabasePropertyTestSetup#builtinAuthentication(Test, String[], String)
+     * @see TestConfiguration#sqlAuthorizationDecorator(Test, String[], String)
+     */
+    public Connection openUserConnection(String user) throws SQLException
+    {
+        return openDefaultConnection(user,
+                getTestConfiguration().getPassword(user));
+    }
+    
+    /**
      * Open a connection to the specified database.
      * If the database does not exist, it will be created.
      * A default username and password will be used for the connection.

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeUserSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeUserSetup.java?view=diff&rev=502358&r1=502357&r2=502358
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeUserSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ChangeUserSetup.java Thu
Feb  1 13:08:54 2007
@@ -19,29 +19,36 @@
  */
 package org.apache.derbyTesting.junit;
 
-import junit.extensions.TestSetup;
 import junit.framework.Test;
 
 /**
  * A decorator that changes the default user and password
  * for the current configuration. Its tearDown method restores
  * the previous configuration.
+ * Optionally the passwordToken can be changed as well. 
  * 
  */
 final class ChangeUserSetup extends ChangeConfigurationSetup {
     
     private final String user;
     private final String password;
-    
-    ChangeUserSetup(Test test, String user, String password)
+    private final String passwordToken;
+
+    ChangeUserSetup(Test test, String user, String password, String passwordToken)
     {
         super(test);
         this.user = user;
         this.password = password;
+        this.passwordToken = passwordToken;
+    }
+    
+    ChangeUserSetup(Test test, String user, String password)
+    {
+        this(test, user, password, null);
     }
     
     TestConfiguration getNewConfiguration(TestConfiguration old)
     {
-        return new TestConfiguration(old, user, password);
+        return new TestConfiguration(old, user, password, passwordToken);
     }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java?view=diff&rev=502358&r1=502357&r2=502358
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
Thu Feb  1 13:08:54 2007
@@ -79,9 +79,14 @@
      * using the BUILTIN provider and the set of users passed in.
      * The password for each user is set to the user's name with 
      * the value of passwordToken appended.
+     * <BR>
+     * The decorated test can use BaseJDBCTestCase.openUserConnection(String user)
+     * method to simplify using authentication.
      * <P>
      * Assumption is that no authentication was enabled upon entry.
      * <P>
+     * Current user is set to the first user in the list users[0].
+     * <P>
      * The authentication is removed by the decorator's tearDown method.
      * @param test Test to be decorated.
      * @param users Set of users for authentication.
@@ -99,7 +104,8 @@
         for (int i = 0; i < users.length; i++)
         {
             String user = users[i];
-            userProps.setProperty("derby.user." + user, user.concat(passwordToken));
+            userProps.setProperty("derby.user." + user,
+                    TestConfiguration.getPassword(user, passwordToken));
         }
         
         // Need to setup the decorators carefully.
@@ -118,7 +124,9 @@
         // outer (added last) to inner.
         
         test = new DatabasePropertyTestSetup(test, authProps, true);
-        test = new ChangeUserSetup(test, users[0], users[0].concat(passwordToken));
+        test = new ChangeUserSetup(test, users[0],
+                TestConfiguration.getPassword(users[0], passwordToken),
+                passwordToken);
         test = new DatabasePropertyTestSetup(test, userProps, false);
         
         return test;

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?view=diff&rev=502358&r1=502357&r2=502358
==============================================================================
--- 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
Thu Feb  1 13:08:54 2007
@@ -343,10 +343,24 @@
      * The tearDown reverts the configuration to the previous
      * configuration.
      * 
+     * The database owner of this default SQL authorization mode
+     * database is TEST_DBO. This decorator sets the default user
+     * to be TEST_DBO.
+     * 
      * Tests can use this in conjunction with
      * DatabasePropertyTestSetup.builtinAuthentication
      * to set up BUILTIN authentication and changeUserDecorator
-     * to switch users.
+     * to switch users. The database owner TEST_DBO must be included
+     * in the list of users provided to builtinAuthentication.
+     * This decorator must be the outer one in this mode.
+     * <code>
+     * test = DatabasePropertyTestSetup.builtinAuthentication(test,
+                new String[] {"TEST_DBO","U1","U2",},
+                "nh32ew");
+       test = TestConfiguration.sqlAuthorizationDecorator(test);
+     * </code>
+     * A utility version of sqlAuthorizationDecorator is provided
+     * that combines the two setups.
      * 
      * @param test Test to be decorated
      * @return decorated test.
@@ -365,8 +379,37 @@
             protected void tearDown() {
             }
         };
-
-        return new DatabaseChangeSetup(setSQLAuthMode, DEFAULT_DBNAME_SQL, true);
+        
+        return changeUserDecorator(
+            new DatabaseChangeSetup(setSQLAuthMode, DEFAULT_DBNAME_SQL, true),
+            "TEST_DBO", "");
+    }
+    
+    /**
+     * Utility version of sqlAuthorizationDecorator that also sets
+     * up authentication. A combination of
+     * DatabasePropertyTestSetup.builtinAuthentication wrapped in
+     * sqlAuthorizationDecorator.
+     * <BR>
+     * The database owner of this default SQL authorization mode
+     * database is TEST_DBO. This decorator sets the default user
+     * to be TEST_DBO.
+     * <BR>
+     * Assumption is that no authentication is enabled on the default
+     * SQL authorization database on entry.
+     * 
+     * @param users Set of users excluding the database owner, that will
+     * be added by this decorator.
+     */
+    public static Test sqlAuthorizationDecorator(Test test,
+            String[] users, String passwordToken)
+    {
+        String[] usersWithDBO = new String[users.length + 1];
+        usersWithDBO[0] = "TEST_DBO";
+        System.arraycopy(users, 0, usersWithDBO, 1, users.length);
+        return sqlAuthorizationDecorator(
+            DatabasePropertyTestSetup.builtinAuthentication(test, 
+                    usersWithDBO, passwordToken));
     }
     
     /**
@@ -461,12 +504,15 @@
      * @param user New default user
      * @param password New default password.
      */
-    TestConfiguration(TestConfiguration copy, String user, String password)
+    TestConfiguration(TestConfiguration copy, String user,
+            String password, String passwordToken)
     {
         this.defaultDbName = copy.defaultDbName;
         this.usedDbNames.addAll(copy.usedDbNames);
         this.userName = user;
         this.userPassword = password;
+        this.passwordToken = passwordToken == null ?
+                copy.passwordToken : passwordToken;
 
         this.isVerbose = copy.isVerbose;
         this.port = copy.port;
@@ -719,6 +765,16 @@
     }
     
     /**
+     * Open connection to the default database.
+     * If the database does not exist, it will be created.
+     *
+     * @return connection to default database.
+     */
+    Connection openDefaultConnection(String user, String password)
+        throws SQLException {
+        return connector.openConnection(user, password);
+    }    
+    /**
      * Open connection to the specified database.
      * If the database does not exist, it will be created.
      * A default username and password will be used for the connection.
@@ -877,6 +933,14 @@
     private boolean isVerbose;
     
     /**
+     * Password token used by the builtin authentication decorators.
+     * Default simple scheme is the password is a function
+     * of the user and a password token. password token
+     * is set by DatabasePropertyTestSetup.builtinAuthentication
+     */
+    private String passwordToken = "";
+    
+    /**
      * Indirection for obtaining connections based upon
      * this configuration.
      */
@@ -911,5 +975,26 @@
     	}
     }
     
-        
+    
+    /*
+    ** BUILTIN password handling.
+    */
+    
+    /**
+     * Get the password that is a function of the user
+     * name and the passed in token.
+     */
+    static final String getPassword(String user, String token)
+    {
+        return user.concat(token);
+    }
+    
+    /**
+     * Get the password that is a function of the user
+     * name and the token for the current configuration.
+     */
+    final String getPassword(String user)
+    {
+        return getPassword(user, passwordToken);
+    }
 }



Mime
View raw message