1
/* Copyright 2002, 2003 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
28
import nu.xom.Attribute;
29
import nu.xom.Builder;
30
import nu.xom.Document;
31
import nu.xom.Element;
32
import nu.xom.ParsingException;
33
import nu.xom.Serializer;
37
* This mostly verifies that white space
38
* is properly escaped on output.
41
* @author Elliotte Rusty Harold
45
public class RoundTripTest extends XOMTestCase {
47
private Builder builder;
48
private Serializer serializer;
49
private ByteArrayOutputStream out;
51
public RoundTripTest(String name) {
55
protected void setUp() {
56
builder = new Builder();
57
out = new ByteArrayOutputStream();
58
serializer = new Serializer(out);
61
public void testTabInAttributeValue()
62
throws IOException, ParsingException {
63
Element test = new Element("test");
64
test.addAttribute(new Attribute("tab", "\t"));
65
Document doc = new Document(test);
66
serializer.write(doc);
67
byte[] input = out.toByteArray();
68
Document reparsed = builder.build(new ByteArrayInputStream(input));
69
Element root = reparsed.getRootElement();
70
Attribute tab = root.getAttribute("tab");
72
"Round trip did not preserve tab in attribute value",
75
assertEquals("Unexpected error on round trip", doc, reparsed);
78
public void testCarriageReturnInAttributeValue()
79
throws IOException, ParsingException {
80
Element test = new Element("test");
81
test.addAttribute(new Attribute("cr", "\r"));
82
Document doc = new Document(test);
83
serializer.write(doc);
84
byte[] input = out.toByteArray();
85
Document reparsed = builder.build(new ByteArrayInputStream(input));
86
Element root = reparsed.getRootElement();
87
Attribute cr = root.getAttribute("cr");
89
"Round trip did not preserve carriage return in attribute value",
92
assertEquals("Unexpected error on round trip", doc, reparsed);
95
public void testCarriageReturnInText()
96
throws IOException, ParsingException {
97
Element test = new Element("test");
98
test.appendChild("\r");
99
Document doc = new Document(test);
100
serializer.write(doc);
101
byte[] input = out.toByteArray();
102
Document reparsed = builder.build(new ByteArrayInputStream(input));
103
Element root = reparsed.getRootElement();
104
String value = root.getValue();
106
"Round trip did not preserve carriage return in text",
109
assertEquals("Unexpected error on round trip", doc, reparsed);
112
public void testLineFeedInAttributeValue()
113
throws IOException, ParsingException {
114
Element test = new Element("test");
115
test.addAttribute(new Attribute("lf", "\n"));
116
Document doc = new Document(test);
117
serializer.write(doc);
118
byte[] input = out.toByteArray();
119
Document reparsed = builder.build(new ByteArrayInputStream(input));
120
Element root = reparsed.getRootElement();
121
Attribute lf = root.getAttribute("lf");
123
"Round trip did not preserve carriage return in attribute value",
126
assertEquals("Unexpected error on round trip", doc, reparsed);
129
public void testSpacesInAttributeValue()
130
throws IOException, ParsingException {
131
Element test = new Element("test");
132
test.addAttribute(new Attribute("spaces", " "));
133
Document doc = new Document(test);
134
serializer.write(doc);
135
byte[] input = out.toByteArray();
136
Document reparsed = builder.build(new ByteArrayInputStream(input));
137
Element root = reparsed.getRootElement();
138
Attribute spaces = root.getAttribute("spaces");
140
"Round trip did not preserve spaces in attribute value",
141
" ", spaces.getValue()
143
assertEquals("Unexpected error on round trip", doc, reparsed);