1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
//
// MonoTests.System.Xml.XsdValidatingReaderTests.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// (C)2003 Atsushi Enomoto
// (C)2005-2006 Novell, Inc.
//
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XsdValidatingReaderTests : Assertion
{
public XsdValidatingReaderTests ()
{
}
XmlReader xtr;
XmlValidatingReader xvr;
private XmlValidatingReader PrepareXmlReader (string xml)
{
XmlReader reader = new XmlTextReader (xml, XmlNodeType.Document, null);
// XmlDocument doc = new XmlDocument ();
// doc.LoadXml (xml);
// XmlReader reader = new XmlNodeReader (doc);
return new XmlValidatingReader (reader);
}
[Test]
public void TestEmptySchema ()
{
string xml = "<root/>";
xvr = PrepareXmlReader (xml);
xvr.ValidationType = ValidationType.Schema;
xvr.Read (); // Is is missing schema component.
}
[Test]
public void TestSimpleValidation ()
{
string xml = "<root/>";
xvr = PrepareXmlReader (xml);
AssertEquals (ValidationType.Auto, xvr.ValidationType);
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
schema.Items.Add (elem);
xvr.Schemas.Add (schema);
xvr.Read (); // root
AssertEquals (ValidationType.Auto, xvr.ValidationType);
xvr.Read (); // EOF
xml = "<hoge/>";
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
try {
xvr.Read ();
Fail ("element mismatch is incorrectly allowed");
} catch (XmlSchemaException) {
}
xml = "<hoge xmlns='urn:foo' />";
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
try {
xvr.Read ();
Fail ("Element in different namespace is incorrectly allowed.");
} catch (XmlSchemaException) {
}
}
[Test]
public void TestReadTypedValueSimple ()
{
string xml = "<root>12</root>";
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
schema.Items.Add (elem);
// Lap 1:
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
// Read directly from root.
object o = xvr.ReadTypedValue ();
AssertEquals (ReadState.Initial, xvr.ReadState);
AssertNull (o);
xvr.Read (); // element root
AssertEquals (XmlNodeType.Element, xvr.NodeType);
AssertNotNull (xvr.SchemaType);
Assert (xvr.SchemaType is XmlSchemaDatatype);
o = xvr.ReadTypedValue (); // read "12"
AssertEquals (XmlNodeType.EndElement, xvr.NodeType);
AssertNotNull (o);
AssertEquals (typeof (decimal), o.GetType ());
decimal n = (decimal) o;
AssertEquals (12, n);
Assert (!xvr.EOF);
AssertEquals ("root", xvr.Name);
AssertNull (xvr.SchemaType); // EndElement's type
// Lap 2:
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
xvr.Read (); // root
XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
AssertNotNull (dt);
AssertEquals (typeof (decimal), dt.ValueType);
AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
xvr.Read (); // text "12"
AssertNull (xvr.SchemaType);
o = xvr.ReadTypedValue ();
// ReadTypedValue is different from ReadString().
AssertNull (o);
}
[Test]
[Ignore ("XML Schema validator should not be available for validating non namespace-aware XmlReader that handled colon as a name character")]
public void TestNamespacesFalse ()
{
// This tests if Namespaces=false is specified, then
// the reader's NamespaceURI should be always string.Empty and
// validation should be done against such schema that has target ns as "".
string xml = "<x:root xmlns:x='urn:foo' />";
xvr = PrepareXmlReader (xml);
xvr.Namespaces = false;
AssertEquals (ValidationType.Auto, xvr.ValidationType);
XmlSchema schema = new XmlSchema ();
schema.TargetNamespace = "urn:foo";
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
schema.Items.Add (elem);
xvr.Schemas.Add (schema);
xvr.Read (); // root
Assert (!xvr.Namespaces);
AssertEquals ("x:root", xvr.Name);
// LocalName may contain colons.
AssertEquals ("x:root", xvr.LocalName);
// NamespaceURI is not supplied.
AssertEquals ("", xvr.NamespaceURI);
}
[Test]
public void TestReadTypedAttributeValue ()
{
string xml = "<root attr='12'></root>";
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
XmlSchemaComplexType ct = new XmlSchemaComplexType ();
XmlSchemaAttribute attr = new XmlSchemaAttribute ();
attr.Name = "attr";
attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
ct.Attributes.Add (attr);
elem.SchemaType = ct;
schema.Items.Add (elem);
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
xvr.Read ();
AssertEquals ("root", xvr.Name);
Assert (xvr.MoveToNextAttribute ()); // attr
AssertEquals ("attr", xvr.Name);
XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
AssertNotNull (dt);
AssertEquals (typeof (int), dt.ValueType);
AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
object o = xvr.ReadTypedValue ();
AssertEquals (XmlNodeType.Attribute, xvr.NodeType);
AssertEquals (typeof (int), o.GetType ());
int n = (int) o;
AssertEquals (12, n);
Assert (xvr.ReadAttributeValue ()); // can read = seems not proceed.
}
[Test]
public void DuplicateSchemaAssignment ()
{
string xml = @"<data
xmlns='http://www.test.com/schemas/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://www.test.com/schemas/ /home/user/schema.xsd' />";
string xsd = @"<xs:schema
targetNamespace='http://www.test.com/schemas/'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns='http://www.test.com/schemas/' >
<xs:element name='data' /></xs:schema>";
string xmlns = "http://www.test.com/schemas/";
XmlValidatingReader xvr = new XmlValidatingReader (
xml, XmlNodeType.Document, null);
XmlSchemaCollection schemas = new XmlSchemaCollection ();
schemas.Add (XmlSchema.Read (new XmlTextReader (
xsd, XmlNodeType.Document, null), null));
xvr.Schemas.Add (schemas);
while (!xvr.EOF)
xvr.Read ();
}
[Test] // bug #76234
public void DTDValidatorNamespaceHandling ()
{
string xml = "<xml xmlns='urn:a'> <foo> <a:bar xmlns='urn:b' xmlns:a='urn:a' /> <bug /> </foo> </xml>";
XmlValidatingReader vr = new XmlValidatingReader (
xml, XmlNodeType.Document, null);
vr.Read ();
vr.Read (); // whitespace
AssertEquals ("#1", String.Empty, vr.NamespaceURI);
vr.Read (); // foo
AssertEquals ("#2", "urn:a", vr.NamespaceURI);
vr.Read (); // whitespace
vr.Read (); // a:bar
AssertEquals ("#3", "urn:a", vr.NamespaceURI);
vr.Read (); // whitespace
vr.Read (); // bug
AssertEquals ("#4", "urn:a", vr.NamespaceURI);
}
[Test]
public void MultipleSchemaInSchemaLocation ()
{
XmlTextReader xtr = new XmlTextReader ("Test/XmlFiles/xsd/multi-schemaLocation.xml");
XmlValidatingReader vr = new XmlValidatingReader (xtr);
while (!vr.EOF)
vr.Read ();
}
[Test]
public void ReadTypedValueSimpleTypeRestriction ()
{
string xml = "<root>xx</root><!-- after -->";
string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='root'>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:minLength value='2' />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>";
XmlTextReader xir =
new XmlTextReader (xml, XmlNodeType.Document, null);
XmlTextReader xsr =
new XmlTextReader (xsd, XmlNodeType.Document, null);
XmlValidatingReader vr = new XmlValidatingReader (xir);
vr.Schemas.Add (XmlSchema.Read (xsr, null));
vr.Read (); // root
AssertEquals ("xx", vr.ReadTypedValue ());
AssertEquals (XmlNodeType.EndElement, vr.NodeType);
}
// If we normalize string before validating with facets,
// this test will fail. It will also fail if ReadTypedValue()
// ignores whitespace nodes.
[Test]
public void ReadTypedValueWhitespaces ()
{
string xml = "<root> </root><!-- after -->";
string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='root'>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:minLength value='2' />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>";
XmlTextReader xir =
new XmlTextReader (xml, XmlNodeType.Document, null);
XmlTextReader xsr =
new XmlTextReader (xsd, XmlNodeType.Document, null);
XmlValidatingReader vr = new XmlValidatingReader (xir);
vr.Schemas.Add (XmlSchema.Read (xsr, null));
vr.Read (); // root
AssertEquals (" ", vr.ReadTypedValue ());
AssertEquals (XmlNodeType.EndElement, vr.NodeType);
}
[Test] // bug #77241
public void EmptyContentAllowWhitespace ()
{
string doc = @"
<root>
<!-- some comment -->
<child/>
</root>
";
string schema = @"
<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<xsd:element name=""root"">
<xsd:complexType>
<xsd:sequence>
<xsd:element name=""child"" type=""xsd:string"" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
";
XmlValidatingReader reader = new XmlValidatingReader (
new XmlTextReader (new StringReader (doc)));
reader.Schemas.Add (null,
new XmlTextReader (new StringReader (schema)));
while (reader.Read ())
;
}
[Test] // bug #79650
#if NET_2_0
// annoyance
[ExpectedException (typeof (XmlSchemaValidationException))]
#else
[ExpectedException (typeof (XmlSchemaException))]
#endif
public void EnumerationFacetOnAttribute ()
{
string xml = "<test mode='NOT A ENUMERATION VALUE' />";
XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/79650.xsd"), null);
XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
xvr.ValidationType = ValidationType.Schema;
xvr.Schemas.Add (schema);
while (!xvr.EOF)
xvr.Read ();
}
class XmlErrorResolver : XmlResolver
{
public override ICredentials Credentials {
set { }
}
public override object GetEntity (Uri uri, string role, Type type)
{
throw new Exception ();
}
}
[Test] // bug #79924
public void ValidationTypeNoneIgnoreSchemaLocations ()
{
string xml = "<project xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='nosuchschema.xsd'/>";
XmlValidatingReader vr = new XmlValidatingReader (
new XmlTextReader (new StringReader (xml)));
vr.ValidationType = ValidationType.None;
vr.XmlResolver = new XmlErrorResolver ();
while (!vr.EOF)
vr.Read ();
}
}
}
|