2 // XmlDataDocumentTest2.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
36 using NUnit.Framework;
38 namespace MonoTests.System.Xml
41 public class XmlDataDocumentTest2 : Assertion
43 string xml = "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";
46 [ExpectedException (typeof (ArgumentException))]
47 public void TestCtorNullArgs ()
49 new XmlDataDocument (null);
53 public void TestDefaultCtor ()
55 XmlDataDocument doc = new XmlDataDocument ();
56 AssertNotNull (doc.DataSet);
57 AssertEquals ("NewDataSet", doc.DataSet.DataSetName);
61 [ExpectedException (typeof (InvalidOperationException))]
62 public void TestMultipleLoadError ()
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);
72 public void TestMultipleLoadNoError ()
74 DataSet ds = new DataSet ();
75 DataTable dt = new DataTable ();
76 dt.Columns.Add ("col1");
79 XmlDataDocument doc = new XmlDataDocument (ds);
84 [ExpectedException (typeof (ArgumentException))]
85 public void TestMultipleDataDocFromDataSet ()
87 DataSet ds = new DataSet ();
88 XmlDataDocument doc = new XmlDataDocument (ds);
89 XmlDataDocument doc2 = new XmlDataDocument (ds);
93 public void TestLoadXml ()
95 XmlDataDocument doc = new XmlDataDocument ();
96 doc.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");
98 doc = new XmlDataDocument ();
99 doc.LoadXml ("<test>value</test>");
103 public void TestCreateElementAndRow ()
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
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
123 // document element - no mapped row
\r
124 XmlElement el = doc.DocumentElement;
\r
125 AssertNull (doc.GetRowFromElement (el));
\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
133 // col1 - it is column. no mapped row
\r
134 el = el.FirstChild as XmlElement;
\r
135 row = doc.GetRowFromElement (el);
\r
138 // col2 - it is column. np mapped row
\r
139 el = el.NextSibling as XmlElement;
\r
140 row = doc.GetRowFromElement (el);
\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
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
155 // adding a node before setting EnforceConstraints
\r
158 doc.DocumentElement.AppendChild (el);
\r
159 Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");
\r
160 } catch (InvalidOperationException) {
\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
171 // Irrelevant element
\r
172 XmlElement el2 = doc.CreateElement ("hoge");
\r
173 row = doc.GetRowFromElement (el2);
\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
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
190 public void TypedDataDocument ()
192 string xml = @"<top xmlns=""urn:test"">
195 <d>2004-02-14T10:37:03</d>
199 <d>2004-02-17T12:41:49</d>
202 string xmlschema = @"<xs:schema id=""webstore"" targetNamespace=""urn:test"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
203 <xs:element name=""top"">
205 <xs:sequence maxOccurs=""unbounded"">
206 <xs:element name=""foo"">
208 <xs:sequence maxOccurs=""unbounded"">
209 <xs:element name=""s"" type=""xs:string""/>
210 <xs:element name=""d"" type=""xs:dateTime""/>
218 XmlDataDocument doc = new XmlDataDocument ();
219 doc.DataSet.ReadXmlSchema (new StringReader (xmlschema));
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 ());