SyndicationElementExtensionTest.cs 6.5 KB

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