XmlDictionaryReaderAbstractTest.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // XmlDictionaryReaderAbstractTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.IO;
  30. using System.Xml;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Xml
  33. {
  34. [TestFixture]
  35. public class XmlDictionaryReaderAbstractTest
  36. {
  37. XmlDictionaryReader GetReader (string xml)
  38. {
  39. XmlReader xr = XmlReader.Create (
  40. new StringReader (xml));
  41. return new SimpleExtReader (xr);
  42. }
  43. [Test]
  44. public void IndexOfLocalName ()
  45. {
  46. XmlDictionaryReader r = GetReader ("<root/>");
  47. r.Read ();
  48. Assert.AreEqual (-1, r.IndexOfLocalName (new string []{}, ""), "#1");
  49. Assert.AreEqual (-1, r.IndexOfLocalName (new string []{"foo"}, "foo"), "#2");
  50. Assert.AreEqual (-1, r.IndexOfLocalName (new string []{"root"}, "foo"), "#3");
  51. // matches
  52. Assert.AreEqual (0, r.IndexOfLocalName (new string []{"root"}, ""), "#4");
  53. }
  54. [Test]
  55. public void IsLocalName ()
  56. {
  57. XmlDictionaryReader r = GetReader ("<root/>");
  58. r.Read ();
  59. Assert.IsTrue (r.IsLocalName ("root"), "#1");
  60. Assert.IsFalse (r.IsLocalName ("foo"), "#2");
  61. XmlDictionaryString ds;
  62. XmlDictionary dict = new XmlDictionary ();
  63. dict.Add ("root");
  64. dict.TryLookup ("root", out ds);
  65. Assert.IsTrue (r.IsLocalName (ds), "#3");
  66. dict.Add ("foo");
  67. dict.TryLookup ("foo", out ds);
  68. Assert.IsFalse (r.IsLocalName (ds), "#4");
  69. }
  70. [Test]
  71. public void TryGetNamesAsDictionaryString ()
  72. {
  73. XmlDictionaryReader r = GetReader ("<root/>");
  74. r.Read ();
  75. XmlDictionaryString ds;
  76. Assert.IsFalse (
  77. r.TryGetLocalNameAsDictionaryString (out ds),
  78. "#1");
  79. Assert.IsFalse (
  80. r.TryGetNamespaceUriAsDictionaryString (out ds),
  81. "#2");
  82. }
  83. [Test]
  84. public void ReadBooleanArray ()
  85. {
  86. XmlDictionaryReader r = GetReader ("<root><item>true</item><item> false </item><item> true</item></root>");
  87. r.Read ();
  88. r.Read ();
  89. bool [] array = r.ReadBooleanArray ("item", "");
  90. Assert.AreEqual (3, array.Length, "#1");
  91. Assert.AreEqual (true, array [0], "#2");
  92. Assert.AreEqual (false, array [1], "#3");
  93. Assert.AreEqual (true, array [2], "#4");
  94. }
  95. [Test]
  96. public void ReadArrayBoolean ()
  97. {
  98. XmlDictionaryReader r = GetReader ("<root><item>true</item><item> false </item><item> true</item></root>");
  99. r.Read ();
  100. r.Read ();
  101. bool [] array = new bool [5];
  102. r.ReadArray ("item", "", array, 1, 3);
  103. Assert.AreEqual (true, array [1], "#1");
  104. Assert.AreEqual (false, array [2], "#2");
  105. Assert.AreEqual (true, array [3], "#3");
  106. }
  107. [Test]
  108. public void ReadInt32Array ()
  109. {
  110. XmlDictionaryReader r = GetReader ("<root><item>1</item><item> 100 </item><item>-50</item></root>");
  111. Assert.IsNotNull (r.Quotas, "premise");
  112. r.Read ();
  113. r.Read ();
  114. int [] array = r.ReadInt32Array ("item", "");
  115. Assert.AreEqual (3, array.Length, "#1");
  116. Assert.AreEqual (1, array [0], "#2");
  117. Assert.AreEqual (100, array [1], "#3");
  118. Assert.AreEqual (-50, array [2], "#4");
  119. }
  120. [Test]
  121. public void ReadArrayInt32 ()
  122. {
  123. XmlDictionaryReader r = GetReader ("<root><item>1</item><item> 100 </item><item>-50</item></root>");
  124. Assert.IsNotNull (r.Quotas, "premise");
  125. r.Read ();
  126. r.Read ();
  127. int [] array = new int [5];
  128. r.ReadArray ("item", "", array, 1, 3);
  129. Assert.AreEqual (1, array [1], "#1");
  130. Assert.AreEqual (100, array [2], "#2");
  131. Assert.AreEqual (-50, array [3], "#3");
  132. }
  133. }
  134. public class SimpleExtReader : XmlDictionaryReader
  135. {
  136. XmlReader reader;
  137. IXmlLineInfo lineInfo;
  138. public SimpleExtReader (XmlReader reader)
  139. {
  140. this.reader = reader;
  141. this.lineInfo = reader as IXmlLineInfo;
  142. }
  143. #region Properties
  144. // This is the only one non-overriden property.
  145. public XmlReader Reader {
  146. get { return reader; }
  147. }
  148. public int LineNumber {
  149. get { return lineInfo != null ? lineInfo.LineNumber : 0; }
  150. }
  151. public int LinePosition {
  152. get { return lineInfo != null ? lineInfo.LinePosition : 0; }
  153. }
  154. public override XmlNodeType NodeType
  155. {
  156. get { return reader.NodeType; }
  157. }
  158. public override string Name {
  159. get { return reader.Name; }
  160. }
  161. public override string LocalName {
  162. get { return reader.LocalName; }
  163. }
  164. public override string NamespaceURI {
  165. get { return reader.NamespaceURI; }
  166. }
  167. public override string Prefix {
  168. get { return reader.Prefix; }
  169. }
  170. public override bool HasValue {
  171. get { return reader.HasValue; }
  172. }
  173. public override int Depth {
  174. get { return reader.Depth; }
  175. }
  176. public override string Value {
  177. get { return reader.Value; }
  178. }
  179. public override string BaseURI {
  180. get { return reader.BaseURI; }
  181. }
  182. public override bool IsEmptyElement {
  183. get { return reader.IsEmptyElement; }
  184. }
  185. public override bool IsDefault {
  186. get { return reader.IsDefault; }
  187. }
  188. public override char QuoteChar {
  189. get { return reader.QuoteChar; }
  190. }
  191. public override string XmlLang {
  192. get { return reader.XmlLang; }
  193. }
  194. public override XmlSpace XmlSpace {
  195. get { return reader.XmlSpace; }
  196. }
  197. public override int AttributeCount {
  198. get { return reader.AttributeCount; }
  199. }
  200. public override string this [int i] {
  201. get { return reader [i]; }
  202. }
  203. public override string this [string name] {
  204. get { return reader [name]; }
  205. }
  206. public override string this [string localName, string namespaceURI] {
  207. get { return reader [localName, namespaceURI]; }
  208. }
  209. public override bool EOF {
  210. get { return reader.EOF; }
  211. }
  212. public override ReadState ReadState {
  213. get { return reader.ReadState; }
  214. }
  215. public override XmlNameTable NameTable {
  216. get { return reader.NameTable; }
  217. }
  218. #endregion
  219. #region Methods
  220. public override string GetAttribute (string name)
  221. {
  222. return reader.GetAttribute (name);
  223. }
  224. public override string GetAttribute (string localName, string namespaceURI)
  225. {
  226. return reader.GetAttribute (localName, namespaceURI);
  227. }
  228. public override string GetAttribute (int i)
  229. {
  230. return reader.GetAttribute (i);
  231. }
  232. public bool HasLineInfo ()
  233. {
  234. return lineInfo != null ? lineInfo.HasLineInfo () : false;
  235. }
  236. public override bool MoveToAttribute (string name)
  237. {
  238. return reader.MoveToAttribute (name);
  239. }
  240. public override bool MoveToAttribute (string localName, string namespaceURI)
  241. {
  242. return reader.MoveToAttribute (localName, namespaceURI);
  243. }
  244. public override void MoveToAttribute (int i)
  245. {
  246. reader.MoveToAttribute (i);
  247. }
  248. public override bool MoveToFirstAttribute ()
  249. {
  250. return reader.MoveToFirstAttribute ();
  251. }
  252. public override bool MoveToNextAttribute ()
  253. {
  254. return reader.MoveToNextAttribute ();
  255. }
  256. public override bool MoveToElement ()
  257. {
  258. return reader.MoveToElement ();
  259. }
  260. public override void Close ()
  261. {
  262. reader.Close ();
  263. }
  264. public override bool Read ()
  265. {
  266. return Reader.Read ();
  267. }
  268. public override string ReadString ()
  269. {
  270. return reader.ReadString ();
  271. }
  272. public override string ReadInnerXml ()
  273. {
  274. return reader.ReadInnerXml ();
  275. }
  276. public override string ReadOuterXml ()
  277. {
  278. return reader.ReadOuterXml ();
  279. }
  280. public override string LookupNamespace (string prefix)
  281. {
  282. return reader.LookupNamespace (prefix);
  283. }
  284. public override void ResolveEntity ()
  285. {
  286. reader.ResolveEntity ();
  287. }
  288. public override bool ReadAttributeValue () {
  289. return reader.ReadAttributeValue ();
  290. }
  291. #endregion
  292. }
  293. }