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
|
//
// System.Xml.XmlSchemaSetTests.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C) 2004 Novell Inc.
//
#if NET_2_0
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlSchemaSetTests
{
[Test]
public void Add ()
{
XmlSchemaSet ss = new XmlSchemaSet ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
ss.Add (null, new XmlNodeReader (doc)); // null targetNamespace
ss.Compile ();
// same document, different targetNamespace
ss.Add ("ab", new XmlNodeReader (doc));
// Add(null, xmlReader) -> targetNamespace in the schema
doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo' />");
ss.Add (null, new XmlNodeReader (doc));
Assert.AreEqual (3, ss.Count);
bool chameleon = false;
bool ab = false;
bool urnfoo = false;
foreach (XmlSchema schema in ss.Schemas ()) {
if (schema.TargetNamespace == null)
chameleon = true;
else if (schema.TargetNamespace == "ab")
ab = true;
else if (schema.TargetNamespace == "urn:foo")
urnfoo = true;
}
Assert.IsTrue (chameleon, "chameleon schema missing");
Assert.IsTrue (ab, "target-remapped schema missing");
Assert.IsTrue (urnfoo, "target specified in the schema ignored");
}
[Test]
[ExpectedException (typeof (XmlSchemaException))]
public void AddWrongTargetNamespace ()
{
string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='el' type='xs:int' /></xs:schema>";
string xml = "<el xmlns='urn:foo'>a</el>";
XmlSchemaSet xss = new XmlSchemaSet ();
// unlike null, "" is regarded as an explicit
// empty namespace indication.
xss.Add ("", new XmlTextReader (new StringReader (xsd)));
}
[Test]
public void AddSchemaThenReader ()
{
XmlSchemaSet ss = new XmlSchemaSet ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
XmlSchema xs = new XmlSchema ();
xs.TargetNamespace = "ab";
ss.Add (xs);
ss.Add ("ab", new XmlNodeReader (doc));
}
[Test]
[Category ("NotWorking")] // How can we differentiate this
// case and the testcase above?
[ExpectedException (typeof (ArgumentException))]
public void AddReaderTwice ()
{
XmlSchemaSet ss = new XmlSchemaSet ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
ss.Add ("ab", new XmlNodeReader (doc));
ss.Add ("ab", new XmlNodeReader (doc));
}
[Test]
public void AddSchemaTwice ()
{
XmlSchemaSet ss = new XmlSchemaSet ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
}
[Test]
public void CompilationSettings ()
{
Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
new XmlSchemaSet ().CompilationSettings = null;
}
[Test]
public void DisableUpaCheck ()
{
string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:complexType name='Foo'>
<xs:sequence>
<xs:choice minOccurs='0'>
<xs:element name='el'/>
</xs:choice>
<xs:element name='el' />
</xs:sequence>
</xs:complexType>
</xs:schema>";
XmlSchema xs = XmlSchema.Read (new XmlTextReader (
schema, XmlNodeType.Document, null), null);
XmlSchemaSet xss = new XmlSchemaSet ();
xss.Add (xs);
xss.CompilationSettings.EnableUpaCheck = false;
xss.Compile ();
}
[Test]
public void AddRollbackIsCompiled ()
{
XmlSchemaSet ss = new XmlSchemaSet ();
ss.Add (new XmlSchema ());
ss.Compile ();
Assert.IsTrue (ss.IsCompiled, "#1");
XmlSchema sc = new XmlSchema (); // compiled one
sc.Compile (null);
ss.Add (sc);
Assert.IsFalse (ss.IsCompiled, "#2");
ss.Add (new XmlSchema ()); // not-compiled one
Assert.IsFalse (ss.IsCompiled, "#3");
XmlSchema s;
s = new XmlSchema ();
s.TargetNamespace = "urn:foo";
XmlSchemaElement el;
el = new XmlSchemaElement ();
el.Name = "root";
s.Items.Add (el);
ss.Add (s);
s = new XmlSchema ();
s.TargetNamespace = "urn:foo";
el = new XmlSchemaElement ();
el.Name = "foo";
s.Items.Add (el);
ss.Add (s);
ss.Compile ();
Assert.IsTrue (ss.IsCompiled, "#4");
ss.RemoveRecursive (s);
Assert.IsTrue (ss.IsCompiled, "#5");
}
[Test] // bug #77489
public void CrossSchemaReferences ()
{
string schema1 = @"<xsd:schema id=""Base.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<xsd:complexType name=""itemBase"" abstract=""true"">
<xsd:attribute name=""id"" type=""xsd:string""
use=""required""/>
<xsd:attribute name=""type"" type=""xsd:string""
use=""required""/>
</xsd:complexType>
</xsd:schema>";
string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<xsd:complexType name=""item"">
<xsd:complexContent>
<xsd:extension base=""itemBase"">
<xsd:attribute name=""itemName""
type=""xsd:string"" use=""required""/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet ();
schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
schemas.Compile ();
}
[Test]
public void ImportSubstitutionGroupDBR ()
{
// This bug happened when
// 1) a schema imports another schema,
// 2) there is a substitutionGroup which is involved in
// complexContent schema conformance check, and
// 3) the included schema is already added to XmlSchemaSet.
XmlSchemaSet xss = new XmlSchemaSet ();
xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-base.xsd");
xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-ext.xsd");
// should not result in lack of substitutionGroup
// (and conformance error as its result)
xss.Compile ();
}
[Test]
public void AddWithNullTargetNS () // bug #571650
{
var xsdraw = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo' /></xs:schema>";
var schemas = new XmlSchemaSet ();
var xsd = schemas.Add ("", XmlReader.Create (new StringReader (xsdraw)));
Assert.IsNull (xsd.TargetNamespace, "#1");
}
[Test] // part of bug #670945
public void TwoSchemasInSameDocumentUri ()
{
string xsd1 = @"
<xs:schema
targetNamespace='http://www.onvif.org/ver10/schema'
elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:tt='http://www.onvif.org/ver10/schema'>
<xs:complexType name='SystemDateTime'>
<xs:sequence>
<xs:element name='foobar' type='xs:string' minOccurs='0' />
<xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
</xs:sequence>
<!-- xs:anyAttribute processContents='lax'/ -->
</xs:complexType>
<xs:complexType name='SystemDateTimeExtension'>
<xs:sequence>
<xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
</xs:sequence>
</xs:complexType>
</xs:schema>";
string xsd2 = @"
<xs:schema
targetNamespace='http://www.onvif.org/ver10/device/wsdl'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:tt='http://www.onvif.org/ver10/schema'
xmlns:tds='http://www.onvif.org/ver10/device/wsdl'
elementFormDefault='qualified'>
<xs:element name='GetSystemDateAndTime'>
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element name='GetSystemDateAndTimeResponse'>
<xs:complexType>
<xs:sequence>
<xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>";
var xss = new XmlSchemaSet ();
var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
xs1.LineNumber = 5;
xss.Add (xs1);
var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
xs2.LineNumber = 50;
xss.Add (xs2);
xss.Compile ();
Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
}
[Test] // bug #13716
public void ResolveSchemaUriUsingXmlResolver ()
{
var resolver = new Bug13716XmlResolver ();
string xml = "<people xmlns='testschema'><person name='Ian'><books><book>Clean Code</book></books></person></people>";
string ns = "testschema";
string xsdPath = "my.xsd";
var readerSettings = new XmlReaderSettings ();
//readerSettings.XmlResolver = resolver;
readerSettings.Schemas.XmlResolver = resolver;
readerSettings.Schemas.Add (ns, xsdPath);
readerSettings.ValidationType = ValidationType.Schema;
using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
{
while (!xr.EOF)
xr.Read ();
}
}
public class Bug13716XmlResolver : XmlUrlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
{
string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
<xs:element name='people' />
</xs:schema>";
return new MemoryStream (Encoding.UTF8.GetBytes (xsd));
}
}
}
}
#endif
|