SyndicationLinkTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // SyndicationLinkTest.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.ServiceModel.Syndication;
  37. using NUnit.Framework;
  38. using QName = System.Xml.XmlQualifiedName;
  39. namespace MonoTests.System.ServiceModel.Syndication
  40. {
  41. [TestFixture]
  42. public class SyndicationLinkTest
  43. {
  44. [Test]
  45. public void Constructor ()
  46. {
  47. // null Uri is allowed
  48. SyndicationLink link = new SyndicationLink (null);
  49. link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
  50. Assert.AreEqual ("empty.xml", link.Uri.ToString (), "#1-1");
  51. Assert.AreEqual (null, link.BaseUri, "#1-2");
  52. Assert.AreEqual (0, link.Length, "#1-3");
  53. Assert.AreEqual (null, link.MediaType, "#1-4");
  54. link = new SyndicationLink (null, null, null, null, 0);
  55. }
  56. [Test]
  57. public void TestUri ()
  58. {
  59. SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
  60. link.Uri = null;
  61. Assert.IsNull (link.Uri, "#1");
  62. Assert.IsNull (link.GetAbsoluteUri (), "#2");
  63. }
  64. [Test]
  65. public void TestBaseUri ()
  66. {
  67. // relative
  68. SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
  69. Assert.IsNull (link.BaseUri, "#1");
  70. // absolute
  71. link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  72. Assert.IsNull (link.BaseUri, "#2");
  73. // absolute #2
  74. link = new SyndicationLink ();
  75. link.Uri = new Uri ("http://mono-project.com/index.rss");
  76. Assert.IsNull (link.BaseUri, "#3");
  77. }
  78. [Test]
  79. [ExpectedException (typeof (ArgumentException))]
  80. [Category ("NotDotNet")] // LAMESPEC. See below.
  81. public void SetRelativeUriAsBaseUri ()
  82. {
  83. SyndicationLink link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
  84. // LAMESPEC: setting relative Uri as BaseUri is allowed (likely broken)
  85. link.BaseUri = new Uri ("base.xml", UriKind.Relative);
  86. // and below causes ArgumentOutOfRangeException.
  87. // Assert.IsNull (link.GetAbsoluteUri (), "#1");
  88. }
  89. [Test]
  90. public void MediaType ()
  91. {
  92. SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  93. link.MediaType = "text/xml";
  94. Assert.AreEqual ("text/xml", link.MediaType, "#1");
  95. link.MediaType = null;
  96. Assert.IsNull (link.MediaType, "#2");
  97. link.MediaType = "WTF"; // no error
  98. }
  99. [Test]
  100. public void RelationshipType ()
  101. {
  102. SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  103. link.RelationshipType = "alternate";
  104. Assert.AreEqual ("alternate", link.RelationshipType, "#1");
  105. link.RelationshipType = null;
  106. Assert.IsNull (link.RelationshipType, "#2");
  107. link.RelationshipType = "WTF"; // no error
  108. }
  109. [Test]
  110. public void Length ()
  111. {
  112. SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  113. link.Length = 0;
  114. link.Length = long.MaxValue;
  115. }
  116. [Test]
  117. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  118. public void NegativeLength ()
  119. {
  120. SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  121. link.Length = -1;
  122. }
  123. [Test]
  124. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  125. public void NegativeLength2 ()
  126. {
  127. new SyndicationLink (null, null, null, null, -1);
  128. }
  129. [Test]
  130. public void AttributeElementExtensions ()
  131. {
  132. // The properties do not affect extension attributes.
  133. SyndicationLink link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  134. Assert.AreEqual (0, link.ElementExtensions.Count, "#0");
  135. Assert.IsFalse (link.AttributeExtensions.ContainsKey (new QName ("mediaType")), "#3");
  136. link.MediaType = "text/xml";
  137. }
  138. [Test]
  139. public void GetAbsoluteUri ()
  140. {
  141. // no Uri
  142. SyndicationLink link = new SyndicationLink ();
  143. Assert.IsNull (link.GetAbsoluteUri (), "#1");
  144. // Uri is relative
  145. link = new SyndicationLink (new Uri ("empty.xml", UriKind.Relative));
  146. Assert.IsNull (link.GetAbsoluteUri (), "#2");
  147. // Uri is absolute
  148. link = new SyndicationLink (new Uri ("http://mono-project.com/index.rss"));
  149. Assert.AreEqual ("http://mono-project.com/index.rss", link.GetAbsoluteUri ().ToString (), "#3");
  150. // only BaseUri - null result
  151. link = new SyndicationLink ();
  152. link.BaseUri = new Uri ("http://mono-project.com/index.rss");
  153. Assert.IsNull (link.GetAbsoluteUri (), "#4");
  154. }
  155. [Test]
  156. public void Clone ()
  157. {
  158. SyndicationLink link = new SyndicationLink (null, null, "my RSS", "text/xml", 1);
  159. link.BaseUri = new Uri ("http://mono-project.com/index.rss");
  160. SyndicationLink clone = link.Clone ();
  161. Assert.AreEqual (link.BaseUri, clone.BaseUri, "#1");
  162. Assert.AreEqual ("my RSS", clone.Title, "#2");
  163. Assert.AreEqual ("text/xml", clone.MediaType, "#3");
  164. Assert.IsNull (clone.RelationshipType, "#4");
  165. Assert.IsNull (clone.Uri, "#5");
  166. }
  167. [Test]
  168. public void CreateAlternateLink ()
  169. {
  170. SyndicationLink link = SyndicationLink.CreateAlternateLink (null);
  171. Assert.IsNull (link.Uri, "#1");
  172. Assert.IsNull (link.MediaType, "#2");
  173. Assert.AreEqual ("alternate", link.RelationshipType, "#3");
  174. }
  175. [Test]
  176. public void CreateMediaEnclosureLink ()
  177. {
  178. SyndicationLink link = SyndicationLink.CreateMediaEnclosureLink (null, null, 1);
  179. Assert.IsNull (link.Uri, "#1");
  180. Assert.IsNull (link.MediaType, "#2");
  181. Assert.AreEqual (1, link.Length, "#3");
  182. Assert.AreEqual ("enclosure", link.RelationshipType, "#4");
  183. }
  184. [Test]
  185. public void CreateSelfLink ()
  186. {
  187. SyndicationLink link = SyndicationLink.CreateSelfLink (null);
  188. Assert.IsNull (link.Uri, "#1");
  189. Assert.IsNull (link.MediaType, "#2");
  190. Assert.AreEqual ("self", link.RelationshipType, "#3");
  191. }
  192. }
  193. }
  194. #endif