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/io/read TestMaps.java
Date Sat, 01 May 2004 09:42:22 GMT
rdonkin     2004/05/01 02:42:22

  Modified:    betwixt/src/java/org/apache/commons/betwixt Tag:
                        REFACTORING-BRANCH_2004-01-13 XMLIntrospector.java
               betwixt/src/java/org/apache/commons/betwixt/expression Tag:
                        REFACTORING-BRANCH_2004-01-13 MapEntryAdder.java
               betwixt/src/java/org/apache/commons/betwixt/io/read Tag:
                        REFACTORING-BRANCH_2004-01-13 ArrayBindAction.java
                        BeanBindAction.java ReadContext.java
               betwixt/src/java/org/apache/commons/betwixt/strategy Tag:
                        REFACTORING-BRANCH_2004-01-13
                        DefaultActionMappingStrategy.java
               betwixt/src/test/org/apache/commons/betwixt/io/read Tag:
                        REFACTORING-BRANCH_2004-01-13 TestMaps.java
  Log:
  Added support for array valued adders
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.27.2.12 +21 -4     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.27.2.11
  retrieving revision 1.27.2.12
  diff -u -r1.27.2.11 -r1.27.2.12
  --- XMLIntrospector.java	18 Apr 2004 09:48:06 -0000	1.27.2.11
  +++ XMLIntrospector.java	1 May 2004 09:42:22 -0000	1.27.2.12
  @@ -826,6 +826,9 @@
                                         
                           children[n].setUpdater( adder.getKeyUpdater() );
                           children[n].setSingularPropertyType(  keyType );
  +                        if (children[n].getPropertyType() == null) {
  +                            children[n].setPropertyType( valueType );
  +                        }
                           if ( isPrimitiveType(keyType) ) {
                               children[n].setHollow(false);
                           }
  @@ -837,8 +840,22 @@
   
                           children[n].setUpdater( adder.getValueUpdater() );
                           children[n].setSingularPropertyType( valueType );
  +                        if (children[n].getPropertyType() == null) {
  +                            children[n].setPropertyType( valueType );
  +                        }
                           if ( isPrimitiveType( valueType) ) {
                               children[n].setHollow(false);
  +                        }
  +                        if ( isLoopType( valueType )) {
  +                            // need to attach a hollow descriptor
  +                            // don't know the element name
  +                            // so use null name (to match anything)
  +                            ElementDescriptor loopDescriptor = new ElementDescriptor();
  +                            loopDescriptor.setHollow(true);
  +                            loopDescriptor.setSingularPropertyType( valueType );
  +                            loopDescriptor.setPropertyType( valueType );
  +                            children[n].addElementDescriptor(loopDescriptor);
  +                            
                           }
                           if ( getLog().isTraceEnabled() ) { 
                               getLog().trace( "Value descriptor: " + children[n]);
  
  
  
  No                   revision
  No                   revision
  1.4.2.2   +18 -4     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.4.2.1
  retrieving revision 1.4.2.2
  diff -u -r1.4.2.1 -r1.4.2.2
  --- MapEntryAdder.java	29 Mar 2004 20:50:21 -0000	1.4.2.1
  +++ MapEntryAdder.java	1 May 2004 09:42:22 -0000	1.4.2.2
  @@ -60,7 +60,9 @@
    */ 
   package org.apache.commons.betwixt.expression;
   
  +import java.lang.reflect.Array;
   import java.lang.reflect.Method;
  +import java.util.Collection;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -243,6 +245,18 @@
                   value = context.getObjectStringConverter()
                           .stringToObject( (String) value, valueType, null, context );
               }
  +            
  +            // special case for collection objects into arrays                    
  +            if (value instanceof Collection && valueType.isArray()) {
  +                Collection valuesAsCollection = (Collection) value;
  +                Class componentType = valueType.getComponentType();
  +                if (componentType != null) {
  +                    Object[] valuesAsArray = 
  +                        (Object[]) Array.newInstance(componentType, valuesAsCollection.size());
  +                    value = valuesAsCollection.toArray(valuesAsArray);
  +                }
  +            }
  +            
                    
               Object[] arguments = { key, value };
               try {
  
  
  
  No                   revision
  No                   revision
  1.1.2.2   +2 -2      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/ArrayBindAction.java
  
  Index: ArrayBindAction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/ArrayBindAction.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- ArrayBindAction.java	27 Apr 2004 19:58:40 -0000	1.1.2.1
  +++ ArrayBindAction.java	1 May 2004 09:42:22 -0000	1.1.2.2
  @@ -95,7 +95,7 @@
           ReadContext context)
           throws Exception {
           originalUpdater = context.getCurrentUpdater();
  -        MappingAction nextBindAction = super.next(namespace, name, attributes, context);
  +        MappingAction nextBindAction = BeanBindAction.INSTANCE;
           beanMapping.setDelegate(nextBindAction);
           return beanMapping;
       }
  
  
  
  1.1.2.13  +11 -10    jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/BeanBindAction.java
  
  Index: BeanBindAction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/BeanBindAction.java,v
  retrieving revision 1.1.2.12
  retrieving revision 1.1.2.13
  diff -u -r1.1.2.12 -r1.1.2.13
  --- BeanBindAction.java	27 Apr 2004 20:00:54 -0000	1.1.2.12
  +++ BeanBindAction.java	1 May 2004 09:42:22 -0000	1.1.2.13
  @@ -96,10 +96,6 @@
   
           ElementDescriptor computedDescriptor = context.getCurrentDescriptor();
   
  -        if (computedDescriptor == null) {
  -            log.trace("No Descriptor");
  -        }
  -
           if (log.isTraceEnabled()) {
               log.trace("Element Pushed: " + name);
           }
  @@ -108,7 +104,12 @@
           MappingAction action = MappingAction.EMPTY;
   
           Object instance = null;
  -        Class beanClass = computedDescriptor.getSingularPropertyType();
  +        Class beanClass = null;
  +        if (computedDescriptor == null) {
  +            log.trace("No Descriptor");
  +        } else {
  +            beanClass = computedDescriptor.getSingularPropertyType();
  +        }
           // TODO: this is a bit of a workaround 
           // need to come up with a better way of doing maps
           if (beanClass != null && !Map.class.isAssignableFrom(beanClass)) {
  
  
  
  1.4.2.12  +7 -4      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ReadContext.java
  
  Index: ReadContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ReadContext.java,v
  retrieving revision 1.4.2.11
  retrieving revision 1.4.2.12
  diff -u -r1.4.2.11 -r1.4.2.12
  --- ReadContext.java	19 Apr 2004 21:27:23 -0000	1.4.2.11
  +++ ReadContext.java	1 May 2004 09:42:22 -0000	1.4.2.12
  @@ -336,6 +336,9 @@
   	  * @param mappedClazz the Class which has been mapped at the current path, not null
   	  */
   	public void markClassMap(Class mappedClazz) throws IntrospectionException {
  +        if (mappedClazz.isArray()) {
  +            mappedClazz = mappedClazz.getComponentType();
  +        }
   		elementMappingStack.push(mappedClazz);
           
           XMLBeanInfo mappedClassInfo = getXMLIntrospector().introspect(mappedClazz);
  
  
  
  No                   revision
  No                   revision
  1.1.2.2   +6 -3      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/Attic/DefaultActionMappingStrategy.java
  
  Index: DefaultActionMappingStrategy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/Attic/DefaultActionMappingStrategy.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- DefaultActionMappingStrategy.java	27 Apr 2004 19:59:04 -0000	1.1.2.1
  +++ DefaultActionMappingStrategy.java	1 May 2004 09:42:22 -0000	1.1.2.2
  @@ -50,8 +50,11 @@
           ElementDescriptor activeDescriptor = context.getCurrentDescriptor();
           if (activeDescriptor != null) {
               if (activeDescriptor.isHollow()) {
  -                result = BeanBindAction.INSTANCE;
  -                
  +                if (isArrayDescriptor(activeDescriptor)) {
  +                    result = ArrayBindAction.createMappingAction(activeDescriptor);
  +                } else {
  +                    result = BeanBindAction.INSTANCE;
  +                }
               }
               else if (activeDescriptor.isSimple())
               {
  
  
  
  No                   revision
  No                   revision
  1.1.2.2   +66 -4     jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/read/Attic/TestMaps.java
  
  Index: TestMaps.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/read/Attic/TestMaps.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- TestMaps.java	22 Feb 2004 17:09:29 -0000	1.1.2.1
  +++ TestMaps.java	1 May 2004 09:42:22 -0000	1.1.2.2
  @@ -132,4 +132,66 @@
    
       }
   
  +    public void testMapWithArray() throws Exception {
  +
  +        AddressBook addressBook = new AddressBook();
  +        AddressBean[] johnsAddresses = new AddressBean[2];
  +        johnsAddresses[0] = new AddressBean("12 here", "Chicago", "USA", "1234");
  +        johnsAddresses[1] =
  +            new AddressBean("333 there", "Los Angeles", "USA", "99999");
  +        String name = "John";
  +        addressBook.addAddressBookItem(name, johnsAddresses);
  +        StringWriter outputWriter = new StringWriter();
  +        outputWriter.write("<?xml version='1.0' ?>\n");
  +        BeanWriter beanWriter = new BeanWriter(outputWriter);
  +        beanWriter.enablePrettyPrint();
  +        beanWriter.write(addressBook);
  +    
  +        String xml =
  +            "<?xml version='1.0' ?>\n"
  +                + "  <AddressBook id=\"1\">\n"
  +                + "    <addressBookItems>\n"
  +                + "      <entry id=\"2\">\n"
  +                + "        <key>John</key>\n"
  +                + "        <value id=\"3\">\n"
  +                + "          <AddressBean id=\"4\">\n"
  +                + "            <city>Chicago</city>\n"
  +                + "            <code>1234</code>\n"
  +                + "            <country>USA</country>\n"
  +                + "            <street>12 here</street>\n"
  +                + "          </AddressBean>\n"
  +                + "          <AddressBean id=\"5\">\n"
  +                + "            <city>Los Angeles</city>\n"
  +                + "            <code>99999</code>\n"
  +                + "            <country>USA</country>\n"
  +                + "            <street>333 there</street>\n"
  +                + "          </AddressBean>\n"
  +                + "        </value>\n"
  +                + "      </entry>\n"
  +                + "    </addressBookItems>\n"
  +                + "  </AddressBook>\n";
  +    
  +        assertEquals(xml, outputWriter.toString());
  +        BeanReader reader = new BeanReader();
  +        reader.registerBeanClass(AddressBook.class);
  +        StringReader xmlReader = new StringReader(outputWriter.toString());
  +        AddressBook result = (AddressBook) reader.parse(xmlReader);
  +        assertNotNull("Expected to get an AddressBook!", result);
  +        assertNotNull(
  +            "Expected AddressBook to have some address entryitems!",
  +            result.getAddressBookItems());
  +        AddressBean[] resultAddresses =
  +            (AddressBean[]) result.getAddressBookItems().get(name);
  +        assertNotNull(
  +            "Expected to have some addresses for " + name,
  +            resultAddresses);
  +        assertEquals(
  +            "Got wrong city in first address for " + name,
  +            johnsAddresses[0].getCity(),
  +            resultAddresses[0].getCity());
  +        assertEquals(
  +            "Got wrong city in second address for " + name,
  +            johnsAddresses[1].getCity(),
  +            resultAddresses[1].getCity());
  +    }
   }
  
  
  

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