SyndicationElementExtensionTest.cs 6.8 KB

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