|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import nu.xom.Element; |
|
25 |
| import nu.xom.IllegalCharacterDataException; |
|
26 |
| import nu.xom.Node; |
|
27 |
| import nu.xom.Text; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public class TextTest extends XOMTestCase { |
|
40 |
| |
|
41 |
| |
|
42 |
18
| public TextTest(String name) {
|
|
43 |
18
| super(name);
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
1
| public void testConstructor() {
|
|
48 |
1
| Text a1 = new Text("test");
|
|
49 |
1
| assertEquals("test", a1.getValue());
|
|
50 |
| } |
|
51 |
| |
|
52 |
| |
|
53 |
1
| public void testSetter() {
|
|
54 |
| |
|
55 |
1
| String[] legal = {
|
|
56 |
| "Hello", |
|
57 |
| "hello there", |
|
58 |
| " spaces on both ends ", |
|
59 |
| " quotes \" \" quotes", |
|
60 |
| " single \'\' quotes", |
|
61 |
| " both double and single \"\'\"\' quotes", |
|
62 |
| " angle brackets < > <<<", |
|
63 |
| " carriage returns \r\r\r", |
|
64 |
| " CDATA end: ]]>", |
|
65 |
| " <![CDATA[ CDATA end: ]]>", |
|
66 |
| " & ", |
|
67 |
| " ampersands & &&& &name; " |
|
68 |
| }; |
|
69 |
| |
|
70 |
1
| Text t = new Text("name");
|
|
71 |
| |
|
72 |
| |
|
73 |
1
| for (int i = 0; i < legal.length; i++) {
|
|
74 |
12
| t.setValue(legal[i]);
|
|
75 |
12
| assertEquals(legal[i], t.getValue());
|
|
76 |
| } |
|
77 |
| |
|
78 |
1
| t.setValue(null);
|
|
79 |
1
| assertEquals("", t.getValue());
|
|
80 |
| |
|
81 |
1
| try {
|
|
82 |
1
| t.setValue("test \u0000 test ");
|
|
83 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
84 |
| } |
|
85 |
| catch (IllegalCharacterDataException success) { |
|
86 |
1
| assertEquals("test \u0000 test ", success.getData());
|
|
87 |
1
| assertNotNull(success.getMessage());
|
|
88 |
| } |
|
89 |
| |
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
1
| public void testToXML() {
|
|
94 |
| |
|
95 |
1
| String[] easyCases = {
|
|
96 |
| "Hello", |
|
97 |
| "hello there", |
|
98 |
| " spaces on both ends ", |
|
99 |
| " quotes \" \" quotes", |
|
100 |
| " single \'\' quotes", |
|
101 |
| " both double and single \"\'\"\' quotes" |
|
102 |
| }; |
|
103 |
| |
|
104 |
1
| Text t = new Text("name");
|
|
105 |
| |
|
106 |
| |
|
107 |
1
| for (int i = 0; i < easyCases.length; i++) {
|
|
108 |
6
| t.setValue(easyCases[i]);
|
|
109 |
6
| assertEquals(easyCases[i], t.toXML());
|
|
110 |
| } |
|
111 |
| |
|
112 |
1
| t.setValue("<>");
|
|
113 |
1
| assertEquals("<>", t.toXML());
|
|
114 |
1
| t.setValue("&");
|
|
115 |
1
| assertEquals("&amp;", t.toXML());
|
|
116 |
1
| t.setValue("]]>");
|
|
117 |
1
| assertEquals("]]>", t.toXML());
|
|
118 |
1
| t.setValue("\r");
|
|
119 |
1
| assertEquals("
", t.toXML());
|
|
120 |
| |
|
121 |
| } |
|
122 |
| |
|
123 |
| |
|
124 |
1
| public void testPunctuationCharactersInToXML() {
|
|
125 |
| |
|
126 |
1
| String data = "=,.!@#$%^*()_-\"'[]{}+/?;:`|\\";
|
|
127 |
1
| Text t = new Text(data);
|
|
128 |
1
| assertEquals(data, t.toXML());
|
|
129 |
| |
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
1
| public void testEquals() {
|
|
134 |
| |
|
135 |
1
| Text c1 = new Text("test");
|
|
136 |
1
| Text c2 = new Text("test");
|
|
137 |
1
| Text c3 = new Text("skjlchsakdjh");
|
|
138 |
| |
|
139 |
1
| assertEquals(c1, c1);
|
|
140 |
1
| assertEquals(c1.hashCode(), c1.hashCode());
|
|
141 |
1
| assertTrue(!c1.equals(c2));
|
|
142 |
1
| assertTrue(!c1.equals(c3));
|
|
143 |
| |
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
1
| public void testCopy() {
|
|
148 |
| |
|
149 |
1
| Text c1 = new Text("test");
|
|
150 |
1
| Text c2 = (Text) c1.copy();
|
|
151 |
| |
|
152 |
1
| assertEquals(c1.getValue(), c2.getValue());
|
|
153 |
1
| assertEquals(c1, c2);
|
|
154 |
1
| assertTrue(!c1.equals(c2));
|
|
155 |
1
| assertNull(c2.getParent());
|
|
156 |
| |
|
157 |
| } |
|
158 |
| |
|
159 |
| |
|
160 |
1
| public void testCopyisNotACDATASection() {
|
|
161 |
| |
|
162 |
1
| Text c1 = new Text("test");
|
|
163 |
1
| Node c2 = c1.copy();
|
|
164 |
1
| assertEquals(Text.class, c2.getClass());
|
|
165 |
| |
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
1
| public void testSurrogates() {
|
|
172 |
| |
|
173 |
1
| String goodString = "test: \uD8F5\uDF80 ";
|
|
174 |
1
| Text c = new Text(goodString);
|
|
175 |
1
| assertEquals(goodString, c.getValue());
|
|
176 |
| |
|
177 |
| |
|
178 |
1
| try {
|
|
179 |
1
| new Text("test: \uD8F5\uDBF0 ");
|
|
180 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
181 |
| } |
|
182 |
| catch (IllegalCharacterDataException success) { |
|
183 |
1
| assertNotNull(success.getMessage());
|
|
184 |
1
| assertEquals("test: \uD8F5\uDBF0 ", success.getData());
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
1
| try {
|
|
189 |
1
| new Text("test: \uD8F5\uD8F5 ");
|
|
190 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
191 |
| } |
|
192 |
| catch (IllegalCharacterDataException success) { |
|
193 |
1
| assertEquals("test: \uD8F5\uD8F5 ", success.getData());
|
|
194 |
1
| assertNotNull(success.getMessage());
|
|
195 |
| } |
|
196 |
| |
|
197 |
| |
|
198 |
1
| try {
|
|
199 |
1
| new Text("test: \uD8F5 ");
|
|
200 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
201 |
| } |
|
202 |
| catch (IllegalCharacterDataException success) { |
|
203 |
1
| assertNotNull(success.getMessage());
|
|
204 |
1
| assertEquals("test: \uD8F5 ", success.getData());
|
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
1
| try {
|
|
209 |
1
| new Text("test: \uDF80 ");
|
|
210 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
211 |
| } |
|
212 |
| catch (IllegalCharacterDataException success) { |
|
213 |
1
| assertNotNull(success.getMessage());
|
|
214 |
1
| assertEquals("test: \uDF80 ", success.getData());
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
1
| try {
|
|
219 |
1
| new Text("test: \uDCF5\uD8F5 ");
|
|
220 |
0
| fail("Should raise an IllegalCharacterDataException");
|
|
221 |
| } |
|
222 |
| catch (IllegalCharacterDataException success) { |
|
223 |
1
| assertEquals("test: \uDCF5\uD8F5 ", success.getData());
|
|
224 |
1
| assertNotNull(success.getMessage());
|
|
225 |
| } |
|
226 |
| |
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
1
| public void testNonBMPText() {
|
|
231 |
| |
|
232 |
1
| StringBuffer sb = new StringBuffer(2);
|
|
233 |
1
| for (char high = '\uD800'; high <= '\uDB7F'; high++) {
|
|
234 |
896
| for (char low = '\uDC00'; low <= '\uDFFF'; low++) {
|
|
235 |
917504
| sb.setLength(0);
|
|
236 |
917504
| sb.append(high);
|
|
237 |
917504
| sb.append(low);
|
|
238 |
917504
| String s = sb.toString();
|
|
239 |
917504
| Text t = new Text(s);
|
|
240 |
917504
| assertEquals(s, t.getValue());
|
|
241 |
| } |
|
242 |
| } |
|
243 |
| |
|
244 |
| } |
|
245 |
| |
|
246 |
| |
|
247 |
1
| public void testEndOfBMP() {
|
|
248 |
| |
|
249 |
1
| try {
|
|
250 |
1
| new Text("\uFFFE");
|
|
251 |
0
| fail("allowed FFFE");
|
|
252 |
| } |
|
253 |
| catch (IllegalCharacterDataException success) { |
|
254 |
1
| assertEquals("\uFFFE", success.getData());
|
|
255 |
1
| assertNotNull(success.getMessage());
|
|
256 |
| } |
|
257 |
| |
|
258 |
1
| try {
|
|
259 |
1
| new Text("\uFFFF");
|
|
260 |
0
| fail("allowed FFFF");
|
|
261 |
| } |
|
262 |
| catch (IllegalCharacterDataException success) { |
|
263 |
1
| assertEquals("\uFFFF", success.getData());
|
|
264 |
1
| assertNotNull(success.getMessage());
|
|
265 |
| } |
|
266 |
| |
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
1
| public void testLeafNode() {
|
|
271 |
| |
|
272 |
1
| Text c1 = new Text("data");
|
|
273 |
1
| assertEquals(0, c1.getChildCount());
|
|
274 |
1
| try {
|
|
275 |
1
| c1.getChild(0);
|
|
276 |
0
| fail("Didn't throw IndexOutofBoundsException");
|
|
277 |
| } |
|
278 |
| catch (IndexOutOfBoundsException success) { |
|
279 |
| |
|
280 |
| } |
|
281 |
| |
|
282 |
1
| assertNull(c1.getParent());
|
|
283 |
| |
|
284 |
1
| Element element = new Element("test");
|
|
285 |
1
| element.appendChild(c1);
|
|
286 |
1
| assertEquals(element, c1.getParent());
|
|
287 |
1
| assertEquals(c1, element.getChild(0));
|
|
288 |
| |
|
289 |
1
| element.removeChild(c1);
|
|
290 |
1
| assertEquals(0, element.getChildCount());
|
|
291 |
| |
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
1
| public void testToStringWithLineFeed() {
|
|
296 |
| |
|
297 |
1
| Text t = new Text("content\ncontent");
|
|
298 |
1
| assertEquals("[nu.xom.Text: content\\ncontent]", t.toString());
|
|
299 |
| |
|
300 |
| } |
|
301 |
| |
|
302 |
| |
|
303 |
1
| public void testToStringWithCarriageReturn() {
|
|
304 |
| |
|
305 |
1
| Text t = new Text("content\rcontent");
|
|
306 |
1
| assertEquals("[nu.xom.Text: content\\rcontent]", t.toString());
|
|
307 |
| |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
1
| public void testToStringWithCarriageReturnLinefeed() {
|
|
312 |
| |
|
313 |
1
| Text t = new Text("content\r\ncontent");
|
|
314 |
1
| assertEquals("[nu.xom.Text: content\\r\\ncontent]", t.toString());
|
|
315 |
| |
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
1
| public void testToStringWithTab() {
|
|
320 |
| |
|
321 |
1
| Text t = new Text("content\tcontent");
|
|
322 |
1
| assertEquals("[nu.xom.Text: content\\tcontent]", t.toString());
|
|
323 |
| |
|
324 |
| } |
|
325 |
| |
|
326 |
| |
|
327 |
1
| public void testToString() {
|
|
328 |
| |
|
329 |
1
| Text t = new Text("content");
|
|
330 |
1
| assertEquals("[nu.xom.Text: content]", t.toString());
|
|
331 |
| |
|
332 |
1
| t.setValue("012345678901234567890123456789012345678901234567890123456789");
|
|
333 |
1
| assertEquals(
|
|
334 |
| "[nu.xom.Text: 01234567890123456789012345678901234...]", |
|
335 |
| t.toString() |
|
336 |
| ); |
|
337 |
| |
|
338 |
| } |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
1
| public void testCarriageReturnInText() {
|
|
343 |
1
| Text text = new Text("data\rdata");
|
|
344 |
1
| String xml = text.toXML();
|
|
345 |
1
| assertEquals("data
data", xml);
|
|
346 |
| } |
|
347 |
| |
|
348 |
| |
|
349 |
1
| public void testHighSurrogateWithNoLowSurrogate() {
|
|
350 |
| |
|
351 |
1
| String data = String.valueOf((char) 0xD800);
|
|
352 |
1
| try {
|
|
353 |
1
| new Text(data);
|
|
354 |
0
| fail("Allowed single high surrogate in text node");
|
|
355 |
| } |
|
356 |
| catch (IllegalCharacterDataException success) { |
|
357 |
1
| assertEquals(data, success.getData());
|
|
358 |
1
| assertNotNull(success.getMessage());
|
|
359 |
| } |
|
360 |
| |
|
361 |
| } |
|
362 |
| |
|
363 |
| |
|
364 |
| } |