You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by eb...@apache.org on 2004/09/21 19:18:27 UTC

cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestConfigurationFactory.java TestJNDIConfiguration.java

ebourg      2004/09/21 10:18:27

  Modified:    configuration/xdocs changes.xml
               configuration/src/java/org/apache/commons/configuration
                        JNDIConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestConfigurationFactory.java
                        TestJNDIConfiguration.java
  Log:
  The context used by JNDIConfiguration can be specified in its constructor or through the setContext() method.
  The context can be accessed with the getContext() method which is now public.
  Changing the prefix of a JNDIConfiguration will now reset the base context used (bug 31345)
  
  Revision  Changes    Path
  1.44      +18 -8     jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- changes.xml	19 Sep 2004 22:01:50 -0000	1.43
  +++ changes.xml	21 Sep 2004 17:18:27 -0000	1.44
  @@ -7,15 +7,24 @@
   
     <body>
       <release version="1.0-rc2" date="in CVS">
  +      <action dev="ebourg" type="fix" issue="31345">
  +        Changing the prefix of a JNDIConfiguration will now reset the base context used.
  +      </action>
  +      <action dev="ebourg" type="add" due-to="Eric Jung">
  +        The context used by JNDIConfiguration can be specified in its
  +        constructor or through the setContext() method. The context can be
  +        accessed with the getContext() method which is now public.
  +      </action>
         <action dev="henning" type="add">
  -        Make the behaviour on missing properties for the get methods that return objects
  -        configurable. A property <code>throwExceptionOnMissing</code> can be set and then
  -        the getters throw an <code>NoSuchElementException</code>. The old default behaviour
  -        of returning a <code>null</code> value has been restored.
  +        Make the behaviour on missing properties for the get methods that
  +        return objects configurable. A property <code>throwExceptionOnMissing</code>
  +        can be set and then the getters throw an <code>NoSuchElementException</code>.
  +        The old default behaviour of returning a <code>null</code> value has
  +        been restored.
         </action>         
         <action dev="epugh" type="add" issue="29714">
  -        Allow configurations extending AbstractConfiguration to change the delimiter
  -        used from "," to something else.
  +        Allow configurations extending AbstractConfiguration to change the
  +        delimiter used from "," to something else.
         </action>         
         <action dev="epugh" type="fix">
           PropertiesConfiguration.save() method has issues with preserving the filename
  @@ -24,8 +33,8 @@
           Test cases for HierarchicalConfigurationXMLReader stores comments as text nodes
         </action>         
         <action dev="epugh" type="fix" issue="30545" due-to="Ricardo Gladwell">
  -        Clarify for ConfigurationDynaBean that the get method should throw an illegalArgumentException
  -        if there is no property specified.
  +        Clarify for ConfigurationDynaBean that the get method should throw an
  +        illegalArgumentException if there is no property specified.
         </action>        
         <action dev="ebourg" type="fix" issue="30839">
           Fixed a ClassCastException when adding a non String property to an XMLConfiguration.
  @@ -44,6 +53,7 @@
           deprecated. These methods will be removed for 1.1.
         </action>
       </release>
  +
       <release version="1.0-rc1" date="2004-08-14">
         <action dev="epugh" type="add" issue="30597" due-to="Oliver Heger">
           HierarchicalConfigurationXMLReader stores comments as text nodes
  
  
  
  1.18      +88 -10    jakarta-commons/configuration/src/java/org/apache/commons/configuration/JNDIConfiguration.java
  
  Index: JNDIConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/JNDIConfiguration.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- JNDIConfiguration.java	8 Jul 2004 15:34:20 -0000	1.17
  +++ JNDIConfiguration.java	21 Sep 2004 17:18:27 -0000	1.18
  @@ -52,13 +52,64 @@
       /** The prefix of the context. */
       private String prefix;
   
  -    /** The JNDI context. */
  +    /** The initial JNDI context. */
       private Context context;
   
  +    /** The base JNDI context. */
  +    private Context baseContext;
  +
       /** The Set of keys that have been virtually cleared. */
       private Set clearedProperties = new HashSet();
   
       /**
  +     * Creates a JNDIConfiguration using the default initial context as the
  +     * root of the properties.
  +     *
  +     * @throws NamingException thrown if an error occurs when initializing the default context
  +     */
  +    public JNDIConfiguration() throws NamingException
  +    {
  +        this((String) null);
  +    }
  +
  +    /**
  +     * Creates a JNDIConfiguration using the default initial context, shifted
  +     * with the specified prefix, as the root of the properties.
  +     *
  +     * @param prefix
  +     *
  +     * @throws NamingException thrown if an error occurs when initializing the default context
  +     */
  +    public JNDIConfiguration(String prefix) throws NamingException
  +    {
  +        this(new InitialContext(), prefix);
  +    }
  +
  +    /**
  +     * Creates a JNDIConfiguration using the specified initial context as the
  +     * root of the properties.
  +     *
  +     * @param context the initial context
  +     */
  +    public JNDIConfiguration(Context context)
  +    {
  +        this(context, null);
  +    }
  +
  +    /**
  +     * Creates a JNDIConfiguration using the specified initial context shifted
  +     * by the specified prefix as the root of the properties.
  +     *
  +     * @param context the initial context
  +     * @param prefix
  +     */
  +    public JNDIConfiguration(Context context, String prefix)
  +    {
  +        this.context = context;
  +        this.prefix = prefix;
  +    }
  +
  +    /**
        * JNDIConfigurations can not be added to.
        *
        * @param key The Key to add the property to.
  @@ -138,7 +189,7 @@
           try
           {
               // find the context matching the specified path
  -            Context context = getContext(path, getContext());
  +            Context context = getContext(path, getBaseContext());
   
               // return all the keys under the context found
               Set keys = new HashSet();
  @@ -217,7 +268,7 @@
       {
           try
           {
  -            NamingEnumeration enumeration = getContext().list("");
  +            NamingEnumeration enumeration = getBaseContext().list("");
               return !enumeration.hasMore();
           }
           catch (NamingException ne)
  @@ -264,7 +315,7 @@
           try
           {
               // throws a NamingException if JNDI doesn't contain the key.
  -            getContext().lookup(key);
  +            getBaseContext().lookup(key);
               return true;
           }
           catch (NamingException ne)
  @@ -289,6 +340,9 @@
       public void setPrefix(String prefix)
       {
           this.prefix = prefix;
  +
  +        // clear the previous baseContext
  +        baseContext = null;
       }
   
       /**
  @@ -304,7 +358,7 @@
           try
           {
               key = StringUtils.replace(key, ".", "/");
  -            return getContext().lookup(key);
  +            return getBaseContext().lookup(key);
           }
           catch (NameNotFoundException e)
           {
  @@ -329,13 +383,37 @@
           throw new UnsupportedOperationException("This operation is not supported");
       }
   
  -    private Context getContext() throws NamingException
  +    /**
  +     * Return the base context with the prefix applied.
  +     */
  +    public Context getBaseContext() throws NamingException
       {
  -        if (context == null)
  +        if (baseContext == null)
           {
  -            Context initCtx = new InitialContext();
  -            context = (Context) initCtx.lookup(prefix == null ? "" : prefix);
  +            baseContext = (Context) getContext().lookup(prefix == null ? "" : prefix);
           }
  +
  +        return baseContext;
  +    }
  +
  +    /**
  +     * Return the initial context used by this configuration. This context is
  +     * independent of the prefix specified.
  +     */
  +    public Context getContext()
  +    {
           return context;
  +    }
  +
  +    /**
  +     * Set the initial context of the configuration.
  +     */
  +    public void setContext(Context context)
  +    {
  +        // forget the removed properties
  +        clearedProperties.clear();
  +
  +        // change the context
  +        this.context = context;
       }
   }
  
  
  
  1.15      +2 -2      jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
  
  Index: TestConfigurationFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationFactory.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- TestConfigurationFactory.java	20 Aug 2004 15:49:27 -0000	1.14
  +++ TestConfigurationFactory.java	21 Sep 2004 17:18:27 -0000	1.15
  @@ -59,7 +59,7 @@
           factory = new ConfigurationFactory();
       }
   
  -    public void testJNDI()
  +    public void testJNDI() throws Exception
       {
           JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
           Object o = jndiConfiguration.getProperty("test.boolean");
  
  
  
  1.9       +42 -1     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java
  
  Index: TestJNDIConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestJNDIConfiguration.java	8 Jul 2004 15:29:50 -0000	1.8
  +++ TestJNDIConfiguration.java	21 Sep 2004 17:18:27 -0000	1.9
  @@ -18,6 +18,8 @@
   
   import junit.framework.TestCase;
   
  +import javax.naming.InitialContext;
  +
   /**
    * Test to see if the JNDIConfiguration works properly.  Currently excluded
    * in the project.xml unitTest section as our JNDI provider doesn't
  @@ -118,6 +120,45 @@
   
           conf.clearProperty(key);
           assertFalse("'" + key + "' still found", conf.containsKey(key));
  +    }
  +
  +    public void testChangePrefix()
  +    {
  +        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
  +        assertEquals("'boolean' property", null, conf.getString("boolean"));
  +
  +        // change the prefix
  +        conf.setPrefix("test");
  +        assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
  +        assertEquals("'boolean' property", "true", conf.getString("boolean"));
  +    }
  +
  +    public void testResetRemovedProperties() throws Exception
  +    {
  +        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
  +
  +        // remove the property
  +        conf.clearProperty("test.boolean");
  +        assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
  +
  +        // change the context
  +        conf.setContext(new InitialContext());
  +
  +        // get the property
  +        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
  +    }
  +
  +    public void testConstructor() throws Exception
  +    {
  +        // test the constructor accepting a context
  +        conf = new JNDIConfiguration(new InitialContext());
  +
  +        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
  +
  +        // test the constructor accepting a context and a prefix
  +        conf = new JNDIConfiguration(new InitialContext(), "test");
  +
  +        assertEquals("'boolean' property", "true", conf.getString("boolean"));
       }
   
   }
  
  
  

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