commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils PassTestException.java ThrowExceptionConverter.java BeanUtilsTestCase.java ConvertUtilsTestCase.java
Date Sat, 15 Mar 2003 11:38:12 GMT
rdonkin     2003/03/15 03:38:12

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        BeanUtilsBean.java ConvertUtilsBean.java
                        PropertyUtilsBean.java package.html
               beanutils/src/test/org/apache/commons/beanutils
                        BeanUtilsTestCase.java ConvertUtilsTestCase.java
  Added:       beanutils/src/test/org/apache/commons/beanutils
                        PassTestException.java ThrowExceptionConverter.java
  Log:
  Added public constructors for the utility objects (BeanUtilsBean, PropertyUtilsBean and
ConvertUtilsBean). Add public accessor properties for the ConvertUtilsBean and PropertyUtilsBean
instances used by a BeanUtilsBean. This allows BeanUtilsBean objects to be created with independent
registered converters and independent caches.  Also added test cases. Addresses the standard
BeanUtils part of enhancement #14848.
  
  Revision  Changes    Path
  1.2       +40 -15    jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
  
  Index: BeanUtilsBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BeanUtilsBean.java	3 Mar 2003 22:33:46 -0000	1.1
  +++ BeanUtilsBean.java	15 Mar 2003 11:38:10 -0000	1.2
  @@ -115,16 +115,38 @@
        * Logging for this instance
        */
       private Log log = LogFactory.getLog(BeanUtils.class);
  +    
  +    /** Used to perform conversions between object types when setting properties */
  +    private ConvertUtilsBean convertUtilsBean;
  +    
  +    /** Used to access properties*/
  +    private PropertyUtilsBean propertyUtilsBean;
   
       // --------------------------------------------------------- Constuctors
   
       /** 
  -     * <p>Base constructor.</p>
  -     *
  -     * <p>For the moment, this will be private. 
  -     * At first, we'll have a straight replacement for current functionality
  +     * <p>Constructs an instance using standard (singleton) property 
  +     * and conversion instances.</p>
        */
  -    protected BeanUtilsBean() {}
  +    public BeanUtilsBean() {
  +        this(ConvertUtilsBean.getInstance(),PropertyUtilsBean.getInstance());
  +    }
  +
  +    /** 
  +     * <p>Constructs an instance using given property and conversion instances.</p>
  +     *
  +     * @param convertUtilsBean use this <code>ConvertUtilsBean</code> 
  +     * to perform conversions from one object to another
  +     * @param propertyUtilsBean use this <code>PropertyUtilsBean</code>
  +     * to access properties
  +     */
  +    public BeanUtilsBean(		
  +                            ConvertUtilsBean convertUtilsBean, 
  +                            PropertyUtilsBean propertyUtilsBean) {
  +                            
  +        this.convertUtilsBean = convertUtilsBean;
  +        this.propertyUtilsBean = propertyUtilsBean;
  +    }
   
       // --------------------------------------------------------- Public Methods
   
  @@ -1013,14 +1035,17 @@
   
       }
       
  -    // --------------------------------------------------------- Other Methods
  -    
  -    protected ConvertUtilsBean getConvertUtils() {
  -        return ConvertUtilsBean.getInstance();
  +    /** 
  +     * Gets the <code>ConvertUtilsBean</code> instance used to perform the
conversions.
  +     */
  +    public ConvertUtilsBean getConvertUtils() {
  +        return convertUtilsBean;
       }
       
  -    
  -    protected PropertyUtilsBean getPropertyUtils() {
  -        return PropertyUtilsBean.getInstance();
  +    /**
  +     * Gets the <code>PropertyUtilsBean</code> instance used to access properties.
  +     */
  +    public PropertyUtilsBean getPropertyUtils() {
  +        return propertyUtilsBean;
       }
   }
  
  
  
  1.2       +5 -5      jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java
  
  Index: ConvertUtilsBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtilsBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConvertUtilsBean.java	3 Mar 2003 22:33:46 -0000	1.1
  +++ ConvertUtilsBean.java	15 Mar 2003 11:38:10 -0000	1.2
  @@ -174,7 +174,7 @@
       // ------------------------------------------------------- Constructors
   
       /** Construct a bean with standard converters registered */
  -    protected ConvertUtilsBean() {
  +    public ConvertUtilsBean() {
           converters.setFast(false);
           deregister();
           converters.setFast(true);
  
  
  
  1.3       +5 -5      jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
  
  Index: PropertyUtilsBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PropertyUtilsBean.java	4 Mar 2003 19:30:22 -0000	1.2
  +++ PropertyUtilsBean.java	15 Mar 2003 11:38:10 -0000	1.3
  @@ -163,7 +163,7 @@
       // ---------------------------------------------------------- Constructors
       
       /** Base constructor */
  -    protected PropertyUtilsBean() {
  +    public PropertyUtilsBean() {
           descriptorsCache = new FastHashMap();
           descriptorsCache.setFast(true);
           mappedDescriptorsCache = new FastHashMap();
  
  
  
  1.15      +33 -1     jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/package.html
  
  Index: package.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/package.html,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- package.html	3 Mar 2003 22:35:42 -0000	1.14
  +++ package.html	15 Mar 2003 11:38:11 -0000	1.15
  @@ -39,6 +39,10 @@
       <li><a href="#conversion.defining">Defining Your Own Converters</a></li>
       <li><a href="#conversion.i18n">Locale Aware Conversions</a></li>
       </ul></li>
  +<li><a href="#instances">Utility Objects And Static Utility Classes</a>
  +    <ul>
  +    <li><a href="#instances.background">Background</a></li>
  +    </ul></li>
   <li><a href="#FAQ">Frequently Asked Questions</a>
       <ul>
       <li><a href="#FAQ.property">Why Can't BeanUtils Find My Method?</a></li>
  @@ -548,7 +552,7 @@
       ...;                    // Return connection to pool
       List rows = rsdc.getRows();
       ...;                   // Process the rows as desired
  -/pre>
  +</pre>
   
   
   <a name="dynamic.wrap"></a>
  @@ -662,6 +666,34 @@
   <code><a href='locale/package-summary.html'>org.apache.commons.beanutils.locale
   </a></code>. These are built along the same
   lines as the basic classes but support localization.</p>
  +
  +
  +<a name="instances"></a>
  +<h1>Utility Objects And Static Utility Classes</h1>
  +<a name="instances.background"></a>
  +<h3>Background</h3>
  +<p>
  +So far, the examples have covered the static utility classes (<code>BeanUtils</code>,
  +<code>ConvertUtils</code> and <code>PropertyUtils</code>). These
are easy to use but are
  +somewhat inflexible. These all share the same registered converters and the same caches.
  +</p>
  +<p>
  +This functionality can also be accessed through utility objects (in fact, the static utility
  +class use worker instances of these classes). For each static utility class, there is a
corresponding
  +class with the same functionality that can be instantiated:
  +</p>
  +<p>
  +<table cols='2' width='60%'>
  +<tr><th>Static Utility Class</th><th>Utility Object</th></tr>
  +<tr><td>BeanUtils</td><td>BeanUtilsBean</td></tr>
  +<tr><td>ConvertUtils</td><td>ConvertUtilsBean</td></tr>
  +<tr><td>PropertyUtils</td><td>PropertyUtilsBean</td></tr>
  +</table>
  +</p>
  +<p>
  +Creating an instances allow gives guarenteed control of the caching and registration 
  +to the code that creates it. 
  +</p>
   
   <a name="FAQ"></a>
   <h1>Frequently Asked Questions</h1>
  
  
  
  1.23      +48 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
  
  Index: BeanUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- BeanUtilsTestCase.java	5 Feb 2003 00:49:22 -0000	1.22
  +++ BeanUtilsTestCase.java	15 Mar 2003 11:38:11 -0000	1.23
  @@ -1249,6 +1249,50 @@
   
       }
   
  +    /** Tests that separate instances can register separate instances */
  +    public void testSeparateInstances() throws Exception {
  +        BeanUtilsBean utilsOne = new BeanUtilsBean(
  +                                                new ConvertUtilsBean(), 
  +                                                new PropertyUtilsBean());
  +        BeanUtilsBean utilsTwo = new BeanUtilsBean(
  +                                                new ConvertUtilsBean(), 
  +                                                new PropertyUtilsBean());        
  +        
  +        
  +        TestBean bean = new TestBean();
  +        
  +        // Make sure what we're testing works
  +        bean.setBooleanProperty(false);
  +        utilsOne.setProperty(bean, "booleanProperty", "true");
  +        assertEquals("Set property failed (1)", bean.getBooleanProperty(), true);
  +        
  +        bean.setBooleanProperty(false);
  +        utilsTwo.setProperty(bean, "booleanProperty", "true");
  +        assertEquals("Set property failed (2)", bean.getBooleanProperty(), true);     
 
  +        
  +        // now change the registered conversion
  +        
  +        utilsOne.getConvertUtils().register(new ThrowExceptionConverter(), Boolean.TYPE);
  +        try {
  +            
  +            bean.setBooleanProperty(false);
  +            utilsOne.setProperty(bean, "booleanProperty", "true");
  +            fail("Registered conversion not used.");
  +            
  +        } catch (PassTestException e) { /* Do nothing */ }
  +        
  +        // make sure that this conversion has no been registered in the other instance
  +        try {
  +        
  +            bean.setBooleanProperty(false);
  +            utilsTwo.setProperty(bean, "booleanProperty", "true");
  +            assertEquals("Set property failed (3)", bean.getBooleanProperty(), true);
  +            
  +        } catch (PassTestException e) {
  +            fail("Registed converter is used by other instances");
  +        }
  +    }
  +
   
       // Ensure that the actual int[] matches the expected int[]
       protected void checkIntArray(int actual[], int expected[]) {
  
  
  
  1.6       +63 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ConvertUtilsTestCase.java
  
  Index: ConvertUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ConvertUtilsTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ConvertUtilsTestCase.java	20 Jul 2002 18:21:21 -0000	1.5
  +++ ConvertUtilsTestCase.java	15 Mar 2003 11:38:11 -0000	1.6
  @@ -582,6 +582,65 @@
   
   
       }
  +    
  +    public void testSeparateConvertInstances() throws Exception {
  +        ConvertUtilsBean utilsOne = new ConvertUtilsBean();
  +        ConvertUtilsBean utilsTwo = new ConvertUtilsBean();
  +        
  +        // make sure that the test work ok before anything's changed
  +        Object
  +        value = utilsOne.convert("true", Boolean.TYPE);
  +        assertTrue(value instanceof Boolean);
  +        assertEquals(
  +                    "Standard conversion failed (1)", 
  +                    ((Boolean) value).booleanValue(), 
  +                    true);
  +        
  +        value = utilsTwo.convert("true", Boolean.TYPE);
  +        assertTrue(value instanceof Boolean);
  +        assertEquals(
  +                    "Standard conversion failed (2)", 
  +                    ((Boolean) value).booleanValue(), 
  +                    true);
  +        
  +        // now register a test
  +        
  +        utilsOne.register(new ThrowExceptionConverter(), Boolean.TYPE);
  +        try {
  +            
  +            utilsOne.convert("true", Boolean.TYPE);
  +            fail("Register converter failed.");
  +            
  +        } catch (PassTestException e) { /* This shows that the registration has worked
*/ }
  +        
  +        try {
  +            // nothing should have changed
  +            value = utilsTwo.convert("true", Boolean.TYPE);
  +            assertTrue(value instanceof Boolean);
  +            assertEquals(
  +                        "Standard conversion failed (3)",
  +                        ((Boolean) value).booleanValue(), 
  +                        true);
  +            
  +        } catch (PassTestException e) { 
  +            // This is a failure since utilsTwo should still have 
  +            // standard converters registered 
  +            fail("Registering a converter for an instance should not effect another instance.");
  +        }
  +        
  +        // nothing we'll test deregister
  +        utilsOne.deregister();
  +        value = utilsOne.convert("true", Boolean.TYPE);
  +        assertTrue(value instanceof Boolean);
  +        assertEquals("Instance deregister failed.", ((Boolean) value).booleanValue(), true);
  +        
  +        value = utilsTwo.convert("true", Boolean.TYPE);
  +        assertTrue(value instanceof Boolean);
  +        assertEquals(
  +                    "Standard conversion failed (4)", 
  +                    ((Boolean) value).booleanValue(), 
  +                    true);
  +    }	
   
   
       // -------------------------------------------------------- Private Methods
  
  
  
  1.1                  jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PassTestException.java
  
  Index: PassTestException.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PassTestException.java,v
1.1 2003/03/15 11:38:11 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/15 11:38:11 $
   *
   * ====================================================================
   *
   * 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.beanutils;
  
  
  /**
   * Just a runtime exception. Useful for check that a method is called.
   * 
   * @author Robert Burrell Donkin
   */
  
  public class PassTestException extends RuntimeException {
  }
  
  
  1.1                  jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ThrowExceptionConverter.java
  
  Index: ThrowExceptionConverter.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ThrowExceptionConverter.java,v
1.1 2003/03/15 11:38:12 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/15 11:38:12 $
   *
   * ====================================================================
   *
   * 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.beanutils;
  
  
  /**
   * Converter implementation that throws a <code>PassTestException</code> 
   * when convert is called.
   * The idea is that catching this exception is a clear signal that this method
   * has been called.
   *
   * @author Robert Burrell Donkin
   */
  
  public class ThrowExceptionConverter implements Converter {
      
      public Object convert(Class type, Object value) {
          throw new PassTestException();
      }	
  }
  
  
  

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


Mime
View raw message