1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.serialization;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Test for XmlStream used in document XML serialization.
*
* @author Håkon Humberset
*/
@Deprecated
public class XmlStreamTestCase {
/** A catch all test checking that regular usage looks good. */
@Test
public void testNormalUsage() {
XmlStream xml = new XmlStream();
xml.setIndent(" ");
xml.beginTag("foo");
xml.addAttribute("bar", "foo");
xml.addAttribute("foo", "bar");
xml.addContent("foo");
xml.beginTag("bar");
xml.endTag();
xml.beginTag("foo");
xml.beginTag("bar");
xml.addAttribute("foo", "bar");
xml.addContent("bar");
xml.endTag();
xml.endTag();
xml.addContent("bar");
xml.beginTag("foo");
xml.addContent("foo");
xml.addContent("bar");
xml.endTag();
xml.endTag();
String expected =
"<foo bar=\"foo\" foo=\"bar\">\n"
+ " foo\n"
+ " <bar/>\n"
+ " <foo>\n"
+ " <bar foo=\"bar\">bar</bar>\n"
+ " </foo>\n"
+ " bar\n"
+ " <foo>foobar</foo>\n"
+ "</foo>\n";
assertEquals(expected, xml.toString());
}
/**
* Test that XML tag and attribute names are checked for validity.
* Only the obvious illegal characters are tested currently.
*/
@Test
public void testIllegalAttributeNames() {
String illegalNames[] = {">foo", "foo<bar", " foo", "bar ", "foo bar", "foo\"bar", "&foo"};
XmlStream xml = new XmlStream();
for (String name : illegalNames) {
try {
xml.beginTag(name);
assertTrue(false);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().indexOf("cannot be used as an XML tag name") != -1);
}
}
xml.beginTag("test");
for (String name : illegalNames) {
try {
xml.addAttribute(name, "");
assertTrue(false);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().indexOf("cannot be used as an XML attribute name") != -1);
}
}
}
/** Test that XML attribute values are XML escaped. */
@Test
public void testAttributeEscaping() {
//String badString = "\"&<>'\n\r\u0000";
String badString = "\"&<>'";
XmlStream xml = new XmlStream();
xml.beginTag("foo");
xml.addAttribute("bar", badString);
xml.endTag();
String expected = "<foo bar=\""&<>'\"/>\n";
assertEquals(expected, xml.toString());
}
/** Test that content is XML escaped. */
@Test
public void testContentEscaping() {
//String badString = "\"&<>'\n\r\u0000";
String badString = "\"&<>'";
XmlStream xml = new XmlStream();
xml.beginTag("foo");
xml.addContent(badString);
xml.endTag();
String expected = "<foo>\"&<>'</foo>\n";
assertEquals(expected, xml.toString());
}
}
|