01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 01. April 2006 by Joerg Schaible
11: */
12: package com.thoughtworks.acceptance;
13:
14: import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
15: import com.thoughtworks.xstream.XStream;
16: import com.thoughtworks.xstream.XStreamer;
17: import com.thoughtworks.xstream.converters.ConversionException;
18:
19: import javax.xml.transform.Transformer;
20: import javax.xml.transform.TransformerException;
21: import javax.xml.transform.TransformerFactory;
22: import javax.xml.transform.stream.StreamResult;
23: import javax.xml.transform.stream.StreamSource;
24:
25: import java.io.ObjectStreamException;
26: import java.io.StringReader;
27: import java.io.StringWriter;
28: import java.net.URL;
29:
30: /**
31: * @author Jörg Schaible
32: */
33: public class XStreamerTest extends AbstractAcceptanceTest {
34:
35: private Transformer transformer;
36:
37: protected void setUp() throws Exception {
38: super .setUp();
39:
40: final TransformerFactory transformerFactory = TransformerFactory
41: .newInstance();
42: final URL url = getClass().getResource("XStreamer.xsl");
43: transformer = transformerFactory
44: .newTransformer(new StreamSource(url.openStream()));
45: }
46:
47: final static class ImplicitXStreamContainer {
48: private XStream myXStream;
49: }
50:
51: public void testDetectsSelfMarshalling() {
52: ImplicitXStreamContainer c = new ImplicitXStreamContainer();
53: c.myXStream = xstream;
54: try {
55: xstream.toXML(c);
56: fail("Thrown " + ConversionException.class.getName()
57: + " expected");
58: } catch (final ConversionException e) {
59: }
60: }
61:
62: public void testCanConvertAnotherInstance()
63: throws TransformerException {
64: XStream x = createXStream();
65: final String xml = normalizedXStreamXML(xstream.toXML(x));
66: final XStream serialized = (XStream) xstream.fromXML(xml);
67: final String xmlSerialized = normalizedXStreamXML(xstream
68: .toXML(serialized));
69: assertEquals(xml, xmlSerialized);
70: }
71:
72: public void testCanBeUsedAfterSerialization()
73: throws TransformerException {
74: xstream = (XStream) xstream.fromXML(xstream
75: .toXML(createXStream()));
76: testCanConvertAnotherInstance();
77: }
78:
79: public void testCanSerializeSelfContained()
80: throws ClassNotFoundException, ObjectStreamException {
81: final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes",
82: "XStream", "BSD");
83: xstream.alias("software", OpenSourceSoftware.class);
84: String xml = new XStreamer().toXML(xstream, oos);
85: assertEquals(oos, new XStreamer().fromXML(xml));
86: }
87:
88: private String normalizedXStreamXML(String xml)
89: throws TransformerException {
90: final StringWriter writer = new StringWriter();
91: transformer.transform(new StreamSource(new StringReader(xml)),
92: new StreamResult(writer));
93: return writer.toString();
94: }
95: }
|