1 7 package org.jboss.test.xml; 8 9 import java.io.FileReader ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.Reader ; 13 import java.io.StringReader ; 14 import java.io.StringWriter ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 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 36 public class MappingTestCase 37 extends TestCase 38 { 39 private static final Logger log = Logger.getLogger(MappingTestCase.class); 40 41 public MappingTestCase(String name) 42 { 43 super(name); 44 } 45 46 public void testUnmarshalling() throws Exception 47 { 48 Reader xmlReader = new FileReader ("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 []{"01", "02"})); 59 60 ArrayList list = new ArrayList (); 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 71 { 72 log.debug("<test-mapping-marshalling>"); 73 74 final Person person = Person.newInstance(); 75 StringWriter xmlOutput = new StringWriter (); 76 77 InputStream is = getResource("xml/person.dtd"); 78 Reader dtdReader = new InputStreamReader (is); 79 80 Marshaller marshaller = new DtdMarshaller(); 82 83 marshaller.mapPublicIdToSystemId("-//DTD Person//EN", "resources/xml/person.dtd"); 85 86 MappingObjectModelProvider provider = new MappingObjectModelProvider(); 88 provider.mapFieldToElement(Person.class, "dateOfBirth", "", "date-of-birth", SimpleTypeBindings.JAVA_UTIL_DATE); 89 90 marshaller.marshal(dtdReader, provider, person, xmlOutput); 92 93 dtdReader.close(); 95 96 final String xml = xmlOutput.getBuffer().toString(); 97 log.debug("marshalled: " + xml); 98 99 Person unmarshalled = unmarshalPerson(new StringReader (xml)); 101 assertEquals(person, unmarshalled); 102 103 log.debug("</test-mapping-marshalling>"); 104 } 105 106 private Person unmarshalPerson(Reader xmlReader) 107 throws Exception 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 .class); 113 factory.mapElementToClass("addresses", ArrayList .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 getResource(String name) 123 { 124 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); 125 if(is == null) 126 throw new IllegalStateException ("Resource not found: " + name); 127 return is; 128 } 129 } | Popular Tags |