2
0

SyndicationLinkTest.cs 6.8 KB

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