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/betwixt/src/test/org/apache/commons/betwixt/digester TestXMLIntrospectorHelper.java
Date Wed, 14 Aug 2002 20:26:22 GMT
rdonkin     2002/08/14 13:26:22

  Modified:    betwixt/src/java/org/apache/commons/betwixt
                        XMLIntrospector.java
               betwixt/src/java/org/apache/commons/betwixt/digester
                        XMLIntrospectorHelper.java
               betwixt/src/test/org/apache/commons/betwixt
                        CustomerBean.java
  Added:       betwixt/src/test/org/apache/commons/betwixt/digester
                        TestXMLIntrospectorHelper.java
  Log:
  Patch ensures that strategies are correctly applied for descriptors created by an <addDefaults/>
in a .betwixt file.  Submitted by Calvin Yu.
  
  Revision  Changes    Path
  1.9       +11 -99    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
  
  Index: XMLIntrospector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- XMLIntrospector.java	2 Jul 2002 20:31:45 -0000	1.8
  +++ XMLIntrospector.java	14 Aug 2002 20:26:22 -0000	1.9
  @@ -495,106 +495,18 @@
                                   List attributes) 
                                       throws 
                                           IntrospectionException {
  -        Class type = propertyDescriptor.getPropertyType();
  -        NodeDescriptor nodeDescriptor = null;
  -        Method readMethod = propertyDescriptor.getReadMethod();
  -        Method writeMethod = propertyDescriptor.getWriteMethod();
  -        
  -        if ( readMethod == null ) {
  -            if (log.isTraceEnabled()) {
  -                log.trace( "No read method" );
  -            }
  -            return;
  +        NodeDescriptor nodeDescriptor = XMLIntrospectorHelper
  +            .createDescriptor(propertyDescriptor,
  +                                 isAttributesForPrimitives(),
  +                                 this);
  +        if (nodeDescriptor == null) {
  +           return;
  +        }
  +        if (nodeDescriptor instanceof ElementDescriptor) {
  +           elements.add(nodeDescriptor);
  +        } else {
  +           attributes.add(nodeDescriptor);
           }
  -        
  -        if ( log.isTraceEnabled() ) {
  -            log.trace( "Read method=" + readMethod.getName() );
  -        }
  -        
  -        // choose response from property type
  -        
  -        // XXX: ignore class property ??
  -        if ( Class.class.equals( type ) && "class".equals( propertyDescriptor.getName()
) ) {
  -            if (log.isTraceEnabled()) {
  -                log.trace( "Ignoring class property" );
  -            }
  -            return;
  -        }
  -        if ( isPrimitiveType( type ) ) {
  -            if (log.isTraceEnabled()) {
  -                log.trace( "Primitive type" );
  -            }
  -            if ( isAttributesForPrimitives() ) {
  -                if (log.isTraceEnabled()) {
  -                    log.trace( "Added attribute" );
  -                }
  -                nodeDescriptor = new AttributeDescriptor();
  -                attributes.add( nodeDescriptor );
  -            }
  -            else {
  -                if (log.isTraceEnabled()) {
  -                    log.trace( "Added element" );
  -                }
  -                nodeDescriptor = new ElementDescriptor(true);
  -                elements.add( nodeDescriptor );
  -            }
  -            nodeDescriptor.setTextExpression( new MethodExpression( readMethod ) );
  -            
  -            if ( writeMethod != null ) {
  -                nodeDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  -            }
  -        }
  -        else if ( isLoopType( type ) ) {
  -            if (log.isTraceEnabled()) {
  -                log.trace("Loop type");
  -            }
  -            ElementDescriptor loopDescriptor = new ElementDescriptor();
  -            loopDescriptor.setContextExpression(
  -                new IteratorExpression( new MethodExpression( readMethod ) )
  -            );
  -            // XXX: need to support some kind of 'add' or handle arrays, s or indexed properties
  -            //loopDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  -            if ( Map.class.isAssignableFrom( type ) ) {
  -                loopDescriptor.setQualifiedName( "entry" );
  -            }
  -            
  -            ElementDescriptor elementDescriptor = new ElementDescriptor();
  -            elementDescriptor.setElementDescriptors( new ElementDescriptor[] { loopDescriptor
} );
  -            elementDescriptor.setWrapCollectionsInElement(isWrapCollectionsInElement());
  -            
  -            nodeDescriptor = elementDescriptor;            
  -            elements.add( nodeDescriptor );
  -        }
  -        else {
  -            if (log.isTraceEnabled()) {
  -                log.trace( "Standard property" );
  -            }
  -            ElementDescriptor elementDescriptor = new ElementDescriptor();
  -            elementDescriptor.setContextExpression( new MethodExpression( readMethod )
);
  -            
  -            if ( writeMethod != null ) {
  -                elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  -            }
  -            
  -            nodeDescriptor = elementDescriptor;            
  -            elements.add( nodeDescriptor );
  -        }
  -
  -        nodeDescriptor.setLocalName( getNameMapper().mapTypeToElementName( propertyDescriptor.getName()
) );
  -        if (nodeDescriptor instanceof AttributeDescriptor) {
  -            // we want to use the attributemapper only when it is an attribute.. 
  -            nodeDescriptor.setLocalName( getAttributeNameMapper().mapTypeToElementName(
propertyDescriptor.getName() ) );
  -        }
  -        else{
  -            nodeDescriptor.setLocalName( getElementNameMapper().mapTypeToElementName( propertyDescriptor.getName()
) );
  -        }        
  -        
  -        nodeDescriptor.setPropertyName( propertyDescriptor.getName() );
  -        nodeDescriptor.setPropertyType( type );        
  -        
  -        // XXX: associate more bean information with the descriptor?
  -        //nodeDescriptor.setDisplayName( propertyDescriptor.getDisplayName() );
  -        //nodeDescriptor.setShortDescription( propertyDescriptor.getShortDescription()
);
       }
       
       /** Factory method to create XMLBeanInfo instances */
  
  
  
  1.6       +42 -13    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java
  
  Index: XMLIntrospectorHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XMLIntrospectorHelper.java	8 Jul 2002 22:34:34 -0000	1.5
  +++ XMLIntrospectorHelper.java	14 Aug 2002 20:26:22 -0000	1.6
  @@ -123,10 +123,12 @@
           boolean useAttributesForPrimitives,
           XMLIntrospector introspector
       ) throws IntrospectionException {
  +        String name = propertyDescriptor.getName();
           Class type = propertyDescriptor.getPropertyType();
  -        
  +       
           if (log.isTraceEnabled()) {
  -            log.trace(propertyDescriptor.getPropertyType());
  +            log.trace("Creating descriptor for property: name="
  +                + name + " type=" + type);
           }
           
           NodeDescriptor nodeDescriptor = null;
  @@ -134,7 +136,10 @@
           Method writeMethod = propertyDescriptor.getWriteMethod();
           
           if ( readMethod == null ) {
  -            log.trace( "No read method" );
  +            if (log.isTraceEnabled()) {
  +                log.trace( "No read method for property: name="
  +                    + name + " type=" + type);
  +            }
               return null;
           }
           
  @@ -145,25 +150,36 @@
           // choose response from property type
           
           // XXX: ignore class property ??
  -        if ( Class.class.equals( type ) && "class".equals( propertyDescriptor.getName()
) ) {
  +        if ( Class.class.equals( type ) && "class".equals( name ) ) {
               log.trace( "Ignoring class property" );
               return null;
           }
           if ( isPrimitiveType( type ) ) {
  -            log.trace( "Primitive type" );
  +            if (log.isTraceEnabled()) {
  +                log.trace( "Primitive type: " + name);
  +            }
               if ( useAttributesForPrimitives ) {
  -                log.trace( "Added attribute" );
  +                if (log.isTraceEnabled()) {
  +                    log.trace( "Adding property as attribute: " + name );
  +                }
                   nodeDescriptor = new AttributeDescriptor();
               }
               else {
  -                log.trace( "Added element" );
  +                if (log.isTraceEnabled()) {
  +                    log.trace( "Adding property as element: " + name );
  +                }
                   nodeDescriptor = new ElementDescriptor(true);
               }
               nodeDescriptor.setTextExpression( new MethodExpression( readMethod ) );
  -            nodeDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +            
  +            if ( writeMethod != null ) {
  +                nodeDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +            }
           }
           else if ( isLoopType( type ) ) {
  -            log.trace("Loop type");
  +            if (log.isTraceEnabled()) {
  +                log.trace("Loop type: " + name);
  +            }
               ElementDescriptor loopDescriptor = new ElementDescriptor();
               loopDescriptor.setContextExpression(
                   new IteratorExpression( new MethodExpression( readMethod ) )
  @@ -181,14 +197,27 @@
               nodeDescriptor = elementDescriptor;            
           }
           else {
  -            log.trace( "Standard property" );
  +            if (log.isTraceEnabled()) {
  +                log.trace( "Standard property: " + name);
  +            }
               ElementDescriptor elementDescriptor = new ElementDescriptor();
               elementDescriptor.setContextExpression( new MethodExpression( readMethod )
);
  -            elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +            if ( writeMethod != null ) {
  +                elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +            }
               
  -            nodeDescriptor = elementDescriptor;            
  +            nodeDescriptor = elementDescriptor;          
           }
  -        nodeDescriptor.setLocalName( propertyDescriptor.getName() );
  +
  +        nodeDescriptor.setLocalName( introspector.getNameMapper().mapTypeToElementName(
name ) );
  +        if (nodeDescriptor instanceof AttributeDescriptor) {
  +            // we want to use the attributemapper only when it is an attribute.. 
  +            nodeDescriptor.setLocalName( introspector.getAttributeNameMapper().mapTypeToElementName(
name ) );
  +        }
  +        else {
  +            nodeDescriptor.setLocalName( introspector.getElementNameMapper().mapTypeToElementName(
name ) );
  +        }        
  +  
           nodeDescriptor.setPropertyName( propertyDescriptor.getName() );
           nodeDescriptor.setPropertyType( type );        
           
  
  
  
  1.3       +10 -0     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java
  
  Index: CustomerBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CustomerBean.java	13 Aug 2002 13:35:34 -0000	1.2
  +++ CustomerBean.java	14 Aug 2002 20:26:22 -0000	1.3
  @@ -85,6 +85,7 @@
       
       private String id;
       private String name;
  +    private String nickName;
       private String[] emails;
       private int[] numbers;
       private AddressBean address;
  @@ -98,6 +99,11 @@
           return id;
       }
       
  +    public String getNickName() {
  +       return nickName;
  +    }
  +
  +    
       public String getName() {
           return name;
       }
  @@ -148,6 +154,10 @@
       public void setName(String name) {
           this.name = name;
       }
  + 
  +    public void setNickName(String nickName) {
  +        this.nickName = nickName;
  +    }    
       
       public void setEmails(String[] emails) {
           this.emails = emails;
  
  
  
  1.1                  jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/TestXMLIntrospectorHelper.java
  
  Index: TestXMLIntrospectorHelper.java
  ===================================================================
  
  /*
   * $Header: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/TestXMLIntrospectorHelper.java,v
1.1 2002/08/14 20:26:22 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2002/08/14 20:26:22 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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/>.
   * 
   * $Id: TestXMLIntrospectorHelper.java,v 1.1 2002/08/14 20:26:22 rdonkin Exp $
   */
  package org.apache.commons.betwixt.digester;
  
  import java.beans.BeanInfo;
  import java.beans.Introspector;
  import java.beans.IntrospectionException;
  import java.beans.PropertyDescriptor;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.betwixt.CustomerBean;
  import org.apache.commons.betwixt.NodeDescriptor;
  import org.apache.commons.betwixt.XMLIntrospector;
  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
  
  /** Test harness for the XMLIntrospectorHelper
    *
    * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
    * @version $Revision: 1.1 $
    */
  public class TestXMLIntrospectorHelper extends TestCase {
  
      public static void main( String[] args ) {
          TestRunner.run( suite() );
      }
  
      public static Test suite() {
          return new TestSuite(TestXMLIntrospectorHelper.class);
      }
  
      public TestXMLIntrospectorHelper(String testName) {
          super(testName);
      }
  
      /**
       * Test the helper's <code>createDescriptor</code> method when a hyphenated
name
       * mapper is set.
       */
      public void testCreateDescriptorWithHyphenatedElementNameMapper() throws Exception {
          XMLIntrospector introspector = new XMLIntrospector();
          introspector.setAttributesForPrimitives(false);
          introspector.setElementNameMapper(new HyphenatedNameMapper());
          BeanInfo beanInfo = Introspector.getBeanInfo(CustomerBean.class);
  
          NodeDescriptor nickNameProperty = createDescriptor("nickName", beanInfo, introspector);
          assertNotNull("nickName property not found", nickNameProperty);
          assertEquals("nick name property", "nick-name", nickNameProperty.getLocalName());
  
          NodeDescriptor projectNamesProperty = createDescriptor("projectNames", beanInfo,
introspector);
          assertNotNull("projectNames property not found", projectNamesProperty);
          assertEquals("project names property", "project-names", projectNamesProperty.getLocalName());
      }
  
      /**
       * Find the specified property and convert it into a descriptor.
       */
      private NodeDescriptor createDescriptor(String propertyName, BeanInfo beanInfo, XMLIntrospector
introspector)
          throws IntrospectionException {
          PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
          for (int i=0; i<properties.length; i++) {
              if (propertyName.equals(properties[i].getName())) {
                  NodeDescriptor desc = XMLIntrospectorHelper
                      .createDescriptor(properties[i],
                                        introspector.isAttributesForPrimitives(),
                                        introspector);
                  return desc;
              } 
          }
          return null;
      }
  
  }
  
  
  

--
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