6318ba5b2d5e84d85b24b7e753eb66103cefc16c
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaSetTests.cs
1 //
2 // System.Xml.XmlSchemaSetTests.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
9 #if NET_2_0
10
11 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Xml;
15 using System.Xml.Schema;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Xml
19 {
20         [TestFixture]
21         public class XmlSchemaSetTests
22         {
23                 [Test]
24                 public void Add ()
25                 {
26                         XmlSchemaSet ss = new XmlSchemaSet ();
27                         XmlDocument doc = new XmlDocument ();
28                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
29                         ss.Add (null, new XmlNodeReader (doc)); // null targetNamespace
30                         ss.Compile ();
31
32                         // same document, different targetNamespace
33                         ss.Add ("ab", new XmlNodeReader (doc));
34
35                         // Add(null, xmlReader) -> targetNamespace in the schema
36                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo' />");
37                         ss.Add (null, new XmlNodeReader (doc));
38
39                         Assert.AreEqual (3, ss.Count);
40
41                         bool chameleon = false;
42                         bool ab = false;
43                         bool urnfoo = false;
44
45                         foreach (XmlSchema schema in ss.Schemas ()) {
46                                 if (schema.TargetNamespace == null)
47                                         chameleon = true;
48                                 else if (schema.TargetNamespace == "ab")
49                                         ab = true;
50                                 else if (schema.TargetNamespace == "urn:foo")
51                                         urnfoo = true;
52                         }
53                         Assert.IsTrue (chameleon, "chameleon schema missing");
54                         Assert.IsTrue (ab, "target-remapped schema missing");
55                         Assert.IsTrue (urnfoo, "target specified in the schema ignored");
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (XmlSchemaException))]
60                 public void AddWrongTargetNamespace ()
61                 {
62                         string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='el' type='xs:int' /></xs:schema>";
63                         string xml = "<el xmlns='urn:foo'>a</el>";
64                         XmlSchemaSet xss = new XmlSchemaSet ();
65                         // unlike null, "" is regarded as an explicit
66                         // empty namespace indication.
67                         xss.Add ("", new XmlTextReader (new StringReader (xsd)));
68                 }
69
70                 [Test]
71                 public void AddSchemaThenReader ()
72                 {
73                         XmlSchemaSet ss = new XmlSchemaSet ();
74                         XmlDocument doc = new XmlDocument ();
75                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
76                         XmlSchema xs = new XmlSchema ();
77                         xs.TargetNamespace = "ab";
78                         ss.Add (xs);
79                         ss.Add ("ab", new XmlNodeReader (doc));
80                 }
81
82                 [Test]
83                 [Category ("NotWorking")] // How can we differentiate this
84                 // case and the testcase above?
85                 [ExpectedException (typeof (ArgumentException))]
86                 public void AddReaderTwice ()
87                 {
88                         XmlSchemaSet ss = new XmlSchemaSet ();
89                         XmlDocument doc = new XmlDocument ();
90                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
91                         ss.Add ("ab", new XmlNodeReader (doc));
92                         ss.Add ("ab", new XmlNodeReader (doc));
93                 }
94
95                 [Test]
96                 public void AddSchemaTwice ()
97                 {
98                         XmlSchemaSet ss = new XmlSchemaSet ();
99                         XmlDocument doc = new XmlDocument ();
100                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
101                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
102                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
103                 }
104
105                 [Test]
106                 public void CompilationSettings ()
107                 {
108                         Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
109                         new XmlSchemaSet ().CompilationSettings = null;
110                 }
111
112                 [Test]
113                 public void DisableUpaCheck ()
114                 {
115                         string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
116   <xs:complexType name='Foo'>
117     <xs:sequence>
118       <xs:choice minOccurs='0'>
119         <xs:element name='el'/>
120       </xs:choice>
121       <xs:element name='el' />
122     </xs:sequence>
123   </xs:complexType>
124 </xs:schema>";
125                         XmlSchema xs = XmlSchema.Read (new XmlTextReader (
126                                 schema, XmlNodeType.Document, null), null);
127                         XmlSchemaSet xss = new XmlSchemaSet ();
128                         xss.Add (xs);
129                         xss.CompilationSettings.EnableUpaCheck = false;
130
131                         xss.Compile ();
132                 }
133
134                 [Test]
135                 public void AddRollbackIsCompiled ()
136                 {
137                         XmlSchemaSet ss = new XmlSchemaSet ();
138                         ss.Add (new XmlSchema ());
139                         ss.Compile ();
140                         Assert.IsTrue (ss.IsCompiled, "#1");
141                         XmlSchema sc = new XmlSchema (); // compiled one
142                         sc.Compile (null);
143                         ss.Add (sc);
144                         Assert.IsFalse (ss.IsCompiled, "#2");
145                         ss.Add (new XmlSchema ()); // not-compiled one
146                         Assert.IsFalse (ss.IsCompiled, "#3");
147                         XmlSchema s;
148
149                         s = new XmlSchema ();
150                         s.TargetNamespace = "urn:foo";
151                         XmlSchemaElement el;
152                         el = new XmlSchemaElement ();
153                         el.Name = "root";
154                         s.Items.Add (el);
155                         ss.Add (s);
156
157                         s = new XmlSchema ();
158                         s.TargetNamespace = "urn:foo";
159                         el = new XmlSchemaElement ();
160                         el.Name = "foo";
161                         s.Items.Add (el);
162                         ss.Add (s);
163                         ss.Compile ();
164                         Assert.IsTrue (ss.IsCompiled, "#4");
165                         ss.RemoveRecursive (s);
166                         Assert.IsTrue (ss.IsCompiled, "#5");
167                 }
168
169                 [Test] // bug #77489
170                 public void CrossSchemaReferences ()
171                 {
172                         string schema1 = @"<xsd:schema id=""Base.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
173         <xsd:complexType name=""itemBase"" abstract=""true""> 
174                 <xsd:attribute name=""id"" type=""xsd:string""
175 use=""required""/> 
176                 <xsd:attribute name=""type"" type=""xsd:string""
177 use=""required""/> 
178         </xsd:complexType> 
179 </xsd:schema>";
180
181                         string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
182         <xsd:complexType name=""item""> 
183                 <xsd:complexContent> 
184                         <xsd:extension base=""itemBase""> 
185                                 <xsd:attribute name=""itemName""
186 type=""xsd:string"" use=""required""/> 
187                         </xsd:extension> 
188                 </xsd:complexContent> 
189         </xsd:complexType> 
190 </xsd:schema>";
191                         XmlSchemaSet schemas = new XmlSchemaSet ();
192                         schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
193                         schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
194                         schemas.Compile ();
195                 }
196
197                 [Test]
198                 public void ImportSubstitutionGroupDBR ()
199                 {
200                         // This bug happened when
201                         // 1) a schema imports another schema,
202                         // 2) there is a substitutionGroup which is involved in
203                         //    complexContent schema conformance check, and
204                         // 3) the included schema is already added to XmlSchemaSet.
205                         XmlSchemaSet xss = new XmlSchemaSet ();
206                         xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-base.xsd");
207                         xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-ext.xsd");
208                         // should not result in lack of substitutionGroup
209                         // (and conformance error as its result)
210                         xss.Compile ();
211                 }
212         }
213 }
214 #endif