KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > MappingTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.xml;
8
9 import java.io.FileReader JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.io.StringReader JavaDoc;
14 import java.io.StringWriter JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import junit.framework.TestCase;
18 import org.jboss.logging.Logger;
19 import org.jboss.test.xml.person.Address;
20 import org.jboss.test.xml.person.Person;
21 import org.jboss.xb.binding.DtdMarshaller;
22 import org.jboss.xb.binding.MappingObjectModelFactory;
23 import org.jboss.xb.binding.MappingObjectModelProvider;
24 import org.jboss.xb.binding.Marshaller;
25 import org.jboss.xb.binding.SimpleTypeBindings;
26 import org.jboss.xb.binding.TypeConverter;
27 import org.jboss.xb.binding.Unmarshaller;
28 import org.jboss.xb.binding.UnmarshallerFactory;
29
30
31 /**
32  *
33  * @version <tt>$Revision: 1.2.2.4 $</tt>
34  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
35  */

36 public class MappingTestCase
37    extends TestCase
38 {
39    private static final Logger log = Logger.getLogger(MappingTestCase.class);
40    
41    public MappingTestCase(String JavaDoc name)
42    {
43       super(name);
44    }
45
46    public void testUnmarshalling() throws Exception JavaDoc
47    {
48       Reader JavaDoc xmlReader = new FileReader JavaDoc("resources/xml/person.xml");
49
50       Person person = unmarshalPerson(xmlReader);
51       xmlReader.close();
52
53       assertNotNull("Person is null", person);
54       assertEquals(person.getFirstName(), "Vasiliy");
55       assertEquals(person.getLastName(), "Poupkin");
56       assertEquals(person.getDateOfBirth(), TypeConverter.JAVA_UTIL_DATE.unmarshal("1980-01-01"));
57
58       assertEquals(person.getPhones(), Arrays.asList(new Object JavaDoc[]{"01", "02"}));
59
60       ArrayList JavaDoc list = new ArrayList JavaDoc();
61       Address addr1 = new Address();
62       addr1.setStreet("prosp. Rad. Ukr. 11A, 70");
63       list.add(addr1);
64       addr1 = new Address();
65       addr1.setStreet("Sky 7");
66       list.add(addr1);
67       assertEquals(person.getAddresses(), list);
68    }
69
70    public void testMarshalling() throws Exception JavaDoc
71    {
72       log.debug("<test-mapping-marshalling>");
73
74       final Person person = Person.newInstance();
75       StringWriter JavaDoc xmlOutput = new StringWriter JavaDoc();
76
77       InputStream JavaDoc is = getResource("xml/person.dtd");
78       Reader JavaDoc dtdReader = new InputStreamReader JavaDoc(is);
79
80       // create an instance of DTD marshaller
81
Marshaller marshaller = new DtdMarshaller();
82
83       // map publicId to systemId as it should appear in the resulting XML file
84
marshaller.mapPublicIdToSystemId("-//DTD Person//EN", "resources/xml/person.dtd");
85
86       // create an instance of ObjectModelProvider with the book instance to be marshalled
87
MappingObjectModelProvider provider = new MappingObjectModelProvider();
88       provider.mapFieldToElement(Person.class, "dateOfBirth", "", "date-of-birth", SimpleTypeBindings.JAVA_UTIL_DATE);
89
90       // marshal the book
91
marshaller.marshal(dtdReader, provider, person, xmlOutput);
92
93       // close DTD reader
94
dtdReader.close();
95
96       final String JavaDoc xml = xmlOutput.getBuffer().toString();
97       log.debug("marshalled: " + xml);
98
99       // check unmarshalled person
100
Person unmarshalled = unmarshalPerson(new StringReader JavaDoc(xml));
101       assertEquals(person, unmarshalled);
102
103       log.debug("</test-mapping-marshalling>");
104    }
105
106    private Person unmarshalPerson(Reader JavaDoc xmlReader)
107       throws Exception JavaDoc
108    {
109       MappingObjectModelFactory factory = new MappingObjectModelFactory();
110       factory.mapElementToClass("person", Person.class);
111       factory.mapElementToField("date-of-birth", Person.class, "dateOfBirth", SimpleTypeBindings.JAVA_UTIL_DATE);
112       factory.mapElementToClass("phones", ArrayList JavaDoc.class);
113       factory.mapElementToClass("addresses", ArrayList JavaDoc.class);
114       factory.mapElementToClass("address", Address.class);
115
116       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
117             .newUnmarshaller();
118
119       return (Person)unmarshaller.unmarshal(xmlReader, factory, null);
120    }
121
122    private static InputStream JavaDoc getResource(String JavaDoc name)
123    {
124       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
125       if(is == null)
126          throw new IllegalStateException JavaDoc("Resource not found: " + name);
127       return is;
128    }
129 }
Popular Tags