2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Test / System.Xml / XmlDataDocumentTest2.cs
1 //
2 // XmlDataDocumentTest2.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32 using System;
33 using System.Data;
34 using System.IO;
35 using System.Xml;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Xml
39 {
40         [TestFixture]
41         public class XmlDataDocumentTest2 : Assertion
42         {
43                 string xml = "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";
44
45                 [Test]
46                 [ExpectedException (typeof (ArgumentException))]
47                 public void TestCtorNullArgs ()
48                 {
49                         new XmlDataDocument (null);
50                 }
51
52                 [Test]
53                 public void TestDefaultCtor ()
54                 {
55                         XmlDataDocument doc = new XmlDataDocument ();
56                         AssertNotNull (doc.DataSet);
57                         AssertEquals ("NewDataSet", doc.DataSet.DataSetName);
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (InvalidOperationException))]
62                 public void TestMultipleLoadError ()
63                 {
64                         DataSet ds = new DataSet ();
65                         ds.ReadXml (new XmlTextReader (xml, XmlNodeType.Document, null));
66                         // If there is already data element, Load() fails.
67                         XmlDataDocument doc = new XmlDataDocument (ds);
68                         doc.LoadXml (xml);
69                 }
70
71                 [Test]
72                 public void TestMultipleLoadNoError ()
73                 {
74                         DataSet ds = new DataSet ();
75                         DataTable dt = new DataTable ();
76                         dt.Columns.Add ("col1");
77                         ds.Tables.Add (dt);
78
79                         XmlDataDocument doc = new XmlDataDocument (ds);
80                         doc.LoadXml (xml);
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentException))]
85                 public void TestMultipleDataDocFromDataSet ()
86                 {
87                         DataSet ds = new DataSet ();
88                         XmlDataDocument doc = new XmlDataDocument (ds);
89                         XmlDataDocument doc2 = new XmlDataDocument (ds);
90                 }
91
92                 [Test]
93                 public void TestLoadXml ()
94                 {
95                         XmlDataDocument doc = new XmlDataDocument ();
96                         doc.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");
97
98                         doc = new XmlDataDocument ();
99                         doc.LoadXml ("<test>value</test>");
100                 }
101
102                 [Test]
103                 public void TestCreateElementAndRow ()
104                 {
105                         DataSet ds = new DataSet ("set");\r
106                         DataTable dt = new DataTable ("tab1");\r
107                         dt.Columns.Add ("col1");\r
108                         dt.Columns.Add ("col2");\r
109                         ds.Tables.Add (dt);\r
110                         DataTable dt2 = new DataTable ("child");\r
111                         dt2.Columns.Add ("ref");\r
112                         dt2.Columns.Add ("val");\r
113                         ds.Tables.Add (dt2);\r
114                         DataRelation rel = new DataRelation ("rel",\r
115                                 dt.Columns [0], dt2.Columns [0]);\r
116                         rel.Nested = true;\r
117                         ds.Relations.Add (rel);\r
118                         XmlDataDocument doc = new XmlDataDocument (ds);\r
119                         doc.LoadXml ("<set><tab1><col1>1</col1><col2/><child><ref>1</ref><val>aaa</val></child></tab1></set>");\r
120                         AssertEquals (1, ds.Tables [0].Rows.Count);\r
121                         AssertEquals (1, ds.Tables [1].Rows.Count);\r
122 \r
123                         // document element - no mapped row\r
124                         XmlElement el = doc.DocumentElement;\r
125                         AssertNull (doc.GetRowFromElement (el));\r
126 \r
127                         // tab1 element - has mapped row\r
128                         el = el.FirstChild as XmlElement;\r
129                         DataRow row = doc.GetRowFromElement (el);\r
130                         AssertNotNull (row);\r
131                         AssertEquals (DataRowState.Added, row.RowState);\r
132 \r
133                         // col1 - it is column. no mapped row\r
134                         el = el.FirstChild as XmlElement;\r
135                         row = doc.GetRowFromElement (el);\r
136                         AssertNull (row);\r
137 \r
138                         // col2 - it is column. np mapped row\r
139                         el = el.NextSibling as XmlElement;\r
140                         row = doc.GetRowFromElement (el);\r
141                         AssertNull (row);\r
142 \r
143                         // child - has mapped row\r
144                         el = el.NextSibling as XmlElement;\r
145                         row = doc.GetRowFromElement (el);\r
146                         AssertNotNull (row);\r
147                         AssertEquals (DataRowState.Added, row.RowState);\r
148 \r
149                         // created (detached) table 1 element (used later)\r
150                         el = doc.CreateElement ("tab1");\r
151                         row = doc.GetRowFromElement (el);\r
152                         AssertEquals (DataRowState.Detached, row.RowState);\r
153                         AssertEquals (1, dt.Rows.Count); // not added yet\r
154 \r
155                         // adding a node before setting EnforceConstraints\r
156                         // raises an error\r
157                         try {\r
158                                 doc.DocumentElement.AppendChild (el);\r
159                                 Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");\r
160                         } catch (InvalidOperationException) {\r
161                         }\r
162 \r
163                         // try again...\r
164                         ds.EnforceConstraints = false;\r
165                         AssertEquals (1, dt.Rows.Count); // not added yet\r
166                         doc.DocumentElement.AppendChild (el);\r
167                         AssertEquals (2, dt.Rows.Count); // added\r
168                         row = doc.GetRowFromElement (el);\r
169                         AssertEquals (DataRowState.Added, row.RowState); // changed\r
170 \r
171                         // Irrelevant element\r
172                         XmlElement el2 = doc.CreateElement ("hoge");\r
173                         row = doc.GetRowFromElement (el2);\r
174                         AssertNull (row);\r
175 \r
176                         // created table 2 element (used later)\r
177                         el = doc.CreateElement ("child");\r
178                         row = doc.GetRowFromElement (el);\r
179                         AssertEquals (DataRowState.Detached, row.RowState);\r
180 \r
181                         // Adding it to irrelevant element performs no row state change.\r
182                         AssertEquals (1, dt2.Rows.Count); // not added yet\r
183                         el2.AppendChild (el);\r
184                         AssertEquals (1, dt2.Rows.Count); // still not added\r
185                         row = doc.GetRowFromElement (el);\r
186                         AssertEquals (DataRowState.Detached, row.RowState); // still detached here\r
187                 }\r
188
189                 // bug #54505
190                 public void TypedDataDocument ()
191                 {
192                         string xml = @"<top xmlns=""urn:test"">
193   <foo>
194     <s>first</s>
195     <d>2004-02-14T10:37:03</d>
196   </foo>
197   <foo>
198     <s>second</s>
199     <d>2004-02-17T12:41:49</d>
200   </foo>
201 </top>";
202                         string xmlschema = @"<xs:schema id=""webstore"" targetNamespace=""urn:test"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
203   <xs:element name=""top"">
204     <xs:complexType>
205       <xs:sequence maxOccurs=""unbounded"">
206         <xs:element name=""foo"">
207           <xs:complexType>
208             <xs:sequence maxOccurs=""unbounded"">
209               <xs:element name=""s"" type=""xs:string""/>
210               <xs:element name=""d"" type=""xs:dateTime""/>
211             </xs:sequence>
212           </xs:complexType>
213         </xs:element>
214       </xs:sequence>
215     </xs:complexType>
216   </xs:element>
217 </xs:schema>";
218                         XmlDataDocument doc = new XmlDataDocument ();
219                         doc.DataSet.ReadXmlSchema (new StringReader (xmlschema));
220                         doc.LoadXml (xml);
221                         DataTable foo = doc.DataSet.Tables ["foo"];
222                         DataRow newRow = foo.NewRow ();
223                         newRow ["s"] = "new";
224                         newRow ["d"] = DateTime.Now;
225                         foo.Rows.Add (newRow);
226                         doc.Save (new StringWriter ());
227                 }
228         }
229 }