commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [betwixt] Problem with Maps and Arrays
Date Sat, 01 May 2004 10:00:06 GMT
hi brian

i've added code supporting the test case you gave me. it's new and  
probably under-tested so feedback would be appreciated.

add this functionality was actually quite a nice test of the refactored  
reading code. creating special actions to provide this functionality  
was clean and (relatively) easy. this was one of the major aims of the  
refactoring and i'm now confident that the design's about right and  
that the code's approaching the time when it's ready to be merged back  
into the main stream.

- robert

On 16 Apr 2004, at 22:00, b p wrote:

> Hi Robert,
>
> I've hit a problem with Maps and arrays that I haven't been able to  
> resolve.  I would like to write/read a Map that has an array (either  
> java array or collection) as the values of the map.  When I write,  
> everything looks ok.  When I read back in, I'm getting a map with no  
> values.  If I wrap the array or collection in another bean (so the  
> value of the map is a bean, not an array or collection), it will work.  
> I can live with that if I need to,  but I would prefer to have the map  
> values be arrays or collections directly.  I haven't been able to  
> track down a good way to do that-- I'm hoping you'll see a relatively  
> easy solution I'm missing :).
>
> Below is a test case that shows the problem.  Any suggestions?
>
> Thanks,
> Brian
>
>
> AddressBook.java
> ---------------------------------
> package org.apache.commons.betwixt.io.read;
> import java.util.HashMap;
> /**
>  * @author Brian Pugh
>  */
> public class AddressBook {
>   private HashMap addressesMap = new HashMap();
>
>   public HashMap getAddressBookItems() {
>     return addressesMap;
>   }
>   public void setAddressBookItems(HashMap map) {
>     this.addressesMap = map;
>   }
>   public void addAddressBookItem(String name, AddressBean[] addresses)  
> {
>     addressesMap.put(name, addresses);
>   }
>
> }
>
>
> Test case (patch to TestMaps.java)
> ----------------------------------------------------
>
> Index: src/test/org/apache/commons/betwixt/io/read/TestMaps.java
> ===================================================================
> RCS file:  
> /home/cvspublic/jakarta-commons/betwixt/src/test/org/apache/commons/ 
> betwixt/io/read/Attic/TestMaps.java,v
> retrieving revision 1.1.2.1
> diff -u -r1.1.2.1 TestMaps.java
> --- src/test/org/apache/commons/betwixt/io/read/TestMaps.java 22 Feb  
> 2004 17:09:29 -0000 1.1.2.1
> +++ src/test/org/apache/commons/betwixt/io/read/TestMaps.java 16 Apr  
> 2004 20:46:55 -0000
> @@ -132,4 +132,60 @@
>
>      }
>
> +    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);
> +      System.out.println(outputWriter.toString());
> +      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());
> +
> +
> +  }
> +
>  }
>
>
>
>
> 		
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th


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


Mime
View raw message