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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
//
// System.Xml.XmlSchemaTests.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// (C) 2002 Atsushi Enomoto
//
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlSchemaTests : XmlSchemaAssertion
{
[Test]
public void TestRead ()
{
XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
AssertEquals (6, schema.Items.Count);
bool fooValidated = false;
bool barValidated = false;
string ns = "urn:bar";
foreach (XmlSchemaObject obj in schema.Items) {
XmlSchemaElement element = obj as XmlSchemaElement;
if (element == null)
continue;
if (element.Name == "Foo") {
AssertElement (element, "Foo",
XmlQualifiedName.Empty, null,
QName ("string", XmlSchema.Namespace), null);
fooValidated = true;
}
if (element.Name == "Bar") {
AssertElement (element, "Bar",
XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
barValidated = true;
}
}
Assert (fooValidated);
Assert (barValidated);
}
[Test]
public void TestReadFlags ()
{
XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
schema.Compile (null);
XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
AssertNotNull (el);
AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
el = schema.Items [1] as XmlSchemaElement;
AssertNotNull (el);
AssertEquals (XmlSchemaDerivationMethod.Extension |
XmlSchemaDerivationMethod.Restriction, el.Block);
}
[Test]
public void TestWriteFlags ()
{
XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
schema.Write (xtw);
}
[Test]
public void TestCompile ()
{
XmlQualifiedName qname;
XmlSchemaComplexContentExtension xccx;
XmlSchemaComplexType cType;
XmlSchemaSequence seq;
XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
// Assert (!schema.IsCompiled);
schema.Compile (null);
Assert (schema.IsCompiled);
string ns = "urn:bar";
XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
AssertNotNull (foo);
XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
AssertNotNull (stringDatatype);
// HogeType
qname = QName ("HogeType", ns);
cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
AssertNotNull (cType);
AssertNull (cType.ContentModel);
AssertCompiledComplexType (cType, qname, 0, 0,
false, null, true, XmlSchemaContentType.ElementOnly);
seq = cType.ContentTypeParticle as XmlSchemaSequence;
AssertNotNull (seq);
AssertEquals (2, seq.Items.Count);
XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
// FugaType
qname = QName ("FugaType", ns);
cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
AssertNotNull (cType);
xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
AssertCompiledComplexContentExtension (
xccx, 0, false, QName ("HogeType", ns));
AssertCompiledComplexType (cType, qname, 0, 0,
false, typeof (XmlSchemaComplexContent),
true, XmlSchemaContentType.ElementOnly);
AssertNotNull (cType.BaseSchemaType);
seq = xccx.Particle as XmlSchemaSequence;
AssertNotNull (seq);
AssertEquals (1, seq.Items.Count);
XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
AssertNotNull (refBaz);
AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
qname = QName ("Bar", ns);
XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
AssertCompiledElement (element, qname, cType);
}
[Test]
[ExpectedException (typeof (XmlSchemaException))]
public void TestCompile_ZeroLength_TargetNamespace ()
{
XmlSchema schema = new XmlSchema ();
schema.TargetNamespace = string.Empty;
Assert (!schema.IsCompiled);
// MS.NET 1.x: The Namespace '' is an invalid URI.
// MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
schema.Compile (null);
}
[Test]
[ExpectedException (typeof (XmlSchemaException))]
public void TestCompileNonSchema ()
{
XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
XmlSchema schema = XmlSchema.Read (xtr, null);
xtr.Close ();
}
[Test]
public void TestSimpleImport ()
{
XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
AssertEquals ("urn:foo", schema.TargetNamespace);
XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
AssertNotNull (import);
schema.Compile (null);
AssertEquals (4, schema.Elements.Count);
AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
AssertNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
}
[Test]
public void TestSimpleMutualImport ()
{
XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
try {
XmlSchema.Read (r, null).Compile (null);
} finally {
r.Close ();
}
}
[Test]
public void TestQualification ()
{
XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
schema.Compile (null);
XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
AssertNotNull (el);
XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
AssertEquals (QName ("Bar", ""), elp.QualifiedName);
schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
schema.Compile (null);
el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
AssertNotNull (el);
ct = el.ElementType as XmlSchemaComplexType;
seq = ct.ContentTypeParticle as XmlSchemaSequence;
elp = seq.Items [0] as XmlSchemaElement;
AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
}
[Test]
public void TestWriteNamespaces ()
{
XmlDocument doc = new XmlDocument ();
XmlSchema xs;
StringWriter sw;
XmlTextWriter xw;
// empty
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.Write (xw);
doc.LoadXml (sw.ToString ());
AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// TargetNamespace
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.TargetNamespace = "urn:foo";
xs.Write (xw);
doc.LoadXml (sw.ToString ());
AssertEquals ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// Zero-length TargetNamespace
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.TargetNamespace = string.Empty;
xs.Write (xw);
doc.LoadXml (sw.ToString ());
AssertEquals ("#2b", "<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// XmlSerializerNamespaces
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.Namespaces.Add ("hoge", "urn:hoge");
xs.Write (xw);
doc.LoadXml (sw.ToString ());
// commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
// AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// TargetNamespace + XmlSerializerNamespaces
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.TargetNamespace = "urn:foo";
xs.Namespaces.Add ("hoge", "urn:hoge");
xs.Write (xw);
doc.LoadXml (sw.ToString ());
// commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
// AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// Add XmlSchema.Namespace to XmlSerializerNamespaces
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
xs.Namespaces.Add ("a", XmlSchema.Namespace);
xs.Write (xw);
doc.LoadXml (sw.ToString ());
AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// UnhandledAttributes + XmlSerializerNamespaces
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
XmlAttribute attr = doc.CreateAttribute ("hoge");
xs.UnhandledAttributes = new XmlAttribute [] {attr};
xs.Namespaces.Add ("hoge", "urn:hoge");
xs.Write (xw);
doc.LoadXml (sw.ToString ());
// commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
// AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
// Adding xmlns to UnhandledAttributes -> no output
xs = new XmlSchema ();
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
attr = doc.CreateAttribute ("xmlns");
attr.Value = "urn:foo";
xs.UnhandledAttributes = new XmlAttribute [] {attr};
xs.Write (xw);
doc.LoadXml (sw.ToString ());
AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
}
[Category ("NotWorking")]
[Test]
public void TestWriteNamespaces2 ()
{
string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
XmlSchema xs = new XmlSchema ();
XmlSerializerNamespaces nss =
new XmlSerializerNamespaces ();
StringWriter sw;
sw = new StringWriter ();
xs.Write (new XmlTextWriter (sw));
AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
xs.Namespaces = nss;
sw = new StringWriter ();
xs.Write (new XmlTextWriter (sw));
AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
nss.Add ("foo", "urn:foo");
sw = new StringWriter ();
xs.Write (new XmlTextWriter (sw));
// commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
// AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
nss.Add ("", "urn:foo");
sw = new StringWriter ();
xs.Write (new XmlTextWriter (sw));
// commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
// AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
nss.Add ("q1", "urn:q1");
sw = new StringWriter ();
xs.Write (new XmlTextWriter (sw));
//Not sure if testing for exact order of these name spaces is
// relevent, so using less strict test that passes on MS.NET
//AssertEquals (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
}
[Test]
public void ReaderPositionAfterRead ()
{
string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
xtr.Read ();
XmlSchema xs = XmlSchema.Read (xtr, null);
AssertEquals (XmlNodeType.EndElement, xtr.NodeType);
}
[Test]
// bug #76865
public void AmbiguityDetectionOnChameleonAnyOther ()
{
string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:complexType name='TestType'>
<xs:sequence>
<xs:any namespace='##other' minOccurs='0' />
<xs:element name='Item' />
<xs:any namespace='##other' minOccurs='0' />
</xs:sequence>
</xs:complexType>
</xs:schema>";
XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
}
[Test]
// bug #77685
public void ReadDoesNotIgnoreDocumentationEmptyElement ()
{
string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='choice'>
<xs:annotation><xs:documentation /></xs:annotation>
</xs:element>
</xs:schema>";
XmlTextReader tr = new XmlTextReader (
schemaxml, XmlNodeType.Document, null);
XmlSchema schema = XmlSchema.Read (tr, null);
XmlSchemaElement element =
schema.Items [0] as XmlSchemaElement;
XmlSchemaAnnotation annotation = element.Annotation;
XmlSchemaDocumentation doc =
annotation.Items [0] as XmlSchemaDocumentation;
AssertEquals (0, doc.Markup.Length);
}
[Test]
// bug #77687
public void CompileFillsSchemaPropertyInExternal ()
{
string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
XmlTextReader tr = new XmlTextReader (schemaFileName);
XmlSchema schema = XmlSchema.Read (tr, null);
XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
AssertNull (inc.Schema);
schema.Compile (null);
tr.Close ();
AssertNotNull (inc.Schema);
}
[Test]
// bug #78985 (contains two identical field path "@key" in
// two different keys where one is in scope within another)
public void DuplicateKeyFieldAttributePath ()
{
string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
XmlTextReader tr = new XmlTextReader (schemaFileName);
XmlValidatingReader vr = new XmlValidatingReader (
new XmlTextReader (xmlFileName));
vr.Schemas.Add (XmlSchema.Read (tr, null));
while (!vr.EOF)
vr.Read ();
}
}
}
|