You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ep...@apache.org on 2004/05/05 00:14:29 UTC

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

epugh       2004/05/04 15:14:29

  Modified:    configuration/src/test/org/apache/commons/configuration
                        TestConfigurationFactory.java
                        TestJNDIConfiguration.java
                        TestJNDIEnvironmentValues.java
                        TestConfigurationXMLDocument.java
  Added:       configuration/src/test/org/apache/commons/configuration
                        MockStaticMemoryInitialContextFactory.java
  Log:
  Fix JNDI unit tests to pass.
  
  Revision  Changes    Path
  1.10      +12 -3     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestConfigurationFactory.java	27 Feb 2004 17:41:34 -0000	1.9
  +++ TestConfigurationFactory.java	4 May 2004 22:14:29 -0000	1.10
  @@ -55,11 +55,14 @@
   
       public void setUp() throws Exception
       {
  +        System.setProperty("java.naming.factory.initial","org.apache.commons.configuration.MockStaticMemoryInitialContextFactory");
           configurationFactory = new ConfigurationFactory();
       }
   
       public void testJNDI()
       {
  +        
  +        
           JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
           jndiConfiguration.setPrefix("");
           Object o = jndiConfiguration.getProperty("test.boolean");
  @@ -235,8 +238,14 @@
               config.getProperty("tables.table(0).fields.field(2).name"));
           assertNotNull(config.getProperty("element2.subelement.subsubelement"));
           assertNotNull(config.getProperty("mail.account.user"));
  -        // Fails, because we don't seem to reach the underlying JNDIConfiguraiton        
  -        //assertNotNull(config.getProperty("test.boolean"));
  +                
  +        assertNotNull(config.getProperty("test.onlyinjndi"));
  +        assertTrue(config.getBoolean("test.onlyinjndi"));
  +        
  +        Configuration subset = config.subset("test");
  +        assertNotNull(subset.getProperty("onlyinjndi"));
  +        assertTrue(subset.getBoolean("onlyinjndi"));
  +        
       }
   
       private void checkUnionConfig() throws Exception
  
  
  
  1.7       +6 -29     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestJNDIConfiguration.java	28 Mar 2004 14:42:45 -0000	1.6
  +++ TestJNDIConfiguration.java	4 May 2004 22:14:29 -0000	1.7
  @@ -18,8 +18,6 @@
   
   import junit.framework.TestCase;
   
  -import java.util.Iterator;
  -
   /**
    * Test to see if the JNDIConfiguration works properly.  Currently excluded
    * in the project.xml unitTest section as our JNDI provider doesn't 
  @@ -35,8 +33,9 @@
       private NonStringTestHolder nonStringTestHolder;
   
       public void setUp() throws Exception {
  -        //InitialContext context = new InitialContext();
  -        //assertNotNull(context);
  +        
  +        System.setProperty("java.naming.factory.initial","org.apache.commons.configuration.MockStaticMemoryInitialContextFactory");
  +        
   
           conf = new JNDIConfiguration();
           conf.setPrefix("");
  @@ -111,32 +110,10 @@
           assertEquals("true", o.toString());
       }
   
  -    /**
  -     * Currently failing in that we don't get back any keys!
  -     * @throws Exception
  -     */
  -    public void testGetKeys() throws Exception {
  -
  -        boolean found = false;
  -        Iterator it = conf.getKeys();
   
  -        assertTrue("no key found", it.hasNext());
   
  -        while (it.hasNext() && !found) {
  -            found = "test.boolean".equals(it.next());
  -        }
  -
  -        assertTrue("'test.boolean' key not found", found);
  -    }
  +   
   
  -    public void testClearProperty() {
  -        assertNotNull("null boolean for the 'test.boolean' key", conf.getBoolean("test.boolean", null));
  -        conf.clearProperty("test.boolean");
  -        assertNull("'test.boolean' property not cleared", conf.getBoolean("test.boolean", null));
  -    }
  -
  -    public void testIsEmpty() {
  -        assertFalse("the configuration shouldn't be empty", conf.isEmpty());
  -    }
  + 
   
   }
  
  
  
  1.4       +41 -11    jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIEnvironmentValues.java
  
  Index: TestJNDIEnvironmentValues.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIEnvironmentValues.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestJNDIEnvironmentValues.java	27 Feb 2004 17:41:34 -0000	1.3
  +++ TestJNDIEnvironmentValues.java	4 May 2004 22:14:29 -0000	1.4
  @@ -16,29 +16,36 @@
    * limitations under the License.
    */
   
  +import java.util.Iterator;
   import java.util.NoSuchElementException;
   
   import junit.framework.TestCase;
   
   public class TestJNDIEnvironmentValues extends TestCase
   {
  +    private JNDIConfiguration conf = null;
       public TestJNDIEnvironmentValues(String testName)
       {
           super(testName);
       }
  +    
  +    public void setUp() throws Exception{
  +        System.setProperty("java.naming.factory.initial","org.apache.commons.configuration.MockStaticMemoryInitialContextFactory");
  +        
  +        conf = new JNDIConfiguration();
  +        conf.setPrefix("");
  +    }
   
       public void testSimpleGet() throws Exception
       {
  -        JNDIConfiguration conf = new JNDIConfiguration();
  -        conf.setPrefix("");
  +        
           String s = conf.getString("test.key");
           assertEquals("jndivalue", s);
       }
   
       public void testMoreGets() throws Exception
       {
  -        JNDIConfiguration conf = new JNDIConfiguration();
  -        conf.setPrefix("");
  +
           String s = conf.getString("test.key");
           assertEquals("jndivalue", s);
           assertEquals("jndivalue2", conf.getString("test.key2"));
  @@ -47,8 +54,6 @@
   
       public void testGetMissingKey() throws Exception
       {
  -        JNDIConfiguration conf = new JNDIConfiguration();
  -        conf.setPrefix("");
           try
           {
               conf.getString("test.imaginarykey");
  @@ -56,14 +61,12 @@
           }
           catch (NoSuchElementException nsee)
           {
  -
  +            assertTrue(nsee.getMessage(),nsee.getMessage().indexOf("test.imaginarykey")!=-1);
           }
   
       }
       public void testGetMissingKeyWithDefault() throws Exception
       {
  -        JNDIConfiguration conf = new JNDIConfiguration();
  -        conf.setPrefix("");
   
           String result = conf.getString("test.imaginarykey", "bob");
           assertEquals("bob", result);
  @@ -71,12 +74,39 @@
       }
       public void testContainsKey() throws Exception
       {
  -        JNDIConfiguration conf = new JNDIConfiguration();
  -        conf.setPrefix("");
   
           assertTrue(conf.containsKey("test.key"));
   
           assertTrue(!conf.containsKey("test.imaginerykey"));
   
  +    }
  +    
  +    public void testClearProperty() {
  +
  +        assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
  +        conf.clearProperty("test.short");
  +        assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
  +    }
  +    
  +    public void testIsEmpty() {
  +        assertFalse("the configuration shouldn't be empty", conf.isEmpty());
  +    }
  +    
  +    /**
  +     * Currently failing in that we don't get back any keys!
  +     * @throws Exception
  +     */
  +    public void testGetKeys() throws Exception {
  +
  +        boolean found = false;
  +        Iterator it = conf.getKeys();
  +
  +        assertTrue("no key found", it.hasNext());
  +
  +        while (it.hasNext() && !found) {
  +            found = "test.boolean".equals(it.next());
  +        }
  +
  +        assertTrue("'test.boolean' key not found", found);
       }
   }
  
  
  
  1.6       +2 -2      jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationXMLDocument.java
  
  Index: TestConfigurationXMLDocument.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationXMLDocument.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestConfigurationXMLDocument.java	8 Mar 2004 23:42:16 -0000	1.5
  +++ TestConfigurationXMLDocument.java	4 May 2004 22:14:29 -0000	1.6
  @@ -80,7 +80,7 @@
           ConfigurationXMLDocument doc = 
           new ConfigurationXMLDocument(config.subset("database"));
           reader = doc.createXMLReader();
  -        assertTrue(reader instanceof HierarchicalConfigurationXMLReader);
  +        assertTrue("Class of reader is " + reader.getClass().getName(),reader instanceof HierarchicalConfigurationXMLReader);
           
           try
           {
  
  
  
  1.1                  jakarta-commons/configuration/src/test/org/apache/commons/configuration/MockStaticMemoryInitialContextFactory.java
  
  Index: MockStaticMemoryInitialContextFactory.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.apache.commons.configuration;
  
  import java.util.Hashtable;
  
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.spi.InitialContextFactory;
  
  import org.codehaus.spice.jndikit.DefaultNameParser;
  import org.codehaus.spice.jndikit.DefaultNamespace;
  import org.codehaus.spice.jndikit.memory.MemoryContext;
  
  
  /**
   * Initial context factory for memorycontext. This factory will
   * retrieve the {@link MemoryContext} from a static variable.
   * Thus this factory will always return the same instance of
   * memory context.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $
   */
  public class MockStaticMemoryInitialContextFactory
      implements InitialContextFactory
  {
      private static final MemoryContext MEMORY_CONTEXT = createMemoryContext();
  
      public Context getInitialContext(final Hashtable environment)
          throws NamingException
      {
          return MEMORY_CONTEXT;
      }
  
      /**
       * Method to create the inital {@link MemoryContext}.
       *
       * @return the new {@link MemoryContext}.
       */
      private static final MemoryContext createMemoryContext()
      {
          final DefaultNamespace namespace =
              new DefaultNamespace(new DefaultNameParser());
          MemoryContext me = new MemoryContext(namespace, new Hashtable(), null);
          
          try
          {
  			Context testContext = me.createSubcontext("test");
              testContext.bind("key", "jndivalue");
              testContext.bind("key2","jndivalue2");
              testContext.bind("short","1");
              testContext.bind("boolean","true");
              testContext.bind("byte","10");
              testContext.bind("double","10.25");
              testContext.bind("float","20.25");
              testContext.bind("integer","10");
              testContext.bind("long","1000000");
              testContext.bind("onlyinjndi","true");
          }
          catch (NamingException ne)
          {
              throw new RuntimeException(ne);
          }
          return me;
      }
  }
  
  
  

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