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.
35 using NUnit
.Framework
;
37 namespace MonoTests
.System
.Xml
40 public class XmlDataDocumentTest2
: Assertion
42 string xml
= "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";
45 [ExpectedException (typeof (ArgumentException
))]
46 public void TestCtorNullArgs ()
48 new XmlDataDocument (null);
52 public void TestDefaultCtor ()
54 XmlDataDocument doc
= new XmlDataDocument ();
55 AssertNotNull (doc
.DataSet
);
56 AssertEquals ("NewDataSet", doc
.DataSet
.DataSetName
);
60 [ExpectedException (typeof (InvalidOperationException
))]
61 public void TestMultipleLoadError ()
63 DataSet ds
= new DataSet ();
64 ds
.ReadXml (new XmlTextReader (xml
, XmlNodeType
.Document
, null));
65 // If there is already data element, Load() fails.
66 XmlDataDocument doc
= new XmlDataDocument (ds
);
71 public void TestMultipleLoadNoError ()
73 DataSet ds
= new DataSet ();
74 DataTable dt
= new DataTable ();
75 dt
.Columns
.Add ("col1");
78 XmlDataDocument doc
= new XmlDataDocument (ds
);
83 [ExpectedException (typeof (ArgumentException
))]
84 public void TestMultipleDataDocFromDataSet ()
86 DataSet ds
= new DataSet ();
87 XmlDataDocument doc
= new XmlDataDocument (ds
);
88 XmlDataDocument doc2
= new XmlDataDocument (ds
);
92 public void TestLoadXml ()
94 XmlDataDocument doc
= new XmlDataDocument ();
95 doc
.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");
99 public void TestCreateElementAndRow ()
101 DataSet ds
= new DataSet ("set");
102 DataTable dt
= new DataTable ("tab1");
103 dt
.Columns
.Add ("col1");
104 dt
.Columns
.Add ("col2");
106 DataTable dt2
= new DataTable ("child");
107 dt2
.Columns
.Add ("ref");
108 dt2
.Columns
.Add ("val");
110 DataRelation rel
= new DataRelation ("rel",
111 dt
.Columns
[0], dt2
.Columns
[0]);
113 ds
.Relations
.Add (rel
);
114 XmlDataDocument doc
= new XmlDataDocument (ds
);
115 doc
.LoadXml ("<set><tab1><col1>1</col1><col2/><child><ref>1</ref><val>aaa</val></child></tab1></set>");
116 AssertEquals (1, ds
.Tables
[0].Rows
.Count
);
117 AssertEquals (1, ds
.Tables
[1].Rows
.Count
);
119 // document element - no mapped row
120 XmlElement el
= doc
.DocumentElement
;
121 AssertNull (doc
.GetRowFromElement (el
));
123 // tab1 element - has mapped row
124 el
= el
.FirstChild
as XmlElement
;
125 DataRow row
= doc
.GetRowFromElement (el
);
127 AssertEquals (DataRowState
.Added
, row
.RowState
);
129 // col1 - it is column. no mapped row
130 el
= el
.FirstChild
as XmlElement
;
131 row
= doc
.GetRowFromElement (el
);
134 // col2 - it is column. np mapped row
135 el
= el
.NextSibling
as XmlElement
;
136 row
= doc
.GetRowFromElement (el
);
139 // child - has mapped row
140 el
= el
.NextSibling
as XmlElement
;
141 row
= doc
.GetRowFromElement (el
);
143 AssertEquals (DataRowState
.Added
, row
.RowState
);
145 // created (detached) table 1 element (used later)
146 el
= doc
.CreateElement ("tab1");
147 row
= doc
.GetRowFromElement (el
);
148 AssertEquals (DataRowState
.Detached
, row
.RowState
);
149 AssertEquals (1, dt
.Rows
.Count
); // not added yet
151 // adding a node before setting EnforceConstraints
154 doc
.DocumentElement
.AppendChild (el
);
155 Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");
156 } catch (InvalidOperationException
) {
160 ds
.EnforceConstraints
= false;
161 AssertEquals (1, dt
.Rows
.Count
); // not added yet
162 doc
.DocumentElement
.AppendChild (el
);
163 AssertEquals (2, dt
.Rows
.Count
); // added
164 row
= doc
.GetRowFromElement (el
);
165 AssertEquals (DataRowState
.Added
, row
.RowState
); // changed
167 // Irrelevant element
168 XmlElement el2
= doc
.CreateElement ("hoge");
169 row
= doc
.GetRowFromElement (el2
);
172 // created table 2 element (used later)
173 el
= doc
.CreateElement ("child");
174 row
= doc
.GetRowFromElement (el
);
175 AssertEquals (DataRowState
.Detached
, row
.RowState
);
177 // Adding it to irrelevant element performs no row state change.
178 AssertEquals (1, dt2
.Rows
.Count
); // not added yet
179 el2
.AppendChild (el
);
180 AssertEquals (1, dt2
.Rows
.Count
); // still not added
181 row
= doc
.GetRowFromElement (el
);
182 AssertEquals (DataRowState
.Detached
, row
.RowState
); // still detached here