| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433 |
- //
- // System.Xml.XmlReaderCommonTests
- //
- // Authors:
- // Atsushi Enomoto <[email protected]>
- //
- // (C) 2003 Atsushi Enomoto
- // Note: Most of testcases are moved from XmlTextReaderTests.cs and
- // XmlNodeReaderTests.cs.
- //
- using System;
- using System.IO;
- using System.Text;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.XPath;
- using NUnit.Framework;
- namespace MonoTests.System.Xml
- {
- [TestFixture]
- public class XmlReaderTests : Assertion
- {
- [SetUp]
- public void GetReady ()
- {
- document = new XmlDocument ();
- document.LoadXml (xml1);
- }
- XmlDocument document;
- const string xml1 = "<root attr1='value1'><child /></root>";
- const string xml2 = "<root><foo/><bar>test.</bar></root>";
- const string xml3 = "<root> test of <b>mixed</b> string.<![CDATA[ cdata string.]]></root>";
- const string xml4 = "<root>test of <b>mixed</b> string.</root>";
- XmlTextReader xtr;
- XmlNodeReader xnr;
- // copy from XmlTextReaderTests
- private void AssertStartDocument (XmlReader xmlReader)
- {
- Assert (xmlReader.ReadState == ReadState.Initial);
- Assert (xmlReader.NodeType == XmlNodeType.None);
- Assert (xmlReader.Depth == 0);
- Assert (!xmlReader.EOF);
- }
- private void AssertNode (
- XmlReader xmlReader,
- XmlNodeType nodeType,
- int depth,
- bool isEmptyElement,
- string name,
- string prefix,
- string localName,
- string namespaceURI,
- string value,
- int attributeCount)
- {
- Assert ("Read() return value", xmlReader.Read ());
- Assert ("ReadState", xmlReader.ReadState == ReadState.Interactive);
- Assert ("!EOF", !xmlReader.EOF);
- AssertNodeValues ("", xmlReader, nodeType, depth,
- isEmptyElement, name, prefix, localName,
- namespaceURI, value, value != String.Empty,
- attributeCount, attributeCount > 0);
- }
- private void AssertNodeValues (
- string label,
- XmlReader xmlReader,
- XmlNodeType nodeType,
- int depth,
- bool isEmptyElement,
- string name,
- string prefix,
- string localName,
- string namespaceURI,
- string value,
- int attributeCount)
- {
- AssertNodeValues (label, xmlReader, nodeType, depth,
- isEmptyElement, name, prefix, localName,
- namespaceURI, value, value != String.Empty,
- attributeCount, attributeCount > 0);
- }
- private void AssertNodeValues (
- string label,
- XmlReader xmlReader,
- XmlNodeType nodeType,
- int depth,
- bool isEmptyElement,
- string name,
- string prefix,
- string localName,
- string namespaceURI,
- string value,
- bool hasValue,
- int attributeCount,
- bool hasAttributes)
- {
- label = String.Concat (label, "(", xmlReader.GetType ().Name, ")");
- AssertEquals (label + ": NodeType", nodeType, xmlReader.NodeType);
- AssertEquals (label + ": IsEmptyElement", isEmptyElement, xmlReader.IsEmptyElement);
- AssertEquals (label + ": name", name, xmlReader.Name);
- AssertEquals (label + ": prefix", prefix, xmlReader.Prefix);
- AssertEquals (label + ": localName", localName, xmlReader.LocalName);
- AssertEquals (label + ": namespaceURI", namespaceURI, xmlReader.NamespaceURI);
- AssertEquals (label + ": Depth", depth, xmlReader.Depth);
- AssertEquals (label + ": hasValue", hasValue, xmlReader.HasValue);
- AssertEquals (label + ": Value", value, xmlReader.Value);
- AssertEquals (label + ": hasAttributes", hasAttributes, xmlReader.HasAttributes);
- AssertEquals (label + ": attributeCount", attributeCount, xmlReader.AttributeCount);
- }
- private void AssertAttribute (
- XmlReader xmlReader,
- string name,
- string prefix,
- string localName,
- string namespaceURI,
- string value)
- {
- AssertEquals ("value", value, xmlReader [name]);
- Assert (xmlReader.GetAttribute (name) == value);
- if (namespaceURI != String.Empty) {
- Assert (xmlReader[localName, namespaceURI] == value);
- Assert (xmlReader.GetAttribute (localName, namespaceURI) == value);
- }
- }
- private void AssertEndDocument (XmlReader xmlReader)
- {
- Assert ("could read", !xmlReader.Read ());
- AssertEquals ("NodeType is not XmlNodeType.None", XmlNodeType.None, xmlReader.NodeType);
- AssertEquals ("Depth is not 0", 0, xmlReader.Depth);
- AssertEquals ("ReadState is not ReadState.EndOfFile", ReadState.EndOfFile, xmlReader.ReadState);
- Assert ("not EOF", xmlReader.EOF);
- xmlReader.Close ();
- AssertEquals ("ReadState is not ReadState.Cosed", ReadState.Closed, xmlReader.ReadState);
- }
- private delegate void TestMethod (XmlReader reader);
- private void RunTest (string xml, TestMethod method)
- {
- xtr = new XmlTextReader (new StringReader (xml));
- method (xtr);
- // DTD validation
- xtr = new XmlTextReader (new StringReader (xml));
- XmlValidatingReader xvr = new XmlValidatingReader (xtr);
- xvr.ValidationType = ValidationType.DTD;
- xvr.EntityHandling = EntityHandling.ExpandCharEntities;
- method (xvr);
- // XSD validation
- xtr = new XmlTextReader (new StringReader (xml));
- xvr = new XmlValidatingReader (xtr);
- xvr.EntityHandling = EntityHandling.ExpandCharEntities;
- method (xvr);
- document.XmlResolver = null;
- document.LoadXml (xml);
- xnr = new XmlNodeReader (document);
- method (xnr);
- #if NET_2_0
- /*
- // XPathNavigatorReader tests
- System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument (new StringReader (xml));
- XmlReader xpr = doc.CreateNavigator ().ReadSubtree ();
- method (xpr);
- */
- #endif
- }
- [Test]
- public void InitialState ()
- {
- RunTest (xml1, new TestMethod (InitialState));
- }
- private void InitialState (XmlReader reader)
- {
- AssertEquals ("Depth", 0, reader.Depth);
- AssertEquals ("EOF", false, reader.EOF);
- AssertEquals ("HasValue", false, reader.HasValue);
- AssertEquals ("IsEmptyElement", false, reader.IsEmptyElement);
- AssertEquals ("LocalName", String.Empty, reader.LocalName);
- AssertEquals ("NodeType", XmlNodeType.None, reader.NodeType);
- AssertEquals ("ReadState", ReadState.Initial, reader.ReadState);
- }
- [Test]
- public void Read ()
- {
- RunTest (xml1, new TestMethod (Read));
- }
- public void Read (XmlReader reader)
- {
- reader.Read ();
- AssertEquals ("<root>.NodeType", XmlNodeType.Element, reader.NodeType);
- AssertEquals ("<root>.Name", "root", reader.Name);
- AssertEquals ("<root>.ReadState", ReadState.Interactive, reader.ReadState);
- AssertEquals ("<root>.Depth", 0, reader.Depth);
- // move to 'child'
- reader.Read ();
- AssertEquals ("<child/>.Depth", 1, reader.Depth);
- AssertEquals ("<child/>.NodeType", XmlNodeType.Element, reader.NodeType);
- AssertEquals ("<child/>.Name", "child", reader.Name);
- reader.Read ();
- AssertEquals ("</root>.Depth", 0, reader.Depth);
- AssertEquals ("</root>.NodeType", XmlNodeType.EndElement, reader.NodeType);
- AssertEquals ("</root>.Name", "root", reader.Name);
- reader.Read ();
- AssertEquals ("end.EOF", true, reader.EOF);
- AssertEquals ("end.NodeType", XmlNodeType.None, reader.NodeType);
- }
- [Test]
- [Category ("NotDotNet")]
- public void ReadAttributeValue ()
- {
- RunTest ("<root attr=''/>", new TestMethod (ReadAttributeValue));
- }
- public void ReadAttributeValue (XmlReader reader)
- {
- reader.Read (); // root
- Assert (reader.MoveToFirstAttribute ());
- // It looks like that MS.NET shows AttributeCount and
- // HasAttributes as the same as element node!
- this.AssertNodeValues ("#1",
- reader, XmlNodeType.Attribute,
- 1, false, "attr", "", "attr", "", "", true, 1, true);
- Assert (reader.ReadAttributeValue ());
- // MS.NET XmlTextReader fails. Its Prefix returns
- // null instead of "". It is fixed in MS.NET 2.0.
- this.AssertNodeValues ("#2",
- reader, XmlNodeType.Text,
- 2, false, "", "", "", "", "", true, 1, true);
- Assert (reader.MoveToElement ());
- this.AssertNodeValues ("#3",
- reader, XmlNodeType.Element,
- 0, true, "root", "", "root", "", "", false, 1, true);
- }
- [Test]
- public void ReadEmptyElement ()
- {
- RunTest (xml2, new TestMethod (ReadEmptyElement));
- }
- public void ReadEmptyElement (XmlReader reader)
- {
- reader.Read (); // root
- AssertEquals (false, reader.IsEmptyElement);
- reader.Read (); // foo
- AssertEquals ("foo", reader.Name);
- AssertEquals (true, reader.IsEmptyElement);
- reader.Read (); // bar
- AssertEquals ("bar", reader.Name);
- AssertEquals (false, reader.IsEmptyElement);
- }
- [Test]
- public void ReadStringFromElement ()
- {
- RunTest (xml3, new TestMethod (ReadStringFromElement));
- }
- public void ReadStringFromElement (XmlReader reader)
- {
- // Note: ReadString() test works only when the reader is
- // positioned at the container element.
- // In case the reader is positioned at the first
- // character node, XmlTextReader and XmlNodeReader works
- // different!!
- reader.Read ();
- string s = reader.ReadString ();
- AssertEquals ("readString.1.ret_val", " test of ", s);
- AssertEquals ("readString.1.Name", "b", reader.Name);
- s = reader.ReadString ();
- AssertEquals ("readString.2.ret_val", "mixed", s);
- AssertEquals ("readString.2.NodeType", XmlNodeType.EndElement, reader.NodeType);
- s = reader.ReadString (); // never proceeds.
- AssertEquals ("readString.3.ret_val", String.Empty, s);
- AssertEquals ("readString.3.NodeType", XmlNodeType.EndElement, reader.NodeType);
- reader.Read ();
- AssertEquals ("readString.4.NodeType", XmlNodeType.Text, reader.NodeType);
- AssertEquals ("readString.4.Value", " string.", reader.Value);
- s = reader.ReadString (); // reads the same Text node.
- AssertEquals ("readString.5.ret_val", " string. cdata string.", s);
- AssertEquals ("readString.5.NodeType", XmlNodeType.EndElement, reader.NodeType);
- }
- [Test]
- public void ReadInnerXml ()
- {
- const string xml = "<root><foo>test of <b>mixed</b> string.</foo><bar /></root>";
- RunTest (xml, new TestMethod (ReadInnerXml));
- }
- public void ReadInnerXml (XmlReader reader)
- {
- reader.Read ();
- reader.Read ();
- AssertEquals ("initial.ReadState", ReadState.Interactive, reader.ReadState);
- AssertEquals ("initial.EOF", false, reader.EOF);
- AssertEquals ("initial.NodeType", XmlNodeType.Element, reader.NodeType);
- string s = reader.ReadInnerXml ();
- AssertEquals ("read_all", "test of <b>mixed</b> string.", s);
- AssertEquals ("after.Name", "bar", reader.Name);
- AssertEquals ("after.NodeType", XmlNodeType.Element, reader.NodeType);
- }
- [Test]
- public void EmptyElement ()
- {
- RunTest ("<foo/>", new TestMethod (EmptyElement));
- }
-
- public void EmptyElement (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, // depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void NestedEmptyTag ()
- {
- string xml = "<foo><bar/></foo>";
- RunTest (xml, new TestMethod (NestedEmptyTag));
- }
- public void NestedEmptyTag (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 1, //depth
- true, // isEmptyElement
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void NestedText ()
- {
- string xml = "<foo>bar</foo>";
- RunTest (xml, new TestMethod (NestedText));
- }
- public void NestedText (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Text, // nodeType
- 1, //depth
- false, // isEmptyElement
- String.Empty, // name
- String.Empty, // prefix
- String.Empty, // localName
- String.Empty, // namespaceURI
- "bar", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void EmptyElementWithAttributes ()
- {
- string xml = @"<foo bar=""baz"" quux='quuux' x:foo='x-foo' xmlns:x = 'urn:xfoo' />";
- RunTest (xml, new TestMethod (EmptyElementWithAttributes ));
- }
- public void EmptyElementWithAttributes (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 4 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- "baz" // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "quux", // name
- String.Empty, // prefix
- "quux", // localName
- String.Empty, // namespaceURI
- "quuux" // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "notexist", // name
- String.Empty, // prefix
- "notexist", // localName
- String.Empty, // namespaceURI
- null // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "x:foo", // name
- "x", // prefix
- "foo", // localName
- "urn:xfoo", // namespaceURI
- "x-foo" // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "x:bar", // name
- "x", // prefix
- "bar", // localName
- "urn:xfoo", // namespaceURI
- null // value
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void ProcessingInstructionBeforeDocumentElement ()
- {
- string xml = "<?foo bar?><baz/>";
- RunTest (xml, new TestMethod (ProcessingInstructionBeforeDocumentElement));
- }
- public void ProcessingInstructionBeforeDocumentElement (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.ProcessingInstruction, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- "bar", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "baz", // name
- String.Empty, // prefix
- "baz", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void CommentBeforeDocumentElement ()
- {
- string xml = "<!--foo--><bar/>";
- RunTest (xml, new TestMethod (CommentBeforeDocumentElement));
- }
- public void CommentBeforeDocumentElement (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Comment, // nodeType
- 0, //depth
- false, // isEmptyElement
- String.Empty, // name
- String.Empty, // prefix
- String.Empty, // localName
- String.Empty, // namespaceURI
- "foo", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void PredefinedEntities ()
- {
- string xml = "<foo><>&'"</foo>";
- RunTest (xml, new TestMethod (PredefinedEntities));
- }
- public void PredefinedEntities (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Text, // nodeType
- 1, //depth
- false, // isEmptyElement
- String.Empty, // name
- String.Empty, // prefix
- String.Empty, // localName
- String.Empty, // namespaceURI
- "<>&'\"", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void CharacterReferences ()
- {
- string xml = "<foo>FOO</foo>";
- RunTest (xml, new TestMethod (CharacterReferences));
- }
- public void CharacterReferences (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Text, // nodeType
- 1, //depth
- false, // isEmptyElement
- String.Empty, // name
- String.Empty, // prefix
- String.Empty, // localName
- String.Empty, // namespaceURI
- "FOO", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void PredefinedEntitiesInAttribute ()
- {
- string xml = "<foo bar='<>&'"'/>";
- RunTest (xml, new TestMethod (PredefinedEntitiesInAttribute ));
- }
- public void PredefinedEntitiesInAttribute (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- "<>&'\"" // value
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void CharacterReferencesInAttribute ()
- {
- string xml = "<foo bar='FOO'/>";
- RunTest (xml, new TestMethod (CharacterReferencesInAttribute));
- }
- public void CharacterReferencesInAttribute (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- "FOO" // value
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void CDATA ()
- {
- string xml = "<foo><![CDATA[<>&]]></foo>";
- RunTest (xml, new TestMethod (CDATA));
- }
- public void CDATA (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.CDATA, // nodeType
- 1, //depth
- false, // isEmptyElement
- String.Empty, // name
- String.Empty, // prefix
- String.Empty, // localName
- String.Empty, // namespaceURI
- "<>&", // value
- 0 // attributeCount
- );
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, //depth
- false, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void EmptyElementInDefaultNamespace ()
- {
- string xml = @"<foo xmlns='http://foo/' />";
- RunTest (xml, new TestMethod (EmptyElementInDefaultNamespace));
- }
- public void EmptyElementInDefaultNamespace (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, // depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- "http://foo/", // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns", // name
- String.Empty, // prefix
- "xmlns", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://foo/" // value
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace (String.Empty));
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void ChildElementInNamespace ()
- {
- string xml = @"<foo:bar xmlns:foo='http://foo/'><baz:quux xmlns:baz='http://baz/' /></foo:bar>";
- RunTest (xml, new TestMethod (ChildElementInNamespace));
- }
- public void ChildElementInNamespace (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, // depth
- false, // isEmptyElement
- "foo:bar", // name
- "foo", // prefix
- "bar", // localName
- "http://foo/", // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns:foo", // name
- "xmlns", // prefix
- "foo", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://foo/" // value
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 1, // depth
- true, // isEmptyElement
- "baz:quux", // name
- "baz", // prefix
- "quux", // localName
- "http://baz/", // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns:baz", // name
- "xmlns", // prefix
- "baz", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://baz/" // value
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertEquals ("http://baz/", xmlReader.LookupNamespace ("baz"));
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, // depth
- false, // isEmptyElement
- "foo:bar", // name
- "foo", // prefix
- "bar", // localName
- "http://foo/", // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertNull (xmlReader.LookupNamespace ("baz"));
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void ChildElementInDefaultNamespace ()
- {
- string xml = @"<foo:bar xmlns:foo='http://foo/'><baz xmlns='http://baz/' /></foo:bar>";
- RunTest (xml, new TestMethod (ChildElementInDefaultNamespace));
- }
- public void ChildElementInDefaultNamespace (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, // depth
- false, // isEmptyElement
- "foo:bar", // name
- "foo", // prefix
- "bar", // localName
- "http://foo/", // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns:foo", // name
- "xmlns", // prefix
- "foo", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://foo/" // value
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 1, // depth
- true, // isEmptyElement
- "baz", // name
- String.Empty, // prefix
- "baz", // localName
- "http://baz/", // namespaceURI
- String.Empty, // value
- 1 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns", // name
- String.Empty, // prefix
- "xmlns", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://baz/" // value
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertEquals ("http://baz/", xmlReader.LookupNamespace (String.Empty));
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.EndElement, // nodeType
- 0, // depth
- false, // isEmptyElement
- "foo:bar", // name
- "foo", // prefix
- "bar", // localName
- "http://foo/", // namespaceURI
- String.Empty, // value
- 0 // attributeCount
- );
- AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void AttributeInNamespace ()
- {
- string xml = @"<foo bar:baz='quux' xmlns:bar='http://bar/' />";
- RunTest (xml, new TestMethod (AttributeInNamespace));
- }
- public void AttributeInNamespace (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, // depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 2 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "bar:baz", // name
- "bar", // prefix
- "baz", // localName
- "http://bar/", // namespaceURI
- "quux" // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "xmlns:bar", // name
- "xmlns", // prefix
- "bar", // localName
- "http://www.w3.org/2000/xmlns/", // namespaceURI
- "http://bar/" // value
- );
- AssertEquals ("http://bar/", xmlReader.LookupNamespace ("bar"));
- AssertEndDocument (xmlReader);
- }
- [Test]
- public void MoveToElementFromAttribute ()
- {
- string xml = @"<foo bar=""baz"" />";
- RunTest (xml, new TestMethod (MoveToElementFromAttribute));
- }
- public void MoveToElementFromAttribute (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- Assert (xmlReader.MoveToFirstAttribute ());
- AssertEquals (XmlNodeType.Attribute, xmlReader.NodeType);
- Assert (xmlReader.MoveToElement ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- }
- [Test]
- public void MoveToElementFromElement ()
- {
- string xml = @"<foo bar=""baz"" />";
- RunTest (xml, new TestMethod (MoveToElementFromElement));
- }
- public void MoveToElementFromElement (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- Assert (!xmlReader.MoveToElement ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- }
- [Test]
- public void MoveToFirstAttributeWithNoAttributes ()
- {
- string xml = @"<foo />";
- RunTest (xml, new TestMethod (MoveToFirstAttributeWithNoAttributes));
- }
- public void MoveToFirstAttributeWithNoAttributes (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- Assert (!xmlReader.MoveToFirstAttribute ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- }
- [Test]
- public void MoveToNextAttributeWithNoAttributes ()
- {
- string xml = @"<foo />";
- RunTest (xml, new TestMethod (MoveToNextAttributeWithNoAttributes));
- }
- public void MoveToNextAttributeWithNoAttributes (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- Assert (!xmlReader.MoveToNextAttribute ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- }
- [Test]
- public void MoveToNextAttribute()
- {
- string xml = @"<foo bar=""baz"" quux='quuux'/>";
- RunTest (xml, new TestMethod (MoveToNextAttribute));
- }
- public void MoveToNextAttribute (XmlReader xmlReader)
- {
- AssertStartDocument (xmlReader);
- AssertNode (
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 2 // attributeCount
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "bar", // name
- String.Empty, // prefix
- "bar", // localName
- String.Empty, // namespaceURI
- "baz" // value
- );
- AssertAttribute (
- xmlReader, // xmlReader
- "quux", // name
- String.Empty, // prefix
- "quux", // localName
- String.Empty, // namespaceURI
- "quuux" // value
- );
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("bar", xmlReader.Name);
- AssertEquals ("baz", xmlReader.Value);
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("quux", xmlReader.Name);
- AssertEquals ("quuux", xmlReader.Value);
- Assert (!xmlReader.MoveToNextAttribute ());
- Assert (xmlReader.MoveToElement ());
- AssertNodeValues (
- "#1",
- xmlReader, // xmlReader
- XmlNodeType.Element, // nodeType
- 0, //depth
- true, // isEmptyElement
- "foo", // name
- String.Empty, // prefix
- "foo", // localName
- String.Empty, // namespaceURI
- String.Empty, // value
- 2 // attributeCount
- );
- AssertEndDocument (xmlReader);
- }
- [Test]
- // [Category ("NotDotNet")] // MS XmlNodeReader never moves to xml declaration.
- [Ignore ("Too inconsistent reference implementations to determine which is correct behavior.")]
- public void MoveToXmlDeclAttributes ()
- {
- string xml = "<?xml version=\"1.0\" standalone=\"yes\"?><root/>";
- RunTest (xml, new TestMethod (MoveToXmlDeclAttributes));
- }
- public void MoveToXmlDeclAttributes (XmlReader xmlReader)
- {
- xmlReader.Read ();
- this.AssertNodeValues ("#1", xmlReader,
- XmlNodeType.XmlDeclaration,
- 0,
- false,
- "xml",
- String.Empty,
- "xml",
- String.Empty,
- "version=\"1.0\" standalone=\"yes\"",
- 2);
- Assert ("MoveToFirstAttribute",
- xmlReader.MoveToFirstAttribute ());
- this.AssertNodeValues ("#2", xmlReader,
- XmlNodeType.Attribute,
- 0, // FIXME: might be 1
- false,
- "version",
- String.Empty,
- "version",
- String.Empty,
- "1.0",
- 2);
- xmlReader.ReadAttributeValue ();
- this.AssertNodeValues ("#3", xmlReader,
- XmlNodeType.Text,
- 1, // FIXME might be 2
- false,
- String.Empty,
- null, // FIXME: should be String.Empty,
- String.Empty,
- null, // FIXME: should be String.Empty,
- "1.0",
- 2);
- xmlReader.MoveToNextAttribute ();
- this.AssertNodeValues ("#4", xmlReader,
- XmlNodeType.Attribute,
- 0, // FIXME: might be 1
- false,
- "standalone",
- String.Empty,
- "standalone",
- String.Empty,
- "yes",
- 2);
- xmlReader.ReadAttributeValue ();
- this.AssertNodeValues ("#5", xmlReader,
- XmlNodeType.Text,
- 1, // FIXME: might be 2
- false,
- String.Empty,
- null, // FIXME: should be String.Empty,
- String.Empty,
- null, // FIXME: should be String.Empty,
- "yes",
- 2);
- }
- [Test]
- public void AttributeOrder ()
- {
- string xml = @"<foo _1='1' _2='2' _3='3' />";
- RunTest (xml, new TestMethod (AttributeOrder));
- }
- public void AttributeOrder (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- Assert (xmlReader.MoveToFirstAttribute ());
- AssertEquals ("_1", xmlReader.Name);
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("_2", xmlReader.Name);
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("_3", xmlReader.Name);
- Assert (!xmlReader.MoveToNextAttribute ());
- }
- [Test]
- [Category ("NotDotNet")]
- public void IndexerAndAttributes ()
- {
- string xml = @"<?xml version='1.0' standalone='no'?><foo _1='1' _2='2' _3='3' />";
- RunTest (xml, new TestMethod (IndexerAndAttributes));
- }
- public void IndexerAndAttributes (XmlReader xmlReader)
- {
- Assert (xmlReader.Read ());
- AssertEquals ("1.0", xmlReader ["version"]);
- AssertEquals ("1.0", xmlReader.GetAttribute ("version"));
- // .NET 1.1 BUG. XmlTextReader returns null, while XmlNodeReader returns "".
- AssertEquals (null, xmlReader ["encoding"]);
- AssertEquals (null, xmlReader.GetAttribute ("encoding"));
- AssertEquals ("no", xmlReader ["standalone"]);
- AssertEquals ("no", xmlReader.GetAttribute ("standalone"));
- AssertEquals ("1.0", xmlReader [0]);
- AssertEquals ("1.0", xmlReader.GetAttribute (0));
- AssertEquals ("no", xmlReader [1]);
- AssertEquals ("no", xmlReader.GetAttribute (1));
- Assert (xmlReader.Read ());
- AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
- AssertEquals ("1", xmlReader ["_1"]);
- Assert (xmlReader.MoveToFirstAttribute ());
- AssertEquals ("_1", xmlReader.Name);
- AssertEquals ("1", xmlReader ["_1"]);
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("_2", xmlReader.Name);
- AssertEquals ("1", xmlReader ["_1"]);
- Assert (xmlReader.MoveToNextAttribute ());
- AssertEquals ("_3", xmlReader.Name);
- AssertEquals ("1", xmlReader ["_1"]);
- Assert (!xmlReader.MoveToNextAttribute ());
- }
- [Test]
- public void ProhibitedMultipleAttributes ()
- {
- string xml = @"<foo _1='1' _1='1' />";
- try {
- RunTest (xml, new TestMethod (ReadAll));
- } catch (XmlException) {
- }
- xml = @"<foo _1='1' _1='2' />";
- try {
- RunTest (xml, new TestMethod (ReadAll));
- } catch (XmlException) {
- }
- }
- public void ReadAll (XmlReader xmlReader)
- {
- while (!xmlReader.EOF)
- xmlReader.Read ();
- }
- [Test]
- public void SurrogatePairContent ()
- {
- string xml = "<root xmlns='𐄀'/>";
- RunTest (xml, new TestMethod (SurrogatePairContent));
- }
- public void SurrogatePairContent (XmlReader xmlReader)
- {
- xmlReader.Read ();
- AssertEquals (true, xmlReader.MoveToAttribute ("xmlns"));
- AssertEquals ("xmlns", xmlReader.Name);
- AssertEquals (2, xmlReader.Value.Length);
- AssertEquals (0xD800, (int) xmlReader.Value [0]);
- AssertEquals (0xDD00, (int) xmlReader.Value [1]);
- }
- [Test]
- public void ReadOuterXmlOnEndElement ()
- {
- string xml = "<root><foo></foo></root>";
- RunTest (xml, new TestMethod (ReadOuterXmlOnEndElement));
- }
- public void ReadOuterXmlOnEndElement (XmlReader xmlReader)
- {
- xmlReader.Read ();
- xmlReader.Read ();
- xmlReader.Read ();
- AssertEquals (String.Empty, xmlReader.ReadOuterXml ());
- }
- [Test]
- public void ReadInnerXmlOnEndElement ()
- {
- string xml = "<root><foo></foo></root>";
- RunTest (xml, new TestMethod (ReadInnerXmlOnEndElement));
- }
- private void ReadInnerXmlOnEndElement (XmlReader xmlReader)
- {
- xmlReader.Read ();
- xmlReader.Read ();
- xmlReader.Read ();
- AssertEquals (String.Empty, xmlReader.ReadInnerXml ());
- }
- }
- }
|