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 04. June 2006 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.copy;
13:
14: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
15: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
16: import com.thoughtworks.xstream.io.xml.AbstractXMLReaderTest;
17: import com.thoughtworks.xstream.io.xml.CompactWriter;
18: import com.thoughtworks.xstream.io.xml.XppReader;
19:
20: import java.io.StringReader;
21: import java.io.StringWriter;
22:
23: public class HierarchicalStreamCopierTest extends AbstractXMLReaderTest {
24:
25: private HierarchicalStreamCopier copier = new HierarchicalStreamCopier();
26:
27: // This test leverages the existing (comprehensive) tests for the XML readers
28: // and adds an additional stage of copying in.
29:
30: // factory method - overriding base class.
31: protected HierarchicalStreamReader createReader(String xml)
32: throws Exception {
33: HierarchicalStreamReader sourceReader = new XppReader(
34: new StringReader(xml));
35:
36: StringWriter buffer = new StringWriter();
37: HierarchicalStreamWriter destinationWriter = new CompactWriter(
38: buffer);
39:
40: copier.copy(sourceReader, destinationWriter);
41:
42: return new XppReader(new StringReader(buffer.toString()));
43: }
44:
45: public void testSkipsValueIfEmpty() {
46: String input = "<root><empty1/><empty2></empty2><not-empty>blah</not-empty></root>";
47: String expected = "<root><empty1/><empty2/><not-empty>blah</not-empty></root>";
48: HierarchicalStreamReader sourceReader = new XppReader(
49: new StringReader(input));
50:
51: StringWriter buffer = new StringWriter();
52: HierarchicalStreamWriter destinationWriter = new CompactWriter(
53: buffer);
54:
55: copier.copy(sourceReader, destinationWriter);
56:
57: assertEquals(expected, buffer.toString());
58: }
59:
60: }
|