001: /* Copyright 2002, 2003 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027:
028: import nu.xom.Attribute;
029: import nu.xom.Builder;
030: import nu.xom.Document;
031: import nu.xom.Element;
032: import nu.xom.ParsingException;
033: import nu.xom.Serializer;
034:
035: /**
036: * <p>
037: * This mostly verifies that white space
038: * is properly escaped on output.
039: * </p>
040: *
041: * @author Elliotte Rusty Harold
042: * @version 1.0
043: *
044: */
045: public class RoundTripTest extends XOMTestCase {
046:
047: private Builder builder;
048: private Serializer serializer;
049: private ByteArrayOutputStream out;
050:
051: public RoundTripTest(String name) {
052: super (name);
053: }
054:
055: protected void setUp() {
056: builder = new Builder();
057: out = new ByteArrayOutputStream();
058: serializer = new Serializer(out);
059: }
060:
061: public void testTabInAttributeValue() throws IOException,
062: ParsingException {
063: Element test = new Element("test");
064: test.addAttribute(new Attribute("tab", "\t"));
065: Document doc = new Document(test);
066: serializer.write(doc);
067: byte[] input = out.toByteArray();
068: Document reparsed = builder.build(new ByteArrayInputStream(
069: input));
070: Element root = reparsed.getRootElement();
071: Attribute tab = root.getAttribute("tab");
072: assertEquals(
073: "Round trip did not preserve tab in attribute value",
074: "\t", tab.getValue());
075: assertEquals("Unexpected error on round trip", doc, reparsed);
076: }
077:
078: public void testCarriageReturnInAttributeValue()
079: throws IOException, ParsingException {
080: Element test = new Element("test");
081: test.addAttribute(new Attribute("cr", "\r"));
082: Document doc = new Document(test);
083: serializer.write(doc);
084: byte[] input = out.toByteArray();
085: Document reparsed = builder.build(new ByteArrayInputStream(
086: input));
087: Element root = reparsed.getRootElement();
088: Attribute cr = root.getAttribute("cr");
089: assertEquals(
090: "Round trip did not preserve carriage return in attribute value",
091: "\r", cr.getValue());
092: assertEquals("Unexpected error on round trip", doc, reparsed);
093: }
094:
095: public void testCarriageReturnInText() throws IOException,
096: ParsingException {
097: Element test = new Element("test");
098: test.appendChild("\r");
099: Document doc = new Document(test);
100: serializer.write(doc);
101: byte[] input = out.toByteArray();
102: Document reparsed = builder.build(new ByteArrayInputStream(
103: input));
104: Element root = reparsed.getRootElement();
105: String value = root.getValue();
106: assertEquals(
107: "Round trip did not preserve carriage return in text",
108: "\r", value);
109: assertEquals("Unexpected error on round trip", doc, reparsed);
110: }
111:
112: public void testLineFeedInAttributeValue() throws IOException,
113: 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(
120: input));
121: Element root = reparsed.getRootElement();
122: Attribute lf = root.getAttribute("lf");
123: assertEquals(
124: "Round trip did not preserve carriage return in attribute value",
125: "\n", lf.getValue());
126: assertEquals("Unexpected error on round trip", doc, reparsed);
127: }
128:
129: public void testSpacesInAttributeValue() throws IOException,
130: 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(
137: input));
138: Element root = reparsed.getRootElement();
139: Attribute spaces = root.getAttribute("spaces");
140: assertEquals(
141: "Round trip did not preserve spaces in attribute value",
142: " ", spaces.getValue());
143: assertEquals("Unexpected error on round trip", doc, reparsed);
144: }
145:
146: }
|