XPathAtomicValueTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. //
  2. // MonoTests.System.Xml.XPathAtomicValueTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2004 Novell Inc,
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Xml;
  32. using System.Xml.Schema;
  33. using System.Xml.XPath;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Xml
  36. {
  37. [TestFixture]
  38. public class XPathAtomicValueTests
  39. {
  40. internal const string XdtNamespace = "http://www.w3.org/2003/11/xpath-datatypes";
  41. static XmlTypeCode [] AllTypeCode =
  42. new ArrayList (Enum.GetValues (typeof (XmlTypeCode))).ToArray (typeof (XmlTypeCode)) as XmlTypeCode [];
  43. static XmlQualifiedName [] allTypeNames;
  44. static XmlSchemaType [] allTypes;
  45. static string [] xstypes = new string [] {
  46. "anyType",
  47. "anySimpleType",
  48. "string",
  49. "boolean",
  50. "decimal",
  51. "float",
  52. "double",
  53. "duration",
  54. "dateTime",
  55. "time",
  56. "date",
  57. "gYearMonth",
  58. "gYear",
  59. "gMonthDay",
  60. "gDay",
  61. "gMonth",
  62. "hexBinary",
  63. "base64Binary",
  64. "anyUri",
  65. "QName",
  66. "NOTATION",
  67. "normalizedString",
  68. "token",
  69. "language",
  70. "NMTOKEN",
  71. "NMTOKENS",
  72. "Name",
  73. "NCName",
  74. "ID",
  75. "IDREF",
  76. "IDREFS",
  77. "ENTITY",
  78. "ENTITIES",
  79. "integer",
  80. "nonPositiveInteger",
  81. "negativeInteger",
  82. "long",
  83. "int",
  84. "short",
  85. "byte",
  86. "nonNegativeInteger",
  87. "unsignedLong",
  88. "unsignedInt",
  89. "unsignedShort",
  90. "unsignedByte",
  91. "positiveInteger"
  92. };
  93. static string [] xdttypes = {
  94. "anyAtomicType",
  95. "untypedAtomic",
  96. "yearMonthDuration",
  97. "dayTimeDuration"
  98. };
  99. private static XmlQualifiedName [] AllTypeNames {
  100. get {
  101. if (allTypeNames == null) {
  102. ArrayList al = new ArrayList ();
  103. foreach (string name in xstypes)
  104. AddXsType (name, XmlSchema.Namespace, al);
  105. foreach (string name in xdttypes)
  106. AddXsType (name, XdtNamespace, al);
  107. allTypeNames = al.ToArray (typeof (XmlQualifiedName)) as XmlQualifiedName [];
  108. }
  109. return allTypeNames;
  110. }
  111. }
  112. private static void AddXsType (string name, string ns, ArrayList al)
  113. {
  114. al.Add (new XmlQualifiedName (name, ns));
  115. }
  116. private static XmlSchemaType [] AllTypes {
  117. get {
  118. if (allTypes == null) {
  119. ArrayList al = new ArrayList ();
  120. foreach (XmlQualifiedName name in AllTypeNames) {
  121. XmlSchemaType t = XmlSchemaType.GetBuiltInSimpleType (name);
  122. if (t == null)
  123. t = XmlSchemaType.GetBuiltInComplexType (name);
  124. al.Add (t);
  125. }
  126. allTypes = al.ToArray (typeof (XmlSchemaType)) as XmlSchemaType [];
  127. }
  128. return allTypes;
  129. }
  130. }
  131. public void AssertAtomicValue (XPathAtomicValue av,
  132. bool isNode,
  133. Type valueType,
  134. XmlSchemaType xmlType,
  135. object typedValue,
  136. Type typedValueType,
  137. string value,
  138. object boolValue,
  139. object dateValue,
  140. object decimalValue,
  141. object doubleValue,
  142. object int32Value,
  143. object int64Value,
  144. object singleValue,
  145. int listCount)
  146. {
  147. Assert.AreEqual (isNode, av.IsNode, "IsNode");
  148. Assert.AreEqual (valueType, av.ValueType, "ValueType");
  149. Assert.AreEqual (xmlType, av.XmlType, "XmlType");
  150. Assert.AreEqual (typedValue, av.TypedValue, "TypedValue");
  151. Assert.AreEqual (typedValueType, typedValue.GetType (), "typedValue.GetType()");
  152. if (value != null)
  153. Assert.AreEqual (value, av.Value, "Value");
  154. else {
  155. try {
  156. value = av.Value;
  157. Assert.Fail ("not supported conversion to String.");
  158. } catch (InvalidCastException) {
  159. }
  160. }
  161. // FIXME: Failure cases could not be tested;
  162. // any kind of Exceptions are thrown as yet.
  163. if (boolValue != null)
  164. Assert.AreEqual (boolValue, av.ValueAsBoolean, "ValueAsBoolean");
  165. /*
  166. else {
  167. try {
  168. boolValue = av.ValueAsBoolean;
  169. Assert.Fail ("not supported conversion to Boolean.");
  170. } catch (InvalidCastException) {
  171. }
  172. }
  173. */
  174. if (dateValue != null)
  175. Assert.AreEqual (dateValue, av.ValueAsDateTime, "ValueAsDateTime");
  176. /*
  177. else {
  178. try {
  179. dateValue = av.ValueAsDateTime;
  180. Assert.Fail ("not supported conversion to DateTime.");
  181. } catch (InvalidCastException) {
  182. }
  183. }
  184. */
  185. if (decimalValue != null)
  186. Assert.AreEqual (decimalValue, av.ValueAsDecimal, "ValueAsDecimal");
  187. /*
  188. else {
  189. try {
  190. decimalValue = av.ValueAsDecimal;
  191. Assert.Fail ("not supported conversion to Decimal.");
  192. } catch (InvalidCastException) {
  193. }
  194. }
  195. */
  196. if (doubleValue != null)
  197. Assert.AreEqual (doubleValue, av.ValueAsDouble, "ValueAsDouble");
  198. /*
  199. else {
  200. try {
  201. doubleValue = av.ValueAsDouble;
  202. Assert.Fail ("not supported conversion to Double.");
  203. } catch (InvalidCastException) {
  204. }
  205. }
  206. */
  207. if (int32Value != null)
  208. Assert.AreEqual (int32Value, av.ValueAsInt32, "ValueAsInt32");
  209. /*
  210. else {
  211. try {
  212. int32Value = av.ValueAsInt32;
  213. Assert.Fail ("not supported conversion to Int32.");
  214. } catch (InvalidCastException) {
  215. }
  216. }
  217. */
  218. if (int64Value != null)
  219. Assert.AreEqual (int64Value, av.ValueAsInt64, "ValueAsInt64");
  220. /*
  221. else {
  222. try {
  223. int64Value = av.ValueAsInt64;
  224. Assert.Fail ("not supported conversion to Int64.");
  225. } catch (InvalidCastException) {
  226. }
  227. }
  228. */
  229. if (singleValue != null)
  230. Assert.AreEqual (singleValue, av.ValueAsSingle, "ValueAsSingle");
  231. /*
  232. else {
  233. try {
  234. singleValue = av.ValueAsSingle;
  235. Assert.Fail ("not supported conversion to Single.");
  236. } catch (InvalidCastException) {
  237. }
  238. }
  239. */
  240. Assert.AreEqual (listCount, av.ValueAsList.Count, "ValueAsList.Count");
  241. }
  242. [Test]
  243. public void BooleanType ()
  244. {
  245. XmlSchemaType xstype = XmlSchemaType.GetBuiltInSimpleType (XmlTypeCode.Boolean);
  246. XPathAtomicValue av;
  247. // true
  248. av = new XPathAtomicValue (true, xstype);
  249. AssertAtomicValue (av,
  250. false,
  251. typeof (bool), // ValueType
  252. xstype, // XmlType
  253. true, // TypedValue
  254. typeof (bool), // actual Type of TypedValue
  255. "true", // string
  256. true, // bool
  257. null, // DateTime
  258. (decimal) 1, // decimal
  259. 1.0, // double
  260. 1, // int32
  261. 1, // int64
  262. (float) 1.0, // single
  263. 1); // array count
  264. // false
  265. av = new XPathAtomicValue (false, xstype);
  266. AssertAtomicValue (av,
  267. false,
  268. typeof (bool), // ValueType
  269. xstype, // XmlType
  270. false, // TypedValue
  271. typeof (bool), // actual Type of TypedValue
  272. "false", // string
  273. false, // bool
  274. null, // DateTime
  275. (decimal) 0, // decimal
  276. 0.0, // double
  277. 0, // int32
  278. 0, // int64
  279. (float) 0.0, // single
  280. 1); // array count
  281. // 0
  282. av = new XPathAtomicValue (false, xstype);
  283. AssertAtomicValue (av,
  284. false,
  285. typeof (bool), // ValueType
  286. xstype, // XmlType
  287. false, // TypedValue
  288. typeof (bool), // actual Type of TypedValue
  289. "false", // string
  290. false, // bool
  291. null, // DateTime
  292. (decimal) 0, // decimal
  293. 0.0, // double
  294. 0, // int32
  295. 0, // int64
  296. (float) 0.0, // single
  297. 1); // array count
  298. // 5
  299. av = new XPathAtomicValue (5, xstype);
  300. AssertAtomicValue (av,
  301. false,
  302. typeof (bool), // ValueType
  303. xstype, // XmlType
  304. true, // TypedValue
  305. typeof (bool), // actual Type of TypedValue
  306. "true", // string
  307. true, // bool
  308. null, // DateTime
  309. (decimal) 5, // decimal
  310. 5.0, // double
  311. 5, // int32
  312. 5, // int64
  313. (float) 5.0, // single
  314. 1); // array count
  315. // short
  316. short shortValue = 3;
  317. av = new XPathAtomicValue (shortValue, xstype);
  318. AssertAtomicValue (av,
  319. false,
  320. typeof (bool), // ValueType
  321. xstype, // XmlType
  322. true, // TypedValue
  323. typeof (bool), // actual Type of TypedValue
  324. "true", // string
  325. true, // bool
  326. null, // DateTime
  327. (decimal) 3, // decimal
  328. 3.0, // double
  329. 3, // int32
  330. 3, // int64
  331. (float) 3.0, // single
  332. 1); // array count
  333. // "1"
  334. av = new XPathAtomicValue ("1", xstype);
  335. AssertAtomicValue (av,
  336. false,
  337. typeof (bool), // ValueType
  338. xstype, // XmlType
  339. true, // TypedValue
  340. typeof (bool), // actual Type of TypedValue
  341. "true", // string
  342. true, // bool
  343. null, // DateTime
  344. (decimal) 1, // decimal
  345. 1.0, // double
  346. 1, // int32
  347. 1, // int64
  348. (float) 1.0, // single
  349. 1); // array count
  350. // new bool [] {true}
  351. av = new XPathAtomicValue (new bool [] {true}, xstype);
  352. AssertAtomicValue (av,
  353. false,
  354. typeof (bool), // ValueType
  355. xstype, // XmlType
  356. true, // TypedValue
  357. typeof (bool), // actual Type of TypedValue
  358. "true", // string
  359. true, // bool
  360. null, // DateTime
  361. (decimal) 1, // decimal
  362. 1.0, // double
  363. 1, // int32
  364. 1, // int64
  365. (float) 1.0, // single
  366. 1); // array count
  367. // new ArrayList (new int [] {6})
  368. av = new XPathAtomicValue (new ArrayList (new int [] {6}), xstype);
  369. AssertAtomicValue (av,
  370. false,
  371. typeof (bool), // ValueType
  372. xstype, // XmlType
  373. true, // TypedValue
  374. typeof (bool), // actual Type of TypedValue
  375. "true", // string
  376. true, // bool
  377. null, // DateTime
  378. (decimal) 6, // decimal
  379. 6.0, // double
  380. 6, // int32
  381. 6, // int64
  382. (float) 6.0, // single
  383. 1); // array count
  384. // Hashtable, [7] = 7
  385. Hashtable ht = new Hashtable ();
  386. ht [7] = 7;
  387. av = new XPathAtomicValue (ht, xstype);
  388. AssertAtomicValue (av,
  389. false,
  390. typeof (bool), // ValueType
  391. xstype, // XmlType
  392. true, // TypedValue
  393. typeof (bool), // actual Type of TypedValue
  394. "true", // string
  395. true, // bool
  396. null, // DateTime
  397. (decimal) 7, // decimal
  398. 7.0, // double
  399. 7, // int32
  400. 7, // int64
  401. (float) 7.0, // single
  402. 1); // array count
  403. // - MS.NET will fail here due to its bug -
  404. // another XPathAtomicValue that is bool
  405. av = new XPathAtomicValue (true, xstype);
  406. av = new XPathAtomicValue (av, xstype);
  407. AssertAtomicValue (av,
  408. false,
  409. typeof (bool), // ValueType
  410. xstype, // XmlType
  411. true, // TypedValue
  412. typeof (bool), // actual Type of TypedValue
  413. "true", // string
  414. true, // bool
  415. null, // DateTime
  416. (decimal) 1, // decimal
  417. 1.0, // double
  418. 1, // int32
  419. 1, // int64
  420. (float) 1.0, // single
  421. 1); // array count
  422. // Array, [0] = XPathAtomicValue
  423. av = new XPathAtomicValue (new XPathAtomicValue [] {av}, xstype);
  424. AssertAtomicValue (av,
  425. false,
  426. typeof (bool), // ValueType
  427. xstype, // XmlType
  428. true, // TypedValue
  429. typeof (bool), // actual Type of TypedValue
  430. "true", // string
  431. true, // bool
  432. null, // DateTime
  433. (decimal) 1, // decimal
  434. 1.0, // double
  435. 1, // int32
  436. 1, // int64
  437. (float) 1.0, // single
  438. 1); // array count
  439. // new bool [] {true, false}
  440. av = new XPathAtomicValue (new bool [] {true, false}, xstype);
  441. try {
  442. object o = av.ValueAsBoolean;
  443. Assert.Fail ("ArrayList must contain just one item to be castable to bool");
  444. } catch (InvalidCastException) {
  445. }
  446. // empty ArrayList
  447. av = new XPathAtomicValue (new ArrayList (), xstype);
  448. try {
  449. object o = av.ValueAsBoolean;
  450. Assert.Fail ("ArrayList must contain just one item to be castable to bool");
  451. } catch (InvalidCastException) {
  452. }
  453. // "True"
  454. av = new XPathAtomicValue ("True", xstype);
  455. try {
  456. object o = av.ValueAsBoolean;
  457. Assert.Fail ("\"True\" is not a boolean representation (\"true\" is).");
  458. } catch (InvalidCastException) {
  459. }
  460. // DateTime
  461. av = new XPathAtomicValue (DateTime.Now, xstype);
  462. try {
  463. object o = av.ValueAsBoolean;
  464. Assert.Fail ("DateTime should not be castable to bool.");
  465. } catch (InvalidCastException) {
  466. }
  467. // XmlText node that contains boolean representation value
  468. XmlDocument doc = new XmlDocument ();
  469. doc.LoadXml ("<root>true</root>");
  470. XmlNode node = doc.DocumentElement.FirstChild;
  471. av = new XPathAtomicValue (node, xstype);
  472. try {
  473. object o = av.ValueAsBoolean;
  474. Assert.Fail ("XmlText cannot be castable to bool.");
  475. } catch (InvalidCastException) {
  476. }
  477. // XPathNavigator whose node points to text node whose
  478. // value represents boolean string.
  479. av = new XPathAtomicValue (node.CreateNavigator (),
  480. xstype);
  481. try {
  482. object o = av.ValueAsBoolean;
  483. Assert.Fail ("XmlText cannot be castable to bool.");
  484. } catch (InvalidCastException) {
  485. }
  486. }
  487. }
  488. }