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 232 233
|
//
// 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 : Assertion
{
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);
AssertEquals (String.Empty, declaration.InnerXml);
AssertEquals ("<?xml version=\"1.0\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", "doesn't check", null);
AssertEquals (String.Empty, declaration.InnerXml);
AssertEquals ("<?xml version=\"1.0\" encoding=\"doesn't check\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", null, "yes");
AssertEquals (String.Empty, declaration.InnerXml);
AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", declaration.OuterXml);
declaration = document.CreateXmlDeclaration ("1.0", "foo", "no");
AssertEquals (String.Empty, declaration.InnerXml);
AssertEquals ("<?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);
AssertNull (cloned.ParentNode);
AssertEquals ("Value incorrectly cloned",
original.Value, cloned.Value);
Assert ("Copies, not pointers", !Object.ReferenceEquals (original,cloned));
}
[Test]
public void Constructor ()
{
try {
XmlDeclaration broken = document.CreateXmlDeclaration ("2.0", null, null);
} catch (ArgumentException) {
return;
} catch (Exception e) {
Fail("first arg null, wrong exception: " + e.ToString());
}
}
[Test]
public void NodeType ()
{
AssertEquals ("incorrect NodeType returned", XmlNodeType.XmlDeclaration, declaration.NodeType);
}
[Test]
public void Names ()
{
AssertEquals ("Name is incorrect", "xml", declaration.Name);
AssertEquals ("LocalName is incorrect", "xml", declaration.LocalName);
}
[Test]
public void EncodingProperty ()
{
XmlDeclaration d1 = document.CreateXmlDeclaration ("1.0", "foo", null);
AssertEquals ("Encoding property", "foo", d1.Encoding);
XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, null);
AssertEquals ("null Encoding property", String.Empty, d2.Encoding);
}
[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");
AssertEquals ("Yes standalone property", "yes", d1.Standalone);
XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, "no");
AssertEquals ("No standalone property", "no", d2.Standalone);
XmlDeclaration d3 = document.CreateXmlDeclaration ("1.0", null, null);
AssertEquals ("null Standalone property", String.Empty, d3.Standalone);
}
[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");
AssertEquals ("Value property", expected, d.Value);
d.Value = expected;
AssertEquals ("Value round-trip", expected, d.Value);
d.Value = " " + expected;
AssertEquals ("Value round-trip (padded)", expected, d.Value);
d.Value = "version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
AssertEquals ("Value round-trip (padded 2)", expected, d.Value);
d.Value = "version=\"1.0\"\tencoding=\"ISO-8859-1\" standalone=\"yes\"" ;
AssertEquals ("Value round-trip (\\t)", expected, d.Value);
d.Value = "version=\"1.0\"\n encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
AssertEquals ("Value round-trip (\\n)", expected, d.Value);
d.Value = "version=\"1.0\" encoding = \"ISO-8859-1\" standalone = \"yes\"" ;
AssertEquals ("Value round-trip (spaces)", expected, d.Value);
d.Value = "version='1.0' encoding='ISO-8859-1' standalone='yes'" ;
AssertEquals ("Value round-trip ('s)", expected, d.Value);
}
[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 ());
AssertEquals ("<?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);
AssertEquals ("deep cloning differs from shallow cloning",
deep.OuterXml, shallow.OuterXml);
}
}
}
|