1 7 package org.jboss.test.xml; 8 9 import java.io.FileInputStream ; 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.Iterator ; 16 import java.util.Calendar ; 17 import junit.framework.TestCase; 18 import org.jboss.logging.Logger; 19 import org.jboss.test.xml.book.Book; 20 import org.jboss.test.xml.book.BookCharacter; 21 import org.jboss.test.xml.book.BookGenericObjectModelFactory; 22 import org.jboss.test.xml.book.BookGenericObjectModelProvider; 23 import org.jboss.test.xml.book.BookObjectFactory; 24 import org.jboss.test.xml.book.BookObjectProvider; 25 import org.jboss.xb.binding.DtdMarshaller; 26 import org.jboss.xb.binding.GenericObjectModelFactory; 27 import org.jboss.xb.binding.Marshaller; 28 import org.jboss.xb.binding.ObjectModelFactory; 29 import org.jboss.xb.binding.ObjectModelProvider; 30 import org.jboss.xb.binding.Unmarshaller; 31 import org.jboss.xb.binding.UnmarshallerFactory; 32 import org.jboss.xb.binding.XsMarshaller; 33 import org.jboss.xb.binding.TypeBinding; 34 import org.jboss.xb.binding.SimpleTypeBindings; 35 36 41 public class SimpleTestCase 42 extends TestCase 43 { 44 private static final Logger log = Logger.getLogger(SimpleTestCase.class); 45 46 public SimpleTestCase(String name) 47 { 48 super(name); 49 } 50 51 public void testUnmarshalBookDtd() throws Exception 52 { 53 ObjectModelFactory factory = new BookObjectFactory(); 55 unmarshalBook("book-dtd.xml", factory); 56 } 57 58 public void testUnmarshalBookXs() throws Exception 59 { 60 ObjectModelFactory factory = new BookObjectFactory(); 62 unmarshalBook("book-xs.xml", factory); 63 } 64 65 public void testUnmarshalBookXsGenericFactory() throws Exception 66 { 67 GenericObjectModelFactory factory = new BookGenericObjectModelFactory(); 69 unmarshalBook("book-xs.xml", factory); 70 } 71 72 public void testMarshallBookDtd() throws Exception 73 { 74 log.debug("<test-marshall-book-dtd>"); 75 76 Book book = createBook(); 78 79 StringWriter xmlOutput = new StringWriter (); 81 82 InputStream is = getResource("xml/book/books.dtd"); 84 Reader dtdReader = new InputStreamReader (is); 85 86 DtdMarshaller marshaller = new DtdMarshaller(); 88 marshaller.addBinding("since", new TypeBinding() 89 { 90 public Object unmarshal(String value) 91 { 92 throw new UnsupportedOperationException ("unmarshal is not implemented."); 94 } 95 96 public String marshal(Object value) 97 { 98 return SimpleTypeBindings.marshalDate((Calendar )value); 99 } 100 } 101 ); 102 103 marshaller.mapPublicIdToSystemId("-//DTD Books//EN", "resources/xml/book/books.dtd"); 105 106 ObjectModelProvider provider = new BookObjectProvider(); 108 109 marshaller.marshal(dtdReader, provider, book, xmlOutput); 111 112 dtdReader.close(); 114 115 checkMarshalledBook(xmlOutput.getBuffer().toString(), book); 116 117 log.debug("</test-marshall-book-dtd>"); 118 } 119 120 public void testMarshallBookXs() throws Exception 121 { 122 log.debug("<test-marshall-book-xs>"); 123 124 Book book = createBook(); 126 127 StringWriter xmlOutput = new StringWriter (); 129 130 InputStream is = getResource("xml/book/books.xsd"); 132 Reader xsReader = new InputStreamReader (is); 133 134 XsMarshaller marshaller = new XsMarshaller(); 136 137 marshaller.addRootElement("http://example.org/ns/books/", "", "book"); 139 140 marshaller.declareNamespace("bk", "http://example.org/ns/books/"); 142 143 marshaller.declareNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 145 marshaller.addAttribute("xsi", "schemaReader", "string", "http://example.org/ns/books/ resources/book/books.xsd"); 146 147 ObjectModelProvider provider = new BookObjectProvider(); 149 150 marshaller.marshal(xsReader, provider, book, xmlOutput); 152 153 xsReader.close(); 155 156 checkMarshalledBook(xmlOutput.toString(), book); 157 158 log.debug("</test-marshall-book-xs>"); 159 } 160 161 public void testMarshallBookDtdGeneric() throws Exception 162 { 163 log.debug("<test-marshall-book-dtd-generic>"); 164 165 Book book = createBook(); 167 168 StringWriter xmlOutput = new StringWriter (); 170 171 InputStream is = getResource("xml/book/books.dtd"); 173 Reader dtdReader = new InputStreamReader (is); 174 175 Marshaller marshaller = new DtdMarshaller(); 177 178 marshaller.mapPublicIdToSystemId("-//DTD Books//EN", "resources/xml/book/books.dtd"); 180 181 ObjectModelProvider provider = new BookGenericObjectModelProvider(); 183 184 marshaller.marshal(dtdReader, provider, book, xmlOutput); 186 187 dtdReader.close(); 189 190 checkMarshalledBook(xmlOutput.getBuffer().toString(), book); 191 192 log.debug("</test-marshall-book-dtd-generic>"); 193 } 194 195 197 private void unmarshalBook(String xmlSource, ObjectModelFactory factory) throws Exception 198 { 199 log.debug("<test-unmarshal-" + xmlSource + '>'); 200 201 InputStream is = new FileInputStream ("resources/xml/book/" + xmlSource); 203 204 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance() 206 .newUnmarshaller(); 207 208 Book book = (Book)unmarshaller.unmarshal(is, factory, null); 210 211 is.close(); 213 214 checkUnmarshalledBook(book); 215 216 log.debug("</test-unmarshal-" + xmlSource + '>'); 217 } 218 219 private void checkMarshalledBook(String content, Book book) 220 throws Exception 221 { 222 log.debug("marshalled content: " + content); 223 224 Book unmarshalled = new Book(); 225 ObjectModelFactory factory = new BookObjectFactory(); 226 227 Unmarshaller reader = UnmarshallerFactory.newInstance() 228 .newUnmarshaller(); 229 230 StringReader strReader = new StringReader (content); 231 reader.unmarshal(strReader, factory, unmarshalled); 232 strReader.close(); 233 234 assertEquals(book, unmarshalled); 235 } 236 237 private void checkUnmarshalledBook(Book book) 238 { 239 log.debug("unmarshalled book: " + book); 240 241 assertEquals("Being a Dog Is a Full-Time Job", book.getTitle()); 242 assertEquals("Charles M. Schulz", book.getAuthor()); 243 assertEquals("0836217462", book.getIsbn()); 244 assertEquals(book.getCharactersTotal(), 2); 245 246 for(Iterator iter = book.getCharacters().iterator(); iter.hasNext();) 247 { 248 BookCharacter character = (BookCharacter)iter.next(); 249 final String name = character.getName(); 250 if(name.equals("Snoopy")) 251 { 252 assertEquals(character.getFriendOf(), "Peppermint Patty"); 253 assertEquals(character.getSince(), "1950-10-04"); 254 assertEquals(character.getQualification(), "extroverted beagle"); 255 } 256 else if(name.equals("Peppermint Patty")) 257 { 258 assertEquals(character.getFriendOf(), null); 259 assertEquals(character.getSince(), "1966-08-22"); 260 assertEquals(character.getQualification(), "bold, brash and tomboyish"); 261 } 262 } 263 } 264 265 private static Book createBook() 266 { 267 Book book = new Book(); 268 book.setIsbn("0836217462"); 269 book.setTitle("Being a Dog Is a Full-Time Job"); 270 book.setAuthor("Charles M. Schulz"); 271 272 BookCharacter character = new BookCharacter(); 273 character.setName("Snoopy"); 274 character.setFriendOf("Peppermint Patty"); 275 character.setSince("1950-10-04"); 276 character.setQualification("extroverted beagle"); 277 book.addCharacter(character); 278 279 character = new BookCharacter(); 280 character.setName("Peppermint Patty"); 281 character.setSince("1966-08-22"); 282 character.setQualification("bold, brash and tomboyish"); 283 book.addCharacter(character); 284 285 return book; 286 } 287 288 private static InputStream getResource(String name) 289 { 290 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); 291 if(is == null) 292 throw new IllegalStateException ("Resource not found: " + name); 293 return is; 294 } 295 } 296 | Popular Tags |