KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > RoundTripTest


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21
22 package nu.xom.tests;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
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;
34
35 /**
36  * <p>
37  * This mostly verifies that white space
38  * is properly escaped on output.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class RoundTripTest extends XOMTestCase {
46
47     private Builder builder;
48     private Serializer serializer;
49     private ByteArrayOutputStream JavaDoc out;
50
51     public RoundTripTest(String JavaDoc name) {
52         super(name);
53     }
54
55     protected void setUp() {
56         builder = new Builder();
57         out = new ByteArrayOutputStream JavaDoc();
58         serializer = new Serializer(out);
59     }
60
61     public void testTabInAttributeValue()
62       throws IOException JavaDoc, 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 JavaDoc(input));
69         Element root = reparsed.getRootElement();
70         Attribute tab = root.getAttribute("tab");
71         assertEquals(
72           "Round trip did not preserve tab in attribute value",
73           "\t", tab.getValue()
74         );
75         assertEquals("Unexpected error on round trip", doc, reparsed);
76     }
77
78     public void testCarriageReturnInAttributeValue()
79       throws IOException JavaDoc, 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 JavaDoc(input));
86         Element root = reparsed.getRootElement();
87         Attribute cr = root.getAttribute("cr");
88         assertEquals(
89           "Round trip did not preserve carriage return in attribute value",
90           "\r", cr.getValue()
91         );
92         assertEquals("Unexpected error on round trip", doc, reparsed);
93     }
94
95     public void testCarriageReturnInText()
96       throws IOException JavaDoc, 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 JavaDoc(input));
103         Element root = reparsed.getRootElement();
104         String JavaDoc value = root.getValue();
105         assertEquals(
106           "Round trip did not preserve carriage return in text",
107           "\r", value
108         );
109         assertEquals("Unexpected error on round trip", doc, reparsed);
110     }
111
112     public void testLineFeedInAttributeValue()
113       throws IOException JavaDoc, 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 JavaDoc(input));
120         Element root = reparsed.getRootElement();
121         Attribute lf = root.getAttribute("lf");
122         assertEquals(
123           "Round trip did not preserve carriage return in attribute value",
124           "\n", lf.getValue()
125         );
126         assertEquals("Unexpected error on round trip", doc, reparsed);
127     }
128
129     public void testSpacesInAttributeValue()
130       throws IOException JavaDoc, 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 JavaDoc(input));
137         Element root = reparsed.getRootElement();
138         Attribute spaces = root.getAttribute("spaces");
139         assertEquals(
140           "Round trip did not preserve spaces in attribute value",
141           " ", spaces.getValue()
142         );
143         assertEquals("Unexpected error on round trip", doc, reparsed);
144     }
145
146 }
147
Popular Tags