KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream 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.Iterator JavaDoc;
16 import java.util.Calendar JavaDoc;
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 /**
37  *
38  * @version <tt>$Revision: 1.6.2.3 $</tt>
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  */

41 public class SimpleTestCase
42    extends TestCase
43 {
44    private static final Logger log = Logger.getLogger(SimpleTestCase.class);
45    
46    public SimpleTestCase(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    public void testUnmarshalBookDtd() throws Exception JavaDoc
52    {
53       // create an object model factory
54
ObjectModelFactory factory = new BookObjectFactory();
55       unmarshalBook("book-dtd.xml", factory);
56    }
57
58    public void testUnmarshalBookXs() throws Exception JavaDoc
59    {
60       // create an object model factory
61
ObjectModelFactory factory = new BookObjectFactory();
62       unmarshalBook("book-xs.xml", factory);
63    }
64
65    public void testUnmarshalBookXsGenericFactory() throws Exception JavaDoc
66    {
67       // create an object model factory
68
GenericObjectModelFactory factory = new BookGenericObjectModelFactory();
69       unmarshalBook("book-xs.xml", factory);
70    }
71
72    public void testMarshallBookDtd() throws Exception JavaDoc
73    {
74       log.debug("<test-marshall-book-dtd>");
75
76       // obtain an instance of Book to marshal
77
Book book = createBook();
78
79       // get the output writter to write the XML content
80
StringWriter JavaDoc xmlOutput = new StringWriter JavaDoc();
81
82       // get the DTD source
83
InputStream JavaDoc is = getResource("xml/book/books.dtd");
84       Reader JavaDoc dtdReader = new InputStreamReader JavaDoc(is);
85
86       // create an instance of DTD marshaller
87
DtdMarshaller marshaller = new DtdMarshaller();
88       marshaller.addBinding("since", new TypeBinding()
89       {
90          public Object JavaDoc unmarshal(String JavaDoc value)
91          {
92             // todo: implement unmarshal
93
throw new UnsupportedOperationException JavaDoc("unmarshal is not implemented.");
94          }
95
96          public String JavaDoc marshal(Object JavaDoc value)
97          {
98             return SimpleTypeBindings.marshalDate((Calendar JavaDoc)value);
99          }
100       }
101       );
102
103       // map publicId to systemId as it should appear in the resulting XML file
104
marshaller.mapPublicIdToSystemId("-//DTD Books//EN", "resources/xml/book/books.dtd");
105
106       // create an instance of ObjectModelProvider with the book instance to be marshalled
107
ObjectModelProvider provider = new BookObjectProvider();
108
109       // marshal the book
110
marshaller.marshal(dtdReader, provider, book, xmlOutput);
111
112       // close DTD reader
113
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 JavaDoc
121    {
122       log.debug("<test-marshall-book-xs>");
123
124       // obtain an instance of Book to marshal
125
Book book = createBook();
126
127       // get the output writter to write the XML content
128
StringWriter JavaDoc xmlOutput = new StringWriter JavaDoc();
129
130       // get the XML Schema source
131
InputStream JavaDoc is = getResource("xml/book/books.xsd");
132       Reader JavaDoc xsReader = new InputStreamReader JavaDoc(is);
133
134       // create an instance of XML Schema marshaller
135
XsMarshaller marshaller = new XsMarshaller();
136
137       // we need to specify what elements are top most (roots) providing namespace URI, prefix and local name
138
marshaller.addRootElement("http://example.org/ns/books/", "", "book");
139
140       // declare default namespace
141
marshaller.declareNamespace("bk", "http://example.org/ns/books/");
142
143       // add schema location by declaring xsi namespace and adding xsi:schemaReader attribute
144
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       // create an instance of Object Model Provider with no book
148
ObjectModelProvider provider = new BookObjectProvider();
149
150       // marshall Book instance passing it as an argument instead of using the one that is returned by the BookObjectProvider
151
marshaller.marshal(xsReader, provider, book, xmlOutput);
152
153       // close XML Schema reader
154
xsReader.close();
155
156       checkMarshalledBook(xmlOutput.toString(), book);
157
158       log.debug("</test-marshall-book-xs>");
159    }
160
161    public void testMarshallBookDtdGeneric() throws Exception JavaDoc
162    {
163       log.debug("<test-marshall-book-dtd-generic>");
164
165       // obtain an instance of Book to marshal
166
Book book = createBook();
167
168       // get the output writter to write the XML content
169
StringWriter JavaDoc xmlOutput = new StringWriter JavaDoc();
170
171       // get the DTD source
172
InputStream JavaDoc is = getResource("xml/book/books.dtd");
173       Reader JavaDoc dtdReader = new InputStreamReader JavaDoc(is);
174
175       // create an instance of DTD marshaller
176
Marshaller marshaller = new DtdMarshaller();
177
178       // map publicId to systemId as it should appear in the resulting XML file
179
marshaller.mapPublicIdToSystemId("-//DTD Books//EN", "resources/xml/book/books.dtd");
180
181       // create an instance of ObjectModelProvider with the book instance to be marshalled
182
ObjectModelProvider provider = new BookGenericObjectModelProvider();
183
184       // marshal the book
185
marshaller.marshal(dtdReader, provider, book, xmlOutput);
186
187       // close DTD reader
188
dtdReader.close();
189
190       checkMarshalledBook(xmlOutput.getBuffer().toString(), book);
191
192       log.debug("</test-marshall-book-dtd-generic>");
193    }
194
195    // Private
196

197    private void unmarshalBook(String JavaDoc xmlSource, ObjectModelFactory factory) throws Exception JavaDoc
198    {
199       log.debug("<test-unmarshal-" + xmlSource + '>');
200
201       // get the XML stream
202
InputStream JavaDoc is = new FileInputStream JavaDoc("resources/xml/book/" + xmlSource);
203
204       // create unmarshaller
205
Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
206             .newUnmarshaller();
207
208       // let the object model factory to create an instance of Book and populate it with data from XML
209
Book book = (Book)unmarshaller.unmarshal(is, factory, null);
210
211       // close the XML stream
212
is.close();
213
214       checkUnmarshalledBook(book);
215
216       log.debug("</test-unmarshal-" + xmlSource + '>');
217    }
218
219    private void checkMarshalledBook(String JavaDoc content, Book book)
220       throws Exception JavaDoc
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 JavaDoc strReader = new StringReader JavaDoc(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 JavaDoc iter = book.getCharacters().iterator(); iter.hasNext();)
247       {
248          BookCharacter character = (BookCharacter)iter.next();
249          final String JavaDoc 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 JavaDoc getResource(String JavaDoc name)
289    {
290       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
291       if(is == null)
292          throw new IllegalStateException JavaDoc("Resource not found: " + name);
293       return is;
294    }
295 }
296
Popular Tags