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