2
0

SyndicationElementExtensionTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. XmlWriter GetWriter (StringWriter sw)
  107. {
  108. XmlWriterSettings s = new XmlWriterSettings ();
  109. s.OmitXmlDeclaration = true;
  110. return XmlWriter.Create (sw, s);
  111. }
  112. [Test]
  113. [ExpectedException (typeof (ArgumentNullException))]
  114. public void WriteTo_NullWriter ()
  115. {
  116. new SyndicationElementExtension (5).WriteTo (null);
  117. }
  118. [Test]
  119. [Category("NotWorking")]
  120. public void WriteTo_Reader ()
  121. {
  122. string xml = "<root><child /><child2 /></root>";
  123. StringWriter sw = new StringWriter ();
  124. using (XmlWriter w = GetWriter (sw)) {
  125. XmlReader r = XmlReader.Create (new StringReader (xml));
  126. r.Read ();
  127. r.Read (); // at child
  128. new SyndicationElementExtension (r).WriteTo (w);
  129. }
  130. Assert.AreEqual ("<child></child>", sw.ToString ());
  131. }
  132. [Test]
  133. [Category("NotWorking")]
  134. public void WriteToTwice_Reader ()
  135. {
  136. string xml = "<root><child /><child2 /></root>";
  137. StringWriter sw = new StringWriter ();
  138. using (XmlWriter w = GetWriter (sw)) {
  139. XmlReader r = XmlReader.Create (new StringReader (xml));
  140. r.Read ();
  141. r.Read (); // at child
  142. SyndicationElementExtension x = new SyndicationElementExtension (r);
  143. w.WriteStartElement ("root");
  144. x.WriteTo (w);
  145. x.WriteTo (w); // it is VALID.
  146. w.WriteEndElement ();
  147. }
  148. Assert.AreEqual ("<root><child></child><child></child></root>", sw.ToString ());
  149. }
  150. [Test]
  151. public void WriteTo_ExtensionObject ()
  152. {
  153. StringWriter sw = new StringWriter ();
  154. using (XmlWriter w = GetWriter (sw))
  155. new SyndicationElementExtension (5).WriteTo (w);
  156. Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int>", sw.ToString ());
  157. }
  158. [Test]
  159. public void GetObject_DataContract ()
  160. {
  161. new SyndicationElementExtension (5).GetObject<int> ();
  162. new SyndicationElementExtension (5).GetObject<double> ();
  163. new SyndicationElementExtension ("5").GetObject<int> ();
  164. }
  165. [Test]
  166. [ExpectedException (typeof (ArgumentNullException))]
  167. public void GetObject_DataContract_XmlObjectSerializerNull ()
  168. {
  169. new SyndicationElementExtension (5).GetObject<int> ((XmlObjectSerializer) null);
  170. }
  171. [Test]
  172. [ExpectedException (typeof (ArgumentNullException))]
  173. public void GetObject_DataContract_XmlSerializerNull ()
  174. {
  175. new SyndicationElementExtension (5).GetObject<int> ((XmlSerializer) null);
  176. }
  177. [Test]
  178. public void GetObject_XmlReader ()
  179. {
  180. string xml = "<root>3</root>";
  181. XmlReader r = XmlReader.Create (new StringReader (xml));
  182. SyndicationElementExtension x = new SyndicationElementExtension (r);
  183. Assert.AreEqual (3, x.GetObject<int> (), "#1");
  184. Assert.AreEqual (3, x.GetObject<int> (), "#2"); // it is VALID
  185. }
  186. [Test]
  187. [ExpectedException (typeof (SerializationException))]
  188. [Category("NotWorking")]
  189. public void GetObject_DataContractError ()
  190. {
  191. new SyndicationElementExtension (DBNull.Value).GetObject<int> (); // Nullable<int> as well
  192. }
  193. }
  194. }