001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xml;
023:
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.Reader;
027: import java.io.StringReader;
028: import java.io.StringWriter;
029: import java.net.URL;
030: import java.util.Calendar;
031: import java.util.Iterator;
032: import org.jboss.logging.Logger;
033: import org.jboss.test.xml.book.Book;
034: import org.jboss.test.xml.book.BookCharacter;
035: import org.jboss.test.xml.book.BookGenericObjectModelFactory;
036: import org.jboss.test.xml.book.BookGenericObjectModelProvider;
037: import org.jboss.test.xml.book.BookObjectFactory;
038: import org.jboss.test.xml.book.BookObjectProvider;
039: import org.jboss.xb.binding.AbstractMarshaller;
040: import org.jboss.xb.binding.DtdMarshaller;
041: import org.jboss.xb.binding.GenericObjectModelFactory;
042: import org.jboss.xb.binding.Marshaller;
043: import org.jboss.xb.binding.ObjectModelFactory;
044: import org.jboss.xb.binding.ObjectModelProvider;
045: import org.jboss.xb.binding.SimpleTypeBindings;
046: import org.jboss.xb.binding.TypeBinding;
047: import org.jboss.xb.binding.Unmarshaller;
048: import org.jboss.xb.binding.UnmarshallerFactory;
049: import org.jboss.xb.binding.XercesXsMarshaller;
050: import junit.framework.TestCase;
051:
052: /**
053: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
054: * @version <tt>$Revision: 57211 $</tt>
055: */
056: public class SimpleTestCase extends TestCase {
057: private static final Logger log = Logger
058: .getLogger(SimpleTestCase.class);
059:
060: public SimpleTestCase(String name) {
061: super (name);
062: }
063:
064: public void testUnmarshalBookDtd() throws Exception {
065: // create an object model factory
066: ObjectModelFactory factory = new BookObjectFactory();
067: unmarshalBook("book-dtd.xml", factory);
068: }
069:
070: public void testUnmarshalBookXs() throws Exception {
071: // create an object model factory
072: ObjectModelFactory factory = new BookObjectFactory();
073: unmarshalBook("book-xs.xml", factory);
074: }
075:
076: public void testUnmarshalBookXsGenericFactory() throws Exception {
077: // create an object model factory
078: GenericObjectModelFactory factory = new BookGenericObjectModelFactory();
079: unmarshalBook("book-xs.xml", factory);
080: }
081:
082: public void testMarshallBookDtd() throws Exception {
083: log.debug("--- " + getName());
084:
085: // obtain an instance of Book to marshal
086: Book book = createBook();
087:
088: // get the output writter to write the XML content
089: StringWriter xmlOutput = new StringWriter();
090:
091: // get the DTD source
092: InputStream is = getResource("xml/book/books.dtd");
093: Reader dtdReader = new InputStreamReader(is);
094:
095: // create an instance of DTD marshaller
096: DtdMarshaller marshaller = new DtdMarshaller();
097: marshaller.addBinding("since", new TypeBinding() {
098: public Object unmarshal(String value) {
099: // todo: implement unmarshal
100: throw new UnsupportedOperationException(
101: "unmarshal is not implemented.");
102: }
103:
104: public String marshal(Object value) {
105: return SimpleTypeBindings.marshalDate((Calendar) value);
106: }
107: });
108:
109: // map publicId to systemId as it should appear in the resulting XML file
110: marshaller.mapPublicIdToSystemId("-//DTD Books//EN",
111: "resources/xml/book/books.dtd");
112:
113: // create an instance of ObjectModelProvider with the book instance to be marshalled
114: ObjectModelProvider provider = new BookObjectProvider();
115:
116: // marshal the book
117: marshaller.marshal(dtdReader, provider, book, xmlOutput);
118:
119: // close DTD reader
120: dtdReader.close();
121:
122: String xml = xmlOutput.getBuffer().toString();
123: checkMarshalledBook(xml, book);
124: }
125:
126: public void testMarshallBookXercesXs() throws Exception {
127: log.debug("--- " + getName());
128:
129: System.setProperty(Marshaller.PROP_MARSHALLER,
130: XercesXsMarshaller.class.getName());
131: marshallingTest();
132: }
133:
134: public void testMarshallBookDtdGeneric() throws Exception {
135: log.debug("--- " + getName());
136:
137: // obtain an instance of Book to marshal
138: Book book = createBook();
139:
140: // get the output writter to write the XML content
141: StringWriter xmlOutput = new StringWriter();
142:
143: // get the DTD source
144: InputStream is = getResource("xml/book/books.dtd");
145: Reader dtdReader = new InputStreamReader(is);
146:
147: // create an instance of DTD marshaller
148: Marshaller marshaller = new DtdMarshaller();
149:
150: // map publicId to systemId as it should appear in the resulting XML file
151: marshaller.mapPublicIdToSystemId("-//DTD Books//EN",
152: "resources/xml/book/books.dtd");
153:
154: // create an instance of ObjectModelProvider with the book instance to be marshalled
155: ObjectModelProvider provider = new BookGenericObjectModelProvider();
156:
157: // marshal the book
158: marshaller.marshal(dtdReader, provider, book, xmlOutput);
159:
160: // close DTD reader
161: dtdReader.close();
162:
163: String xml = xmlOutput.getBuffer().toString();
164: if (log.isTraceEnabled()) {
165: log.trace("marshalled with dtd: " + xml);
166: }
167: checkMarshalledBook(xml, book);
168: }
169:
170: // Private
171:
172: private void marshallingTest() throws Exception {
173: // obtain an instance of Book to marshal
174: Book book = createBook();
175:
176: // get the output writter to write the XML content
177: StringWriter xmlOutput = new StringWriter();
178:
179: // create an instance of XML Schema marshaller
180: AbstractMarshaller marshaller = (AbstractMarshaller) Marshaller.FACTORY
181: .getInstance();
182:
183: // we need to specify what elements are top most (roots) providing namespace URI, prefix and local name
184: marshaller.addRootElement("http://example.org/ns/books/", "",
185: "book");
186:
187: // declare default namespace
188: marshaller.declareNamespace(null,
189: "http://example.org/ns/books/");
190:
191: // add schema location by declaring xsi namespace and adding xsi:schemaReader attribute
192: marshaller.declareNamespace("xsi",
193: "http://www.w3.org/2001/XMLSchema-instance");
194: marshaller
195: .addAttribute("xsi", "schemaReader", "string",
196: "http://example.org/ns/books/ resources/book/books.xsd");
197:
198: // create an instance of Object Model Provider with no book
199: ObjectModelProvider provider = new BookObjectProvider();
200:
201: // marshall Book instance passing it as an argument instead of using the one that is returned by the BookObjectProvider
202: marshaller.marshal(getResourceUrl("xml/book/books.xsd")
203: .toString(), provider, book, xmlOutput);
204:
205: String xml = xmlOutput.getBuffer().toString();
206: if (log.isTraceEnabled()) {
207: log.debug("marshalled with "
208: + marshaller.getClass().getName() + ": " + xml);
209: }
210: checkMarshalledBook(xml, book);
211: }
212:
213: private void unmarshalBook(String xmlSource,
214: ObjectModelFactory factory) throws Exception {
215: log.debug("<test-unmarshal-" + xmlSource + '>');
216:
217: // get the XML stream
218: URL xmlUrl = getResourceUrl("xml/book/" + xmlSource);
219:
220: // create unmarshaller
221: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
222: .newUnmarshaller();
223:
224: // let the object model factory to create an instance of Book and populate it with data from XML
225: Book book = (Book) unmarshaller.unmarshal(xmlUrl.openStream(),
226: factory, null);
227:
228: checkUnmarshalledBook(book);
229:
230: log.debug("</test-unmarshal-" + xmlSource + '>');
231: }
232:
233: private void checkMarshalledBook(String content, Book book)
234: throws Exception {
235: Book unmarshalled = new Book();
236: ObjectModelFactory factory = new BookObjectFactory();
237:
238: Unmarshaller reader = UnmarshallerFactory.newInstance()
239: .newUnmarshaller();
240:
241: StringReader strReader = new StringReader(content);
242: reader.unmarshal(strReader, factory, unmarshalled);
243: strReader.close();
244:
245: assertEquals(book, unmarshalled);
246: }
247:
248: private void checkUnmarshalledBook(Book book) {
249: log.debug("unmarshalled book: " + book);
250:
251: assertEquals("Being a Dog Is a Full-Time Job", book.getTitle());
252: assertEquals("Charles M. Schulz", book.getAuthor());
253: assertEquals("0836217462", book.getIsbn());
254: assertEquals(book.getCharactersTotal(), 2);
255:
256: for (Iterator iter = book.getCharacters().iterator(); iter
257: .hasNext();) {
258: BookCharacter character = (BookCharacter) iter.next();
259: final String name = character.getName();
260: if (name.equals("Snoopy")) {
261: assertEquals(character.getFriendOf(),
262: "Peppermint Patty");
263: assertEquals(character.getSince(), "1950-10-04");
264: assertEquals(character.getQualification(),
265: "extroverted beagle");
266: } else if (name.equals("Peppermint Patty")) {
267: assertEquals(character.getFriendOf(), null);
268: assertEquals(character.getSince(), "1966-08-22");
269: assertEquals(character.getQualification(),
270: "bold, brash and tomboyish");
271: }
272: }
273: }
274:
275: private static Book createBook() {
276: Book book = new Book();
277: book.setIsbn("0836217462");
278: book.setTitle("Being a Dog Is a Full-Time Job");
279: book.setAuthor("Charles M. Schulz");
280:
281: BookCharacter character = new BookCharacter();
282: character.setName("Snoopy");
283: character.setFriendOf("Peppermint Patty");
284: character.setSince("1950-10-04");
285: character.setQualification("extroverted beagle");
286: book.addCharacter(character);
287:
288: character = new BookCharacter();
289: character.setName("Peppermint Patty");
290: character.setSince("1966-08-22");
291: character.setQualification("bold, brash and tomboyish");
292: book.addCharacter(character);
293:
294: return book;
295: }
296:
297: private static InputStream getResource(String name) {
298: InputStream is = Thread.currentThread().getContextClassLoader()
299: .getResourceAsStream(name);
300: if (is == null) {
301: throw new IllegalStateException("Resource not found: "
302: + name);
303: }
304: return is;
305: }
306:
307: private static URL getResourceUrl(String name) {
308: URL url = Thread.currentThread().getContextClassLoader()
309: .getResource(name);
310: if (url == null) {
311: throw new IllegalStateException("Resource not found: "
312: + name);
313: }
314: return url;
315: }
316: }
|