**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / Test / System.Xml / XsdValidatingReaderTests.cs
blobc989eb62a410f8191724c21eb57b27b2782cc9a3
1 //
2 // MonoTests.System.Xml.XsdValidatingReaderTests.cs
3 //
4 // Author:
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // (C)2003 Atsushi Enomoto
8 //
9 using System;
10 using System.Xml;
11 using System.Xml.Schema;
12 using NUnit.Framework;
14 namespace MonoTests.System.Xml
16 [TestFixture]
17 public class XsdValidatingReaderTests : Assertion
19 public XsdValidatingReaderTests ()
23 XmlReader xtr;
24 XmlValidatingReader xvr;
26 private XmlValidatingReader PrepareXmlReader (string xml)
28 XmlReader reader = new XmlTextReader (xml, XmlNodeType.Document, null);
29 // XmlDocument doc = new XmlDocument ();
30 // doc.LoadXml (xml);
31 // XmlReader reader = new XmlNodeReader (doc);
33 return new XmlValidatingReader (reader);
36 [Test]
37 public void TestEmptySchema ()
39 string xml = "<root/>";
40 xvr = PrepareXmlReader (xml);
41 xvr.ValidationType = ValidationType.Schema;
42 xvr.Read (); // Is is missing schema component.
45 [Test]
46 public void TestSimpleValidation ()
48 string xml = "<root/>";
49 xvr = PrepareXmlReader (xml);
50 AssertEquals (ValidationType.Auto, xvr.ValidationType);
51 XmlSchema schema = new XmlSchema ();
52 XmlSchemaElement elem = new XmlSchemaElement ();
53 elem.Name = "root";
54 schema.Items.Add (elem);
55 xvr.Schemas.Add (schema);
56 xvr.Read (); // root
57 AssertEquals (ValidationType.Auto, xvr.ValidationType);
58 xvr.Read (); // EOF
60 xml = "<hoge/>";
61 xvr = PrepareXmlReader (xml);
62 xvr.Schemas.Add (schema);
63 try {
64 xvr.Read ();
65 Fail ("element mismatch is incorrectly allowed");
66 } catch (XmlSchemaException) {
69 xml = "<hoge xmlns='urn:foo' />";
70 xvr = PrepareXmlReader (xml);
71 xvr.Schemas.Add (schema);
72 try {
73 xvr.Read ();
74 Fail ("Element in different namespace is incorrectly allowed.");
75 } catch (XmlSchemaException) {
79 [Test]
80 public void TestReadTypedValueSimple ()
82 string xml = "<root>12</root>";
83 XmlSchema schema = new XmlSchema ();
84 XmlSchemaElement elem = new XmlSchemaElement ();
85 elem.Name = "root";
86 elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
87 schema.Items.Add (elem);
89 // Lap 1:
91 xvr = PrepareXmlReader (xml);
92 xvr.Schemas.Add (schema);
93 // Read directly from root.
94 object o = xvr.ReadTypedValue ();
95 AssertEquals (ReadState.Initial, xvr.ReadState);
96 AssertNull (o);
98 xvr.Read (); // element root
99 AssertEquals (XmlNodeType.Element, xvr.NodeType);
100 AssertNotNull (xvr.SchemaType);
101 Assert (xvr.SchemaType is XmlSchemaDatatype);
102 o = xvr.ReadTypedValue (); // read "12"
103 AssertEquals (XmlNodeType.EndElement, xvr.NodeType);
104 AssertNotNull (o);
105 AssertEquals (typeof (decimal), o.GetType ());
106 decimal n = (decimal) o;
107 AssertEquals (12, n);
108 Assert (!xvr.EOF);
109 AssertEquals ("root", xvr.Name);
110 AssertNull (xvr.SchemaType); // EndElement's type
112 // Lap 2:
114 xvr = PrepareXmlReader (xml);
115 xvr.Schemas.Add (schema);
116 xvr.Read (); // root
117 XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
118 AssertNotNull (dt);
119 AssertEquals (typeof (decimal), dt.ValueType);
120 AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
121 xvr.Read (); // text "12"
122 AssertNull (xvr.SchemaType);
123 o = xvr.ReadTypedValue ();
124 // ReadTypedValue is different from ReadString().
125 AssertNull (o);
128 [Test]
129 [Ignore ("XML Schema validator should not be available for validating non namespace-aware XmlReader that handled colon as a name character")]
130 public void TestNamespacesFalse ()
132 // This tests if Namespaces=false is specified, then
133 // the reader's NamespaceURI should be always string.Empty and
134 // validation should be done against such schema that has target ns as "".
135 string xml = "<x:root xmlns:x='urn:foo' />";
136 xvr = PrepareXmlReader (xml);
137 xvr.Namespaces = false;
138 AssertEquals (ValidationType.Auto, xvr.ValidationType);
139 XmlSchema schema = new XmlSchema ();
140 schema.TargetNamespace = "urn:foo";
141 XmlSchemaElement elem = new XmlSchemaElement ();
142 elem.Name = "root";
143 schema.Items.Add (elem);
144 xvr.Schemas.Add (schema);
145 xvr.Read (); // root
146 Assert (!xvr.Namespaces);
147 AssertEquals ("x:root", xvr.Name);
148 // LocalName may contain colons.
149 AssertEquals ("x:root", xvr.LocalName);
150 // NamespaceURI is not supplied.
151 AssertEquals ("", xvr.NamespaceURI);
154 [Test]
155 public void TestReadTypedAttributeValue ()
157 string xml = "<root attr='12'></root>";
158 XmlSchema schema = new XmlSchema ();
159 XmlSchemaElement elem = new XmlSchemaElement ();
160 elem.Name = "root";
161 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
162 XmlSchemaAttribute attr = new XmlSchemaAttribute ();
163 attr.Name = "attr";
164 attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
165 ct.Attributes.Add (attr);
166 elem.SchemaType = ct;
167 schema.Items.Add (elem);
169 xvr = PrepareXmlReader (xml);
170 xvr.Schemas.Add (schema);
171 xvr.Read ();
172 AssertEquals ("root", xvr.Name);
173 Assert (xvr.MoveToNextAttribute ()); // attr
174 AssertEquals ("attr", xvr.Name);
175 XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
176 AssertNotNull (dt);
177 AssertEquals (typeof (int), dt.ValueType);
178 AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
179 object o = xvr.ReadTypedValue ();
180 AssertEquals (XmlNodeType.Attribute, xvr.NodeType);
181 AssertEquals (typeof (int), o.GetType ());
182 int n = (int) o;
183 AssertEquals (12, n);
184 Assert (xvr.ReadAttributeValue ()); // can read = seems not proceed.