Rss20ItemFormatterTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // Rss20ItemFormatterTest.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. #if !MOBILE
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.IO;
  33. using System.Runtime.Serialization;
  34. using System.Text;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.ServiceModel.Syndication;
  38. using NUnit.Framework;
  39. using QName = System.Xml.XmlQualifiedName;
  40. namespace MonoTests.System.ServiceModel.Syndication
  41. {
  42. [TestFixture]
  43. public class Rss20ItemFormatterTest
  44. {
  45. [Test]
  46. [ExpectedException (typeof (ArgumentNullException))]
  47. public void ConstructorNullItem ()
  48. {
  49. new Rss20ItemFormatter ((SyndicationItem) null);
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentNullException))]
  53. public void ConstructorNullType ()
  54. {
  55. new Rss20ItemFormatter ((Type) null);
  56. }
  57. /*
  58. [Test]
  59. public void ItemType ()
  60. {
  61. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  62. Assert.IsNull (f.ItemType, "#1");
  63. f = new Rss20ItemFormatter (new SyndicationItem ());
  64. Assert.IsNull (f.ItemType, "#2");
  65. }
  66. */
  67. [Test]
  68. public void Version ()
  69. {
  70. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  71. Assert.AreEqual ("Rss20", f.Version, "#1");
  72. }
  73. [Test]
  74. public void SerializeExtensionsAsAtom ()
  75. {
  76. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  77. Assert.IsTrue (f.SerializeExtensionsAsAtom, "#1");
  78. }
  79. [Test]
  80. [ExpectedException (typeof (InvalidOperationException))]
  81. public void DefaultConstructorThenWriteXml ()
  82. {
  83. StringWriter sw = new StringWriter ();
  84. using (XmlWriter w = CreateWriter (sw))
  85. new Rss20ItemFormatter ().WriteTo (w);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public void WriteToNull ()
  90. {
  91. SyndicationItem item = new SyndicationItem ();
  92. new Rss20ItemFormatter (item).WriteTo (null);
  93. }
  94. [Test]
  95. public void WriteTo_EmptyItem ()
  96. {
  97. SyndicationItem item = new SyndicationItem ();
  98. StringWriter sw = new StringWriter ();
  99. using (XmlWriter w = CreateWriter (sw))
  100. new Rss20ItemFormatter (item).WriteTo (w);
  101. // either title or description must exist (RSS 2.0 spec)
  102. Assert.AreEqual ("<item><description /></item>", sw.ToString ());
  103. }
  104. [Test]
  105. public void WriteTo_TitleOnlyItem ()
  106. {
  107. SyndicationItem item = new SyndicationItem ();
  108. item.Title = new TextSyndicationContent ("title text");
  109. StringWriter sw = new StringWriter ();
  110. using (XmlWriter w = CreateWriter (sw))
  111. new Rss20ItemFormatter (item).WriteTo (w);
  112. Assert.AreEqual ("<item><title>title text</title></item>", sw.ToString ());
  113. }
  114. [Test]
  115. public void WriteTo_CategoryAuthorsContributors ()
  116. {
  117. SyndicationItem item = new SyndicationItem ();
  118. item.Categories.Add (new SyndicationCategory ("myname", "myscheme", "mylabel"));
  119. item.Authors.Add (new SyndicationPerson ("[email protected]", "John Doe", "http://john.doe.name"));
  120. item.Contributors.Add (new SyndicationPerson ("[email protected]", "Jane Doe", "http://jane.doe.name"));
  121. StringWriter sw = new StringWriter ();
  122. using (XmlWriter w = CreateWriter (sw))
  123. new Rss20ItemFormatter (item).WriteTo (w);
  124. // contributors are serialized as Atom extension
  125. Assert.AreEqual ("<item><author>[email protected]</author><category domain=\"myscheme\">myname</category><description /><contributor xmlns=\"http://www.w3.org/2005/Atom\"><name>Jane Doe</name><uri>http://jane.doe.name</uri><email>[email protected]</email></contributor></item>", sw.ToString ());
  126. }
  127. [Test]
  128. [Category("NotWorking")]
  129. public void WriteTo ()
  130. {
  131. SyndicationItem item = new SyndicationItem ();
  132. item.BaseUri = new Uri ("http://mono-project.com");
  133. item.Copyright = new TextSyndicationContent ("No rights reserved");
  134. item.Content = new XmlSyndicationContent (null, 5, (XmlObjectSerializer) null);
  135. item.Id = "urn:myid";
  136. item.PublishDate = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc));
  137. item.LastUpdatedTime = new DateTimeOffset (DateTime.SpecifyKind (new DateTime (2008, 1, 1), DateTimeKind.Utc));
  138. item.SourceFeed = new SyndicationFeed ();
  139. item.SourceFeed.Title = new TextSyndicationContent ("source title");
  140. StringWriter sw = new StringWriter ();
  141. using (XmlWriter w = CreateWriter (sw))
  142. new Rss20ItemFormatter (item).WriteTo (w);
  143. Assert.AreEqual ("<item xml:base=\"http://mono-project.com/\"><guid isPermaLink=\"false\">urn:myid</guid><description /><source>source title</source><pubDate>Sat, 01 Jan 2000 00:00:00 Z</pubDate><updated xmlns=\"http://www.w3.org/2005/Atom\">2008-01-01T00:00:00Z</updated><rights type=\"text\" xmlns=\"http://www.w3.org/2005/Atom\">No rights reserved</rights><content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int></content></item>", sw.ToString ());
  144. }
  145. [Test]
  146. public void SerializeExtensionsAsAtomFalse ()
  147. {
  148. SyndicationItem item = new SyndicationItem ();
  149. item.Contributors.Add (new SyndicationPerson ("[email protected]", "Jane Doe", "http://jane.doe.name"));
  150. StringWriter sw = new StringWriter ();
  151. using (XmlWriter w = CreateWriter (sw))
  152. new Rss20ItemFormatter (item, false).WriteTo (w);
  153. // skip contributors
  154. Assert.AreEqual ("<item><description /></item>", sw.ToString ());
  155. }
  156. [Test]
  157. public void ISerializableWriteXml ()
  158. {
  159. SyndicationItem item = new SyndicationItem ();
  160. item.Title = new TextSyndicationContent ("title text");
  161. StringWriter sw = new StringWriter ();
  162. using (XmlWriter w = CreateWriter (sw))
  163. ((IXmlSerializable) new Rss20ItemFormatter (item)).WriteXml (w);
  164. Assert.AreEqual ("<title>title text</title>", sw.ToString ());
  165. }
  166. XmlWriter CreateWriter (StringWriter sw)
  167. {
  168. XmlWriterSettings s = new XmlWriterSettings ();
  169. s.OmitXmlDeclaration = true;
  170. return XmlWriter.Create (sw, s);
  171. }
  172. XmlReader CreateReader (string xml)
  173. {
  174. return XmlReader.Create (new StringReader (xml));
  175. }
  176. [Test]
  177. public void CanRead ()
  178. {
  179. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  180. Assert.IsFalse (f.CanRead (CreateReader ("<rss>")), "#1");
  181. Assert.IsTrue (f.CanRead (CreateReader ("<item>")), "#2");
  182. Assert.IsFalse (f.CanRead (CreateReader ("<item xmlns='urn:foo'>")), "#3");
  183. Assert.IsFalse (f.CanRead (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#4");
  184. Assert.IsFalse (f.CanRead (CreateReader ("<hoge>")), "#5");
  185. XmlReader r = CreateReader ("<item></item>");
  186. r.Read (); // element
  187. r.Read (); // endelement
  188. Assert.IsFalse (f.CanRead (r), "#6");
  189. r = CreateReader ("<item><title>test</title></item>");
  190. r.Read (); // item
  191. r.Read (); // title
  192. Assert.IsFalse (f.CanRead (r), "#7");
  193. }
  194. [Test]
  195. [ExpectedException (typeof (XmlException))]
  196. public void ReadFromInvalid ()
  197. {
  198. new Rss20ItemFormatter ().ReadFrom (CreateReader ("<rss>"));
  199. }
  200. [Test]
  201. public void ReadFrom1 ()
  202. {
  203. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  204. Assert.IsNull (f.Item, "#1");
  205. f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
  206. SyndicationItem item1 = f.Item;
  207. Assert.IsNotNull (f.Item.Title, "#2");
  208. Assert.AreEqual ("test", f.Item.Title.Text, "#3");
  209. f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
  210. Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4");
  211. }
  212. [Test]
  213. public void ReadFrom2 () {
  214. SyndicationItem item = new SyndicationItem ();
  215. Rss20ItemFormatter f = new Rss20ItemFormatter (item);
  216. Assert.IsTrue (object.ReferenceEquals (item, f.Item), "Item #1");
  217. f.ReadFrom (CreateReader ("<item><title>test</title></item>"));
  218. Assert.IsFalse (object.ReferenceEquals(item, f.Item), "Item #2");
  219. }
  220. [Test]
  221. public void ReadXml_TitleOnly ()
  222. {
  223. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  224. ((IXmlSerializable) f).ReadXml (CreateReader ("<item><title>test</title></item>"));
  225. Assert.IsNotNull (f.Item.Title, "#1");
  226. Assert.AreEqual ("test", f.Item.Title.Text, "#2");
  227. ((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok
  228. }
  229. [Test]
  230. [ExpectedException (typeof (XmlException))]
  231. public void ReadXmlFromContent ()
  232. {
  233. ((IXmlSerializable) new Rss20ItemFormatter ()).ReadXml (CreateReader ("<title>test</title>"));
  234. }
  235. [Test]
  236. public void ReadXml_Extension ()
  237. {
  238. new Rss20ItemFormatter<MySyndicationItem1> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
  239. new Rss20ItemFormatter<MySyndicationItem2> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
  240. try {
  241. new Rss20ItemFormatter<MySyndicationItem3> ().ReadFrom (CreateReader ("<item><foo>test</foo></item>"));
  242. Assert.Fail ("should trigger TryParseElement");
  243. } catch (ApplicationException) {
  244. }
  245. }
  246. class MySyndicationItem1 : SyndicationItem
  247. {
  248. protected override bool TryParseElement (XmlReader reader, string version)
  249. {
  250. Assert.AreEqual ("Rss20", version, "#1");
  251. Assert.IsFalse (base.TryParseElement (reader, version), "#2");
  252. return false;
  253. }
  254. }
  255. class MySyndicationItem2 : SyndicationItem
  256. {
  257. protected override bool TryParseElement (XmlReader reader, string version)
  258. {
  259. reader.Skip (); // without it, the caller expects that the reader did not proceed.
  260. return true;
  261. }
  262. }
  263. class MySyndicationItem3 : SyndicationItem
  264. {
  265. protected override bool TryParseElement (XmlReader reader, string version)
  266. {
  267. throw new ApplicationException ();
  268. }
  269. }
  270. [Test]
  271. [ExpectedException (typeof (XmlException))]
  272. [Category("NotWorking")]
  273. public void ReadFrom_WrongDate1 ()
  274. {
  275. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  276. f.ReadFrom (CreateReader ("<item><pubDate /></item>"));
  277. }
  278. [Test]
  279. [ExpectedException (typeof (XmlException))]
  280. [Category("NotWorking")]
  281. public void ReadFrom_WrongDate2 ()
  282. {
  283. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  284. f.ReadFrom (CreateReader ("<item><pubDate>2000-01-01T00:00:00</pubDate></item>"));
  285. }
  286. [Test]
  287. public void ReadFrom_Comments ()
  288. {
  289. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  290. f.ReadFrom (CreateReader ("<item><comments>comment</comments></item>"));
  291. Assert.IsNotNull (f.Item, "#1");
  292. // 'comments' is treated as extensions ...
  293. Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2");
  294. }
  295. [Test]
  296. public void ReadFrom_Enclosure ()
  297. {
  298. Rss20ItemFormatter f = new Rss20ItemFormatter ();
  299. // .NET bug: it allows extension attributes, but rejects extension elements.
  300. // f.ReadFrom (CreateReader ("<item><enclosure url='urn:foo' length='50' type='text/html' wcf='wtf'><extended /></enclosure></item>"));
  301. f.ReadFrom (CreateReader ("<item><enclosure url='urn:foo' length='50' type='text/html' wcf='wtf'></enclosure></item>"));
  302. // 'enclosure' is treated as SyndicationLink
  303. Assert.AreEqual (1, f.Item.Links.Count, "#1");
  304. SyndicationLink link = f.Item.Links [0];
  305. Assert.AreEqual (50, link.Length, "#2");
  306. Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3");
  307. Assert.AreEqual ("text/html", link.MediaType, "#4");
  308. Assert.AreEqual ("enclosure", link.RelationshipType, "#5");
  309. Assert.AreEqual (1, link.AttributeExtensions.Count, "#6");
  310. //Assert.AreEqual (1, link.ElementExtensions.Count, "#7");
  311. }
  312. [Test]
  313. public void GetSchema ()
  314. {
  315. Assert.IsNull (((IXmlSerializable) new Rss20ItemFormatter ()).GetSchema ());
  316. }
  317. [Test]
  318. public void ReadFromGuidPermaLink ()
  319. {
  320. const string xml1 = "<item><guid isPermaLink=\"false\">urn:myid</guid><description /></item>";
  321. using (XmlReader r = CreateReader (xml1)) {
  322. var rss = new Rss20ItemFormatter ();
  323. rss.ReadFrom (r);
  324. Assert.AreEqual ("urn:myid", rss.Item.Id);
  325. }
  326. const string xml2 = "<item><guid isPermaLink=\"true\">urn:myid</guid><description /></item>";
  327. using (XmlReader r = CreateReader (xml2)) {
  328. var rss = new Rss20ItemFormatter ();
  329. rss.ReadFrom (r);
  330. Assert.AreEqual ("urn:myid", rss.Item.Id);
  331. }
  332. }
  333. }
  334. }
  335. #endif