* roottypes.cs: Rename from tree.cs.
[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                 public void AddSchemaThenReader ()
60                 {
61                         XmlSchemaSet ss = new XmlSchemaSet ();
62                         XmlDocument doc = new XmlDocument ();
63                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
64                         XmlSchema xs = new XmlSchema ();
65                         xs.TargetNamespace = "ab";
66                         ss.Add (xs);
67                         ss.Add ("ab", new XmlNodeReader (doc));
68                 }
69
70                 [Test]
71                 [Category ("NotWorking")] // How can we differentiate this
72                 // case and the testcase above?
73                 [ExpectedException (typeof (ArgumentException))]
74                 public void AddReaderTwice ()
75                 {
76                         XmlSchemaSet ss = new XmlSchemaSet ();
77                         XmlDocument doc = new XmlDocument ();
78                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
79                         ss.Add ("ab", new XmlNodeReader (doc));
80                         ss.Add ("ab", new XmlNodeReader (doc));
81                 }
82
83                 [Test]
84                 public void AddSchemaTwice ()
85                 {
86                         XmlSchemaSet ss = new XmlSchemaSet ();
87                         XmlDocument doc = new XmlDocument ();
88                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
89                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
90                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
91                 }
92
93                 [Test]
94                 public void CompilationSettings ()
95                 {
96                         Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
97                         new XmlSchemaSet ().CompilationSettings = null;
98                 }
99
100                 [Test]
101                 public void DisableUpaCheck ()
102                 {
103                         string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
104   <xs:complexType name='Foo'>
105     <xs:sequence>
106       <xs:choice minOccurs='0'>
107         <xs:element name='el'/>
108       </xs:choice>
109       <xs:element name='el' />
110     </xs:sequence>
111   </xs:complexType>
112 </xs:schema>";
113                         XmlSchema xs = XmlSchema.Read (new XmlTextReader (
114                                 schema, XmlNodeType.Document, null), null);
115                         XmlSchemaSet xss = new XmlSchemaSet ();
116                         xss.Add (xs);
117                         xss.CompilationSettings.EnableUpaCheck = false;
118
119                         xss.Compile ();
120                 }
121
122                 [Test]
123                 public void AddRollbackIsCompiled ()
124                 {
125                         XmlSchemaSet ss = new XmlSchemaSet ();
126                         ss.Add (new XmlSchema ());
127                         ss.Compile ();
128                         Assert.IsTrue (ss.IsCompiled, "#1");
129                         XmlSchema sc = new XmlSchema (); // compiled one
130                         sc.Compile (null);
131                         ss.Add (sc);
132                         Assert.IsFalse (ss.IsCompiled, "#2");
133                         ss.Add (new XmlSchema ()); // not-compiled one
134                         Assert.IsFalse (ss.IsCompiled, "#3");
135                         XmlSchema s;
136
137                         s = new XmlSchema ();
138                         s.TargetNamespace = "urn:foo";
139                         XmlSchemaElement el;
140                         el = new XmlSchemaElement ();
141                         el.Name = "root";
142                         s.Items.Add (el);
143                         ss.Add (s);
144
145                         s = new XmlSchema ();
146                         s.TargetNamespace = "urn:foo";
147                         el = new XmlSchemaElement ();
148                         el.Name = "foo";
149                         s.Items.Add (el);
150                         ss.Add (s);
151                         ss.Compile ();
152                         Assert.IsTrue (ss.IsCompiled, "#4");
153                         ss.RemoveRecursive (s);
154                         Assert.IsTrue (ss.IsCompiled, "#5");
155                 }
156         }
157 }
158 #endif