2
0

XmlSyndicationContentTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // XmlSyndicationContentTest.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.Collections.ObjectModel;
  31. using System.IO;
  32. using System.Runtime.Serialization;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using System.ServiceModel.Syndication;
  37. using NUnit.Framework;
  38. using QName = System.Xml.XmlQualifiedName;
  39. namespace MonoTests.System.ServiceModel.Syndication
  40. {
  41. [TestFixture]
  42. public class XmlSyndicationContentTest
  43. {
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void ConstructorNullReader ()
  47. {
  48. new XmlSyndicationContent ((XmlReader) null);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void ConstructorNullExtensionObject ()
  53. {
  54. new XmlSyndicationContent ("text/plain", null, (XmlObjectSerializer) null);
  55. }
  56. [Test]
  57. public void ConstructorNullSerializer ()
  58. {
  59. // allowed
  60. new XmlSyndicationContent ("text/plain", 5, (XmlObjectSerializer) null);
  61. new XmlSyndicationContent ("text/plain", 5, (XmlSerializer) null);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentNullException))]
  65. public void ConstructorNullExtension ()
  66. {
  67. new XmlSyndicationContent ("text/plain", null);
  68. }
  69. [Test]
  70. public void ConstructorNullType ()
  71. {
  72. // allowed
  73. new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
  74. }
  75. [Test]
  76. public void Type ()
  77. {
  78. XmlSyndicationContent t = new XmlSyndicationContent (null, 3, (XmlObjectSerializer) null);
  79. Assert.AreEqual ("text/xml", t.Type, "#1");
  80. t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
  81. Assert.AreEqual ("text/plain", t.Type, "#2");
  82. }
  83. [Test]
  84. public void Clone ()
  85. {
  86. XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 3, (XmlObjectSerializer) null);
  87. t = t.Clone () as XmlSyndicationContent;
  88. Assert.AreEqual ("text/plain", t.Type, "#1");
  89. }
  90. [Test]
  91. public void WriteTo ()
  92. {
  93. XmlSyndicationContent t = new XmlSyndicationContent ("text/plain", 6, (XmlObjectSerializer) null);
  94. StringWriter sw = new StringWriter ();
  95. using (XmlWriter w = CreateWriter (sw))
  96. t.WriteTo (w, "root", String.Empty);
  97. Assert.AreEqual ("<root type=\"text/plain\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></root>", sw.ToString ());
  98. }
  99. XmlWriter CreateWriter (StringWriter sw)
  100. {
  101. XmlWriterSettings s = new XmlWriterSettings ();
  102. s.OmitXmlDeclaration = true;
  103. return XmlWriter.Create (sw, s);
  104. }
  105. [Test]
  106. public void GetReaderAtContent ()
  107. {
  108. var x = new SyndicationElementExtension (6);
  109. // premise.
  110. Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int>", x.GetReader ().ReadOuterXml (), "#1");
  111. var t = new XmlSyndicationContent ("text/xml", 6, (XmlObjectSerializer) null);
  112. Assert.AreEqual ("<content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></content>", t.GetReaderAtContent ().ReadOuterXml (), "#2");
  113. }
  114. [Test]
  115. public void GetReaderAtContent2 ()
  116. {
  117. var inxml = "<xcontent type=\"text/xhtml\" xmlns=\"XXX-http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></xcontent>";
  118. var ms = new MemoryStream ();
  119. using (var xw = XmlWriter.Create (ms))
  120. new XmlSyndicationContent (XmlReader.Create (new StringReader (inxml))).WriteTo (xw, "contentsss", "urn:x");
  121. ms.Position = 0;
  122. var expected = "<?xml version='1.0' encoding='utf-8'?><contentsss type='text/xml' xmlns='urn:x'><int xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>6</int></contentsss>".Replace ('\'', '"');
  123. Assert.AreEqual (expected, new StreamReader (ms).ReadToEnd (), "#1");
  124. }
  125. }
  126. }