My attachment appears to have been stripped, presumably by my corporate
firewall.
Please find the code inline below:

import java.io.*;
import java.util.*;

import junit.framework.*;

import org.exolab.castor.xml.*;

public class TestCastorCollectionsXml extends TestCase
{
    public void testString() throws Exception
    {
        String s = "My String";
        String xml = marshal(s);
        String newS = (String) unmarshal(xml, s.getClass());
        assertEquals(s, newS);
    }

    public void testArrayList() throws Exception
    {
        List list = new ArrayList();
        for (int i = 0; i < 5; i++)
        {
            list.add("Item " + i);
        }

        String xml = marshal(list);
        Object newList = unmarshal(xml, list.getClass());
        assertEquals(list, newList);
    }

    public void testHashMap() throws Exception
    {
        Map map = new HashMap();
        for (int i = 0; i < 5; i++)
        {
            map.put("Key " + i, "Value " + i);
        }

        String xml = marshal(map);
        Object newMap = unmarshal(xml, map.getClass());
        assertEquals(map, newMap);

    }

    public void testHashSet() throws Exception
    {
        Set set = new HashSet();
        for (int i = 0; i < 5; i++)
        {
            set.add("Item " + i);
        }

        String xml = marshal(set);
        Object newSet = unmarshal(xml, set.getClass());
        assertEquals(set, newSet);
    }

    protected String marshal(Object object) throws Exception
    {
        String xml = null;

        try
        {
            StringWriter writer = new StringWriter();
            Marshaller marshaller = new Marshaller(writer);
            marshaller.marshal(object);
            writer.close();
            xml = writer.getBuffer().toString();
        }
        finally
        {
            System.out.println("\nMarshal");
            System.out.println("Object: " + object);
            System.out.println("XML:\n" + xml);
        }

        return xml;
    }

    protected Object unmarshal(String xml, Class clazz) throws Exception
    {
        Object object = null;

        try
        {
            StringReader reader = new StringReader(xml);
            Unmarshaller unmarshaller = new Unmarshaller(clazz);
            object = unmarshaller.unmarshal(reader);
            reader.close();
        }
        finally
        {
            System.out.println("\nUnmarshal");
            System.out.println("Object: " + object);
            System.out.println("XML:\n" + xml);
        }

        return object;
    }
}

Paul Smith


-----Original Message-----
From: Smith, Paul: IT (LDN) 
Sent: 25 October 2005 07:56
To: [email protected]
Subject: [castor-user] Xml Marshal/Unmarshal for Java Collections Fails


Hi,

I am having some difficulty working with Castor (castor-0.9.9) to
marshal/unmarshal Java collections. I expected the attached JUnit
test-case to pass, it simply marshals and then unmarshals various Java
collection implementations that contain three Strings. It fails for all
collections other than ArrayList.

The ArrayList test produces sensible xml, but for the others it produces
(example is HashMap):
        <hash-map empty="false"/>

I have tried writing my own CollectionHandler which correctly deals with
HashMap. I can see that it is being called by Castor (from debugging
CollectionHandlers.getCollectionType) and I am sure that the
implementation is correct but the same xml is still produced.

Any ideas? What am I missing?

 <<TestCastorCollectionsXml.java>> 
Thanks

Paul Smith


------------------------------------------------------------------------
For more information about Barclays Capital, please
visit our web site at http://www.barcap.com.


Internet communications are not secure and therefore the Barclays 
Group does not accept legal responsibility for the contents of this 
message.  Although the Barclays Group operates anti-virus programmes, 
it does not accept responsibility for any damage whatsoever that is 
caused by viruses being passed.  Any views or opinions presented are 
solely those of the author and do not necessarily represent those of the 
Barclays Group.  Replies to this email may be monitored by the Barclays 
Group for operational or business reasons.

------------------------------------------------------------------------


-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to