XmlBinaryWriterSessionTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // XmlBinaryWriterSessionTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Xml;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Xml
  34. {
  35. [TestFixture]
  36. public class XmlBinaryWriterSessionTest
  37. {
  38. [Test]
  39. [ExpectedException (typeof (InvalidOperationException))]
  40. public void TryAddDuplicate ()
  41. {
  42. XmlDictionary dic = new XmlDictionary ();
  43. XmlDictionaryString d1 = dic.Add ("foo");
  44. XmlBinaryWriterSession s = new XmlBinaryWriterSession ();
  45. int idx;
  46. s.TryAdd (d1, out idx);
  47. s.TryAdd (d1, out idx);
  48. }
  49. [Test]
  50. public void TryAddIndex ()
  51. {
  52. XmlDictionary dic = new XmlDictionary ();
  53. XmlDictionaryString d1 = dic.Add ("foo");
  54. XmlDictionaryString d2 = dic.Add ("bar");
  55. XmlDictionaryString d3 = dic.Add ("baz");
  56. XmlBinaryWriterSession s = new XmlBinaryWriterSession ();
  57. int idx;
  58. s.TryAdd (d1, out idx);
  59. Assert.AreEqual (0, idx, "#1");
  60. s.TryAdd (d3, out idx);
  61. Assert.AreEqual (1, idx, "#2"); // not 2
  62. }
  63. [Test]
  64. public void WriterAddsStringsToSession ()
  65. {
  66. var ms = new MemoryStream ();
  67. var d = new MyXmlDictionary ();
  68. var s = new MyXmlBinaryWriterSession ();
  69. var w = XmlDictionaryWriter.CreateBinaryWriter (ms, d, s);
  70. w.WriteStartElement ("root1");
  71. w.WriteEndElement ();
  72. Assert.AreEqual (0, d.List.Count, "#1");
  73. Assert.AreEqual (0, s.List.Count, "#2");
  74. w.WriteStartElement (d.Add ("root2"), XmlDictionaryString.Empty);
  75. w.WriteEndElement ();
  76. Assert.AreEqual (1, d.List.Count, "#3");
  77. Assert.AreEqual (0, s.List.Count, "#4");
  78. w.WriteStartElement (new XmlDictionary ().Add ("root3"), XmlDictionaryString.Empty);
  79. w.WriteEndElement ();
  80. Assert.AreEqual (1, d.List.Count, "#5");
  81. Assert.AreEqual (1, s.List.Count, "#6");
  82. }
  83. class MyXmlDictionary : XmlDictionary
  84. {
  85. public List<XmlDictionaryString> List = new List<XmlDictionaryString> ();
  86. public override XmlDictionaryString Add (string s)
  87. {
  88. var r = base.Add (s);
  89. List.Add (r);
  90. return r;
  91. }
  92. }
  93. class MyXmlBinaryWriterSession : XmlBinaryWriterSession
  94. {
  95. public List<XmlDictionaryString> List = new List<XmlDictionaryString> ();
  96. public override bool TryAdd (XmlDictionaryString s, out int key)
  97. {
  98. if (!base.TryAdd (s, out key))
  99. return false;
  100. List.Add (s);
  101. return true;
  102. }
  103. }
  104. }
  105. }