[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System.Data / Test / System.Data / XmlDataReaderTest.cs
1 // Authors:
2 //   Nagappan A <anagappan@novell.com>
3 //
4 // Copyright (c) 2007 Novell, Inc
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 #if NET_2_0
27 using System;
28 using System.Data;
29 using System.Collections;
30 using System.IO;        
31 using System.Xml;
32 using System.Xml.Serialization;
33 using System.Xml.Schema;
34 using NUnit.Framework;
35
36 namespace Monotests_System.Data
37 {
38         [TestFixture]   
39         public class XmlDataReaderTest
40         {
41                 [Test]
42                 public void XmlLoadTest ()
43                 {
44                                 DataSet ds = new DataSet();
45                                 ds.ReadXmlSchema ("Test/System.Data/TestReadXmlSchema1.xml");
46                                 ds.ReadXml ("Test/System.Data/TestReadXml1.xml");
47                 }
48                 
49                 // Test for Bug#377146
50                 [Test]
51                 public void XmlLoadCustomTypesTest ()
52                 {
53                         string xml = "<CustomTypesData>" + Environment.NewLine +
54                                                 "<CustomTypesTable>" + Environment.NewLine +
55                                         "<Dummy>99</Dummy>" + Environment.NewLine +
56                                                 "<FuncXml> " + Environment.NewLine +
57                                                 "<Func Name=\"CUT_IntPassiveIn()\" Direction=\"PASSIVE_MOCK\">" + Environment.NewLine +
58                                                 "<Param Name=\"paramLen\" Type=\"int\" Len=\"1\" InOut=\"IN\" Union=\"FALSE\" " + Environment.NewLine +
59                                                 "Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"1\" IsExpGetRef=\"\" " + Environment.NewLine +
60                                                 "IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" HandleInput=\"DEC\" " + Environment.NewLine +
61                                                 "Enum=\"\">" + Environment.NewLine + 
62                                                 "</Param>" + Environment.NewLine + Environment.NewLine +
63                                                 "<Param Name=\"single\" Type=\"int\" Len=\"1\" InOut=\"IN\" Union=\"FALSE\" " + Environment.NewLine +
64                                                 "Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"16\" IsExpGetRef=\"\" " + Environment.NewLine +
65                                                 "IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" HandleInput=\"DEC\" " + Environment.NewLine +
66                                                 "Enum=\"\">" + Environment.NewLine + 
67                                                 "</Param>" + Environment.NewLine + Environment.NewLine +
68                                                 "<Param Name=\"arraySizeParam\" Type=\"int*\" Len=\"4\" InOut=\"IN\" " + Environment.NewLine +
69                                                 "Union=\"FALSE\" Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"\" " + Environment.NewLine +
70                                                 "IsExpGetRef=\"\" IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" " + Environment.NewLine +
71                                                 "HandleInput=\"HEX\" Enum=\"\">" + Environment.NewLine + Environment.NewLine +
72                                                 "<Param1 Name=\"arraySizeParam0\" Type=\"int\" Len=\"0\" InOut=\"IN\" " + Environment.NewLine +
73                                                 "Union=\"FALSE\" Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"1\" " + Environment.NewLine +
74                                                 "IsExpGetRef=\"\" IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" " + Environment.NewLine +
75                                                 "HandleInput=\"DEC\" Enum=\"\">" + Environment.NewLine +
76                                                 "</Param1>" + Environment.NewLine + Environment.NewLine +
77                                                 "<Param1 Name=\"arraySizeParam1\" Type=\"int\" Len=\"0\" InOut=\"IN\" " + Environment.NewLine +
78                                                 "Union=\"FALSE\" Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"\" " + Environment.NewLine +
79                                                 "IsExpGetRef=\"\" IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" " + Environment.NewLine +
80                                                 "HandleInput=\"HEX\" Enum=\"\">" + Environment.NewLine +
81                                                 "</Param1>" + Environment.NewLine + Environment.NewLine +
82                                                 "<Param1 Name=\"arraySizeParam2\" Type=\"int\" Len=\"0\" InOut=\"IN\" " + Environment.NewLine +
83                                                 "Union=\"FALSE\" Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"\" " + Environment.NewLine +
84                                                 "IsExpGetRef=\"\" IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" " + Environment.NewLine +
85                                                 "HandleInput=\"HEX\" Enum=\"\">" + Environment.NewLine +
86                                                 "</Param1>" + Environment.NewLine + Environment.NewLine +
87                                                 "<Param1 Name=\"arraySizeParam3\" Type=\"int\" Len=\"0\" InOut=\"IN\" " + Environment.NewLine +
88                                                 "Union=\"FALSE\" Callback=\"\" CSharpType=\"int\" Value=\"\" ExpectedValue=\"\" " + Environment.NewLine +
89                                                 "IsExpGetRef=\"\" IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" " + Environment.NewLine +
90                                                 "HandleInput=\"HEX\" Enum=\"\">" + Environment.NewLine +
91                                                 "</Param1>" + Environment.NewLine + Environment.NewLine +
92                                         "</Param>" + Environment.NewLine +
93                                                 "<Return Name=\"retVal\" Type=\"int\" Len=\"1\" InOut=\"OUT\" Union=\"FALSE\" " + Environment.NewLine +
94                                                 "Callback=\"\" CSharpType=\"int\" Value=\"1\" ExpectedValue=\"\" IsExpGetRef=\"\" " + Environment.NewLine +
95                                                 "IsGetRef=\"\" IsSetRef=\"\" ChildSelected=\"FALSE\" UnionIndex=\"-1\" HandleInput=\"DEC\" " + Environment.NewLine +
96                                                 "Enum=\"\">" + Environment.NewLine +
97                                                 "</Return>" + Environment.NewLine +
98                                                 "</Func>" + Environment.NewLine +
99                                                 "</FuncXml>" + Environment.NewLine +
100                                                 "</CustomTypesTable>" + Environment.NewLine +
101                                                 "</CustomTypesData>" + Environment.NewLine;
102                         
103                         StringReader sr = new StringReader (xml);
104                         XmlTextReader xr = new XmlTextReader (sr);
105                         DataTable tbl = new DataTable("CustomTypesTable");
106                         tbl.Columns.Add("Dummy", typeof(System.UInt32));
107                         tbl.Columns.Add("FuncXml", typeof(CustomTypeXml));
108
109                         DataSet ds = new DataSet("CustomTypesData");
110                         ds.Tables.Add(tbl);
111
112                         ds.ReadXml(xr);
113
114                         Assert.AreEqual (1, ds.Tables["CustomTypesTable"].Rows.Count, "XDR2");
115                         
116                         xr.Close ();
117                 }
118                 
119                 [Serializable]
120                 public class CustomTypeXml : IXmlSerializable
121                 {
122                     private XmlNode mFuncXmlNode;
123
124                     #region Constructors
125                     public CustomTypeXml()
126                     {
127                     }
128
129                     public CustomTypeXml(string str)
130                     {
131                         XmlDocument doc = new XmlDocument();
132                         doc.LoadXml(str);
133                         mFuncXmlNode = (XmlNode)(doc.DocumentElement);
134                     }
135
136                     public CustomTypeXml(XmlNode xNode)
137                     {
138                         mFuncXmlNode = xNode;
139                     }
140                     #endregion
141
142                     #region Node (set/get)
143                     public XmlNode Node
144                     {
145                         get
146                         {
147                             return mFuncXmlNode;
148                         }
149                         set
150                         {
151                             this.mFuncXmlNode = value;
152                         }
153                     }
154                     #endregion
155                     #region ToString
156                     public override string ToString()
157                     {
158                         return this.Node.OuterXml;
159                     }
160                     #endregion
161
162                     /* IXmlSerializable overrides */
163                     #region WriteXml
164                     void IXmlSerializable.WriteXml(XmlWriter writer)
165                     {
166                         XmlDocument doc = new XmlDocument();
167                         doc.LoadXml(mFuncXmlNode.OuterXml);
168
169                         // On function level
170                         if (doc.DocumentElement.Name == "Func")
171                         {
172                             try { doc.DocumentElement.Attributes.Remove(doc.DocumentElement.Attributes["ReturnType"]); }
173                             catch { }
174                             try { doc.DocumentElement.Attributes.Remove(doc.DocumentElement.Attributes["ReturnTId"]); }
175                             catch { }
176                             try { doc.DocumentElement.Attributes.Remove(doc.DocumentElement.Attributes["CSharpType"]); }
177                             catch { }
178                         }
179                         else
180                         {
181                             UpgradeSchema(doc.DocumentElement);
182                         }
183
184                         // Make sure lrt is saved according to latest schema
185                         foreach (XmlNode n in doc.DocumentElement.ChildNodes)
186                         {
187                             UpgradeSchema(n);
188                         }
189
190                         doc.WriteTo(writer);
191                     }
192                     #endregion
193                     #region ReadXml
194                     void IXmlSerializable.ReadXml(XmlReader reader)
195                     {
196                         XmlDocument doc = new XmlDocument();
197                         string str = reader.ReadString();
198                         try
199                         {
200                             doc.LoadXml(str);
201                         }
202                         catch
203                         {
204                             doc.LoadXml(reader.ReadOuterXml());
205                         }
206                         mFuncXmlNode = (XmlNode)(doc.DocumentElement);
207                     }
208                     #endregion
209                     #region GetSchema
210                     XmlSchema IXmlSerializable.GetSchema()
211                     {
212                         return (null);
213                     }
214                     #endregion
215
216                     /* Private utils */
217                     #region private UpgradeSchema
218                     private void UpgradeSchema(XmlNode xNode)
219                     {
220                         // Attribute removals (cleanup)
221                         try { xNode.Attributes.Remove(xNode.Attributes["TId"]); }
222                         catch { }
223                         try { xNode.Attributes.Remove(xNode.Attributes["OnError"]); }
224                         catch { }
225                         try { xNode.Attributes.Remove(xNode.Attributes["Check"]); }
226                         catch { }
227                         try { xNode.Attributes.Remove(xNode.Attributes["ParamType"]); }
228                         catch { }
229                         try { xNode.Attributes.Remove(xNode.Attributes["RealLen"]); }
230                         catch { }
231
232                         // Attribute removals (order)
233                         try
234                         {
235                             XmlAttribute attr = xNode.Attributes["IsExpGetRef"];
236                             xNode.Attributes.Remove(xNode.Attributes["IsExpGetRef"]);
237                             xNode.Attributes.InsertAfter(attr, xNode.Attributes["ExpectedValue"]);
238                         }
239                         catch { }
240
241                         // Attribute value formats (prefix, etc.)
242                         string tmp = xNode.Attributes["HandleInput"].Value;
243                         tmp = tmp.Replace("E_LRT_INPUT_HANDLE_", "");
244                         xNode.Attributes["HandleInput"].Value = tmp;
245
246                         foreach (XmlNode n in xNode.ChildNodes)
247                         {
248                             UpgradeSchema(n);
249                         }
250                     }
251                     #endregion
252                 }       
253         }
254 }
255 #endif
256