Rss20ItemFormatterTest.cs 12 KB

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