| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158 |
- //
- // 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 ("any prefix which name starts from \"xml\" must not be allowed.");
- } 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);
- xtw.WriteElementString ("quux", null);
- Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText);
- xtw.WriteElementString ("", "quuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
- xtw.WriteElementString (null, "quuuux");
- Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
- }
- [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"));
- Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
- xtw.WriteEndElement ();
- xtw.WriteStartElement ("two");
- xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
- xtw.WriteString("quux");
- Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"));
- Assert.IsNull (xtw.LookupPrefix ("http://abc.def"));
- Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"));
- Assert.IsNull (xtw.LookupPrefix ("http://bogus"));
- }
- [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);
- xtw.WriteStartElement ("bar");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText);
- xtw.WriteStartElement ("baz");
- xtw.WriteAttributeString ("xml", "space", null, "preserve");
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
- xtw.WriteStartElement ("quux");
- xtw.WriteStartAttribute ("xml", "space", null);
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
- // 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);
- Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
- xtw.WriteEndElement ();
- Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
- 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"));
- xtw.WriteStartElement ("b");
- Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
- 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);
- }
- [Test]
- public void DontOutputRemovalDefaultNSDeclaration2 ()
- {
- xtw.WriteStartDocument ();
- // IMPORTANT DIFFERENCE!! ns = "", not null
- xtw.WriteStartElement ("foo", "");
- xtw.WriteAttributeString ("xmlns", "probe");
- Assert.IsNull (xtw.LookupPrefix ("probe"));
- xtw.WriteStartElement ("b");
- Assert.IsNull (xtw.LookupPrefix ("probe"));
- 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 /></b></foo>", StringWriterText);
- }
- [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 ()
- {
- // Compare with the test 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 ();
- }
- #if NET_2_0
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void RejectWritingAtErrorState ()
- {
- try {
- xtw.WriteEndElement ();
- } catch (Exception) {
- }
- xtw.WriteStartElement ("foo");
- }
- #endif
- }
- }
|