Atom10ItemFormatterTest.cs 14 KB

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