001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.oxm.xstream;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.StringWriter;
021: import java.util.Arrays;
022: import java.util.Properties;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.stream.XMLEventWriter;
027: import javax.xml.stream.XMLOutputFactory;
028: import javax.xml.stream.XMLStreamWriter;
029: import javax.xml.transform.dom.DOMResult;
030: import javax.xml.transform.sax.SAXResult;
031: import javax.xml.transform.stream.StreamResult;
032:
033: import com.thoughtworks.xstream.converters.Converter;
034: import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
035: import org.custommonkey.xmlunit.XMLTestCase;
036: import org.easymock.MockControl;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Text;
040: import org.xml.sax.ContentHandler;
041:
042: import org.springframework.xml.transform.StaxResult;
043: import org.springframework.xml.transform.StringResult;
044: import org.springframework.xml.transform.StringSource;
045:
046: public class XStreamMarshallerTest extends XMLTestCase {
047:
048: private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
049:
050: private XStreamMarshaller marshaller;
051:
052: private Flight flight;
053:
054: protected void setUp() throws Exception {
055: marshaller = new XStreamMarshaller();
056: Properties aliases = new Properties();
057: aliases.setProperty("flight", Flight.class.getName());
058: marshaller.setAliases(aliases);
059: flight = new Flight();
060: flight.setFlightNumber(42L);
061: }
062:
063: public void testMarshalDOMResult() throws Exception {
064: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
065: .newInstance();
066: DocumentBuilder builder = documentBuilderFactory
067: .newDocumentBuilder();
068: Document document = builder.newDocument();
069: DOMResult domResult = new DOMResult(document);
070: marshaller.marshal(flight, domResult);
071: Document expected = builder.newDocument();
072: Element flightElement = expected.createElement("flight");
073: expected.appendChild(flightElement);
074: Element numberElement = expected.createElement("flightNumber");
075: flightElement.appendChild(numberElement);
076: Text text = expected.createTextNode("42");
077: numberElement.appendChild(text);
078: assertXMLEqual("Marshaller writes invalid DOMResult", expected,
079: document);
080: }
081:
082: public void testMarshalStreamResultWriter() throws Exception {
083: StringWriter writer = new StringWriter();
084: StreamResult result = new StreamResult(writer);
085: marshaller.marshal(flight, result);
086: assertXMLEqual("Marshaller writes invalid StreamResult",
087: EXPECTED_STRING, writer.toString());
088: }
089:
090: public void testMarshalStreamResultOutputStream() throws Exception {
091: ByteArrayOutputStream os = new ByteArrayOutputStream();
092: StreamResult result = new StreamResult(os);
093: marshaller.marshal(flight, result);
094: String s = new String(os.toByteArray(), "UTF-8");
095: assertXMLEqual("Marshaller writes invalid StreamResult",
096: EXPECTED_STRING, s);
097: }
098:
099: public void testMarshalSaxResult() throws Exception {
100: MockControl handlerControl = MockControl
101: .createStrictControl(ContentHandler.class);
102: handlerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
103: ContentHandler handlerMock = (ContentHandler) handlerControl
104: .getMock();
105: handlerMock.startDocument();
106: handlerMock.startElement("", "flight", "flight", null);
107: handlerMock.startElement("", "number", "number", null);
108: handlerMock.characters(new char[] { '4', '2' }, 0, 2);
109: handlerMock.endElement("", "number", "number");
110: handlerMock.endElement("", "flight", "flight");
111: handlerMock.endDocument();
112:
113: handlerControl.replay();
114: SAXResult result = new SAXResult(handlerMock);
115: marshaller.marshal(flight, result);
116: handlerControl.verify();
117: }
118:
119: public void testMarshalStaxResultXMLStreamWriter() throws Exception {
120: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
121: StringWriter writer = new StringWriter();
122: XMLStreamWriter streamWriter = outputFactory
123: .createXMLStreamWriter(writer);
124: StaxResult result = new StaxResult(streamWriter);
125: marshaller.marshal(flight, result);
126: assertXMLEqual("Marshaller writes invalid StreamResult",
127: EXPECTED_STRING, writer.toString());
128: }
129:
130: public void testMarshalStaxResultXMLEventWriter() throws Exception {
131: XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
132: StringWriter writer = new StringWriter();
133: XMLEventWriter eventWriter = outputFactory
134: .createXMLEventWriter(writer);
135: StaxResult result = new StaxResult(eventWriter);
136: marshaller.marshal(flight, result);
137: assertXMLEqual("Marshaller writes invalid StreamResult",
138: EXPECTED_STRING, writer.toString());
139: }
140:
141: public void testConverters() throws Exception {
142: marshaller
143: .setConverters(new Converter[] { new EncodedByteArrayConverter() });
144: byte[] buf = new byte[] { 0x1, 0x2 };
145: StringResult result = new StringResult();
146: marshaller.marshal(buf, result);
147: assertXMLEqual("<byte-array>AQI=</byte-array>", result
148: .toString());
149: StringSource source = new StringSource(result.toString());
150: byte[] bufResult = (byte[]) marshaller.unmarshal(source);
151: assertTrue("Invalid result", Arrays.equals(buf, bufResult));
152: }
153:
154: }
|