**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / Test / System.Xml / XmlDataDocumentTest2.cs
blobd031dfb828859ad9403e7b6c355db6c45645823d
1 //
2 // XmlDataDocumentTest2.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
8 //
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:
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.
32 using System;
33 using System.Data;
34 using System.Xml;
35 using NUnit.Framework;
37 namespace MonoTests.System.Xml
39 [TestFixture]
40 public class XmlDataDocumentTest2 : Assertion
42 string xml = "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";
44 [Test]
45 [ExpectedException (typeof (ArgumentException))]
46 public void TestCtorNullArgs ()
48 new XmlDataDocument (null);
51 [Test]
52 public void TestDefaultCtor ()
54 XmlDataDocument doc = new XmlDataDocument ();
55 AssertNotNull (doc.DataSet);
56 AssertEquals ("NewDataSet", doc.DataSet.DataSetName);
59 [Test]
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);
67 doc.LoadXml (xml);
70 [Test]
71 public void TestMultipleLoadNoError ()
73 DataSet ds = new DataSet ();
74 DataTable dt = new DataTable ();
75 dt.Columns.Add ("col1");
76 ds.Tables.Add (dt);
78 XmlDataDocument doc = new XmlDataDocument (ds);
79 doc.LoadXml (xml);
82 [Test]
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);
91 [Test]
92 public void TestLoadXml ()
94 XmlDataDocument doc = new XmlDataDocument ();
95 doc.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");
98 [Test]
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");
105 ds.Tables.Add (dt);
106 DataTable dt2 = new DataTable ("child");
107 dt2.Columns.Add ("ref");
108 dt2.Columns.Add ("val");
109 ds.Tables.Add (dt2);
110 DataRelation rel = new DataRelation ("rel",
111 dt.Columns [0], dt2.Columns [0]);
112 rel.Nested = true;
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);
126 AssertNotNull (row);
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);
132 AssertNull (row);
134 // col2 - it is column. np mapped row
135 el = el.NextSibling as XmlElement;
136 row = doc.GetRowFromElement (el);
137 AssertNull (row);
139 // child - has mapped row
140 el = el.NextSibling as XmlElement;
141 row = doc.GetRowFromElement (el);
142 AssertNotNull (row);
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
152 // raises an error
153 try {
154 doc.DocumentElement.AppendChild (el);
155 Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");
156 } catch (InvalidOperationException) {
159 // try again...
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);
170 AssertNull (row);
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