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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
//
// System.Xml.XmlDeclarationTests.cs
//
// Author: Duncan Mak (duncan@ximian.com)
// Author: Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) Ximian, Inc.
// (C) 2003 Martin Willemoes Hansen
//
using System;
using System.IO;
using System.Xml;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlDeclarationTests
{
XmlDocument document;
XmlDeclaration declaration;
[SetUp]
public void GetReady ()
{
document = new XmlDocument ();
document.LoadXml ("<foo><bar></bar></foo>");
declaration = document.CreateXmlDeclaration ("1.0", null, null);
}
[Test]
public void InnerAndOuterXml ()
{
declaration = document.CreateXmlDeclaration ("1.0", null, null);
Assert.AreEqual (String.Empty, declaration.InnerXml);
Assert.AreEqual ("<?xml version=\"1.0\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", "doesn't check", null);
Assert.AreEqual (String.Empty, declaration.InnerXml);
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"doesn't check\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", null, "yes");
Assert.AreEqual (String.Empty, declaration.InnerXml);
Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", "foo", "no");
Assert.AreEqual (String.Empty, declaration.InnerXml);
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"foo\" standalone=\"no\"?>", declaration.OuterXml);
}
internal void XmlNodeBaseProperties (XmlNode original, XmlNode cloned)
{
// assertequals (original.nodetype + " was incorrectly cloned.",
// original.baseuri, cloned.baseuri);
Assert.IsNull (cloned.ParentNode);
Assert.AreEqual (original.Value, cloned.Value, "Value incorrectly cloned");
Assert.IsTrue (!Object.ReferenceEquals (original, cloned), "Copies, not pointers");
}
[Test]
public void Constructor ()
{
try {
XmlDeclaration broken = document.CreateXmlDeclaration ("2.0", null, null);
} catch (ArgumentException) {
return;
} catch (Exception e) {
Assert.Fail ("first arg null, wrong exception: " + e.ToString());
}
}
[Test]
public void NodeType ()
{
Assert.AreEqual (XmlNodeType.XmlDeclaration, declaration.NodeType, "incorrect NodeType returned");
}
[Test]
public void Names ()
{
Assert.AreEqual ("xml", declaration.Name, "Name is incorrect");
Assert.AreEqual ("xml", declaration.LocalName, "LocalName is incorrect");
}
[Test]
public void EncodingProperty ()
{
XmlDeclaration d1 = document.CreateXmlDeclaration ("1.0", "foo", null);
Assert.AreEqual ("foo", d1.Encoding, "Encoding property");
XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, null);
Assert.AreEqual (String.Empty, d2.Encoding, "null Encoding property");
}
[Test]
public void ValidInnerText ()
{
declaration.InnerText = "version='1.0'";
declaration.InnerText = "version='1.0' encoding='euc-jp'";
declaration.InnerText = "version='1.0' standalone='no'";
declaration.InnerText = "version='1.0' encoding='iso-8859-1' standalone=\"yes\"";
declaration.InnerText = @"version = '1.0' encoding =
'euc-jp' standalone = 'yes' ";
declaration.InnerText = " version = '1.0'";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText ()
{
declaration.InnerText = "version='1.0a'";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText2 ()
{
declaration.InnerText = "version='1.0' encoding='euc-kr\"";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText3 ()
{
declaration.InnerText = "version='2.0'";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText4 ()
{
declaration.InnerText = "version='1.0' standalone='Yeah!!!!!'";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText5 ()
{
declaration.InnerText = "version='1.0'standalone='yes'";
}
[Test]
[ExpectedException (typeof (XmlException))]
public void InvalidInnerText6 ()
{
declaration.InnerText = "version='1.0'standalone='yes' encoding='utf-8'";
}
[Test]
public void StandaloneProperty ()
{
XmlDeclaration d1 = document.CreateXmlDeclaration ("1.0", null, "yes");
Assert.AreEqual ("yes", d1.Standalone, "Yes standalone property");
XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, "no");
Assert.AreEqual ("no", d2.Standalone, "No standalone property");
XmlDeclaration d3 = document.CreateXmlDeclaration ("1.0", null, null);
Assert.AreEqual (String.Empty, d3.Standalone, "null Standalone property");
}
[Test]
public void ValueProperty ()
{
string expected = "version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
XmlDeclaration d = document.CreateXmlDeclaration ("1.0", "ISO-8859-1", "yes");
Assert.AreEqual (expected, d.Value, "Value property");
d.Value = expected;
Assert.AreEqual (expected, d.Value, "Value round-trip");
d.Value = " " + expected;
Assert.AreEqual (expected, d.Value, "Value round-trip (padded)");
d.Value = "version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
Assert.AreEqual (expected, d.Value, "Value round-trip (padded 2)");
d.Value = "version=\"1.0\"\tencoding=\"ISO-8859-1\" standalone=\"yes\"" ;
Assert.AreEqual (expected, d.Value, "Value round-trip (\\t)");
d.Value = "version=\"1.0\"\n encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
Assert.AreEqual (expected, d.Value, "Value round-trip (\\n)");
d.Value = "version=\"1.0\" encoding = \"ISO-8859-1\" standalone = \"yes\"" ;
Assert.AreEqual (expected, d.Value, "Value round-trip (spaces)");
d.Value = "version='1.0' encoding='ISO-8859-1' standalone='yes'" ;
Assert.AreEqual (expected, d.Value, "Value round-trip ('s)");
}
[Test]
public void Bug79496 ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.WriteStartDocument (true);
xtw.WriteStartElement ("person");
xtw.WriteEndElement ();
xtw.Flush ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml (sw.ToString ());
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><person />", doc.OuterXml);
}
[Test]
public void XmlCommentCloneNode ()
{
XmlNode original = declaration;
XmlNode shallow = declaration.CloneNode (false); // shallow
XmlNodeBaseProperties (original, shallow);
XmlNode deep = declaration.CloneNode (true); // deep
XmlNodeBaseProperties (original, deep);
Assert.AreEqual (deep.OuterXml, shallow.OuterXml, "deep cloning differs from shallow cloning");
}
}
}
|