commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester SetPropertyRuleTestCase.java BeanPropertySetterRuleTestCase.java
Date Sat, 18 Jan 2003 18:47:09 GMT
craigmcc    2003/01/18 10:47:09

  Modified:    digester build.xml
               digester/src/java/org/apache/commons/digester
                        BeanPropertySetterRule.java SetPropertyRule.java
               digester/src/test/org/apache/commons/digester
                        BeanPropertySetterRuleTestCase.java
  Added:       digester/src/test/org/apache/commons/digester
                        SetPropertyRuleTestCase.java
  Log:
  Make BeanPropertySetterRule and SetPropertyRule throw an exception if an
  invalid property name is specified.  The previous behavior was to silently
  ignore these setters, which caused lots of user confusion.
  
  Added unit tests to validate this new behavior as well.
  
  PR: 16233
  Submitted by:	Howard Miller <me@howardmiller.co.uk>
  
  Revision  Changes    Path
  1.31      +12 -1     jakarta-commons/digester/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/build.xml,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- build.xml	25 Oct 2002 21:31:48 -0000	1.30
  +++ build.xml	18 Jan 2003 18:47:08 -0000	1.31
  @@ -252,6 +252,7 @@
                                  test.callmethod,
                                  test.objectparam,
                                  test.bpsr,
  +                               test.spr,
                                  test.xmlrules,
                                  test.node
                                 "
  @@ -350,6 +351,16 @@
       <java classname="${test.runner}" fork="yes"
           failonerror="${test.failonerror}">
         <arg value="org.apache.commons.digester.RuleTestCase"/>
  +      <classpath refid="test.classpath"/>
  +    </java>
  +  </target>
  +
  +  <target name="test.spr" depends="compile.tests"
  +   description="Run tests for SetPropertyRule ...">
  +    <echo message="Running SetPropertyRule tests ..."/>
  +    <java classname="${test.runner}" fork="yes"
  +        failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.digester.SetPropertyRuleTestCase"/>
         <classpath refid="test.classpath"/>
       </java>
     </target>
  
  
  
  1.10      +17 -23    jakarta-commons/digester/src/java/org/apache/commons/digester/BeanPropertySetterRule.java
  
  Index: BeanPropertySetterRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/BeanPropertySetterRule.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BeanPropertySetterRule.java	2 Oct 2002 19:23:12 -0000	1.9
  +++ BeanPropertySetterRule.java	18 Jan 2003 18:47:08 -0000	1.10
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -66,6 +66,7 @@
   import java.util.HashMap;
   
   import org.apache.commons.beanutils.BeanUtils;
  +import org.apache.commons.beanutils.PropertyUtils;
   
   
   /**
  @@ -206,30 +207,23 @@
               property = name;
           }
   
  +        // Get a reference to the top object
  +        Object top = digester.peek();
  +
           // log some debugging information
           if (digester.log.isDebugEnabled()) {
               digester.log.debug("[BeanPropertySetterRule]{" + digester.match +
  -                    "} Setting property " + property + " with text " +
  -                    bodyText);
  +                    "} Set " + top.getClass().getName() + " property " +
  +                               property + " with text " + bodyText);
           }
   
  -        // going to use beanutils so need to specify property using map
  -        HashMap map = new HashMap();
  -        map.put(property, bodyText);
  +        // Force an exception if the property does not exist
  +        // (BeanUtils.setProperty() silently returns in this case)
  +        PropertyUtils.getProperty(top, property);
  +
  +        // Set the property (with conversion as necessary)
  +        BeanUtils.setProperty(top, property, bodyText);
   
  -        // examine top object
  -        Object top = digester.peek();
  -        if (top == null) {
  -            // don't try to set property if null
  -            // just log and return
  -            if (digester.log.isDebugEnabled()) {
  -                digester.log.debug("[BeanPropertySetterRule]{" +
  -                        digester.match + "} Top object is null.");
  -            }
  -        } else {
  -            // populate property on top object
  -            BeanUtils.populate(top, map);
  -        }
       }
   
   
  
  
  
  1.9       +16 -9     jakarta-commons/digester/src/java/org/apache/commons/digester/SetPropertyRule.java
  
  Index: SetPropertyRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetPropertyRule.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SetPropertyRule.java	23 Mar 2002 17:45:58 -0000	1.8
  +++ SetPropertyRule.java	18 Jan 2003 18:47:08 -0000	1.9
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -67,6 +67,7 @@
   
   import org.xml.sax.Attributes;
   import org.apache.commons.beanutils.BeanUtils;
  +import org.apache.commons.beanutils.PropertyUtils;
   
   
   /**
  @@ -147,7 +148,6 @@
           // Identify the actual property name and value to be used
           String actualName = null;
           String actualValue = null;
  -        HashMap values = new HashMap();
           for (int i = 0; i < attributes.getLength(); i++) {
               String name = attributes.getLocalName(i);
               if ("".equals(name)) {
  @@ -160,16 +160,23 @@
                   actualValue = value;
               }
           }
  -        values.put(actualName, actualValue);
   
  -        // Populate the corresponding property of the top object
  +        // Get a reference to the top object
           Object top = digester.peek();
  +
  +        // Log some debugging information
           if (digester.log.isDebugEnabled()) {
               digester.log.debug("[SetPropertyRule]{" + digester.match +
                       "} Set " + top.getClass().getName() + " property " +
                       actualName + " to " + actualValue);
           }
  -        BeanUtils.populate(top, values);
  +
  +        // Force an exception if the property does not exist
  +        // (BeanUtils.setProperty() sildently returns in this case)
  +        PropertyUtils.getProperty(top, actualName);
  +
  +        // Set the property (with conversion as necessary)
  +        BeanUtils.setProperty(top, actualName, actualValue);
   
       }
   
  
  
  
  1.8       +36 -1     jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java
  
  Index: BeanPropertySetterRuleTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BeanPropertySetterRuleTestCase.java	4 Sep 2002 18:01:16 -0000	1.7
  +++ BeanPropertySetterRuleTestCase.java	18 Jan 2003 18:47:08 -0000	1.8
  @@ -3,7 +3,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -63,6 +63,7 @@
   import java.io.InputStream;
   import java.io.Reader;
   import java.io.StringReader;
  +import java.lang.reflect.InvocationTargetException;
   import java.util.ArrayList;
   import java.util.List;
   
  @@ -325,6 +326,40 @@
                   bean.getGamma() == null);
   
       }
  +
  +    /**
  +     * Test that trying to set an unknown property throws an exception.
  +     */
  +    public void testSetUnknownProperty() {
  +
  +        // going to be setting properties on a SimpleTestBean
  +        digester.addObjectCreate("root",
  +                                 "org.apache.commons.digester.SimpleTestBean");
  +
  +        // attempt to set an unknown property name
  +        digester.addRule("root/alpha",
  +                         new BeanPropertySetterRule("unknown"));
  +
  +        // Attempt to parse the input
  +        try {
  +            SimpleTestBean bean = (SimpleTestBean)
  +                digester.parse(xmlTestReader());
  +            fail("Should have thrown NoSuchMethodException");
  +        } catch (Exception e) {
  +            e.printStackTrace(System.err);
  +            if (e instanceof InvocationTargetException) {
  +                Throwable t =
  +                    ((InvocationTargetException) e).getTargetException();
  +                if (t instanceof NoSuchMethodException) {
  +                    ; // Expected result
  +                } else {
  +                    fail("Should have thrown NoSuchMethodException, threw " + t);
  +                }
  +            }
  +        }
  +
  +    }
  +
   
       /**
        * Test that you can successfully automatically set properties.
  
  
  
  1.1                  jakarta-commons/digester/src/test/org/apache/commons/digester/SetPropertyRuleTestCase.java
  
  Index: SetPropertyRuleTestCase.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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.digester;
  
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.Reader;
  import java.io.StringReader;
  import java.lang.reflect.InvocationTargetException;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.xml.sax.SAXException;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * <p> Test case for <code>SetPropertyRule</code>.</p>
   */
  public class SetPropertyRuleTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
      /**
       * Simple test xml document used in the tests.
       */
      protected final static String TEST_XML_1 =
          "<?xml version='1.0'?><root>" +
          "<set name='alpha' value='ALPHA VALUE'/>" +
          "<set name='beta' value='BETA VALUE'/>" +
          "</root>";
  
      /**
       * Simple test xml document used in the tests.
       */
      protected final static String TEST_XML_2 =
          "<?xml version='1.0'?><root>" +
          "<set name='unknown' value='UNKNOWN VALUE'/>" +
          "</root>";
  
  
      /**
       * The digester instance we will be processing.
       */
      protected Digester digester = null;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public SetPropertyRuleTestCase(String name) {
  
          super(name);
  
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() {
  
          digester = new Digester();
  
      }
  
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite() {
  
          return (new TestSuite(SetPropertyRuleTestCase.class));
  
      }
  
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown() {
  
          digester = null;
  
      }
  
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      /**
       * Positive test for SetPropertyRule.
       */
      public void testPositive() throws Exception {
  
          // Set up the rules we need
          digester.addObjectCreate("root",
                                   "org.apache.commons.digester.SimpleTestBean");
          digester.addSetProperty("root/set", "name", "value");
  
          // Parse the input
          SimpleTestBean bean =
              (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_1));
  
          // Check that the properties were set correctly
          assertEquals("alpha property set",
                       "ALPHA VALUE",
                       bean.getAlpha());
          assertEquals("beta property set",
                       "BETA VALUE",
                       bean.getBeta());
          assertNull("gamma property not set",
                     bean.getGamma());
  
      }
  
  
      /**
       * Negative test for SetPropertyRule.
       */
      public void testNegative() {
  
          // Set up the rules we need
          digester.addObjectCreate("root",
                                   "org.apache.commons.digester.SimpleTestBean");
          digester.addSetProperty("root/set", "name", "value");
  
          // Parse the input (should fail)
          try {
              SimpleTestBean bean =
                  (SimpleTestBean) digester.parse(xmlTestReader(TEST_XML_2));
              fail("Should have thrown NoSuchMethodException");
          } catch (Exception e) {
              if (e instanceof NoSuchMethodException) {
                  ; // Expected result
              } else if (e instanceof InvocationTargetException) {
                  Throwable t =
                      ((InvocationTargetException) e).getTargetException();
                  if (t instanceof NoSuchMethodException) {
                      ; // Expected result
                  } else {
                      fail("Should have thrown ITE->NoSuchMethodException, threw " + t);
                  }
              } else if (e instanceof SAXException) {
                  Exception ee = ((SAXException) e).getException();
                  if (ee != null) {
                      if (ee instanceof NoSuchMethodException) {
                          ; // Expected result
                      } else {
                          fail("Should have thrown SE->NoSuchMethodException, threw " +
ee);
                      }
                  } else {
                      fail("Should have thrown NoSuchMethodException, threw " +
                           e.getClass().getName());
                  }
              } else {
                  fail("Should have thrown NoSuchMethodException, threw " + e);
              }
          }
  
      }
  
  
      /**
       * Get input stream from specified String containing XML data.
       */
      private Reader xmlTestReader(String xml) throws IOException {
          return new StringReader(xml);
      }
  
  }
  
  
  
  
  

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


Mime
View raw message