| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174 |
- //
- // System.Xml.XmlTextWriterTests
- //
- // Authors:
- // Kral Ferch <[email protected]>
- // Martin Willemoes Hansen <[email protected]>
- //
- // (C) 2002 Kral Ferch
- // (C) 2003 Martin Willemoes Hansen
- //
- using System;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.Xml
- {
- [TestFixture]
- public class XmlTextWriterTests
- {
- StringWriter sw;
- XmlTextWriter xtw;
- [SetUp]
- public void GetReady ()
- {
- sw = new StringWriter ();
- CreateXmlTextWriter ();
- }
- private void CreateXmlTextWriter ()
- {
- xtw = new XmlTextWriter (sw);
- xtw.QuoteChar = '\'';
- }
- private string StringWriterText
- {
- get { return sw.GetStringBuilder ().ToString (); }
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeBefore ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString("bar", "baz");
- xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
- }
- [Test]
- public void AttributeNamespacesNonNamespaceAttributeAfter ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
- xtw.WriteAttributeString("bar", "baz");
- Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
- }
- [Test]
- public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
- {
- xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText);
- }
- [Test]
- public void WriteAttributeString_XmlNs_Valid ()
- {
- xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xmlns", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xmlns:test='http://abc.def' />", StringWriterText, "#5");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlNs_Invalid1 ()
- {
- // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
- xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlNs_Invalid2 ()
- {
- // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
- xtw.WriteAttributeString (null, "xmlns", "http://somenamespace.com", "http://abc.def");
- }
- [Test]
- public void XmlSpace_Valid () // bug #77084
- {
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual ("xml:space='preserve'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xml", "space", "whatever", "default");
- Assert.AreEqual ("xml:space='default'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("xml", "space", "whatever", "default");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xml:space='default' />", StringWriterText, "#3");
- }
- [Test]
- public void WriteAttributeString_XmlPrefix_Valid ()
- {
- xtw.WriteAttributeString ("xml", "something", "whatever", "default");
- Assert.AreEqual ("xml:something='default'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("xml", "else", null, "whatever");
- Assert.AreEqual ("xml:else='whatever'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString ("xml", "something", "whatever", "default");
- xtw.WriteAttributeString ("xml", "else", null, "whatever");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<person xml:something='default' xml:else='whatever' />",
- StringWriterText, "#3");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteAttributeString_XmlSpace_Invalid ()
- {
- // only preserve and default are valid values for xml:space
- xtw.WriteAttributeString ("xml", "space", null, "something");
- }
- [Test]
- public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
- {
- xtw.WriteAttributeString ("a", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:a='http://abc.def'", StringWriterText, "#1");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeNamespacesWithNullInNamespaceParam ()
- {
- // you cannot use prefix with an empty namespace
- xtw.WriteAttributeString ("a", "abc", null, "http://abc.def");
- }
- [Test]
- public void AttributeNamespacesWithTextInNamespaceParam ()
- {
- xtw.WriteAttributeString ("a", "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("a:abc='http://abc.def'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
- Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#3");
- }
- [Test]
- [Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
- public void AutoCreatePrefixes ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
- xtw.WriteAttributeString (null, "def", "http://somenamespace.com", "http://def.ghi");
- xtw.WriteAttributeString (null, "ghi", "http://othernamespace.com", "http://ghi.jkl");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<root d1p1:abc='http://abc.def' d1p1:def='http://def.ghi' d1p2:ghi='http://ghi.jkl' xmlns:d1p2='http://othernamespace.com' xmlns:d1p1='http://somenamespace.com' />", StringWriterText, "#1");
- }
- [Test]
- [Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
- public void AutoCreatePrefixes2 ()
- {
- xtw.WriteStartElement ("person");
- xtw.WriteAttributeString (null, "name", "http://somenamespace.com", "Driesen");
- xtw.WriteAttributeString (null, "initials", "http://othernamespace.com", "GD");
- xtw.WriteAttributeString (null, "firstName", "http://somenamespace.com", "Gert");
- xtw.WriteStartElement ("address");
- xtw.WriteAttributeString (null, "street", "http://somenamespace.com", "Campus");
- xtw.WriteAttributeString (null, "number", "http://othernamespace.com", "1");
- xtw.WriteAttributeString (null, "zip", "http://newnamespace.com", "3000");
- xtw.WriteAttributeString (null, "box", "http://othernamespace.com", "a");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual (
- "<person" +
- " d1p1:name='Driesen'" +
- " d1p2:initials='GD'" +
- " d1p1:firstName='Gert'" +
- " xmlns:d1p2='http://othernamespace.com'" +
- " xmlns:d1p1='http://somenamespace.com'>" +
- "<address" +
- " d1p1:street='Campus'" +
- " d1p2:number='1'" +
- " d2p1:zip='3000'" +
- " d1p2:box='a'" +
- " xmlns:d2p1='http://newnamespace.com' />" +
- "</person>", StringWriterText, "#2");
- }
- [Test]
- [Category ("NotDotNet")]
- public void AttributeNamespacesXmlnsXmlns ()
- {
- xtw.WriteStartElement ("foo");
- try {
- xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
- // This should not be allowed, even though MS.NET doesn't treat as an error.
- // See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
- Assert.Fail ("A prefix must not start with \"xml\".");
- } catch (ArgumentException) {}
- xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
- }
- [Test]
- public void WriteAttributeString_EmptyLocalName ()
- {
- xtw.WriteAttributeString ("", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("", "http://somenamespace.com", "something");
- Assert.AreEqual ("d0p1:='something'", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("x", "", "http://somenamespace.com", "something");
- Assert.AreEqual ("x:='something'", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "something");
- Assert.AreEqual ("='something'", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "", "something");
- Assert.AreEqual ("='something'", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString (null, "http://somenamespace.com", "something");
- Assert.AreEqual ("d0p1:='something'", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteAttributeString ("x", null, "http://somenamespace.com", "something");
- Assert.AreEqual ("x:='something'", StringWriterText, "#8");
- }
- [Test]
- public void WriteStartAttribute_EmptyLocalName ()
- {
- xtw.WriteStartAttribute ("", "");
- Assert.AreEqual ("='", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", "", "");
- Assert.AreEqual ("='", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", "", "http://somenamespace.com");
- Assert.AreEqual ("d0p1:='", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("x", "", "http://somenamespace.com");
- Assert.AreEqual ("x:='", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null);
- Assert.AreEqual ("='", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null, "");
- Assert.AreEqual ("='", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("", null, "http://somenamespace.com");
- Assert.AreEqual ("d0p1:='", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartAttribute ("x", null, "http://somenamespace.com");
- Assert.AreEqual ("x:='", StringWriterText, "#8");
- }
- [Test]
- public void AttributeWriteAttributeString ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("<foo foo='bar'", StringWriterText);
- xtw.WriteAttributeString ("bar", "");
- Assert.AreEqual ("<foo foo='bar' bar=''", StringWriterText);
- xtw.WriteAttributeString ("baz", null);
- Assert.AreEqual ("<foo foo='bar' bar='' baz=''", StringWriterText);
- xtw.WriteAttributeString ("hoge", "a\nb");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b'", StringWriterText);
- xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
- Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b' fuga=' a\t
b\t'", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteString ("bar");
-
- xtw.WriteAttributeString ("baz", "quux");
- }
- [Test]
- public void AttributeWriteAttributeStringWithoutParentElement ()
- {
- xtw.WriteAttributeString ("foo", "bar");
- Assert.AreEqual ("foo='bar'", StringWriterText);
- xtw.WriteAttributeString ("baz", "quux");
- Assert.AreEqual ("foo='bar' baz='quux'", StringWriterText);
- }
- [Test]
- public void WriteStartElement_EmptyLocalName ()
- {
- xtw.WriteStartElement ("", "");
- Assert.AreEqual ("<", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", "", "");
- Assert.AreEqual ("<", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", "", "http://somenamespace.com");
- Assert.AreEqual ("<", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("x", "", "http://somenamespace.com");
- Assert.AreEqual ("<x:", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null);
- Assert.AreEqual ("<", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null, "");
- Assert.AreEqual ("<", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("", null, "http://somenamespace.com");
- Assert.AreEqual ("<", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("x", null, "http://somenamespace.com");
- Assert.AreEqual ("<x:", StringWriterText, "#8");
- }
- [Test]
- public void WriteElementString_EmptyLocalName ()
- {
- xtw.WriteElementString ("", "");
- Assert.AreEqual ("< />", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "", "");
- Assert.AreEqual ("< />", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "http://somenamespace.com", "whatever");
- Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString ("", "http://somenamespace.com", "");
- Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#4");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, null);
- Assert.AreEqual ("< />", StringWriterText, "#5");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, null, null);
- Assert.AreEqual ("< />", StringWriterText, "#6");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, "http://somenamespace.com", "whatever");
- Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#7");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteElementString (null, "http://somenamespace.com", null);
- Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#8");
- }
- [Test]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
- #endif
- public void WriteStartElement_Prefix_EmptyNamespace ()
- {
- xtw.WriteStartElement ("x", "whatever", "");
- Assert.AreEqual ("<whatever", StringWriterText, "#1");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<whatever />", StringWriterText, "#2");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_Prefix_NullNamespace ()
- {
- xtw.WriteStartElement ("x", "whatever", null);
- }
- [Test]
- public void WriteStartElement_XmlPrefix ()
- {
- xtw.WriteStartElement ("xml", "something", "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<xml:something", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("XmL", null, "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<XmL:", StringWriterText, "#2");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("xmlsomething", "name", "http://www.w3.org/XML/1998/namespace");
- Assert.AreEqual ("<xmlsomething:name", StringWriterText, "#3");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_XmlPrefix_Invalid1 ()
- {
- xtw.WriteStartElement ("xml", null, "http://somenamespace.com");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteStartElement_XmlPrefix_Invalid2 ()
- {
- xtw.WriteStartElement ("XmL", null, "http://somenamespace.com");
- }
- [Test]
- public void WriteStartElement_XmlPrefix_Invalid3 ()
- {
- // from XML 1.0 (third edition) specification:
- //
- // [Definition: A Name is a token beginning with a letter or one of a
- // few punctuation characters, and continuing with letters, digits,
- // hyphens, underscores, colons, or full stops, together known as name
- // characters.] Names beginning with the string "xml", or with any string
- // which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for
- // standardization in this or future versions of this specification.
- //
- // from the Namespaces in XML 1.0 specification:
- //
- // Prefixes beginning with the three-letter sequence x, m, l, in any case
- // combination, are reserved for use by XML and XML-related specifications.
- //
- // should this prefix then not be considered invalid ?
- //
- // both Mono and MS.NET 1.x/2.0 accept it though
- xtw.WriteStartElement ("xmlsomething", null, "http://somenamespace.com");
- Assert.AreEqual ("<xmlsomething:", StringWriterText, "#1");
- sw.GetStringBuilder ().Length = 0;
- CreateXmlTextWriter ();
- xtw.WriteStartElement ("XmLsomething", null, "http://somenamespace.com");
- Assert.AreEqual ("<XmLsomething:", StringWriterText, "#2");
- }
- [Test]
- public void CDataValid ()
- {
- xtw.WriteCData ("foo");
- Assert.AreEqual ("<![CDATA[foo]]>", StringWriterText, "WriteCData had incorrect output.");
- }
- [Test]
- public void CDataNull ()
- {
- xtw.WriteCData (null);
- Assert.AreEqual ("<![CDATA[]]>", StringWriterText, "WriteCData had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CDataInvalid ()
- {
- xtw.WriteCData("foo]]>bar");
- }
-
- [Test]
- public void CloseOpenElements ()
- {
- xtw.WriteStartElement("foo");
- xtw.WriteStartElement("bar");
- xtw.WriteStartElement("baz");
- xtw.Close();
- Assert.AreEqual ("<foo><bar><baz /></bar></foo>", StringWriterText,
- "Close didn't write out end elements properly.");
- }
- [Test]
- public void CloseWriteAfter ()
- {
- xtw.WriteElementString ("foo", "bar");
- xtw.Close ();
- // WriteEndElement and WriteStartDocument aren't tested here because
- // they will always throw different exceptions besides 'The Writer is closed.'
- // and there are already tests for those exceptions.
- try {
- xtw.WriteCData ("foo");
- Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Don't rely on English message assertion.
- // It is enough to check an exception occurs.
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteComment ("foo");
- Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteProcessingInstruction ("foo", "bar");
- Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteStartElement ("foo", "bar", "baz");
- Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- try {
- xtw.WriteAttributeString ("foo", "bar");
- Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
- }
- try {
- xtw.WriteString ("foo");
- Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
- }
- }
- [Test]
- public void CommentValid ()
- {
- xtw.WriteComment ("foo");
- Assert.AreEqual ("<!--foo-->", StringWriterText, "WriteComment had incorrect output.");
- }
- [Test]
- public void CommentInvalid ()
- {
- try {
- xtw.WriteComment("foo-");
- Assert.Fail("Should have thrown an ArgumentException.");
- } catch (ArgumentException) { }
- try {
- xtw.WriteComment("foo-->bar");
- Assert.Fail("Should have thrown an ArgumentException.");
- } catch (ArgumentException) { }
- }
- [Test]
- public void ConstructorsAndBaseStream ()
- {
- Assert.IsTrue (Object.ReferenceEquals (null, this.xtw.BaseStream), "BaseStream property returned wrong value.");
- MemoryStream ms;
- StreamReader sr;
- XmlTextWriter xtw;
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.Unicode);
- string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
- string actualXmlDeclaration = sr.ReadToEnd();
- Assert.AreEqual (expectedXmlDeclaration, actualXmlDeclaration);
- Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
- xtw.WriteStartDocument (true);
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.Unicode);
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, new UTF8Encoding ());
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, null);
- xtw.WriteStartDocument ();
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
- ms = new MemoryStream ();
- xtw = new XmlTextWriter (ms, null);
- xtw.WriteStartDocument (true);
- xtw.Flush ();
- ms.Seek (0, SeekOrigin.Begin);
- sr = new StreamReader (ms, Encoding.UTF8);
- Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
- Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
- }
- [Test]
- public void DocumentStart ()
- {
- xtw.WriteStartDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", StringWriterText,
- "XmlDeclaration is incorrect.");
- try {
- xtw.WriteStartDocument ();
- Assert.Fail("Should have thrown an InvalidOperationException.");
- } catch (InvalidOperationException) {
- // Don't rely on English message assertion.
- // It is enough to check an exception occurs.
- // Assert.AreEqual ("WriteStartDocument should be the first call.", e.Message, "Exception message is incorrect.");
- }
- xtw = new XmlTextWriter (sw = new StringWriter ());
- xtw.QuoteChar = '\'';
- xtw.WriteStartDocument (true);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
- xtw = new XmlTextWriter (sw = new StringWriter ());
- xtw.QuoteChar = '\'';
- xtw.WriteStartDocument (false);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
- }
- [Test]
- public void ElementAndAttributeSameXmlns ()
- {
- xtw.WriteStartElement ("ped", "foo", "urn:foo");
- xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
- }
- [Test]
- [Category ("NotDotNet")]
- public void ElementXmlnsNeedEscape ()
- {
- xtw.WriteStartElement ("test", "foo", "'");
- xtw.WriteEndElement ();
- // MS.NET output is : xmlns:test='''
- Assert.AreEqual ("<test:foo xmlns:test=''' />", StringWriterText);
- }
- [Test]
- public void ElementEmpty ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo />", StringWriterText, "Incorrect output.");
- }
- [Test]
- public void ElementWriteElementString ()
- {
- xtw.WriteElementString ("foo", "bar");
- Assert.AreEqual ("<foo>bar</foo>", StringWriterText, "WriteElementString has incorrect output.");
- xtw.WriteElementString ("baz", "");
- Assert.AreEqual ("<foo>bar</foo><baz />", StringWriterText, "#2");
- xtw.WriteElementString ("quux", null);
- Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText, "#3");
- xtw.WriteElementString ("", "quuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText, "#4");
- xtw.WriteElementString (null, "quuuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText, "#5");
- }
- [Test]
- public void FormattingTest ()
- {
- xtw.Formatting = Formatting.Indented;
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteElementString ("bar", "");
- xtw.Close ();
- Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void FormattingInvalidXmlForFun ()
- {
- xtw.Formatting = Formatting.Indented;
- xtw.IndentChar = 'x';
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteStartElement ("bar");
- xtw.WriteElementString ("baz", "");
- xtw.Close ();
- Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void FormattingFromRemarks ()
- {
- // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
- xtw.Formatting = Formatting.Indented;
- xtw.WriteStartElement ("ol");
- xtw.WriteStartElement ("li");
- xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
- xtw.WriteElementString ("b", "E");
- xtw.WriteElementString ("i", "lephant");
- xtw.WriteString (" walks slowly.");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
- }
- [Test]
- public void LookupPrefix ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteStartElement ("one");
- xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
- xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
- Assert.AreEqual ("foo", xtw.LookupPrefix ("http://abc.def"), "#1");
- Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"), "#2");
- xtw.WriteEndElement ();
- xtw.WriteStartElement ("two");
- xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
- xtw.WriteString("quux");
- Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"), "#3");
- Assert.IsNull (xtw.LookupPrefix ("http://abc.def"), "#4");
- Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"), "#5");
- Assert.IsNull (xtw.LookupPrefix ("http://bogus"), "#6");
- }
- [Test]
- public void NamespacesAttributesPassingInNamespaces ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo");
- // These shouldn't throw any exceptions since they don't pass in
- // a namespace.
- xtw.WriteAttributeString ("bar", "baz");
- xtw.WriteAttributeString ("", "a", "", "b");
- xtw.WriteAttributeString (null, "c", "", "d");
- xtw.WriteAttributeString ("", "e", null, "f");
- xtw.WriteAttributeString (null, "g", null, "h");
- Assert.AreEqual ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
- }
- [Test]
- public void NamespacesElementsPassingInNamespaces ()
- {
- xtw.Namespaces = false;
- // These shouldn't throw any exceptions since they don't pass in
- // a namespace.
- xtw.WriteElementString ("foo", "bar");
- xtw.WriteStartElement ("baz");
- xtw.WriteStartElement ("quux", "");
- xtw.WriteStartElement ("quuux", null);
- xtw.WriteStartElement (null, "a", null);
- xtw.WriteStartElement (null, "b", "");
- xtw.WriteStartElement ("", "c", null);
- xtw.WriteStartElement ("", "d", "");
- Assert.AreEqual ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid1 ()
- {
- // These should throw ArgumentException because they pass in a
- // namespace when Namespaces = false.
- xtw.Namespaces = false;
- xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid2 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "http://netsack.com/");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid3 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid4 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid5 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "bar", "");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesElementsPassingInNamespacesInvalid6 ()
- {
- xtw.Namespaces = false;
- xtw.WriteStartElement ("foo", "", "");
- }
- [Test]
- public void NamespacesNoNamespaceClearsDefaultNamespace ()
- {
- xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
- xtw.WriteStartElement(String.Empty, "bar", String.Empty);
- xtw.WriteElementString("baz", String.Empty, String.Empty);
- xtw.WriteEndElement();
- xtw.WriteEndElement();
- Assert.AreEqual ("<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
- StringWriterText, "XmlTextWriter is incorrectly outputting namespaces.");
- }
- [Test]
- public void NamespacesPrefix ()
- {
- xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
- xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
- xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
- StringWriterText, "XmlTextWriter is incorrectly outputting prefixes.");
- }
- [Test]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
- #endif
- public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
- {
- xtw.WriteStartElement ("foo", "bar", "");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
- {
- xtw.WriteStartElement ("foo", "bar", null);
- }
- [Test]
- public void NamespacesSettingWhenWriteStateNotStart ()
- {
- xtw.WriteStartElement ("foo");
- try {
- xtw.Namespaces = false;
- Assert.Fail ("Expected an InvalidOperationException.");
- } catch (InvalidOperationException) {}
- Assert.IsTrue (xtw.Namespaces);
- }
- [Test]
- public void ProcessingInstructionValid ()
- {
- xtw.WriteProcessingInstruction("foo", "bar");
- Assert.AreEqual ("<?foo bar?>", StringWriterText, "WriteProcessingInstruction had incorrect output.");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid1 ()
- {
- xtw.WriteProcessingInstruction("fo?>o", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid2 ()
- {
- xtw.WriteProcessingInstruction("foo", "ba?>r");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid3 ()
- {
- xtw.WriteProcessingInstruction("", "bar");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void ProcessingInstructionInvalid4 ()
- {
- xtw.WriteProcessingInstruction(null, "bar");
- }
- [Test]
- public void QuoteCharDoubleQuote ()
- {
- xtw.QuoteChar = '"';
- // version, encoding, standalone
- xtw.WriteStartDocument (true);
-
- // namespace declaration
- xtw.WriteElementString ("foo", "http://netsack.com", "bar");
- Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void QuoteCharInvalid ()
- {
- xtw.QuoteChar = 'x';
- }
- [Test]
- public void WriteBase64 ()
- {
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] fooBar = encoding.GetBytes("foobar");
- xtw.WriteBase64 (fooBar, 0, 6);
- Assert.AreEqual ("Zm9vYmFy", StringWriterText);
- try {
- xtw.WriteBase64 (fooBar, 3, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentException) {}
- try {
- xtw.WriteBase64 (fooBar, -1, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xtw.WriteBase64 (fooBar, 3, -1);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentOutOfRangeException) {}
- try {
- xtw.WriteBase64 (null, 0, 6);
- Assert.Fail ("Expected an Argument Exception to be thrown.");
- } catch (ArgumentNullException) {}
- }
- [Test]
- public void WriteBinHex ()
- {
- byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
- xtw.WriteBinHex (bytes, 0, 11);
- Assert.AreEqual ("040E22365E7286C2FF0005", StringWriterText);
- }
- [Test]
- public void WriteCharEntity ()
- {
- xtw.WriteCharEntity ('a');
- Assert.AreEqual ("a", StringWriterText);
- xtw.WriteCharEntity ('A');
- Assert.AreEqual ("aA", StringWriterText);
- xtw.WriteCharEntity ('1');
- Assert.AreEqual ("aA1", StringWriterText);
- xtw.WriteCharEntity ('K');
- Assert.AreEqual ("aA1K", StringWriterText);
- try {
- xtw.WriteCharEntity ((char)0xd800);
- } catch (ArgumentException) {}
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndAttribute ()
- {
- xtw.WriteEndAttribute ();
- }
- [Test]
- public void WriteEndDocument ()
- {
- try {
- xtw.WriteEndDocument ();
- Assert.Fail ("Expected an Exception.");
- // in .NET 2.0 it is InvalidOperationException.
- // in .NET 1,1 it is ArgumentException.
- } catch (Exception) {}
- }
- [Test]
- public void WriteEndDocument2 ()
- {
- xtw.WriteStartDocument ();
- try
- {
- xtw.WriteEndDocument ();
- Assert.Fail ("Expected an Exception.");
- // in .NET 2.0 it is InvalidOperationException.
- // in .NET 1,1 it is ArgumentException.
- } catch (Exception) {}
- }
- [Test]
- public void WriteEndDocument3 ()
- {
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("bar", null);
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
- xtw.WriteEndDocument ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
- Assert.AreEqual (WriteState.Start, xtw.WriteState);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteEndElement ()
- {
- // no matching StartElement
- xtw.WriteEndElement ();
- }
- [Test]
- public void WriteEndElement2 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo />", StringWriterText);
- xtw.WriteStartElement ("bar");
- xtw.WriteStartAttribute ("baz", null);
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo /><bar baz='' />", StringWriterText);
- }
- [Test]
- public void FullEndElement ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo>", StringWriterText);
- xtw.WriteStartElement ("bar");
- xtw.WriteAttributeString ("foo", "bar");
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
- xtw.WriteStartElement ("baz");
- xtw.WriteStartAttribute ("bar", null);
- xtw.WriteFullEndElement ();
- Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
- }
- [Test]
- public void WriteQualifiedName ()
- {
- xtw.WriteStartElement (null, "test", null);
- xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
- xtw.WriteQualifiedName ("bob", "http://localhost/");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
- }
- [Test]
- public void WriteQualifiedNameNonDeclaredAttribute ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("a", "");
- xtw.WriteQualifiedName ("attr", "urn:a");
- xtw.WriteWhitespace (" ");
- xtw.WriteQualifiedName ("attr", "urn:b");
- xtw.WriteEndAttribute ();
- xtw.WriteEndElement ();
- string xml = sw.ToString ();
- Assert.IsTrue (xml.IndexOf ("<foo ") >= 0, "foo");
- Assert.IsTrue (xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0, "qnames");
- Assert.IsTrue (xml.IndexOf (" xmlns:d1p1='urn:a'") > 0, "xmlns:a");
- Assert.IsTrue (xml.IndexOf (" xmlns:d1p2='urn:b'") > 0, "xmlns:b");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteQualifiedNameNonDeclaredContent ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteQualifiedName ("abc", "urn:abc");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteQualifiedNameNonNCName ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "urn:default");
- xtw.WriteStartElement ("child");
- xtw.WriteStartAttribute ("a", "");
- xtw.WriteQualifiedName ("x:def", "urn:def");
- }
- [Test]
- public void WriteRaw ()
- {
- xtw.WriteRaw("&<>\"'");
- Assert.AreEqual ("&<>\"'", StringWriterText);
- xtw.WriteRaw(null);
- Assert.AreEqual ("&<>\"'", StringWriterText);
- xtw.WriteRaw("");
- Assert.AreEqual ("&<>\"'", StringWriterText);
- }
- [Test]
- public void WriteRawInvalidInAttribute ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("bar", null);
- xtw.WriteRaw ("&<>\"'");
- xtw.WriteEndAttribute ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
- }
- [Test]
- public void WriteStateTest ()
- {
- Assert.AreEqual (WriteState.Start, xtw.WriteState);
- xtw.WriteStartDocument ();
- Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
- xtw.WriteStartElement ("root");
- Assert.AreEqual (WriteState.Element, xtw.WriteState);
- xtw.WriteElementString ("foo", "bar");
- Assert.AreEqual (WriteState.Content, xtw.WriteState);
- xtw.Close ();
- Assert.AreEqual (WriteState.Closed, xtw.WriteState);
- }
- [Test]
- public void WriteString ()
- {
- xtw.WriteStartDocument ();
- try {
- xtw.WriteString("foo");
- } catch (InvalidOperationException) {}
- }
- [Test]
- public void WriteString2 ()
- {
- xtw.WriteStartDocument ();
- // Testing attribute values
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "&<>");
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", StringWriterText);
- }
- [Test]
- public void WriteAttributeStringSingleQuoteChar()
- {
- // When QuoteChar is single quote then replaces single quotes within attributes
- // but not double quotes.
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "\"baz\"");
- xtw.WriteAttributeString ("quux", "'baz'");
- Assert.AreEqual ("<foo bar='\"baz\"' quux=''baz''", StringWriterText);
- }
- [Test]
- public void WriteAttributeStringDoubleQuoteChar()
- {
- // When QuoteChar is double quote then replaces double quotes within attributes
- // but not single quotes.
- xtw.QuoteChar = '"';
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "\"baz\"");
- xtw.WriteAttributeString ("quux", "'baz'");
- Assert.AreEqual ("<foo bar=\""baz"\" quux=\"'baz'\"", StringWriterText);
- }
- [Test]
- public void WriteStringWithEntities()
- {
- // Testing element values
- xtw.QuoteChar = '\'';
- xtw.WriteElementString ("foo", "&<>\"'");
- Assert.AreEqual ("<foo>&<>\"'</foo>", StringWriterText);
- }
- [Test]
- public void XmlLang ()
- {
- Assert.IsNull (xtw.XmlLang);
-
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
- xtw.WriteAttributeString ("boo", "yah");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
-
- xtw.WriteElementString("bar", "baz");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
-
- xtw.WriteString("baz");
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
-
- xtw.WriteStartElement ("quux");
- xtw.WriteStartAttribute ("xml", "lang", null);
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
-
- xtw.WriteString("langbar");
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langfoo", xtw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
-
- xtw.WriteEndAttribute ();
- // Commented out there: it is implementation-dependent.
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual ("langbar", xtw.XmlLang);
- // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
- // check if xml:lang repeats output even if same as current scope.
- xtw.WriteStartElement ("joe");
- xtw.WriteAttributeString ("xml", "lang", null, "langbar");
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
-
- xtw.WriteElementString ("quuux", "squonk");
- Assert.AreEqual ("langbar", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- Assert.AreEqual ("langfoo", xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", StringWriterText);
-
- xtw.WriteEndElement ();
- Assert.IsNull (xtw.XmlLang);
- Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", StringWriterText);
-
- xtw.Close ();
- Assert.IsNull (xtw.XmlLang);
- }
- // TODO: test operational aspects
- [Test]
- public void XmlSpaceTest ()
- {
- xtw.WriteStartElement ("foo");
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#1");
- xtw.WriteStartElement ("bar");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#2");
- Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText, "#3");
- xtw.WriteStartElement ("baz");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#4");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText, "#5");
- xtw.WriteStartElement ("quux");
- xtw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#6");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText, "#7");
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- xtw.WriteString ("default");
- // Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
-
- xtw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace, "#8");
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText, "#9");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#10");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#11");
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#12");
- xtw.WriteStartElement ("quux");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue1 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue2 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue3 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "Default");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void XmlSpaceTestInvalidValue4 ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xml", "space", null, "bubba");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespaceNonWhitespace ()
- {
- xtw.WriteWhitespace ("x");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespace_Null ()
- {
- xtw.WriteWhitespace ((string) null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteWhitespace_Empty ()
- {
- xtw.WriteWhitespace (string.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_Null ()
- {
- xtw.WriteNmToken ((string) null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_Empty ()
- {
- xtw.WriteNmToken (string.Empty);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteNmToken_InvalidChars ()
- {
- xtw.WriteNmToken ("\uFFFF");
- }
- [Test]
- public void WriteNmToken ()
- {
- xtw.WriteNmToken ("some:name");
- Assert.AreEqual ("some:name", StringWriterText);
- }
- [Test]
- public void XmlSpaceRaw ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='", StringWriterText);
- xtw.WriteString ("default");
- // Commented out there: it is implementation-dependent
- // and incompatible between .NET 1.0 and 1.1
- // Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
- // Assert.AreEqual ("<foo xml:space='", StringWriterText);
- xtw.WriteEndAttribute ();
- Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
- Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
- }
- [Test]
- public void WriteAttributes ()
- {
- XmlDocument doc = new XmlDocument();
- StringWriter sw = new StringWriter();
- XmlWriter wr = new XmlTextWriter(sw);
- StringBuilder sb = sw.GetStringBuilder();
- XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
- XmlTextReader xtr = new XmlTextReader("<?xml version='1.0' encoding='utf-8' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
- xtr.Read(); // read XMLDecl
- wr.WriteAttributes(xtr, false);
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
- "#WriteAttributes.XmlDecl.1");
- sb.Remove(0, sb.Length); // init
- ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
- xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
- xtr.Read(); // read XMLDecl
- Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
- sw = new StringWriter ();
- wr = new XmlTextWriter (sw);
- // This block raises an error on MS.NET 1.0.
- wr.WriteAttributes(xtr, false);
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
- "#WriteAttributes.XmlDecl.2");
- sw = new StringWriter ();
- wr = new XmlTextWriter (sw);
- sb.Remove(0, sb.Length); // init
- xtr.Read(); // read root
- Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
- wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
- wr.WriteAttributes(xtr, false);
- wr.WriteEndElement();
- wr.Close();
- // This method don't always have to take this double-quoted style...
- Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
- "#WriteAttributes.Element");
- xtr.Close ();
- }
- [Test]
- public void WriteWhitespace ()
- {
- xtw.WriteStartElement ("a");
- xtw.WriteWhitespace ("\n\t");
- xtw.WriteStartElement ("b");
- xtw.WriteWhitespace ("\n\t");
- xtw.WriteEndElement ();
- xtw.WriteWhitespace ("\n");
- xtw.WriteEndElement ();
- xtw.WriteWhitespace ("\n");
- xtw.Flush ();
- Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
- }
- [Test]
- public void FlushDoesntCloseTag ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "baz");
- xtw.Flush ();
- Assert.AreEqual ("<foo bar='baz'", StringWriterText);
- }
- [Test]
- public void WriteWhitespaceClosesTag ()
- {
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("bar", "baz");
- xtw.WriteWhitespace (" ");
- Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
- }
- [Test]
- public void DontOutputMultipleXmlns ()
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
- XmlDocument doc2 = new XmlDocument();
- doc2.LoadXml(doc.InnerXml);
- Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
- doc2.OuterXml);
- }
- [Test]
- public void DontOutputNonDeclaredXmlns ()
- {
- string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(xml);
- XmlDocument doc2 = new XmlDocument();
- doc2.LoadXml(doc.InnerXml);
- Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration ()
- {
- xtw.WriteStartDocument ();
- xtw.WriteStartElement ("foo");
- xtw.WriteAttributeString ("xmlns", "probe");
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#1");
- xtw.WriteStartElement ("b");
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#2");
- xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteEndElement (); // b
- xtw.WriteEndElement (); // foo
- xtw.WriteEndDocument ();
- xtw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText, "#3");
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration2 ()
- {
- xtw.WriteStartDocument ();
- // IMPORTANT DIFFERENCE!! ns = "", not null
- xtw.WriteStartElement ("foo", "");
- xtw.WriteAttributeString ("xmlns", "probe");
- Assert.IsNull (xtw.LookupPrefix ("probe"), "#1");
- xtw.WriteStartElement ("b");
- Assert.IsNull (xtw.LookupPrefix ("probe"), "#2");
- xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteStartElement (null, "b2", ""); // *Don't* output xmlns=""
- xtw.WriteEndElement (); // b2
- xtw.WriteEndElement (); // b
- xtw.WriteEndElement (); // foo
- xtw.WriteEndDocument ();
- xtw.Close ();
- Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText, "#3");
- }
- [Test]
- public void DoOutputRemovalDefaultNSDeclaration ()
- {
- xtw.WriteStartElement ("docelem", "a-namespace");
-
- XmlDocument doc = new XmlDocument ();
- doc.CreateElement ("hola").WriteTo (xtw);
- // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
- }
- [Test]
- public void WriteAttributeTakePrecedenceOnXmlns ()
- {
- xtw.WriteStartElement ("root", "urn:foo");
- xtw.WriteAttributeString ("xmlns", "urn:bar");
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixNull ()
- {
- xtw.LookupPrefix (null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void LookupPrefixEmpty ()
- {
- xtw.LookupPrefix (String.Empty);
- }
- [Test]
- public void LookupPrefixIgnoresXmlnsAttribute ()
- {
- Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("root");
- Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
- xtw.WriteAttributeString ("xmlns", "urn:foo");
- // Surprisingly to say, it is ignored!!
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("hoge");
- // (still after flushing previous start element.)
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- xtw.WriteStartElement ("fuga", "urn:foo");
- // Is this testing on the correct way? Yes, here it is.
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
- }
- [Test]
- public void WriteInvalidNames ()
- {
- xtw.WriteStartElement ("foo<>");
- xtw.WriteAttributeString ("ho<>ge", "value");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixWithoutNS ()
- {
- xtw.WriteStartAttribute ("some", "foo", null);
- }
- [Test]
- public void AttributeWriteStartAttributeXmlnsNullNS ()
- {
- xtw.WriteStartAttribute ("xmlns", "foo", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteEndAttributeXmlnsNullNs ()
- {
- // This test checks if the specified namespace URI is
- // incorrectly empty or not. Compare it with
- // AttributeWriteStartAttributeXmlnsNullNS().
- xtw.WriteStartAttribute ("xmlns", "foo", null);
- xtw.WriteEndAttribute ();
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
- {
- xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
- {
- xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
- }
- [Test]
- public void WriteRawProceedToProlog ()
- {
- XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
- xtw.WriteRaw ("");
- Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
- }
- [Test]
- public void Indent ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
- StringWriter sw = new StringWriter ();
- sw.NewLine = "_";
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.Formatting = Formatting.Indented;
- doc.WriteContentTo (xtw);
- Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
- }
- [Test]
- public void Indent2 ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter xtw = new XmlTextWriter (sw);
- xtw.Formatting = Formatting.Indented;
- // sadly, this silly usage of this method is actually
- // used in WriteNode() in MS.NET.
- xtw.WriteProcessingInstruction ("xml",
- "version=\"1.0\"");
- xtw.WriteComment ("sample XML fragment");
- Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
- @"<?xml version=""1.0""?>{0}" +
- "<!--sample XML fragment-->", Environment.NewLine),
- sw.ToString ());
- }
- [Test]
- public void CloseTwice ()
- {
- StringWriter sw = new StringWriter ();
- XmlTextWriter writer = new XmlTextWriter (sw);
- writer.Close ();
- // should not result in an exception
- writer.Close ();
- }
- [Test]
- public void WriteRawWriteString ()
- {
- // WriteRaw () -> WriteString ().
- xtw.WriteRaw ("");
- xtw.WriteString ("foo");
- Assert.AreEqual (WriteState.Content, xtw.WriteState);
- }
- [Test]
- public void LookupOverridenPrefix ()
- {
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
- xtw.WriteStartElement ("baz", "foo", "abc");
- Assert.IsNull (xtw.LookupPrefix ("xyz"));
- }
- [Test]
- public void DuplicatingNamespaceMappingInAttributes ()
- {
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
- xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteStartElement ("out");
- xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- xtw.WriteEndElement ();
- string xml = sw.ToString ();
- Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo");
- Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar");
- Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz");
- Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2");
- Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1");
- Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p");
- Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining");
- }
- [Test]
- public void WriteXmlSpaceIgnoresNS ()
- {
- xtw.WriteStartElement ("root");
- xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
- xtw.WriteEndElement ();
- Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
- }
- [Test] // bug #75546
- public void WriteEmptyNSQNameInAttribute ()
- {
- XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
- xtw.WriteStartElement ("foo", "urn:goo");
- xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
- xtw.WriteStartAttribute ("foo", "");
- xtw.WriteQualifiedName ("n1", "urn:bar");
- xtw.WriteEndAttribute ();
- xtw.WriteStartAttribute ("foo", "");
- xtw.WriteQualifiedName ("n2", "");
- xtw.WriteEndAttribute ();
- }
- [Test]
- public void WriteDocType ()
- {
- // we have the following test matrix:
- //
- // | name | publicid | systemid | subset|
- // |------------------------------------|
- // | X | X | X | X | #01
- // | X | E | X | X | #02
- // | X | X | E | X | #03
- // | X | X | X | E | #04
- // | X | E | E | X | #05
- // | X | X | E | E | #06
- // | X | E | X | E | #07
- // | X | E | E | E | #08
- // | X | N | X | X | #09
- // | X | X | N | X | #10
- // | X | X | X | N | #11
- // | X | N | N | X | #12
- // | X | X | N | N | #13
- // | X | N | X | N | #14
- // | X | N | N | N | #15
- //
- // Legend:
- // -------
- // X = Has value
- // E = Zero-length string
- // N = Null
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
- " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
- sw.ToString (), "#01");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
- sw.ToString (), "#02");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- string.Empty, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[sub]>",
- sw.ToString (), "#03");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
- sw.ToString (), "#04");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
- sw.ToString (), "#05");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- string.Empty, string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[]>",
- sw.ToString (), "#06");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
- sw.ToString (), "#07");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
- sw.ToString (), "#08");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
- sw.ToString (), "#09");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- (string) null, "sub");
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\"[sub]>",
- sw.ToString (), "#10");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
- sw.ToString (), "#11");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null, (string) null, "sub");
- Assert.AreEqual ("<!DOCTYPE test[sub]>",
- sw.ToString (), "#12");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
- (string) null, (string) null);
- Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
- " \"\">",
- sw.ToString (), "#13");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null,
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
- Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
- " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
- sw.ToString (), "#14");
- sw.GetStringBuilder ().Length = 0;
- xtw = new XmlTextWriter (sw);
- xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
- Assert.AreEqual ("<!DOCTYPE test>",
- sw.ToString (), "#15");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteDocType_EmptyName ()
- {
- xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void WriteDocType_NullName ()
- {
- xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
- }
- [Test] // bug #76095
- public void SurrogatePairsInWriteString ()
- {
- MemoryStream ms = new MemoryStream ();
- XmlWriter writer = new XmlTextWriter(ms, null);
- writer.WriteElementString("a", "\ud800\udf39");
- writer.Close();
- byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
- 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
- NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
- }
- [Test] // see also bug #77082
- public void WriteDocTypeIndent ()
- {
- string expected = String.Format (@"<?xml version='1.0'?>{0}<!DOCTYPE root PUBLIC '' 'urn:foo'[]>{0}<root />", Environment.NewLine);
- xtw.Formatting = Formatting.Indented;
- xtw.WriteProcessingInstruction ("xml", "version='1.0'");
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteStartElement ("root");
- xtw.WriteEndElement ();
- xtw.Close ();
- Assert.AreEqual (expected, StringWriterText);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void WriteDocTypeTwice ()
- {
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteDocType ("root", "", "urn:foo", "");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterDocType ()
- {
- xtw.WriteDocType ("root", "", "urn:foo", "");
- xtw.WriteStartDocument ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterWhitespace ()
- {
- xtw.WriteWhitespace (" ");
- xtw.WriteStartDocument ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void XmlDeclAfterPI ()
- {
- xtw.WriteProcessingInstruction ("pi", "");
- xtw.WriteStartDocument ();
- }
- [Test]
- public void WriteRawEmptyCloseStartTag ()
- {
- xtw.WriteStartElement ("stream", "stream","http://etherx.jabber.org/streams");
- xtw.WriteAttributeString ("version", "1.0");
- xtw.WriteAttributeString ("to", "[email protected]");
- xtw.WriteAttributeString ("from", "server");
- xtw.WriteAttributeString ("xmlns", "jabber:client");
- xtw.WriteRaw ("");// Ensure that the tag is closed
- xtw.Flush ();
- Assert.AreEqual ("<stream:stream version='1.0' to='[email protected]' from='server' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>", StringWriterText);
- }
- #if NET_2_0
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void RejectWritingAtErrorState ()
- {
- try {
- xtw.WriteEndElement ();
- } catch (Exception) {
- }
- xtw.WriteStartElement ("foo");
- }
- #endif
- }
- }
|