XmlDataDocumentTest2.cs 9.3 KB

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