SyndicationElementExtensionTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // SyndicationElementExtensionTest.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. namespace MonoTests.System.ServiceModel.Syndication
  39. {
  40. [TestFixture]
  41. public class SyndicationElementExtensionTest
  42. {
  43. [Test]
  44. [ExpectedException (typeof (ArgumentNullException))]
  45. public void ConstructorExtensionNull ()
  46. {
  47. new SyndicationElementExtension ((object) null);
  48. }
  49. [Test]
  50. public void ConstructorXmlObjectSerializerNull ()
  51. {
  52. // null XmlObjectSerializer is allowed.
  53. var x = new SyndicationElementExtension (5, (XmlObjectSerializer) null);
  54. Assert.AreEqual ("int", x.OuterName, "#1");
  55. Assert.AreEqual ("http://schemas.microsoft.com/2003/10/Serialization/", x.OuterNamespace, "#2");
  56. }
  57. [Test]
  58. public void ConstructorOuterNameNull ()
  59. {
  60. // null name strings are allowed.
  61. var x = new SyndicationElementExtension (null, null, 5, null);
  62. Assert.AreEqual ("int", x.OuterName, "#1");
  63. Assert.AreEqual ("http://schemas.microsoft.com/2003/10/Serialization/", x.OuterNamespace, "#2");
  64. }
  65. [Test]
  66. public void ConstructorXmlSerializerNull ()
  67. {
  68. // null XmlSerializer is allowed.
  69. new SyndicationElementExtension (5, (XmlSerializer) null);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (InvalidOperationException))]
  73. public void ConstructorXmlSerializerNonSerializable ()
  74. {
  75. new SyndicationElementExtension (new NonXmlSerializable (null), (XmlSerializer) null);
  76. }
  77. public class NonXmlSerializable
  78. {
  79. public NonXmlSerializable (string dummy)
  80. {
  81. }
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentNullException))]
  85. public void ConstructorXmlReaderNull ()
  86. {
  87. new SyndicationElementExtension ((XmlReader) null);
  88. }
  89. [Test]
  90. [ExpectedException (typeof (XmlException))]
  91. public void ConstructorXmlReaderUnexpectedEndElement ()
  92. {
  93. string xml = "<root></root>";
  94. XmlReader r = XmlReader.Create (new StringReader (xml));
  95. r.Read ();
  96. r.Read (); // at end element
  97. new SyndicationElementExtension (r);
  98. }
  99. [Test]
  100. [ExpectedException (typeof (XmlException))]
  101. public void ConstructorXmlReaderUnexpectedText ()
  102. {
  103. string xml = "<root>2</root>";
  104. XmlReader r = XmlReader.Create (new StringReader (xml));
  105. r.Read ();
  106. r.Read (); // at text
  107. new SyndicationElementExtension (r);
  108. }
  109. [Test]
  110. public void ConstructorXmlReader ()
  111. {
  112. string xml = "<root xmlns='x'>2</root>";
  113. XmlReader r = XmlReader.Create (new StringReader (xml));
  114. var e = new SyndicationElementExtension (r);
  115. Assert.AreEqual ("root", e.OuterName, "#1");
  116. Assert.AreEqual ("x", e.OuterNamespace, "#2");
  117. }
  118. XmlWriter GetWriter (StringWriter sw)
  119. {
  120. XmlWriterSettings s = new XmlWriterSettings ();
  121. s.OmitXmlDeclaration = true;
  122. return XmlWriter.Create (sw, s);
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentNullException))]
  126. public void WriteTo_NullWriter ()
  127. {
  128. new SyndicationElementExtension (5).WriteTo (null);
  129. }
  130. [Test]
  131. [Category("NotWorking")]
  132. public void WriteTo_Reader ()
  133. {
  134. string xml = "<root><child /><child2 /></root>";
  135. StringWriter sw = new StringWriter ();
  136. using (XmlWriter w = GetWriter (sw)) {
  137. XmlReader r = XmlReader.Create (new StringReader (xml));
  138. r.Read ();
  139. r.Read (); // at child
  140. new SyndicationElementExtension (r).WriteTo (w);
  141. }
  142. Assert.AreEqual ("<child></child>", sw.ToString ());
  143. }
  144. [Test]
  145. [Category("NotWorking")]
  146. public void WriteToTwice_Reader ()
  147. {
  148. string xml = "<root><child /><child2 /></root>";
  149. StringWriter sw = new StringWriter ();
  150. using (XmlWriter w = GetWriter (sw)) {
  151. XmlReader r = XmlReader.Create (new StringReader (xml));
  152. r.Read ();
  153. r.Read (); // at child
  154. SyndicationElementExtension x = new SyndicationElementExtension (r);
  155. w.WriteStartElement ("root");
  156. x.WriteTo (w);
  157. x.WriteTo (w); // it is VALID.
  158. w.WriteEndElement ();
  159. }
  160. Assert.AreEqual ("<root><child></child><child></child></root>", sw.ToString ());
  161. }
  162. [Test]
  163. public void WriteTo_ExtensionObject ()
  164. {
  165. StringWriter sw = new StringWriter ();
  166. using (XmlWriter w = GetWriter (sw))
  167. new SyndicationElementExtension (5).WriteTo (w);
  168. Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int>", sw.ToString ());
  169. }
  170. [Test]
  171. public void GetObject_DataContract ()
  172. {
  173. new SyndicationElementExtension (5).GetObject<int> ();
  174. new SyndicationElementExtension (5).GetObject<double> ();
  175. new SyndicationElementExtension ("5").GetObject<int> ();
  176. }
  177. [Test]
  178. [ExpectedException (typeof (ArgumentNullException))]
  179. public void GetObject_DataContract_XmlObjectSerializerNull ()
  180. {
  181. new SyndicationElementExtension (5).GetObject<int> ((XmlObjectSerializer) null);
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentNullException))]
  185. public void GetObject_DataContract_XmlSerializerNull ()
  186. {
  187. new SyndicationElementExtension (5).GetObject<int> ((XmlSerializer) null);
  188. }
  189. [Test]
  190. public void GetObject_XmlReader ()
  191. {
  192. string xml = "<root>3</root>";
  193. XmlReader r = XmlReader.Create (new StringReader (xml));
  194. SyndicationElementExtension x = new SyndicationElementExtension (r);
  195. Assert.AreEqual (3, x.GetObject<int> (), "#1");
  196. Assert.AreEqual (3, x.GetObject<int> (), "#2"); // it is VALID
  197. }
  198. [Test]
  199. [ExpectedException (typeof (SerializationException))]
  200. [Category("NotWorking")]
  201. public void GetObject_DataContractError ()
  202. {
  203. new SyndicationElementExtension (DBNull.Value).GetObject<int> (); // Nullable<int> as well
  204. }
  205. }
  206. }