You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/01/09 20:42:07 UTC

cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils DynaBeanUtilsTestCase.java DynaPropertyUtilsTestCase.java

craigmcc    02/01/09 11:42:07

  Modified:    beanutils build.xml
               beanutils/src/test/org/apache/commons/beanutils
                        DynaPropertyUtilsTestCase.java
  Added:       beanutils/src/test/org/apache/commons/beanutils
                        DynaBeanUtilsTestCase.java
  Log:
  Add unit tests for accessing DynaBeans through BeanUtils as well as
  through PropertyUtils.
  
  Revision  Changes    Path
  1.25      +11 -1     jakarta-commons/beanutils/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/build.xml,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- build.xml	9 Jan 2002 19:27:30 -0000	1.24
  +++ build.xml	9 Jan 2002 19:42:07 -0000	1.25
  @@ -3,7 +3,7 @@
   
   <!--
           "Bean Utilities" component of the Jakarta Commons Subproject
  -        $Id: build.xml,v 1.24 2002/01/09 19:27:30 craigmcc Exp $
  +        $Id: build.xml,v 1.25 2002/01/09 19:42:07 craigmcc Exp $
   -->
   
   
  @@ -217,6 +217,7 @@
                                   test.basic.dynabean,
                                   test.dyna.property,
                                   test.property,
  +                                test.dyna.bean,
                                   test.bean,
                                   test.method
                                  "
  @@ -247,6 +248,15 @@
       <java classname="${test.runner}" fork="yes"
           failonerror="${test.failonerror}">
         <arg value="org.apache.commons.beanutils.PropertyUtilsTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </target>
  +
  +  <target name="test.dyna.bean" depends="compile.tests">
  +    <echo message="Running DynaBean BeanUtils tests ..."/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.beanutils.DynaBeanUtilsTestCase"/>
         <classpath refid="test.classpath"/>
       </java>
     </target>
  
  
  
  1.2       +5 -4      jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java
  
  Index: DynaPropertyUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DynaPropertyUtilsTestCase.java	9 Jan 2002 19:27:30 -0000	1.1
  +++ DynaPropertyUtilsTestCase.java	9 Jan 2002 19:42:07 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java,v 1.1 2002/01/09 19:27:30 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/01/09 19:27:30 $
  + * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java,v 1.2 2002/01/09 19:42:07 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/01/09 19:42:07 $
    *
    * ====================================================================
    *
  @@ -80,7 +80,7 @@
    * Test accessing DynaBeans transparently via PropertyUtils.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2002/01/09 19:27:30 $
  + * @version $Revision: 1.2 $ $Date: 2002/01/09 19:42:07 $
    */
   
   public class DynaPropertyUtilsTestCase extends TestCase {
  @@ -185,6 +185,7 @@
       public void tearDown() {
   
           bean = null;
  +        nested = null;
   
       }
   
  
  
  
  1.1                  jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
  
  Index: DynaBeanUtilsTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v 1.1 2002/01/09 19:42:07 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/01/09 19:42:07 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.beanutils;
  
  import java.lang.reflect.InvocationTargetException;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * Test case for BeanUtils when the underlying bean is actually a DynaBean.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/01/09 19:42:07 $
   */
  
  public class DynaBeanUtilsTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The basic test bean for each test.
       */
      protected DynaBean bean = null;
  
  
      /**
       * The nested bean pointed at by the "nested" property.
       */
      protected TestBean nested = null;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public DynaBeanUtilsTestCase(String name) {
  
          super(name);
  
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() throws Exception {
  
          // Instantiate a new DynaBean instance
          DynaClass dynaClass = createDynaClass();
          bean = dynaClass.newInstance();
  
          // Initialize the DynaBean's property values (like TestBean)
          bean.set("booleanProperty", new Boolean(true));
          bean.set("booleanSecond", new Boolean(true));
          bean.set("doubleProperty", new Double(321.0));
          bean.set("floatProperty", new Float((float) 123.0));
          int intArray[] = { 0, 10, 20, 30, 40 };
          bean.set("intArray", intArray);
          int intIndexed[] = { 0, 10, 20, 30, 40 };
          bean.set("intIndexed", intIndexed);
          bean.set("intProperty", new Integer(123));
          List listIndexed = new ArrayList();
          listIndexed.add("String 0");
          listIndexed.add("String 1");
          listIndexed.add("String 2");
          listIndexed.add("String 3");
          listIndexed.add("String 4");
          bean.set("listIndexed", listIndexed);
          bean.set("longProperty", new Long((long) 321));
          HashMap mappedProperty = new HashMap();
          mappedProperty.put("First Key", "First Value");
          mappedProperty.put("Second Key", "Second Value");
          bean.set("mappedProperty", mappedProperty);
          HashMap mappedIntProperty = new HashMap();
          mappedIntProperty.put("One", new Integer(1));
          mappedIntProperty.put("Two", new Integer(2));
          bean.set("mappedIntProperty", mappedIntProperty);
          nested = new TestBean();
          bean.set("nested", nested);
          // Property "nullProperty" is not initialized, so it should return null
          bean.set("shortProperty", new Short((short) 987));
          String stringArray[] =
              { "String 0", "String 1", "String 2", "String 3", "String 4" };
          bean.set("stringArray", stringArray);
          String stringIndexed[] =
              { "String 0", "String 1", "String 2", "String 3", "String 4" };
          bean.set("stringIndexed", stringIndexed);
          bean.set("stringProperty", "This is a string");
  
      }
  
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite() {
  
          return (new TestSuite(DynaBeanUtilsTestCase.class));
  
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
  
          bean = null;
          nested = null;
  
      }
  
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      /**
       *  tests the string and int arrays of TestBean
       */
      public void testGetArrayProperty()
      {
          try
          {
              String arr[] = BeanUtils.getArrayProperty( bean, "stringArray");
              String comp[] = (String[]) bean.get("stringArray");
  
              assertTrue("String array length = " + comp.length, 
                     ( comp.length == arr.length ));
          
              arr = BeanUtils.getArrayProperty( bean, "intArray");
              int iarr[] = (int[]) bean.get("intArray");
  
              assertTrue("String array length = " + iarr.length, 
                     ( iarr.length == arr.length ));
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
  
      }
  
  
      /**
       *  tests getting an indexed property
       */
      public void testGetIndexedProperty1()
      {
          try
          {
              String val = BeanUtils.getIndexedProperty( bean, "intIndexed[3]");
              String comp =  String.valueOf( bean.get("intIndexed", 3) );
              assertTrue("intIndexed[3] == " + comp, val.equals( comp ));  
                    
              val = BeanUtils.getIndexedProperty( bean, "stringIndexed[3]");
              comp = (String) bean.get("stringIndexed", 3);
              assertTrue("stringIndexed[3] == " + comp, val.equals( comp ) );
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
      }
      
      /**
       *  tests getting an indexed property
       */
      public void testGetIndexedProperty2()
      {
          try
          {
              String val  = BeanUtils.getIndexedProperty( bean, "intIndexed", 3);
              String comp =  String.valueOf(bean.get("intIndexed", 3));
  
              assertTrue("intIndexed,3 == " + comp,   val.equals( comp ));
  
              val = BeanUtils.getIndexedProperty( bean, "stringIndexed",3);
              comp = (String) bean.get("stringIndexed", 3);
             
              assertTrue("stringIndexed,3 == " + comp ,  val.equals(comp));
          
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
      }
  
      /**
       *  tests getting a nested property
       */
      public void testGetNestedProperty()
      {
          try
          {
              String val  = BeanUtils.getNestedProperty( bean, "nested.stringProperty");
              String comp =  nested.getStringProperty();
              assertTrue("nested.StringProperty == " + comp,  
                     val.equals( comp ));       
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
      }
  
      /**
       *  tests getting a 'whatever' property
       */
      public void testGetGeneralProperty()
      {
          try
          {
              String val  = BeanUtils.getProperty( bean, "nested.intIndexed[2]");
              String comp =  String.valueOf( bean.get("intIndexed", 2) );
  
              assertTrue("nested.intIndexed[2] == " + comp,
                  val.equals( comp ));       
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
      }
  
   /**
       *  tests getting a 'whatever' property
       */
      public void testGetSimpleProperty()
      {
          try
          {
              String val  = BeanUtils.getSimpleProperty( bean, "shortProperty");
              String comp = String.valueOf(bean.get("shortProperty"));
  
              assertTrue("shortProperty == " + comp,
                     val.equals(comp));     
          }
          catch( IllegalAccessException e)
          {
              fail("IllegalAccessException");
          }
          catch( InvocationTargetException e )
          {
              fail("InvocationTargetException");
          }
          catch( NoSuchMethodException e )
          {
              fail("NoSuchMethodException");
          }
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * Create and return a <code>DynaClass</code> instance for our test
       * <code>DynaBean</code>.
       */
      protected DynaClass createDynaClass() {
  
          int intArray[] = new int[0];
          String stringArray[] = new String[0];
  
          DynaClass dynaClass = new BasicDynaClass
              ("TestDynaClass", null,
               new DynaProperty[] {
                   new DynaProperty("booleanProperty", Boolean.TYPE),
                   new DynaProperty("booleanSecond", Boolean.TYPE),
                   new DynaProperty("doubleProperty", Double.TYPE),
                   new DynaProperty("floatProperty", Float.TYPE),
                   new DynaProperty("intArray", intArray.getClass()),
                   new DynaProperty("intIndexed", intArray.getClass()),
                   new DynaProperty("intProperty", Integer.TYPE),
                   new DynaProperty("listIndexed", List.class),
                   new DynaProperty("longProperty", Long.TYPE),
                   new DynaProperty("mappedProperty", Map.class),
                   new DynaProperty("mappedIntProperty", Map.class),
                   new DynaProperty("nested", TestBean.class),
                   new DynaProperty("nullProperty", String.class),
                   new DynaProperty("shortProperty", Short.TYPE),
                   new DynaProperty("stringArray", stringArray.getClass()),
                   new DynaProperty("stringIndexed", stringArray.getClass()),
                   new DynaProperty("stringProperty", String.class),
               });
          return (dynaClass);
  
      }
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>