2
0

Atom10ItemFormatterTest.cs 14 KB

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