XPathAtomicValueTests.cs 13 KB

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