You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2003/07/31 23:40:58 UTC

cvs commit: jakarta-commons/betwixt/src/test/org/apache/commons/betwixt TestBeanReader.java

rdonkin     2003/07/31 14:40:58

  Modified:    betwixt/src/java/org/apache/commons/betwixt/expression
                        Context.java MapEntryAdder.java MethodUpdater.java
               betwixt/src/java/org/apache/commons/betwixt/io
                        AbstractBeanWriter.java BeanReader.java
                        BeanRuleSet.java
               betwixt/src/test/org/apache/commons/betwixt
                        TestBeanReader.java
  Log:
  Moved common dynamic configuration in common class. Also Added support for pluggable object <-> string strategies
  
  Revision  Changes    Path
  1.5       +61 -11    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java
  
  Index: Context.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Context.java	12 Jan 2003 13:52:03 -0000	1.4
  +++ Context.java	31 Jul 2003 21:40:58 -0000	1.5
  @@ -66,6 +66,9 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  +import org.apache.commons.betwixt.BindingConfiguration;
  +import org.apache.commons.betwixt.strategy.ObjectStringConverter;
  +
   /** <p><code>Context</code> describes the context used to evaluate
     * bean expressions.
     * This is mostly a bean together with a number of context variables.
  @@ -93,26 +96,41 @@
       /** Evaluate this bean */
       private Object bean;
       /** Variables map */
  -    private Map variables = new HashMap();
  +    private Map variables;
       /** 
        * Logging uses commons-logging <code>Log</code> 
        * named <code>org.apache.commons.betwixt</code> 
        */
       private Log log; 
  +    /** Configuration for dynamic binding properties */
  +    private BindingConfiguration bindingConfiguration;
       
  -    /** Construct context with default log */
  +    /** 
  +     * Construct context with default log 
  +     */
       public Context() {
  -        this.log = LogFactory.getLog( getClass() );
  +        this( null, LogFactory.getLog( Context.class ) );
       }
       
       /** Convenience constructor sets evaluted bean and log.
         *
         * @param bean evaluate expressions against this bean
         * @param log log to this logger
  +      * @deprecated use constructor which takes a BindingConfiguration
         */
       public Context(Object bean, Log log) {
  -        this.bean = bean;
  -        this.log = log;
  +        this( bean, log, new BindingConfiguration() );
  +    }
  +
  +    
  +    /** Convenience constructor sets evaluted bean and log.
  +      *
  +      * @param bean evaluate expressions against this bean
  +      * @param log log to this logger
  +      * @param converter not null
  +      */
  +    public Context(Object bean, Log log, BindingConfiguration bindingConfiguration) {
  +        this( bean, new HashMap(), log,  bindingConfiguration );
       }
       
       /** Convenience constructor sets evaluted bean, context variables and log.
  @@ -120,11 +138,24 @@
         * @param bean evaluate expressions against this bean 
         * @param variables context variables
         * @param log log to this logger
  +      * @deprecated use constructor which takes a converter
         */
       public Context(Object bean, Map variables, Log log) {
  +        this( bean, variables, log, new BindingConfiguration() );
  +    }
  +    
  +    /** Convenience constructor sets evaluted bean, context variables and log.
  +      *
  +      * @param bean evaluate expressions against this bean 
  +      * @param variables context variables
  +      * @param log log to this logger
  +      * @param converter not null
  +      */
  +    public Context(Object bean, Map variables, Log log, BindingConfiguration bindingConfiguration) {
           this.bean = bean;
           this.variables = variables;
           this.log = log;
  +        this.bindingConfiguration = bindingConfiguration;
       }
   
       /** Returns a new child context with the given bean but the same log and variables. 
  @@ -133,7 +164,7 @@
        * @return new Context with new bean but shared variables 
        */
       public Context newContext(Object newBean) {
  -        return new Context(newBean, variables, log);
  +        return new Context(newBean, variables, log, bindingConfiguration);
       }
       
       /** 
  @@ -202,5 +233,24 @@
        */
       public void setLog(Log log) {
           this.log = log;
  +    }
  +    
  +    /** 
  +     * Gets object &lt;-&gt; string converter.
  +     * @return the Converter to be used for conversions, not null
  +     */
  +    public ObjectStringConverter getObjectStringConverter() {
  +        return bindingConfiguration.getObjectStringConverter();
  +    }
  +    
  +    /** 
  +     * Should <code>ID</code>'s and <code>IDREF</code> attributes 
  +     * be used to cross-reference matching objects? 
  +     *
  +     * @return true if <code>ID</code> and <code>IDREF</code> 
  +     * attributes should be used to cross-reference instances
  +     */
  +    public boolean getMapIDs() {
  +        return bindingConfiguration.getMapIDs();
       }
   }
  
  
  
  1.2       +9 -9      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java
  
  Index: MapEntryAdder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MapEntryAdder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MapEntryAdder.java	11 Apr 2003 21:27:42 -0000	1.1
  +++ MapEntryAdder.java	31 Jul 2003 21:40:58 -0000	1.2
  @@ -63,8 +63,6 @@
   
   import java.lang.reflect.Method;
   
  -import org.apache.commons.beanutils.ConvertUtils;
  -
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -237,12 +235,14 @@
           if ( bean != null ) {
               if ( key instanceof String ) {
                   // try to convert into primitive types
  -                key = ConvertUtils.convert( (String) key, keyType );
  +                key = context.getObjectStringConverter()
  +                        .stringToObject( (String) key, valueType, null, context );
               }
               
               if ( value instanceof String ) {
                   // try to convert into primitive types
  -                value = ConvertUtils.convert( (String) value, keyType );
  +                value = context.getObjectStringConverter()
  +                        .stringToObject( (String) value, keyType, null, context );
               }
                    
               Object[] arguments = { key, value };
  
  
  
  1.9       +7 -7      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java
  
  Index: MethodUpdater.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- MethodUpdater.java	29 Jul 2003 21:31:24 -0000	1.8
  +++ MethodUpdater.java	31 Jul 2003 21:40:58 -0000	1.9
  @@ -63,7 +63,6 @@
   
   import java.lang.reflect.Method;
   
  -import org.apache.commons.beanutils.ConvertUtils;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -117,7 +116,8 @@
                   if ( log.isTraceEnabled() ) {
                       log.trace("Converting primitive to " + valueType);
                   }
  -                newValue = ConvertUtils.convert( (String) newValue, valueType );
  +                newValue = context.getObjectStringConverter()
  +                    .stringToObject( (String) newValue, valueType, null, context );
               }
               if ( newValue != null ) {
                   // check that it is of the correct type
  
  
  
  1.18      +63 -35    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
  
  Index: AbstractBeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- AbstractBeanWriter.java	29 Jul 2003 21:32:15 -0000	1.17
  +++ AbstractBeanWriter.java	31 Jul 2003 21:40:58 -0000	1.18
  @@ -72,10 +72,13 @@
   import org.apache.commons.betwixt.Descriptor;
   import org.apache.commons.betwixt.XMLBeanInfo;
   import org.apache.commons.betwixt.XMLIntrospector;
  +import org.apache.commons.betwixt.BindingConfiguration;
   import org.apache.commons.betwixt.expression.Context;
   import org.apache.commons.betwixt.expression.Expression;
   import org.apache.commons.betwixt.io.id.SequentialIDGenerator;
   import org.apache.commons.betwixt.digester.XMLIntrospectorHelper;
  +import org.apache.commons.betwixt.strategy.ObjectStringConverter;
  +import org.apache.commons.betwixt.strategy.DefaultObjectStringConverter;
   import org.apache.commons.collections.ArrayStack;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -112,10 +115,10 @@
       private ArrayStack beanStack = new ArrayStack();
       /** Used to generate ID attribute values*/
       private IDGenerator idGenerator = new SequentialIDGenerator();
  -    /** Should generated <code>ID</code> attribute values be added to the elements? */
  -    private boolean writeIDs = true;
       /** Should empty elements be written out? */
       private boolean writeEmptyElements = true;
  +    /** Dynamic binding configuration settings */
  +    private BindingConfiguration bindingConfiguration = new BindingConfiguration();
       
       /**
        * Marks the start of the bean writing.
  @@ -144,8 +147,8 @@
        * <p> This writes an xml fragment representing the bean to the current stream.</p>
        *
        * <p>This method will throw a <code>CyclicReferenceException</code> when a cycle
  -     * is encountered in the graph <strong>only</strong> if the <code>WriteIDs</code>
  -     * property is false.</p>
  +     * is encountered in the graph <strong>only</strong> if the <code>getMapIDs()</code>
  +     * setting of the </code>BindingConfiguration</code> is false.</p>
        *
        * @throws IOException if an IO problem occurs during writing 
        * @throws SAXException if an SAX problem occurs during writing  
  @@ -174,8 +177,8 @@
        * using the given <code>qualifiedName</code>.</p>
        *
        * <p>This method will throw a <code>CyclicReferenceException</code> when a cycle
  -     * is encountered in the graph <strong>only</strong> if the <code>WriteIDs</code>
  -     * property is false.</p>
  +     * is encountered in the graph <strong>only</strong> if the <code>getMapIDs()</code>
  +     * setting of the <code>BindingConfiguration</code> is false.</p>
        *
        * @param qualifiedName the string naming root element
        * @param bean the <code>Object</code> to write out as xml
  @@ -191,7 +194,7 @@
                           IOException, 
                           SAXException,
                           IntrospectionException {
  -        writeBean( "", qualifiedName, qualifiedName, bean);
  +        writeBean( "", qualifiedName, qualifiedName, bean, makeContext( bean ) );
       }
       
       /** 
  @@ -199,8 +202,8 @@
        * using the given <code>qualifiedName</code>.</p>
        *
        * <p>This method will throw a <code>CyclicReferenceException</code> when a cycle
  -     * is encountered in the graph <strong>only</strong> if the <code>WriteIDs</code>
  -     * property is false.</p>
  +     * is encountered in the graph <strong>only</strong> if the <code>getMapIDs()</code>
  +     * setting of the <code>BindingConfiguration</code> is false.</p>
        *
        * @param namespaceUri the namespace uri
        * @param localName the local name
  @@ -215,7 +218,8 @@
                   String namespaceUri,
                   String localName,
                   String qualifiedName, 
  -                Object bean) 
  +                Object bean,
  +                Context context) 
                       throws 
                           IOException, 
                           SAXException,
  @@ -230,7 +234,7 @@
           if ( beanInfo != null ) {
               ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
               if ( elementDescriptor != null ) {
  -                Context context = new Context( bean, log );
  +                context = context.newContext( bean );
                   if ( qualifiedName == null ) {
                       qualifiedName = elementDescriptor.getQualifiedName();
                   }
  @@ -256,7 +260,7 @@
                           
                   } else {
                       pushBean ( context.getBean() );
  -                    if ( writeIDs ) {
  +                    if ( getBindingConfiguration().getMapIDs() ) {
                           ref = (String) idMap.get( context.getBean() );
                       }
                       if ( ref == null ) {
  @@ -267,7 +271,7 @@
                               id = idGenerator.nextId();
                               idMap.put( bean, id );
                               
  -                            if ( writeIDs ) {
  +                            if ( getBindingConfiguration().getMapIDs() ) {
                                   // write element with id
                                   writeElement(
                                       namespaceUri,
  @@ -353,6 +357,22 @@
           this.idGenerator = idGenerator;
       }
       
  +    /**
  +     * Gets the dynamic configuration setting to be used for bean reading.
  +     * @return the BindingConfiguration settings, not null
  +     */
  +    public BindingConfiguration getBindingConfiguration() {
  +        return bindingConfiguration;
  +    }
  +    
  +    /**
  +     * Sets the dynamic configuration setting to be used for bean reading.
  +     * @param the BindingConfiguration settings, not null
  +     */
  +    public void setBindingConfiguration(BindingConfiguration bindingConfiguration) {
  +        this.bindingConfiguration = bindingConfiguration;
  +    }
  +    
       /** 
        * <p>Should generated <code>ID</code> attribute values be added to the elements?</p>
        * 
  @@ -360,9 +380,10 @@
        * then a {@link CyclicReferenceException} will be thrown by the write method.</p>
        * 
        * @return true if <code>ID</code> and <code>IDREF</code> attributes are to be written
  +     * @deprecated use {@link BindingConfiguration#getMapIDs}
        */
       public boolean getWriteIDs() {
  -        return writeIDs;
  +        return getBindingConfiguration().getMapIDs();
       }
   
       /** 
  @@ -371,9 +392,10 @@
        * will be thrown whenever a cyclic occurs in the bean graph.
        *
        * @param writeIDs true if <code>ID</code>'s and <code>IDREF</code>'s should be written
  +     * @deprecated use {@link BindingConfiguration#setMapIDs}
        */
       public void setWriteIDs(boolean writeIDs) {
  -        this.writeIDs = writeIDs;
  +        getBindingConfiguration().setMapIDs( writeIDs );
       }
       
       /**
  @@ -713,10 +735,10 @@
                                       if (object == null) {
                                           continue;
                                       }
  -                                    writeBean( namespaceUri, localName, qualifiedName, object );
  +                                    writeBean( namespaceUri, localName, qualifiedName, object, context );
                                   }
                               } else {
  -                                writeBean( namespaceUri, localName, qualifiedName, childBean );
  +                                writeBean( namespaceUri, localName, qualifiedName, childBean, context );
                               }
                           }                    
                       } else {
  @@ -733,7 +755,10 @@
                       Expression expression = childDescriptors[i].getTextExpression();
                       if ( expression != null ) {
                           Object value = expression.evaluate( context );
  -                        String text = convertToString( value );
  +                        String text = convertToString( 
  +                                                        value, 
  +                                                        childDescriptors[i], 
  +                                                        context );
                           if ( text != null && text.length() > 0 ) {
                               bodyText(text);
                           }               
  @@ -745,7 +770,7 @@
               Expression expression = elementDescriptor.getTextExpression();
               if ( expression != null ) {
                   Object value = expression.evaluate( context );
  -                String text = convertToString(value);
  +                String text = convertToString( value, elementDescriptor, context );
                   if ( text != null && text.length() > 0 ) {
                       bodyText(text);
                   }
  @@ -761,7 +786,7 @@
        */
       protected void pushBean( Object bean ) {
           // check that we don't have a cyclic reference when we're not writing IDs
  -        if ( !writeIDs ) {
  +        if ( !getBindingConfiguration().getMapIDs() ) {
               Iterator it = beanStack.iterator();
               while ( it.hasNext() ) {
                   Object next = it.next();
  @@ -840,7 +865,7 @@
           Expression expression = descriptor.getTextExpression();
           if ( expression != null ) {
               Object value = expression.evaluate( context );
  -            String text = convertToString(value);
  +            String text = convertToString( value, descriptor, context );
               if ( text != null && text.length() > 0 ) {
                   log.trace( "Element has body text which isn't empty." );
                   return false;
  @@ -1056,7 +1081,7 @@
                       Expression expression = attributes[index].getTextExpression();
                       if ( expression != null ) {
                           Object value = expression.evaluate( context );
  -                        return convertToString( value );
  +                        return convertToString( value, attributes[index], context );
                       }
                   }
                   return "";
  @@ -1461,13 +1486,16 @@
         * @param value the Object to represent as a String, possibly null
         * @return String representation, not null
         */
  -    private String convertToString(Object value) {
  -        if ( value != null ) {
  -            String text = ConvertUtils.convert( value );
  -            if ( text != null ) {
  -                return text;
  -            }
  -        }
  -        return "";
  +    private String convertToString( Object value , Descriptor descriptor, Context context ) {
  +        return getBindingConfiguration()
  +            .getObjectStringConverter().objectToString( value, descriptor.getPropertyType(), null, context );
  +    }
  +    
  +    /**
  +      * Factory method for new contexts.
  +      * Ensure that they are correctly configured.
  +      */
  +    private Context makeContext(Object bean) {
  +        return new Context( bean, log, bindingConfiguration );
       }
   }
  
  
  
  1.14      +37 -10    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java
  
  Index: BeanReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- BeanReader.java	1 Jul 2003 19:07:25 -0000	1.13
  +++ BeanReader.java	31 Jul 2003 21:40:58 -0000	1.14
  @@ -67,6 +67,8 @@
   
   import javax.xml.parsers.SAXParser;
   
  +import org.apache.commons.betwixt.expression.Context;
  +import org.apache.commons.betwixt.BindingConfiguration;
   import org.apache.commons.betwixt.ElementDescriptor;
   import org.apache.commons.betwixt.XMLBeanInfo;
   import org.apache.commons.betwixt.XMLIntrospector;
  @@ -92,8 +94,8 @@
       private Log log = LogFactory.getLog( BeanReader.class );
       /** The registered classes */
       private Set registeredClasses = new HashSet();
  -    /** Should the reader use <code>ID</code>'s to match */
  -    private boolean matchIDs = true;
  +    /** Dynamic binding configuration settings */
  +    private BindingConfiguration bindingConfiguration = new BindingConfiguration();
       
       /**
        * Construct a new BeanReader with default properties.
  @@ -300,18 +302,36 @@
        *
        * @return true if <code>ID</code> and <code>IDREF</code> 
        * attributes should be used to match instances
  +     * @deprecated use {@link BindingConfiguration#getMapIDs}
        */
       public boolean getMatchIDs() {
  -        return matchIDs;
  +        return getBindingConfiguration().getMapIDs();
       }
       
       /**
        * Set whether the read should use <code>ID</code> attributes to match beans.
        *
        * @param matchIDs pass true if <code>ID</code>'s should be matched
  +     * @deprecated use {@link BindingConfiguration#setMapIDs}
        */
       public void setMatchIDs(boolean matchIDs) {
  -        this.matchIDs = matchIDs;
  +        getBindingConfiguration().setMapIDs( matchIDs );
  +    }
  +    
  +    /**
  +     * Gets the dynamic configuration setting to be used for bean reading.
  +     * @return the BindingConfiguration settings, not null
  +     */
  +    public BindingConfiguration getBindingConfiguration() {
  +        return bindingConfiguration;
  +    }
  +    
  +    /**
  +     * Sets the dynamic configuration setting to be used for bean reading.
  +     * @param the BindingConfiguration settings, not null
  +     */
  +    public void setBindingConfiguration(BindingConfiguration bindingConfiguration) {
  +        this.bindingConfiguration = bindingConfiguration;
       }
           
       // Implementation methods
  @@ -336,8 +356,15 @@
                                               path ,  
                                               elementDescriptor, 
                                               beanClass, 
  -                                            matchIDs);
  +                                            makeContext( null ));
           addRuleSet( ruleSet );
       }
           
  +    /**
  +      * Factory method for new contexts.
  +      * Ensure that they are correctly configured.
  +      */
  +    private Context makeContext(Object bean) {
  +        return new Context( bean, log, bindingConfiguration );
  +    }
   }
  
  
  
  1.9       +39 -11    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java
  
  Index: BeanRuleSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- BeanRuleSet.java	27 Jul 2003 17:53:57 -0000	1.8
  +++ BeanRuleSet.java	31 Jul 2003 21:40:58 -0000	1.9
  @@ -65,6 +65,8 @@
   import java.util.Map;
   import java.util.Iterator;
   
  +import org.apache.commons.betwixt.expression.Context;
  +import org.apache.commons.betwixt.BindingConfiguration;
   import org.apache.commons.betwixt.AttributeDescriptor;
   import org.apache.commons.betwixt.ElementDescriptor;
   import org.apache.commons.betwixt.TextDescriptor;
  @@ -108,8 +110,8 @@
       private ElementDescriptor baseElementDescriptor;
       /** The bean based  */
       private Class baseBeanClass;
  -    /** Should ID/IDREFs be used to match beans created previously  */
  -    private boolean matchIDs;
  +    /** The (empty) base context from which all Contexts with beans are (directly or indirectly) obtain */
  +    private Context baseContext;
       /** allows an attribute to be specified to overload the types of beans used */
       private String classNameAttribute = "className";
       
  @@ -121,6 +123,7 @@
        * @param baseElementDescriptor the <code>ElementDescriptor</code> used to create the rules
        * @param baseBeanClass the <code>Class</code> whose mapping rules will be created
        * @param matchIDs should ID/IDREFs be used to match beans?
  +     * @deprecated use constructor which takes a base Context
        */
       public BeanRuleSet(
                           XMLIntrospector introspector,
  @@ -132,7 +135,32 @@
           this.basePath = basePath;
           this.baseElementDescriptor = baseElementDescriptor;
           this.baseBeanClass = baseBeanClass;
  -        this.matchIDs = matchIDs;
  +        BindingConfiguration bindingConfiguration = new BindingConfiguration();
  +        bindingConfiguration.setMapIDs( matchIDs );
  +        baseContext = new Context(null, log , bindingConfiguration);
  +    }
  +    
  +    /**
  +     * Base constructor.
  +     *
  +     * @param introspector the <code>XMLIntrospector</code> used to introspect 
  +     * @param basePath specifies the (Digester-style) path under which the rules will be attached
  +     * @param baseElementDescriptor the <code>ElementDescriptor</code> used to create the rules
  +     * @param baseBeanClass the <code>Class</code> whose mapping rules will be created
  +     * @param baseContext the root Context that bean carrying Contexts should be obtained from, 
  +     * not null
  +     */
  +    public BeanRuleSet(
  +                        XMLIntrospector introspector,
  +                        String basePath, 
  +                        ElementDescriptor baseElementDescriptor, 
  +                        Class baseBeanClass,
  +                        Context baseContext) {
  +        this.introspector = introspector;
  +        this.basePath = basePath;
  +        this.baseElementDescriptor = baseElementDescriptor;
  +        this.baseBeanClass = baseBeanClass;
  +        this.baseContext = baseContext;
       }
       
   
  @@ -518,7 +546,7 @@
                           pathPrefix, 
                           descriptor, 
                           beanClass, 
  -                        new Context() );
  +                        baseContext );
               }
               
               /**
  @@ -656,7 +684,7 @@
                           }
                           
                           // add bean for ID matching
  -                        if ( matchIDs ) {
  +                        if ( context.getMapIDs() ) {
                               // XXX need to support custom ID attribute names
                               // XXX i have a feeling that the current mechanism might need to change
                               // XXX so i'm leaving this till later
  @@ -765,7 +793,7 @@
                    * @todo this is a duplicate of the code in BeanCreateRule
                    * we should try refactor to some common place
                    */
  -                if ( matchIDs ) {
  +                if ( context.getMapIDs() ) {
                       String idref = attributes.getValue( "idref" );
                       if ( idref != null ) {
                           // XXX need to check up about ordering
  
  
  
  1.16      +63 -1     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanReader.java
  
  Index: TestBeanReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/TestBeanReader.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- TestBeanReader.java	29 Jul 2003 21:33:31 -0000	1.15
  +++ TestBeanReader.java	31 Jul 2003 21:40:58 -0000	1.16
  @@ -73,6 +73,8 @@
   import java.sql.Timestamp;
   import java.util.List;
   import java.util.Calendar;
  +import java.util.Locale;
  +import java.util.TimeZone;
   import java.text.ParseException;
   import java.text.SimpleDateFormat;
   
  @@ -85,6 +87,7 @@
   import org.apache.commons.beanutils.ConversionException;
   
   import org.apache.commons.betwixt.XMLIntrospector;
  +import org.apache.commons.betwixt.BindingConfiguration;
   import org.apache.commons.betwixt.io.BeanReader;
   import org.apache.commons.betwixt.io.BeanWriter;
   import org.apache.commons.betwixt.io.BeanRuleSet;
  @@ -92,6 +95,7 @@
   import org.apache.commons.betwixt.expression.MapEntryAdder;
   import org.apache.commons.betwixt.expression.MethodUpdater;
   import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
  +import org.apache.commons.betwixt.strategy.ConvertUtilsObjectStringConverter;
   
   import org.apache.commons.digester.Rule;
   import org.apache.commons.digester.ExtendedBaseRules;
  @@ -382,6 +386,62 @@
       }
       
       public void testDateReadConversion() throws Exception {
  +        Calendar calendar = Calendar.getInstance();
  +        calendar.set(2003, 7, 2, 19, 30, 00);
  +        java.util.Date date = calendar.getTime();
  +        
  +        String dateToString = date.toString();
  +        
  +        PartyBean bean = new PartyBean(
  +                "Wedding",
  +                date,
  +                1930,
  +                new AddressBean("Old White Lion Hotel", "Howarth", "Merry Old England", "BD22 8EP"));
  +
  +        StringWriter out = new StringWriter();
  +        out.write("<?xml version='1.0'?>");
  +        
  +        BeanWriter writer = new BeanWriter(out);
  +        XMLIntrospector introspector = writer.getXMLIntrospector();
  +        introspector.setElementNameMapper(new HyphenatedNameMapper());
  +        introspector.setAttributesForPrimitives(false);
  +        
  +        writer.write("party", bean);
  +
  +        String xml = "<?xml version='1.0'?><party>"
  +            + "<venue><street>Old White Lion Hotel</street><city>Howarth</city>"
  +            + "<code>BD22 8EP</code><country>Merry Old England</country></venue>"
  +            + "<date-of-party>" + dateToString 
  +            + "</date-of-party><from-hour>1930</from-hour>"
  +            + "<excuse>Wedding</excuse>"
  +            + "</party>";
  +        
  +        xmlAssertIsomorphic(parseString(xml), parseString(out) , true);
  +        
  +        BeanReader reader = new BeanReader();
  +        reader.setXMLIntrospector(introspector);
  +        reader.registerBeanClass("party", PartyBean.class);
  +        PartyBean readBean = (PartyBean) reader.parse(new StringReader(xml)); 
  +        
  +        assertEquals("FromHours incorrect property value", readBean.getFromHour(), bean.getFromHour());
  +        assertEquals("Excuse incorrect property value", readBean.getExcuse(), bean.getExcuse());
  +        
  +        // check address
  +        AddressBean readAddress = readBean.getVenue();
  +        AddressBean address = bean.getVenue();
  +        assertEquals("address.street incorrect property value", readAddress.getStreet(), address.getStreet());
  +        assertEquals("address.city incorrect property value", readAddress.getCity(), address.getCity());
  +        assertEquals("address.code incorrect property value", readAddress.getCode(), address.getCode());
  +        assertEquals("address.country incorrect property value", readAddress.getCountry(), address.getCountry());
  +        
  +        // check dates
  +        assertEquals("Incorrect date property", date.toGMTString(), readBean.getDateOfParty().toGMTString());  
  +    }
  + 
  +    public void testCustomDateReadConversion() throws Exception {
  +    
  +        BindingConfiguration configuration = new BindingConfiguration(
  +                                            new ConvertUtilsObjectStringConverter(),false);
       
           //SimpleLog log = new SimpleLog("testDateReadConversion:MethodUpdater");
           //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  @@ -452,6 +512,7 @@
           out.write("<?xml version='1.0'?>");
           
           BeanWriter writer = new BeanWriter(out);
  +        writer.setBindingConfiguration(configuration);
           XMLIntrospector introspector = writer.getXMLIntrospector();
           introspector.setElementNameMapper(new HyphenatedNameMapper());
           introspector.setAttributesForPrimitives(false);
  @@ -468,6 +529,7 @@
           xmlAssertIsomorphic(parseString(xml), parseString(out) , true);
           
           BeanReader reader = new BeanReader();
  +        reader.setBindingConfiguration(configuration);
           reader.setXMLIntrospector(introspector);
           reader.registerBeanClass("party", PartyBean.class);
           PartyBean readBean = (PartyBean) reader.parse(new StringReader(xml)); 
  @@ -497,7 +559,7 @@
           
           ConvertUtils.deregister();
       }
  -    
  +
       
       public void testReadMap() throws Exception {
           // we might as well start by writing out 
  
  
  

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