001: /* Copyright 2002-2005 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 nu.xom.Element;
025: import nu.xom.IllegalCharacterDataException;
026: import nu.xom.Node;
027: import nu.xom.Text;
028:
029: /**
030: *
031: * <p>
032: * Basic tests for the <code>Text</code> class.
033: * </p>
034: *
035: * @author Elliotte Rusty Harold
036: * @version 1.1d7
037: *
038: */
039: public class TextTest extends XOMTestCase {
040:
041: public TextTest(String name) {
042: super (name);
043: }
044:
045: public void testConstructor() {
046: Text a1 = new Text("test");
047: assertEquals("test", a1.getValue());
048: }
049:
050: public void testSetter() {
051:
052: String[] legal = { "Hello", "hello there",
053: " spaces on both ends ", " quotes \" \" quotes",
054: " single \'\' quotes",
055: " both double and single \"\'\"\' quotes",
056: " angle brackets < > <<<", " carriage returns \r\r\r",
057: " CDATA end: ]]>", " <![CDATA[ CDATA end: ]]>",
058: " & ", " ampersands & &&& &name; " };
059:
060: Text t = new Text("name");
061:
062: // Things that shouldn't cause an exception
063: for (int i = 0; i < legal.length; i++) {
064: t.setValue(legal[i]);
065: assertEquals(legal[i], t.getValue());
066: }
067:
068: t.setValue(null);
069: assertEquals("", t.getValue());
070:
071: try {
072: t.setValue("test \u0000 test ");
073: fail("Should raise an IllegalCharacterDataException");
074: } catch (IllegalCharacterDataException success) {
075: assertEquals("test \u0000 test ", success.getData());
076: assertNotNull(success.getMessage());
077: }
078:
079: }
080:
081: public void testToXML() {
082:
083: String[] easyCases = { "Hello", "hello there",
084: " spaces on both ends ", " quotes \" \" quotes",
085: " single \'\' quotes",
086: " both double and single \"\'\"\' quotes" };
087:
088: Text t = new Text("name");
089:
090: // Things that shouldn't cause an exception
091: for (int i = 0; i < easyCases.length; i++) {
092: t.setValue(easyCases[i]);
093: assertEquals(easyCases[i], t.toXML());
094: }
095:
096: t.setValue("<>");
097: assertEquals("<>", t.toXML());
098: t.setValue("&");
099: assertEquals("&amp;", t.toXML());
100: t.setValue("]]>");
101: assertEquals("]]>", t.toXML());
102: t.setValue("\r");
103: assertEquals("
", t.toXML());
104:
105: }
106:
107: public void testPunctuationCharactersInToXML() {
108:
109: String data = "=,.!@#$%^*()_-\"'[]{}+/?;:`|\\";
110: Text t = new Text(data);
111: assertEquals(data, t.toXML());
112:
113: }
114:
115: public void testEquals() {
116:
117: Text c1 = new Text("test");
118: Text c2 = new Text("test");
119: Text c3 = new Text("skjlchsakdjh");
120:
121: assertEquals(c1, c1);
122: assertEquals(c1.hashCode(), c1.hashCode());
123: assertTrue(!c1.equals(c2));
124: assertTrue(!c1.equals(c3));
125:
126: }
127:
128: public void testCopy() {
129:
130: Text c1 = new Text("test");
131: Text c2 = (Text) c1.copy();
132:
133: assertEquals(c1.getValue(), c2.getValue());
134: assertEquals(c1, c2);
135: assertTrue(!c1.equals(c2));
136: assertNull(c2.getParent());
137:
138: }
139:
140: public void testCopyisNotACDATASection() {
141:
142: Text c1 = new Text("test");
143: Node c2 = c1.copy();
144: assertEquals(Text.class, c2.getClass());
145:
146: }
147:
148: // Check passing in a string with broken surrogate pairs
149: // and with correct surrogate pairs
150: public void testSurrogates() {
151:
152: String goodString = "test: \uD8F5\uDF80 ";
153: Text c = new Text(goodString);
154: assertEquals(goodString, c.getValue());
155:
156: // Two high-halves
157: try {
158: new Text("test: \uD8F5\uDBF0 ");
159: fail("Should raise an IllegalCharacterDataException");
160: } catch (IllegalCharacterDataException success) {
161: assertNotNull(success.getMessage());
162: assertEquals("test: \uD8F5\uDBF0 ", success.getData());
163: }
164:
165: // Two high-halves
166: try {
167: new Text("test: \uD8F5\uD8F5 ");
168: fail("Should raise an IllegalCharacterDataException");
169: } catch (IllegalCharacterDataException success) {
170: assertEquals("test: \uD8F5\uD8F5 ", success.getData());
171: assertNotNull(success.getMessage());
172: }
173:
174: // One high-half
175: try {
176: new Text("test: \uD8F5 ");
177: fail("Should raise an IllegalCharacterDataException");
178: } catch (IllegalCharacterDataException success) {
179: assertNotNull(success.getMessage());
180: assertEquals("test: \uD8F5 ", success.getData());
181: }
182:
183: // One low half
184: try {
185: new Text("test: \uDF80 ");
186: fail("Should raise an IllegalCharacterDataException");
187: } catch (IllegalCharacterDataException success) {
188: assertNotNull(success.getMessage());
189: assertEquals("test: \uDF80 ", success.getData());
190: }
191:
192: // Low half before high half
193: try {
194: new Text("test: \uDCF5\uD8F5 ");
195: fail("Should raise an IllegalCharacterDataException");
196: } catch (IllegalCharacterDataException success) {
197: assertEquals("test: \uDCF5\uD8F5 ", success.getData());
198: assertNotNull(success.getMessage());
199: }
200:
201: }
202:
203: public void testNonBMPText() {
204:
205: StringBuffer sb = new StringBuffer(2);
206: for (char high = '\uD800'; high <= '\uDB7F'; high++) {
207: for (char low = '\uDC00'; low <= '\uDFFF'; low++) {
208: sb.setLength(0);
209: sb.append(high);
210: sb.append(low);
211: String s = sb.toString();
212: Text t = new Text(s);
213: assertEquals(s, t.getValue());
214: }
215: }
216:
217: }
218:
219: public void testEndOfBMP() {
220:
221: try {
222: new Text("\uFFFE");
223: fail("allowed FFFE");
224: } catch (IllegalCharacterDataException success) {
225: assertEquals("\uFFFE", success.getData());
226: assertNotNull(success.getMessage());
227: }
228:
229: try {
230: new Text("\uFFFF");
231: fail("allowed FFFF");
232: } catch (IllegalCharacterDataException success) {
233: assertEquals("\uFFFF", success.getData());
234: assertNotNull(success.getMessage());
235: }
236:
237: }
238:
239: public void testLeafNode() {
240:
241: Text c1 = new Text("data");
242: assertEquals(0, c1.getChildCount());
243: try {
244: c1.getChild(0);
245: fail("Didn't throw IndexOutofBoundsException");
246: } catch (IndexOutOfBoundsException success) {
247: // success
248: }
249:
250: assertNull(c1.getParent());
251:
252: Element element = new Element("test");
253: element.appendChild(c1);
254: assertEquals(element, c1.getParent());
255: assertEquals(c1, element.getChild(0));
256:
257: element.removeChild(c1);
258: assertEquals(0, element.getChildCount());
259:
260: }
261:
262: public void testToStringWithLineFeed() {
263:
264: Text t = new Text("content\ncontent");
265: assertEquals("[nu.xom.Text: content\\ncontent]", t.toString());
266:
267: }
268:
269: public void testToStringWithCarriageReturn() {
270:
271: Text t = new Text("content\rcontent");
272: assertEquals("[nu.xom.Text: content\\rcontent]", t.toString());
273:
274: }
275:
276: public void testToStringWithCarriageReturnLinefeed() {
277:
278: Text t = new Text("content\r\ncontent");
279: assertEquals("[nu.xom.Text: content\\r\\ncontent]", t
280: .toString());
281:
282: }
283:
284: public void testToStringWithTab() {
285:
286: Text t = new Text("content\tcontent");
287: assertEquals("[nu.xom.Text: content\\tcontent]", t.toString());
288:
289: }
290:
291: public void testToString() {
292:
293: Text t = new Text("content");
294: assertEquals("[nu.xom.Text: content]", t.toString());
295:
296: t
297: .setValue("012345678901234567890123456789012345678901234567890123456789");
298: assertEquals(
299: "[nu.xom.Text: 01234567890123456789012345678901234...]",
300: t.toString());
301:
302: }
303:
304: // Make sure carriage returns are escaped properly by toXML()
305: public void testCarriageReturnInText() {
306: Text text = new Text("data\rdata");
307: String xml = text.toXML();
308: assertEquals("data
data", xml);
309: }
310:
311: public void testHighSurrogateWithNoLowSurrogate() {
312:
313: String data = String.valueOf((char) 0xD800);
314: try {
315: new Text(data);
316: fail("Allowed single high surrogate in text node");
317: } catch (IllegalCharacterDataException success) {
318: assertEquals(data, success.getData());
319: assertNotNull(success.getMessage());
320: }
321:
322: }
323:
324: }
|