SyndicationContentTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // SyndicationContentTest.cs - NUnit Test Cases for Abstract Class SyncicationContent
  3. //
  4. // Author:
  5. // Stephen A Jazdzewski ([email protected])
  6. //
  7. // Copyright (C) 2007 Stephen A Jazdzewski
  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.Text;
  30. using System.Xml;
  31. using NUnit.Framework;
  32. using System.ServiceModel.Syndication;
  33. namespace MonoTests.System.ServiceModel.Syndication
  34. {
  35. [TestFixture]
  36. public class TextSyndicationContentTest {
  37. static string title = "Lorem";
  38. static string lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer vitae.";
  39. string [] text_types = new string [3];
  40. [SetUp]
  41. public void Setup ()
  42. {
  43. // Map Text Content Kind to expected return value from .Type
  44. text_types [(int) TextSyndicationContentKind.Html] = "html";
  45. text_types [(int) TextSyndicationContentKind.Plaintext] = "text";
  46. text_types [(int) TextSyndicationContentKind.XHtml] = "xhtml";
  47. }
  48. [Test]
  49. public void TextSimple ()
  50. {
  51. TextSyndicationContent stext = new TextSyndicationContent (lorem, TextSyndicationContentKind.Plaintext);
  52. Assert.AreEqual (typeof (TextSyndicationContent), stext.GetType (), "#TS1");
  53. Assert.AreEqual (typeof (string), stext.Text.GetType (), "#TS2");
  54. // check for content and type
  55. Assert.AreEqual (lorem, stext.Text.ToString (), "#TS3");
  56. Assert.AreEqual (text_types [(int) TextSyndicationContentKind.Plaintext], stext.Type.ToString (), "#TS4");
  57. stext = new TextSyndicationContent (null);
  58. // Be sure .Text is null
  59. try
  60. {
  61. Assert.AreEqual (typeof (string), stext.Text.GetType (), "#TS6");
  62. Assert.Fail ("#TS7 Expected an NullReferenceException to be thrown.");
  63. }
  64. catch (NullReferenceException) {}
  65. }
  66. static string validhtml = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" " +
  67. "\"http://www.w3.org/TR/html4/strict.dtd\">" +
  68. "<HTML><HEAD><TITLE>" + title + "/TITLE></HEAD>" +
  69. "<BODY><P>" + lorem + "</BODY></HTML>";
  70. [Test]
  71. public void HtmlSimple ()
  72. {
  73. TextSyndicationContent shtml = new TextSyndicationContent (validhtml, TextSyndicationContentKind.Html);
  74. Assert.AreEqual (typeof (TextSyndicationContent), shtml.GetType (), "#HS1");
  75. Assert.AreEqual (typeof (string), shtml.Text.GetType (), "#HS2");
  76. // check for content and type
  77. Assert.AreEqual (validhtml, shtml.Text.ToString (), "#HS3");
  78. Assert.AreEqual (text_types [(int) TextSyndicationContentKind.Html], shtml.Type, "#HS4");
  79. }
  80. static string validxhtml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  81. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " +
  82. "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" +
  83. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" +
  84. "<head><title>" + title + "</title></head>" +
  85. "<body><p>" + lorem + "</p></body></html>";
  86. [Test]
  87. public void XHtmlSimple ()
  88. {
  89. TextSyndicationContent sxhtml = new TextSyndicationContent (validhtml, TextSyndicationContentKind.XHtml);
  90. Assert.AreEqual (typeof (TextSyndicationContent), sxhtml.GetType (), "#XS1");
  91. Assert.AreEqual (typeof (string), sxhtml.Text.GetType (), "#XS2");
  92. // check for content and type
  93. Assert.AreEqual (validhtml, sxhtml.Text.ToString (), "#XS3");
  94. Assert.AreEqual (text_types [(int) TextSyndicationContentKind.XHtml], sxhtml.Type, "#XS4");
  95. }
  96. [Test]
  97. public void Advanced ()
  98. {
  99. // Defaults to Plaintext
  100. TextSyndicationContent stext = new TextSyndicationContent (lorem);
  101. Assert.AreEqual (text_types [(int) TextSyndicationContentKind.Plaintext], stext.Type, "#A1");
  102. }
  103. }
  104. [TestFixture]
  105. public class UrlSyndicationContentTest {
  106. static string media_type = "Text/xml";
  107. static string url = "http://www.Jazd.com/rss";
  108. [Test]
  109. public void Simple ()
  110. {
  111. Uri suri = new Uri (url);
  112. UrlSyndicationContent surl = new UrlSyndicationContent (suri, media_type);
  113. Assert.AreEqual (typeof (UrlSyndicationContent), surl.GetType (), "#S1");
  114. // check for content and type
  115. Assert.AreEqual (url.ToLower (), surl.Url.ToString (), "#S2");
  116. Assert.AreEqual (media_type, surl.Type.ToString (), "#S3");
  117. }
  118. }
  119. [TestFixture]
  120. public class XmlSyndicationContentTest {
  121. static string type = "text/xml";
  122. static string tagname = "stuff";
  123. static string content = "Need some";
  124. static string documentname = "channel";
  125. // May end up being setup
  126. [Test]
  127. public void Simple ()
  128. {
  129. XmlDocument sdoc = new XmlDocument ();
  130. XmlElement selem = sdoc.CreateElement (tagname);
  131. selem.InnerText = content;
  132. StringBuilder syndication_string = new StringBuilder ();
  133. XmlWriterSettings settings = new XmlWriterSettings ();
  134. XmlWriter syndication = XmlWriter.Create (syndication_string, settings);
  135. // Simple tests
  136. XmlSyndicationContent sxml = new XmlSyndicationContent (type, selem);
  137. Assert.AreEqual (typeof (XmlSyndicationContent), sxml.GetType (), "#S1");
  138. Assert.AreEqual (type, sxml.Type.ToString (), "#S2");
  139. // Check correct invalid argument rejection
  140. try
  141. {
  142. sxml.WriteTo (null, "", "");
  143. Assert.Fail ("#S3 Expected an ArgumentNullException to be thrown.");
  144. }
  145. catch (ArgumentNullException) { }
  146. try
  147. {
  148. sxml.WriteTo (syndication, "", "");
  149. Assert.Fail ("#S4 Expected an ArgumentException to be thrown.");
  150. }
  151. catch (ArgumentException) { }
  152. syndication.Close ();
  153. }
  154. [Test]
  155. public void XmlElementExtension ()
  156. {
  157. XmlDocument xdoc = new XmlDocument ();
  158. XmlElement xelem = xdoc.CreateElement (tagname);
  159. xelem.InnerText = content;
  160. // Same as Simple tests as setup for XmlElementExtension tests
  161. XmlSyndicationContent xxml = new XmlSyndicationContent (type, xelem);
  162. Assert.AreEqual (typeof (XmlSyndicationContent), xxml.GetType (), "#XE1");
  163. Assert.AreEqual (type, xxml.Type.ToString (), "#XE2");
  164. Assert.AreSame (xelem, xxml.Extension.Object, "#XE3");
  165. Assert.AreEqual (SyndicationElementExtensionKind.XmlElement, xxml.Extension.ExtensionKind, "#XE4");
  166. Assert.AreEqual (null, xxml.Extension.ObjectSerializer, "#XE5");
  167. // Create fake IO using stringbuilders
  168. StringBuilder element_string = new StringBuilder ();
  169. StringBuilder syndication_string = new StringBuilder ();
  170. XmlWriterSettings settings = new XmlWriterSettings ();
  171. XmlWriter element = XmlWriter.Create (element_string, settings);
  172. XmlWriter syndication = XmlWriter.Create (syndication_string, settings);
  173. // Make sure we get the input data back out
  174. xelem.WriteTo (element);
  175. xxml.WriteTo (syndication, documentname, "");
  176. element.Close ();
  177. syndication.Close ();
  178. // Pickout the 'channel' and 'stuff' tags from original and syndicated document
  179. XmlDocument syndoc = new XmlDocument();
  180. XmlDocument eledoc = new XmlDocument();
  181. syndoc.LoadXml (syndication_string.ToString ());
  182. XmlNodeList synresult = syndoc.GetElementsByTagName (tagname);
  183. XmlNodeList syntype = syndoc.GetElementsByTagName (documentname);
  184. eledoc.LoadXml (element_string.ToString ());
  185. XmlNodeList eleresult = eledoc.GetElementsByTagName (tagname);
  186. // Check document type
  187. Assert.AreEqual(type, syntype.Item (0).Attributes.GetNamedItem ("type").Value.ToString (),
  188. "XE6");
  189. // Check content
  190. Assert.AreEqual (eleresult.Item (0).OuterXml.ToString (), synresult.Item (0).OuterXml.ToString (),
  191. "XE7");
  192. }
  193. static Int32 author = 32765;
  194. static string comment = "No comment.";
  195. [Test]
  196. public void XmlSerializerExtension ()
  197. {
  198. DateTime date = new DateTime (2007, 5, 22);
  199. global::System.Xml.Serialization.XmlSerializer xs =
  200. new global::System.Xml.Serialization.XmlSerializer (typeof (Content));
  201. Content item = new Content ();
  202. string item_object = "Content"; // tag name for serialized object
  203. // fill object with some data
  204. item.author = author;
  205. item.comment = comment;
  206. item.date = date;
  207. XmlSyndicationContent se = new XmlSyndicationContent (type,item,xs);
  208. Assert.AreEqual (typeof (XmlSyndicationContent), se.GetType (), "#SE1");
  209. Assert.AreEqual (type, se.Type.ToString (), "#SE2");
  210. Assert.AreSame (item, se.Extension.Object, "#SE3");
  211. Assert.AreEqual (SyndicationElementExtensionKind.XmlSerializer, se.Extension.ExtensionKind, "#SE4");
  212. Assert.AreSame (xs, se.Extension.ObjectSerializer, "#SE5");
  213. // Create fake IO using stringbuilders
  214. StringBuilder object_string = new StringBuilder ();
  215. StringBuilder syndication_string = new StringBuilder ();
  216. XmlWriterSettings settings = new XmlWriterSettings ();
  217. XmlWriter serobj = XmlWriter.Create (object_string, settings);
  218. XmlWriter syndication = XmlWriter.Create (syndication_string, settings);
  219. xs.Serialize (serobj, item);
  220. se.WriteTo (syndication, documentname, "");
  221. serobj.Close ();
  222. syndication.Close ();
  223. // Pickout the 'Content' tag from original serialized object and syndicated document
  224. XmlDocument syndoc = new XmlDocument ();
  225. XmlDocument serdoc = new XmlDocument ();
  226. syndoc.LoadXml (syndication_string.ToString ());
  227. XmlNodeList synresult = syndoc.GetElementsByTagName (item_object);
  228. XmlNodeList syntype = syndoc.GetElementsByTagName (documentname);
  229. serdoc.LoadXml (object_string.ToString ());
  230. XmlNodeList serresult = serdoc.GetElementsByTagName (item_object);
  231. // Check document type
  232. Assert.AreEqual(type, syntype.Item (0).Attributes.GetNamedItem ("type").Value.ToString (),
  233. "SE6");
  234. // Check content
  235. Assert.AreEqual (serresult.Item (0).OuterXml.ToString (), synresult.Item (0).OuterXml.ToString (),
  236. "SE6");
  237. }
  238. [Test]
  239. // ToDo
  240. public void DataContractExtension ()
  241. {
  242. }
  243. }
  244. public class Content {
  245. public Int32 author;
  246. public string comment;
  247. public DateTime date;
  248. }
  249. }