// // XmlSimpleDictionaryWriterTest.cs // // Author: // Atsushi Enomoto // // Copyright (C) 2005 Novell, Inc. http://www.novell.com // // Copied from System.XML/Test/System.Xml/XmlTextWriterTests.cs // // Authors: // Kral Ferch // Martin Willemoes Hansen // // (C) 2002 Kral Ferch // (C) 2003 Martin Willemoes Hansen // // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.IO; using System.Text; using System.Xml; using NUnit.Framework; namespace MonoTests.System.Xml { [TestFixture] public class XmlSimpleDictionaryWriterTest { [Test] public void WriteXmlnsAttribute () { xw.WriteStartElement ("l1"); xw.WriteXmlAttribute ("lang", "ja"); xw.WriteXmlnsAttribute ("f", "urn:foo"); xw.WriteStartElement ("l2"); xw.WriteXmlnsAttribute ("", ""); xw.WriteEndElement (); xw.WriteStartElement ("l2"); xw.WriteXmlnsAttribute ("", "urn:bar"); xw.WriteEndElement (); xw.WriteEndElement (); xw.Flush (); Assert.AreEqual ("", Output); } [Test] public void WriteXmlnsAttributeNullPrefix () { xw.WriteStartElement ("root", "urn:x"); xw.WriteXmlnsAttribute (null, "urn:foo"); xw.WriteEndElement (); xw.Close (); Assert.AreEqual ("", Output); } #region Copied from XmlTextWriterTests.cs StringWriter sw; XmlDictionaryWriter xw; [SetUp] public void GetReady () { sw = new StringWriter (); XmlTextWriter xtw = new XmlTextWriter (sw); xtw.QuoteChar = '\''; xw = XmlDictionaryWriter.CreateDictionaryWriter (xtw); } private string Output { get { return sw.ToString (); } } [Test] public void AttributeNamespacesNonNamespaceAttributeBefore () { xw.WriteStartElement ("foo"); xw.WriteAttributeString("bar", "baz"); xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def"); Assert.AreEqual ("", Output, "WriteCData had incorrect output."); } [Test] [ExpectedException (typeof (ArgumentException))] public void CDataInvalid () { xw.WriteCData("foo]]>bar"); } [Test] public void CloseOpenElements () { xw.WriteStartElement("foo"); xw.WriteStartElement("bar"); xw.WriteStartElement("baz"); xw.Close(); Assert.AreEqual ("", Output, "Close didn't write out end elements properly."); } [Test] public void CloseWriteAfter () { xw.WriteElementString ("foo", "bar"); xw.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 { xw.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 ("Exception message is incorrect.", "The Writer is closed.", e.Message); } try { xw.WriteComment ("foo"); Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message); } try { xw.WriteProcessingInstruction ("foo", "bar"); Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message); } try { xw.WriteStartElement ("foo", "bar", "baz"); Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message); } try { xw.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 { xw.WriteString ("foo"); Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException."); } catch (InvalidOperationException) { // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message); } } [Test] public void CommentValid () { xw.WriteComment ("foo"); Assert.AreEqual ("", Output, "WriteComment had incorrect output."); } [Test] public void CommentInvalid () { try { xw.WriteComment("foo-"); Assert.Fail ("Should have thrown an ArgumentException."); } catch (ArgumentException) { } try { xw.WriteComment("foo-->bar"); Assert.Fail ("Should have thrown an ArgumentException."); } catch (ArgumentException) { } } [Test] public void DocumentStart () { xw.WriteStartDocument (); Assert.AreEqual ("", Output, "XmlDeclaration is incorrect."); try { xw.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 ("Exception message is incorrect.", // "WriteStartDocument should be the first call.", e.Message); } GetReady (); xw.WriteStartDocument (true); Assert.AreEqual ("", Output); GetReady (); xw.WriteStartDocument (false); Assert.AreEqual ("", Output); } [Test] public void ElementAndAttributeSameXmlns () { xw.WriteStartElement ("ped", "foo", "urn:foo"); xw.WriteStartAttribute ("ped", "foo", "urn:foo"); xw.WriteEndElement (); Assert.AreEqual ("", Output); } [Test] [Category ("NotDotNet")] public void ElementXmlnsNeedEscape () { xw.WriteStartElement ("test", "foo", "'"); xw.WriteEndElement (); // MS.NET output is : xmlns:test=''' Assert.AreEqual ("", Output); } [Test] public void ElementEmpty () { xw.WriteStartElement ("foo"); xw.WriteEndElement (); Assert.AreEqual ("", Output); } [Test] public void ElementWriteElementString () { xw.WriteElementString ("foo", "bar"); Assert.AreEqual ("bar", Output, "WriteElementString has incorrect output."); xw.WriteElementString ("baz", ""); Assert.AreEqual ("bar", Output); xw.WriteElementString ("quux", null); Assert.AreEqual ("bar", Output); xw.WriteElementString ("", "quuux"); Assert.AreEqual ("bar<>quuux", Output); xw.WriteElementString (null, "quuuux"); Assert.AreEqual ("bar<>quuux<>quuuux", Output); } [Test] public void LookupPrefix () { xw.WriteStartElement ("root"); xw.WriteStartElement ("one"); xw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def"); xw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl"); Assert.AreEqual ("foo", xw.LookupPrefix ("http://abc.def")); Assert.AreEqual ("bar", xw.LookupPrefix ("http://ghi.jkl")); xw.WriteEndElement (); xw.WriteStartElement ("two"); xw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr"); xw.WriteString("quux"); Assert.AreEqual ("baz", xw.LookupPrefix ("http://mno.pqr")); Assert.IsNull (xw.LookupPrefix ("http://abc.def")); Assert.IsNull (xw.LookupPrefix ("http://ghi.jkl")); Assert.IsNull (xw.LookupPrefix ("http://bogus")); } [Test] public void NamespacesNoNamespaceClearsDefaultNamespace () { xw.WriteStartElement(String.Empty, "foo", "http://netsack.com/"); xw.WriteStartElement(String.Empty, "bar", String.Empty); xw.WriteElementString("baz", String.Empty, String.Empty); xw.WriteEndElement(); xw.WriteEndElement(); Assert.AreEqual ( "", Output, "XmlTextWriter is incorrectly outputting namespaces."); } [Test] public void NamespacesPrefix () { xw.WriteStartElement ("foo", "bar", "http://netsack.com/"); xw.WriteStartElement ("foo", "baz", "http://netsack.com/"); xw.WriteElementString ("qux", "http://netsack.com/", String.Empty); xw.WriteEndElement (); xw.WriteEndElement (); Assert.AreEqual ("", Output, "XmlTextWriter is incorrectly outputting prefixes."); } [Test] // [ExpectedException (typeof (ArgumentException))] public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty () { xw.WriteStartElement ("foo", "bar", ""); } [Test] [ExpectedException (typeof (ArgumentException))] public void NamespacesPrefixWithEmptyAndNullNamespaceNull () { xw.WriteStartElement ("foo", "bar", null); } [Test] public void ProcessingInstructionValid () { xw.WriteProcessingInstruction("foo", "bar"); Assert.AreEqual ("", Output, "WriteProcessingInstruction had incorrect output."); } [Test] [ExpectedException (typeof (ArgumentException))] public void ProcessingInstructionInvalid1 () { xw.WriteProcessingInstruction("fo?>o", "bar"); } [Test] [ExpectedException (typeof (ArgumentException))] public void ProcessingInstructionInvalid2 () { xw.WriteProcessingInstruction("foo", "ba?>r"); } [Test] [ExpectedException (typeof (ArgumentException))] public void ProcessingInstructionInvalid3 () { xw.WriteProcessingInstruction("", "bar"); } [Test] [ExpectedException (typeof (ArgumentException))] public void ProcessingInstructionInvalid4 () { xw.WriteProcessingInstruction(null, "bar"); } [Test] public void WriteBase64 () { UTF8Encoding encoding = new UTF8Encoding(); byte[] fooBar = encoding.GetBytes("foobar"); xw.WriteBase64 (fooBar, 0, 6); Assert.AreEqual("Zm9vYmFy", Output); try { xw.WriteBase64 (fooBar, 3, 6); Assert.Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentException) {} try { xw.WriteBase64 (fooBar, -1, 6); Assert.Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentOutOfRangeException) {} try { xw.WriteBase64 (fooBar, 3, -1); Assert.Fail ("Expected an Argument Exception to be thrown."); } catch (ArgumentOutOfRangeException) {} try { xw.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}; xw.WriteBinHex (bytes, 0, 11); Assert.AreEqual ("040E22365E7286C2FF0005", Output); } [Test] public void WriteCharEntity () { xw.WriteCharEntity ('a'); Assert.AreEqual ("a", Output); xw.WriteCharEntity ('A'); Assert.AreEqual ("aA", Output); xw.WriteCharEntity ('1'); Assert.AreEqual ("aA1", Output); xw.WriteCharEntity ('K'); Assert.AreEqual ("aA1K", Output); try { xw.WriteCharEntity ((char)0xd800); } catch (ArgumentException) {} } [Test] [ExpectedException (typeof (InvalidOperationException))] public void WriteEndAttribute () { xw.WriteEndAttribute (); } [Test] public void WriteEndDocument () { try { xw.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 () { xw.WriteStartDocument (); try { xw.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 () { xw.WriteStartDocument (); xw.WriteStartElement ("foo"); xw.WriteStartAttribute ("bar", null); Assert.AreEqual ("", Output); Assert.AreEqual (WriteState.Start, xw.WriteState); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void WriteEndElement () { // no matching StartElement xw.WriteEndElement (); } [Test] public void WriteEndElement2 () { xw.WriteStartElement ("foo"); xw.WriteEndElement (); Assert.AreEqual ("", Output); xw.WriteStartElement ("bar"); xw.WriteStartAttribute ("baz", null); xw.WriteEndElement (); Assert.AreEqual ("", Output); } [Test] public void FullEndElement () { xw.WriteStartElement ("foo"); xw.WriteFullEndElement (); Assert.AreEqual ("", Output); xw.WriteStartElement ("bar"); xw.WriteAttributeString ("foo", "bar"); xw.WriteFullEndElement (); Assert.AreEqual ("", Output); xw.WriteStartElement ("baz"); xw.WriteStartAttribute ("bar", null); xw.WriteFullEndElement (); Assert.AreEqual ("", Output); } [Test] public void WriteQualifiedName () { xw.WriteStartElement (null, "test", null); xw.WriteAttributeString ("xmlns", "me", null, "http://localhost/"); xw.WriteQualifiedName ("bob", "http://localhost/"); xw.WriteEndElement (); Assert.AreEqual ("me:bob", Output); } [Test] public void WriteQualifiedNameNonDeclaredAttribute () { xw.WriteStartElement ("foo"); xw.WriteStartAttribute ("a", ""); xw.WriteQualifiedName ("attr", "urn:a"); xw.WriteWhitespace (" "); xw.WriteQualifiedName ("attr", "urn:b"); xw.WriteEndAttribute (); xw.WriteEndElement (); string xml = sw.ToString (); Assert.IsTrue ( xml.IndexOf ("= 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 () { xw.WriteStartElement ("foo"); xw.WriteQualifiedName ("abc", "urn:abc"); } [Test] [ExpectedException (typeof (ArgumentException))] public void WriteQualifiedNameNonNCName () { xw.WriteStartElement ("foo"); xw.WriteAttributeString ("xmlns", "urn:default"); xw.WriteStartElement ("child"); xw.WriteStartAttribute ("a", ""); xw.WriteQualifiedName ("x:def", "urn:def"); } [Test] public void WriteRaw () { xw.WriteRaw("&<>\"'"); Assert.AreEqual ("&<>\"'", Output); xw.WriteRaw(null); Assert.AreEqual ("&<>\"'", Output); xw.WriteRaw(""); Assert.AreEqual ("&<>\"'", Output); } [Test] public void WriteRawInvalidInAttribute () { xw.WriteStartElement ("foo"); xw.WriteStartAttribute ("bar", null); xw.WriteRaw ("&<>\"'"); xw.WriteEndAttribute (); xw.WriteEndElement (); Assert.AreEqual ("", Output); } [Test] public void WriteStateTest () { Assert.AreEqual (WriteState.Start, xw.WriteState); xw.WriteStartDocument (); Assert.AreEqual (WriteState.Prolog, xw.WriteState); xw.WriteStartElement ("root"); Assert.AreEqual (WriteState.Element, xw.WriteState); xw.WriteElementString ("foo", "bar"); Assert.AreEqual (WriteState.Content, xw.WriteState); xw.Close (); Assert.AreEqual (WriteState.Closed, xw.WriteState); } [Test] public void WriteString () { xw.WriteStartDocument (); try { xw.WriteString("foo"); } catch (InvalidOperationException) {} } [Test] public void WriteString2 () { xw.WriteStartDocument (); // Testing attribute values xw.WriteStartElement ("foo"); xw.WriteAttributeString ("bar", "&<>"); Assert.AreEqual ("\"'"); Assert.AreEqual ("&<>\"'", Output); } [Test] public void XmlLang () { Assert.IsNull (xw.XmlLang); xw.WriteStartElement ("foo"); xw.WriteAttributeString ("xml", "lang", null, "langfoo"); Assert.AreEqual ("langfoo", xw.XmlLang); Assert.AreEqual ("baz", Output); xw.WriteString("baz"); Assert.AreEqual ("langfoo", xw.XmlLang); Assert.AreEqual ("bazbaz", Output); xw.WriteStartElement ("quux"); xw.WriteStartAttribute ("xml", "lang", null); Assert.AreEqual ("langfoo", xw.XmlLang); Assert.AreEqual ("bazbazbazbazbazbazbazbazbazbazsquonk", Output); xw.WriteEndElement (); xw.WriteEndElement (); Assert.AreEqual ("langfoo", xw.XmlLang); Assert.AreEqual ("bazbazsquonk", Output); xw.WriteEndElement (); Assert.IsNull (xw.XmlLang); Assert.AreEqual ("bazbazsquonk", Output); xw.Close (); Assert.IsNull (xw.XmlLang); } // TODO: test operational aspects [Test] public void XmlSpaceTest () { xw.WriteStartElement ("foo"); Assert.AreEqual (XmlSpace.None, xw.XmlSpace); xw.WriteStartElement ("bar"); xw.WriteAttributeString ("xml", "space", null, "preserve"); Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace); Assert.AreEqual ("", 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("", 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("", sw.ToString().Trim(), "#WriteAttributes.Element"); xtr.Close (); } [Test] public void WriteWhitespace () { xw.WriteStartElement ("a"); xw.WriteWhitespace ("\n\t"); xw.WriteStartElement ("b"); xw.WriteWhitespace ("\n\t"); xw.WriteEndElement (); xw.WriteWhitespace ("\n"); xw.WriteEndElement (); xw.WriteWhitespace ("\n"); xw.Flush (); Assert.AreEqual ("\n\t\n\t\n\n", Output); } [Test] public void FlushDoesntCloseTag () { xw.WriteStartElement ("foo"); xw.WriteAttributeString ("bar", "baz"); xw.Flush (); Assert.AreEqual (" ", Output); } [Test] public void DontOutputMultipleXmlns () { XmlDocument doc = new XmlDocument(); doc.LoadXml(""); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml(doc.InnerXml); Assert.AreEqual ("", doc2.OuterXml); } [Test] public void DontOutputNonDeclaredXmlns () { string xml = ""; 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 () { xw.WriteStartDocument (); xw.WriteStartElement ("foo"); xw.WriteAttributeString ("xmlns", "probe"); Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe")); xw.WriteStartElement ("b"); Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe")); xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns="" xw.WriteEndElement (); // b2 xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns="" xw.WriteEndElement (); // b2 xw.WriteEndElement (); // b xw.WriteEndElement (); // foo xw.WriteEndDocument (); xw.Close (); Assert.AreEqual ("", Output); } [Test] public void DontOutputRemovalDefaultNSDeclaration2 () { xw.WriteStartDocument (); // IMPORTANT DIFFERENCE!! ns = "", not null xw.WriteStartElement ("foo", ""); xw.WriteAttributeString ("xmlns", "probe"); Assert.IsNull (xw.LookupPrefix ("probe")); xw.WriteStartElement ("b"); Assert.IsNull (xw.LookupPrefix ("probe")); xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns="" xw.WriteEndElement (); // b2 xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns="" xw.WriteEndElement (); // b2 xw.WriteEndElement (); // b xw.WriteEndElement (); // foo xw.WriteEndDocument (); xw.Close (); Assert.AreEqual ("", Output); } [Test] public void DoOutputRemovalDefaultNSDeclaration () { xw.WriteStartElement ("docelem", "a-namespace"); XmlDocument doc = new XmlDocument (); doc.CreateElement ("hola").WriteTo (xw); // This means, WriteTo never passes null NamespaceURI argument to XmlWriter. xw.WriteEndElement (); xw.Close (); Assert.AreEqual ("", Output); } [Test] public void WriteAttributeTakePrecedenceOnXmlns () { xw.WriteStartElement ("root", "urn:foo"); xw.WriteAttributeString ("xmlns", "urn:bar"); xw.WriteEndElement (); xw.Close (); Assert.AreEqual ("", Output); } [Test] [ExpectedException (typeof (ArgumentException))] public void LookupPrefixNull () { xw.LookupPrefix (null); } [Test] [ExpectedException (typeof (ArgumentException))] public void LookupPrefixEmpty () { xw.LookupPrefix (String.Empty); } [Test] public void LookupPrefixIgnoresXmlnsAttribute () { Assert.IsNull (xw.LookupPrefix ("urn:foo")); xw.WriteStartElement ("root"); Assert.IsNull (xw.LookupPrefix ("urn:foo")); xw.WriteAttributeString ("xmlns", "urn:foo"); // Surprisingly to say, it is ignored!! Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo")); xw.WriteStartElement ("hoge"); // (still after flushing previous start element.) Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo")); xw.WriteStartElement ("fuga", "urn:foo"); // Is this testing on the correct way? Yes, here it is. Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo")); } [Test] public void WriteInvalidNames () { xw.WriteStartElement ("foo<>"); xw.WriteAttributeString ("ho<>ge", "value"); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributePrefixWithoutNS () { xw.WriteStartAttribute ("some", "foo", null); } [Test] public void AttributeWriteStartAttributeXmlnsNullNS () { xw.WriteStartAttribute ("xmlns", "foo", null); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteEndAttributeXmlnsNullNs () { // Compare with the test AttributeWriteStartAttributeXmlnsNullNS(). xw.WriteStartAttribute ("xmlns", "foo", null); xw.WriteEndAttribute (); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS () { xw.WriteStartAttribute ("xmlns", "foo", "urn:foo"); } [Test] [ExpectedException (typeof (ArgumentException))] public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS () { xw.WriteStartAttribute ("", "xmlns", "urn:foo"); } [Test] public void WriteRawProceedToProlog () { XmlTextWriter xw = new XmlTextWriter (new StringWriter ()); xw.WriteRaw (""); Assert.AreEqual (WriteState.Prolog, xw.WriteState); } [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 (). xw.WriteRaw (""); xw.WriteString ("foo"); Assert.AreEqual (WriteState.Content, xw.WriteState); } [Test] public void LookupOverridenPrefix () { xw.WriteStartElement ("out"); xw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz"); xw.WriteStartElement ("baz", "foo", "abc"); Assert.IsNull (xw.LookupPrefix ("xyz")); } [Test] public void DuplicatingNamespaceMappingInAttributes () { xw.WriteStartElement ("out"); xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz"); xw.WriteAttributeString ("p", "bar", "urn:bar", "xyz"); xw.WriteAttributeString ("p", "baz", "urn:baz", "xyz"); xw.WriteStartElement ("out"); xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz"); xw.WriteStartElement ("out"); xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz"); xw.WriteEndElement (); xw.WriteEndElement (); xw.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 ("") > 0, "remaining"); } [Test] public void WriteXmlSpaceIgnoresNS () { xw.WriteStartElement ("root"); xw.WriteAttributeString ("xml", "space", "abc", "preserve"); xw.WriteEndElement (); Assert.AreEqual ("", sw.ToString ()); } [Test] // bug #75546 public void WriteEmptyNSQNameInAttribute () { XmlTextWriter xw = new XmlTextWriter (TextWriter.Null); xw.WriteStartElement ("foo", "urn:goo"); xw.WriteAttributeString ("xmlns:bar", "urn:bar"); xw.WriteStartAttribute ("foo", ""); xw.WriteQualifiedName ("n1", "urn:bar"); xw.WriteEndAttribute (); xw.WriteStartAttribute ("foo", ""); xw.WriteQualifiedName ("n2", ""); xw.WriteEndAttribute (); } [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] [ExpectedException (typeof (InvalidOperationException))] public void RejectWritingAtErrorState () { try { xw.WriteEndElement (); } catch (Exception) { } xw.WriteStartElement ("foo"); } #endregion [Test] public void WriteBooleanArray () { bool [] array = new bool [] {true, false, true, true, false}; xw.WriteArray ("", "root", "", array, 1, 3); Assert.AreEqual ("falsetruetrue", Output, "#1"); } [Test] public void WriteNode () { string s = @"79310c9f-18d4-4337-a95a-1865ca54a66e3amesh".Replace ('\'', '"'); var sw = new StringWriter (); var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, new XmlWriterSettings () { OmitXmlDeclaration = true })); var xr = XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (s))); xr.MoveToContent (); while (!xr.EOF && xr.NodeType != XmlNodeType.EndElement) xw.WriteNode (xr, false); xw.Flush (); Assert.AreEqual (s, sw.ToString ()); } } }