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/08/12 18:29:38 UTC

cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration/beanutils TestConfigurationDynaBean.java

epugh       2004/08/12 09:29:38

  Modified:    configuration/xdocs changes.xml
  Added:       configuration/src/java/org/apache/commons/configuration/beanutils
                        ConfigurationDynaBean.java
                        ConfigurationDynaClass.java
               configuration/src/test/org/apache/commons/configuration/beanutils
                        TestConfigurationDynaBean.java
  Log:
  configurationDynaBean support
  
  Revision  Changes    Path
  1.30      +1 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- changes.xml	12 Aug 2004 16:06:01 -0000	1.29
  +++ changes.xml	12 Aug 2004 16:29:38 -0000	1.30
  @@ -7,6 +7,7 @@
   
     <body>
       <release version="1.0rc1" date="2004-06-??">
  +      <action dev="epugh" type="add" due-to="Ricardo Gladwell" issue="30545">new ConfigurationDynaBean </action>        
         <action dev="epugh" type="add" due-to="Ricardo Gladwell" issue="29611">new ConfigurationMap and ConfigurationSet</action>        
         <action dev="epugh" type="fix" due-to="Ricardo Gladwell" issue="30598">Problem adding property XMLConfiguration</action>        
         <action dev="epugh" type="remove">ConfigurationXMLDocument removed until post 1.0.</action>
  
  
  
  1.1                  jakarta-commons/configuration/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java
  
  Index: ConfigurationDynaBean.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.configuration.beanutils;
  
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.beanutils.DynaClass;
  import org.apache.commons.configuration.Configuration;
  import org.apache.commons.configuration.ConversionException;
  import org.apache.commons.configuration.ConfigurationMap;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * <p>The <tt>ConfigurationDynaBean</tt> dynamically reads and
   * writes configurations properties from a wrapped
   * configuration-collection
   * {@link org.apache.commons.configuration.Configuration}
   * instance. It also implements a {@link java.util.Map} interface
   * so that it can be used in JSP 2.0 Expression Language
   * expressions.</p>
   * 
   * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped
   * properties to the appropriate <code>Configuration</code> subset
   * using the
   * {@link org.apache.commons.configuration.Configuration#subset}
   * method. Similarly, indexed properties reference lists of
   * configuration properties using the
   * {@link org.apache.commons.configuration.Configuration#getList(String)}
   * method. Setting an indexed property always throws an exception.</p>
   * 
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   */
  public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean {
  
      private final static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
  
      Configuration configuration;
      ConfigurationMap map;
  
      public ConfigurationDynaBean(Configuration configuration) {
          super(configuration);
          if(log.isTraceEnabled()) log.trace("ConfigurationDynaBean("+configuration+")");
          this.configuration = configuration;
          map = new ConfigurationMap(configuration);
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.Object)
       */
      public void set(String name, Object value) {
          if(log.isTraceEnabled()) log.trace("set("+name+","+value+")");
          if(value == null) throw new NullPointerException("Error trying to set property to null.");
          if(value instanceof List) {
              List list = (List) value;
              Iterator iterator = list.iterator();
              while(iterator.hasNext())
                  configuration.addProperty(name,iterator.next());
          } else if(value instanceof int[]) {
              int[] array = (int[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Integer(array[i]));
          } else if(value instanceof boolean[]) {
              boolean[] array = (boolean[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Boolean(array[i]));
          } else if(value instanceof char[]) {
              char[] array = (char[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Character(array[i]));
          } else if(value instanceof byte[]) {
              byte[] array = (byte[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Byte(array[i]));
          } else if(value instanceof short[]) {
              short[] array = (short[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Short(array[i]));
          } else if(value instanceof int[]) {
              int[] array = (int[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Integer(array[i]));
          } else if(value instanceof long[]) {
              long[] array = (long[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Long(array[i]));
          } else if(value instanceof float[]) {
              float[] array = (float[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,new Float(array[i]));
          } else if(value instanceof double[]) {
              double[] array = (double[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name, new Double(array[i]));
          } else if(value instanceof Object[]) {
              Object[] array = (Object[]) value;
              for(int i = 0; i < array.length; i++)
                  configuration.addProperty(name,array[i]);
          } else
              configuration.setProperty(name, value);
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
       */
      public Object get(String name) {
          if(log.isTraceEnabled()) log.trace("get("+name+")");
          // get configuration property
          Object result = configuration.getProperty(name);
          if(result == null && name != null) {
              // otherwise attempt to create bean from configuration subset
              Configuration subset = configuration.subset(name);
              if(subset != null)
                  result = new ConfigurationDynaBean(configuration.subset(name));
          }
          if(log.isDebugEnabled()) log.debug(name+"=["+result+"]");
          if(result == null)
              throw new IllegalArgumentException
                  ("Property '" + name +"' does not exist.");
          return result;
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#contains(java.lang.String, java.lang.String)
       */
      public boolean contains(String name, String key) {
          Configuration subset = configuration.subset(name);
          if(subset == null)
              throw new IllegalArgumentException
                      ("Mapped property '" + name +"' does not exist.");
          return subset.containsKey(key);
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, int)
       */
      public Object get(String name, int index) {
          try {
              List list = configuration.getList(name);
              if(list.isEmpty())
                  throw new IllegalArgumentException
                      ("Indexed property '" + name +"' does not exist.");
              return list.get(index);
          } catch(ConversionException e) {
              throw new IllegalArgumentException("Property '" + name +"' is not indexed.");
          }
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, java.lang.String)
       */
      public Object get(String name, String key) {
          Configuration subset = configuration.subset(name);
          if(subset == null)
              throw new IllegalArgumentException
                      ("Mapped property '" + name +"' does not exist.");
          return subset.getProperty(key);
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#getDynaClass()
       */
      public DynaClass getDynaClass() {
          return new ConfigurationDynaClass(configuration);
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#remove(java.lang.String, java.lang.String)
       */
      public void remove(String name, String key) {
          Configuration subset = configuration.subset(name);
          if(subset == null)
              throw new IllegalArgumentException
                      ("Mapped property '" + name +"' does not exist.");
          subset.setProperty(key, null);
      }
  
      /**
       * Calling this method always throws a <code>IllegalArgumentException</code>.
       * You cannot set indexed properties for configurations.
       * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, int, java.lang.Object)
       */
      public void set(String name, int index, Object value) {
          try {
              List list = configuration.getList(name);
              if(list == null) 
                  throw new IllegalArgumentException
                      ("Property '" + name +"' does not exist.");
              list.set(index,value);
          } catch(ConversionException e) {
              throw new IllegalArgumentException
                  ("Property '" + name +"' is not indexed.");
          }
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.String, java.lang.Object)
       */
      public void set(String name, String key, Object value) {
          configuration.setProperty(name+"."+key, value);
      }
  
      /**
       * @return the configuration wrapped by this class.
       */
      public Configuration getConfiguration() {
         return configuration;
      }
  }
  
  
  
  1.1                  jakarta-commons/configuration/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaClass.java
  
  Index: ConfigurationDynaClass.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.configuration.beanutils;
  
  import java.util.Iterator;
  import java.util.ArrayList;
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.beanutils.DynaClass;
  import org.apache.commons.beanutils.DynaProperty;
  import org.apache.commons.configuration.Configuration;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * <p>The <tt>ConfigurationDynaClass</tt> dynamically determines
   * properties for a <code>ConfigurationDynaBean</code> from a wrapped
   * configuration-collection
   * {@link org.apache.commons.configuration.Configuration}
   * instance.</p>
   * 
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   */
  public class ConfigurationDynaClass implements DynaClass {
  
      private final static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
  
      Configuration configuration;
  
      /**
       * Construct an instance of a <code>ConfigurationDynaClass</code>
       * wrapping the specified <code>Configuration</code> instance.
       * @param configuration <code>Configuration</code> instance.
       */
      public ConfigurationDynaClass(Configuration configuration) {
          super();
          if(log.isTraceEnabled()) log.trace("ConfigurationDynaClass("+configuration+")");
          this.configuration = configuration;
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
       */
      public DynaProperty getDynaProperty(String name) {
          if(log.isTraceEnabled()) log.trace("getDynaProperty("+name+")");
          if(name == null)
              throw new IllegalArgumentException("No such property name=["+name+"]");
          Object value = configuration.getProperty(name);
          DynaProperty property;
          if(value == null)
              return null;
          else {
              Class type = value.getClass();
  
              if(type == Byte.class) {
                  type = Byte.TYPE;
              } if(type == Character.class) {
                  type = Character.TYPE;
              } else if(type == Boolean.class) {
                  type = Boolean.TYPE;
              } else if (type == Double.class) {
                  type = Double.TYPE;
              } else if (type == Float.class) {
                  type = Float.TYPE;
              } else if (type == Integer.class) {
                  type = Integer.TYPE;
              } else if (type == Long.class) {
                  type = Long.TYPE;
              } else if (type == Short.class) {
                  type = Short.TYPE;
              }
  
              return new DynaProperty(name,type);
          }
      }
  
      /**
       * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
       */
      public DynaProperty[] getDynaProperties() {
          if(log.isTraceEnabled()) log.trace("getDynaProperties()");
          Iterator keys = configuration.getKeys();
          ArrayList properties = new ArrayList();
          while(keys.hasNext()) {
              String key = (String) keys.next();
              DynaProperty property = getDynaProperty(key);
              properties.add(property);
          }
          DynaProperty[] propertyArray = new DynaProperty[properties.size()];
          properties.toArray(propertyArray);
          if(log.isDebugEnabled()) log.debug("Found "+properties.size()+" properties.");
          return propertyArray; 
      }
  
     /**
      * @see org.apache.commons.beanutils.DynaClass#getName()
      */
     public String getName() {
         return ConfigurationDynaBean.class.getName();
     }
  
     /**
      * @see org.apache.commons.beanutils.DynaClass#newInstance()
      */
     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
         return new ConfigurationDynaBean(configuration);
     }
  
  }
  
  
  
  1.1                  jakarta-commons/configuration/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java
  
  Index: TestConfigurationDynaBean.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */ 
  
  package org.apache.commons.configuration.beanutils;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import junit.framework.AssertionFailedError;
  import junit.framework.TestCase;
  
  import org.apache.commons.beanutils.DynaProperty;
  import org.apache.commons.configuration.BaseConfiguration;
  
  /**
   * <p>Test Case for the <code>ConfigurationDynaBean</code> implementation class.
   * These tests were based on the ones in <code>BasicDynaBeanTestCase</code>
   * because the two classes provide similar levels of functionality.</p>
   *
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   * @version $Revision: 1.1 $
   */
  public class TestConfigurationDynaBean extends TestCase {
  
      /**
       * The basic test bean for each test.
       */
      protected ConfigurationDynaBean bean = null;
  
      /**
       * The set of property names we expect to have returned when calling
       * <code>getDynaProperties()</code>.  You should update this list
       * when new properties are added to TestBean.
       */
      String[] properties = {
              "booleanProperty",
              "booleanSecond",
              "doubleProperty",
              "floatProperty",
              "intProperty",
              "longProperty",
              "mappedProperty.key1",
              "mappedProperty.key2",
              "mappedProperty.key3",
              "mappedIntProperty.key1",
              "shortProperty",
              "stringProperty"
      };
      
      Object[] values = {
              Boolean.TRUE,
              Boolean.TRUE,
              new Double(Double.MAX_VALUE),
              new Float(Float.MAX_VALUE),
              new Integer(Integer.MAX_VALUE),
              new Long(Long.MAX_VALUE),
              "First Value",
              "Second Value",
              "Third Value",
              new Integer(Integer.MAX_VALUE),
              new Short(Short.MAX_VALUE),
              "This is a string"
      };
  
      int[] intArray = { 0, 10, 20, 30, 40 };
      String[] stringArray = { "String 0", "String 1", "String 2", "String 3", "String 4" };
  
      /**
       * Construct a new instance of this test case.
       * @param name Name of the test case
       */
      public TestConfigurationDynaBean(String name) {
          super(name);
      }
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() throws Exception {
          BaseConfiguration configuration = new BaseConfiguration();
  
          for(int i = 0; i < properties.length ; i++)
              configuration.setProperty(properties[i], values[i]);
  
          for(int a = 0; a < intArray.length ; a++)
              configuration.addProperty("intIndexed",new Integer(intArray[a]));
  
          for(int a = 0; a < stringArray.length ; a++)
              configuration.addProperty("stringIndexed",stringArray[a]);
  
          List list = new ArrayList();
          for(int i = 0 ; i < stringArray.length ; i++)
              list.add(stringArray[i]);
          configuration.addProperty("listIndexed", list);
  
          bean = new ConfigurationDynaBean(configuration);
  
          bean.set("intArray", intArray);
          bean.set("stringArray", stringArray);
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
          bean = null;
      }
  
      /**
       * Corner cases on getDynaProperty invalid arguments.
       */
      public void testGetDescriptorArguments() {
  
          try {
              DynaProperty descriptor =
                      bean.getDynaClass().getDynaProperty("unknown");
              assertNull("Unknown property descriptor should be null",
                      descriptor);
          } catch (Throwable t) {
              fail("Threw " + t + " instead of returning null");
          }
  
          try {
              bean.getDynaClass().getDynaProperty(null);
              fail("Should throw IllegalArgumentException");
          } catch (java.lang.IllegalArgumentException e) {
              ; // Expected response
          } catch(AssertionFailedError e) {
              ; // ignore other failed responses
          } catch(Throwable t) {
              fail("Threw '" + t + "' instead of 'IllegalArgumentException'");
          }
      }
  
      /**
       * Positive getDynaProperty on property <code>booleanProperty</code>.
       */
      public void testGetDescriptorBoolean() {
          testGetDescriptorBase("booleanProperty", Boolean.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>doubleProperty</code>.
       */
      public void testGetDescriptorDouble() {
          testGetDescriptorBase("doubleProperty", Double.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>floatProperty</code>.
       */
      public void testGetDescriptorFloat() {
          testGetDescriptorBase("floatProperty", Float.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>intProperty</code>.
       */
      public void testGetDescriptorInt() {
          testGetDescriptorBase("intProperty", Integer.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>longProperty</code>.
       */
      public void testGetDescriptorLong() {
          testGetDescriptorBase("longProperty", Long.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>booleanSecond</code>
       * that uses an "is" method as the getter.
       */
      public void testGetDescriptorSecond() {
          testGetDescriptorBase("booleanSecond", Boolean.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>shortProperty</code>.
       */
      public void testGetDescriptorShort() {
          testGetDescriptorBase("shortProperty", Short.TYPE);
      }
  
      /**
       * Positive getDynaProperty on property <code>stringProperty</code>.
       */
      public void testGetDescriptorString() {
          testGetDescriptorBase("stringProperty", String.class);
      }
  
      /**
       * Positive test for getDynaPropertys().  Each property name
       * listed in <code>properties</code> should be returned exactly once.
       */
      public void testGetDescriptors() {
          DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
          assertNotNull("Got descriptors", pd);
          int count[] = new int[properties.length];
          for (int i = 0; i < pd.length; i++) {
              String name = pd[i].getName();
              for (int j = 0; j < properties.length; j++) {
                  if (name.equals(properties[j]))
                      count[j]++;
              }
          }
          for (int j = 0; j < properties.length; j++) {
              if (count[j] < 0)
                  fail("Missing property " + properties[j]);
              else if (count[j] > 1)
                  fail("Duplicate property " + properties[j]);
          }
      }
  
      /**
       * Corner cases on getIndexedProperty invalid arguments.
       */
      public void testGetIndexedArguments() {
          try {
              bean.get("intArray", -1);
          } catch (IndexOutOfBoundsException e) {
              return; // Expected response
          } catch (Throwable t) {
              fail("Threw '" + t + "' instead of 'IndexOutOfBoundsException'");
              return;
          }
          fail("Should throw IndexOutOfBoundsException");
      }
  
      /**
       * Positive and negative tests on getIndexedProperty valid arguments.
       */
      public void testGetIndexedValues() {
          Object value = null;
          for (int i = 0; i < 5; i++) {
  
              try {
                  value = bean.get("intArray", i);
              } catch (Throwable t) {
                  fail("intArray " + i + " threw " + t);
              }
  
              assertNotNull("intArray index " + i + " did not return value.", value);
              assertTrue("intArray index " + i + " did not return Integer.", (value instanceof Integer));
              assertEquals("intArray " + i + " returned incorrect value.", i * 10, ((Integer) value).intValue());
  
              try {
                  value = bean.get("intIndexed", i);
              } catch (Throwable t) {
                  fail("intIndexed index " + i + " threw " + t);
              }
  
              assertNotNull("intIndexed index " + i + "returned value " + i, value);
              assertTrue("intIndexed index " + i + "returned Integer " + i,
                      value instanceof Integer);
              assertEquals("intIndexed index " + i + "returned correct " + i, i * 10,
                      ((Integer) value).intValue());
  
              try {
                  value = bean.get("listIndexed", i);
              } catch (Throwable t) {
                  fail("listIndexed index " + i + " threw " + t);
              }
  
              assertNotNull("listIndexed index " + i + "returned value " + i, value);
              assertTrue("list index " + i + "returned String " + i,
                      value instanceof String);
              assertEquals("listIndexed index " + i + "returned correct " + i,
                      "String " + i, (String) value);
  
              try {
                  value = bean.get("stringArray", i);
              } catch (Throwable t) {
                  fail("stringArray index " + i + " threw " + t);
              }
  
              assertNotNull("stringArray index " + i + " returnde null.", value);
              assertFalse("stringArray index " + i + " returned array instead of String.",
                      value.getClass().isArray());
              assertTrue("stringArray index " + i + " returned "
                      + value.getClass().getName() + "=["+value+"]"
                      + "  instead of String.",
                      value instanceof String);
              assertEquals("stringArray returned correct " + i,
                      "String " + i, (String) value);
  
              try {
                  value = bean.get("stringIndexed", i);
              } catch (Throwable t) {
                  fail("stringIndexed " + i + " threw " + t);
              }
  
              assertNotNull("stringIndexed returned value " + i, value);
              assertTrue("stringIndexed returned String " + i,
                      value instanceof String);
              assertEquals("stringIndexed returned correct " + i,
                      "String " + i, (String) value);
          }
      }
  
      /**
       * Corner cases on getMappedProperty invalid arguments.
       */
      public void testGetMappedArguments() {
          try {
              Object value = bean.get("mappedProperty", "unknown");
              assertNull("Should not return a value", value);
          } catch (Throwable t) {
              fail("Threw " + t + " instead of returning null");
          }
      }
  
      /**
       * Positive and negative tests on getMappedProperty valid arguments.
       */
      public void testGetMappedValues() {
          Object value = null;
  
          try {
              value = bean.get("mappedProperty", "key1");
              assertEquals("Can find first value", "First Value", value);
          } catch (Throwable t) {
              fail("Finding first value threw " + t);
          }
  
          try {
              value = bean.get("mappedProperty", "key2");
              assertEquals("Can find second value", "Second Value", value);
          } catch (Throwable t) {
              fail("Finding second value threw " + t);
          }
  
          try {
              value = bean.get("mappedProperty", "key3");
              assertNotNull("Cannot find third value", value);
          } catch (Throwable t) {
              fail("Finding third value threw " + t);
          }
      }
  
      /**
       * Corner cases on getSimpleProperty invalid arguments.
       */
      public void testGetSimpleArguments() {
          try {
              bean.get(null);
          } catch (IllegalArgumentException e) {
              return; // Expected response
          } catch (Throwable t) {
              fail("Threw " + t + " instead of IllegalArgumentException");
          }
          fail("Should throw IllegalArgumentException");
      }
  
      /**
       * Test getSimpleProperty on a boolean property.
       */
      public void testGetSimpleBoolean() {
  
          try {
              Object value = bean.get("booleanProperty");
              assertNotNull("Got a value", value);
              assertTrue("Got correct type", (value instanceof Boolean));
              assertTrue("Got correct value",
                      ((Boolean) value).booleanValue() == true);
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a double property.
       */
      public void testGetSimpleDouble() {
  
          try {
              Object value = bean.get("doubleProperty");
              assertNotNull("Got a value", value);
              assertTrue("Got correct type", (value instanceof Double));
              assertEquals("Got correct value",
                      ((Double) value).doubleValue(),
                      (double) Double.MAX_VALUE,
                      (double) 0.005);
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a float property.
       */
      public void testGetSimpleFloat() {
  
          try {
              Object value = bean.get("floatProperty");
              assertNotNull("Got a value", value);
              assertTrue("Got correct type", (value instanceof Float));
              assertEquals("Got correct value",
                      ((Float) value).floatValue(),
                      Float.MAX_VALUE,
                      (float) 0.005);
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a int property.
       */
      public void testGetSimpleInt() {
  
          try {
              Object value = bean.get("intProperty");
              assertNotNull("Failed to get value", value);
              assertTrue("Incorrect type", (value instanceof Integer));
              assertEquals("Incorrect value",
                      ((Integer) value).intValue(),
                      Integer.MAX_VALUE);
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a long property.
       */
      public void testGetSimpleLong() {
  
          try {
              Object value = bean.get("longProperty");
              assertNotNull("Got a value", value);
              assertTrue("Returned incorrect type", (value instanceof Long));
              assertEquals("Returned value of Incorrect value",
                      ((Long) value).longValue(),
                      Long.MAX_VALUE);
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a short property.
       */
      public void testGetSimpleShort() {
  
          try {
              Object value = bean.get("shortProperty");
              assertNotNull("Got a value", value);
              assertTrue("Got correct type", (value instanceof Short));
              assertEquals("Got correct value",
                      ((Short) value).shortValue(),
                      Short.MAX_VALUE);
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test getSimpleProperty on a String property.
       */
      public void testGetSimpleString() {
  
          try {
              Object value = bean.get("stringProperty");
              assertNotNull("Got a value", value);
              assertTrue("Got correct type", (value instanceof String));
              assertEquals("Got correct value",
                      (String) value,
                      "This is a string");
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test <code>contains()</code> method for mapped properties.
       */
      public void testMappedContains() {
          try {
              assertTrue("Can't see first key", bean.contains("mappedProperty", "key1"));
          } catch (Exception e) {
              fail("Exception: " + e);
          }
  
          try {
              assertTrue("Can see unknown key",
                      !bean.contains("mappedProperty", "Unknown Key"));
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Test <code>remove()</code> method for mapped properties.
       */
      public void testMappedRemove() {
  
          try {
              assertTrue("Can see first key",
                      bean.contains("mappedProperty", "key1"));
              bean.remove("mappedProperty", "key1");
              assertTrue("Can not see first key",
                      !bean.contains("mappedProperty", "key1"));
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
          try {
              assertTrue("Can not see unknown key",
                      !bean.contains("mappedProperty", "key4"));
              bean.remove("mappedProperty", "key4");
              assertTrue("Can not see unknown key",
                      !bean.contains("mappedProperty", "key4"));
          } catch (Throwable t) {
              fail("Exception: " + t);
          }
  
      }
  
      /**
       * Corner cases on setIndexedProperty invalid arguments.
       */
      public void testSetIndexedArguments() {
          try {
              bean.set("intArray", -1, new Integer(0));
           } catch (IndexOutOfBoundsException e) {
              return; // Expected response
          } catch (Throwable t) {
              fail("Threw " + t + " instead of IndexOutOfBoundsException");
          }
          fail("Should throw IndexOutOfBoundsException");
      }
  
      /**
       * Positive and negative tests on setIndexedProperty valid arguments.
       */
      public void testSetIndexedValues() {
          Object value = null;
  
          try {
              bean.set("intArray", 0, new Integer(1));
              value = (Integer) bean.get("intArray", 0);
          } catch (Throwable t) {
              fail("Threw " + t);
          }
  
          assertNotNull("Returned new value 0", value);
          assertTrue("Returned Integer new value 0",
                  value instanceof Integer);
          assertEquals("Returned correct new value 0", 1,
                  ((Integer) value).intValue());
  
          try {
              bean.set("intIndexed", 1, new Integer(11));
              value = (Integer) bean.get("intIndexed", 1);
          } catch (Throwable t) {
              fail("Threw " + t);
          }
  
          assertNotNull("Returned new value 1", value);
          assertTrue("Returned Integer new value 1",
                  value instanceof Integer);
          assertEquals("Returned correct new value 1", 11,
                  ((Integer) value).intValue());
  
          try {
              bean.set("listIndexed", 2, "New Value 2");
              value = (String) bean.get("listIndexed", 2);
          } catch (Throwable t) {
              fail("Threw " + t);
          }
  
          assertNotNull("Returned new value 2", value);
          assertTrue("Returned String new value 2",
                  value instanceof String);
          assertEquals("Returned correct new value 2", "New Value 2",
                  (String) value);
  
          try {
              bean.set("stringArray", 3, "New Value 3");
              value = (String) bean.get("stringArray", 3);
          } catch (Throwable t) {
              fail("Threw " + t);
          }
  
          assertNotNull("Returned new value 3", value);
          assertTrue("Returned String new value 3",
                  value instanceof String);
          assertEquals("Returned correct new value 3", "New Value 3",
                  (String) value);
  
          try {
              bean.set("stringIndexed", 4, "New Value 4");
              value = (String) bean.get("stringIndexed", 4);
          } catch (Throwable t) {
              fail("Threw " + t);
          }
          assertNotNull("Returned new value 4", value);
          assertTrue("Returned String new value 4",
                  value instanceof String);
          assertEquals("Returned correct new value 4", "New Value 4",
                  (String) value);
      }
  
      /**
       * Positive and negative tests on setMappedProperty valid arguments.
       */
      public void testSetMappedValues() {
  
          try {
              bean.set("mappedProperty", "First Key", "New First Value");
              assertEquals("Can replace old value",
                      "New First Value",
                      (String) bean.get("mappedProperty", "First Key"));
          } catch (Throwable t) {
              fail("Finding fourth value threw " + t);
          }
  
          try {
              bean.set("mappedProperty", "Fourth Key", "Fourth Value");
              assertEquals("Can set new value",
                      "Fourth Value",
                      (String) bean.get("mappedProperty", "Fourth Key"));
          } catch (Throwable t) {
              fail("Finding fourth value threw " + t);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a boolean property.
       */
      public void testSetSimpleBoolean() {
  
          try {
              boolean oldValue =
                      ((Boolean) bean.get("booleanProperty")).booleanValue();
              boolean newValue = !oldValue;
              bean.set("booleanProperty", new Boolean(newValue));
              assertTrue("Matched new value",
                      newValue ==
                      ((Boolean) bean.get("booleanProperty")).booleanValue());
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a double property.
       */
      public void testSetSimpleDouble() {
  
          try {
              double oldValue =
                      ((Double) bean.get("doubleProperty")).doubleValue();
              double newValue = oldValue + 1.0;
              bean.set("doubleProperty", new Double(newValue));
              assertEquals("Matched new value",
                      newValue,
                      ((Double) bean.get("doubleProperty")).doubleValue(),
                      (double) 0.005);
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a float property.
       */
      public void testSetSimpleFloat() {
  
          try {
              float oldValue =
                      ((Float) bean.get("floatProperty")).floatValue();
              float newValue = oldValue + (float) 1.0;
              bean.set("floatProperty", new Float(newValue));
              assertEquals("Matched new value",
                      newValue,
                      ((Float) bean.get("floatProperty")).floatValue(),
                      (float) 0.005);
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a int property.
       */
      public void testSetSimpleInt() {
  
          try {
              int oldValue =
                      ((Integer) bean.get("intProperty")).intValue();
              int newValue = oldValue + 1;
              bean.set("intProperty", new Integer(newValue));
              assertEquals("Matched new value",
                      newValue,
                      ((Integer) bean.get("intProperty")).intValue());
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a long property.
       */
      public void testSetSimpleLong() {
  
          try {
              long oldValue =
                      ((Long) bean.get("longProperty")).longValue();
              long newValue = oldValue + 1;
              bean.set("longProperty", new Long(newValue));
              assertEquals("Matched new value",
                      newValue,
                      ((Long) bean.get("longProperty")).longValue());
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a short property.
       */
      public void testSetSimpleShort() {
  
          try {
              short oldValue =
                      ((Short) bean.get("shortProperty")).shortValue();
              short newValue = (short) (oldValue + 1);
              bean.set("shortProperty", new Short(newValue));
              assertEquals("Matched new value",
                      newValue,
                      ((Short) bean.get("shortProperty")).shortValue());
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Test setSimpleProperty on a String property.
       */
      public void testSetSimpleString() {
  
          try {
              String oldValue = (String) bean.get("stringProperty");
              String newValue = oldValue + " Extra Value";
              bean.set("stringProperty", newValue);
              assertEquals("Matched new value",
                      newValue,
                      (String) bean.get("stringProperty"));
          } catch (Throwable e) {
              fail("Exception: " + e);
          }
  
      }
  
      /**
       * Tests set on a null value: should throw NPE.
       */
      public void testAddNullPropertyValue() {
          try {
              bean.set("nullProperty", null);
          } catch(NullPointerException e) {
              return;
          } catch(Throwable t) {
              fail("Threw " + t + " instead of NullPointerException");
              return;
          }
          fail("Should have thrown NullPointerException");
      }
  
      /**
       * Base for testGetDescriptorXxxxx() series of tests.
       *
       * @param name Name of the property to be retrieved
       * @param type Expected class type of this property
       */
      protected void testGetDescriptorBase(String name, Class type) {
          DynaProperty descriptor = null;
          try {
              descriptor = bean.getDynaClass().getDynaProperty(name);
          } catch (Throwable t) {
              fail("Threw an exception: " + t);
          }
          assertNotNull("Failed to get descriptor", descriptor);
          assertEquals("Got incorrect type", type, descriptor.getType());
      }
  
  }
  
  
  

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