2 // MonoTests.System.Xml.XsdValidatingReaderTests.cs
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C)2003 Atsushi Enomoto
11 using System
.Xml
.Schema
;
12 using NUnit
.Framework
;
14 namespace MonoTests
.System
.Xml
17 public class XsdValidatingReaderTests
: Assertion
19 public XsdValidatingReaderTests ()
24 XmlValidatingReader xvr
;
26 private XmlValidatingReader
PrepareXmlReader (string xml
)
28 XmlReader reader
= new XmlTextReader (xml
, XmlNodeType
.Document
, null);
29 // XmlDocument doc = new XmlDocument ();
31 // XmlReader reader = new XmlNodeReader (doc);
33 return new XmlValidatingReader (reader
);
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.
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 ();
54 schema
.Items
.Add (elem
);
55 xvr
.Schemas
.Add (schema
);
57 AssertEquals (ValidationType
.Auto
, xvr
.ValidationType
);
61 xvr
= PrepareXmlReader (xml
);
62 xvr
.Schemas
.Add (schema
);
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
);
74 Fail ("Element in different namespace is incorrectly allowed.");
75 } catch (XmlSchemaException
) {
80 public void TestReadTypedValueSimple ()
82 string xml
= "<root>12</root>";
83 XmlSchema schema
= new XmlSchema ();
84 XmlSchemaElement elem
= new XmlSchemaElement ();
86 elem
.SchemaTypeName
= new XmlQualifiedName ("integer", XmlSchema
.Namespace
);
87 schema
.Items
.Add (elem
);
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
);
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
);
105 AssertEquals (typeof (decimal), o
.GetType ());
106 decimal n
= (decimal) o
;
107 AssertEquals (12, n
);
109 AssertEquals ("root", xvr
.Name
);
110 AssertNull (xvr
.SchemaType
); // EndElement's type
114 xvr
= PrepareXmlReader (xml
);
115 xvr
.Schemas
.Add (schema
);
117 XmlSchemaDatatype dt
= xvr
.SchemaType
as XmlSchemaDatatype
;
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().
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 ();
143 schema
.Items
.Add (elem
);
144 xvr
.Schemas
.Add (schema
);
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
);
155 public void TestReadTypedAttributeValue ()
157 string xml
= "<root attr='12'></root>";
158 XmlSchema schema
= new XmlSchema ();
159 XmlSchemaElement elem
= new XmlSchemaElement ();
161 XmlSchemaComplexType ct
= new XmlSchemaComplexType ();
162 XmlSchemaAttribute attr
= new XmlSchemaAttribute ();
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
);
172 AssertEquals ("root", xvr
.Name
);
173 Assert (xvr
.MoveToNextAttribute ()); // attr
174 AssertEquals ("attr", xvr
.Name
);
175 XmlSchemaDatatype dt
= xvr
.SchemaType
as XmlSchemaDatatype
;
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 ());
183 AssertEquals (12, n
);
184 Assert (xvr
.ReadAttributeValue ()); // can read = seems not proceed.