XmlDataDocumentTest2.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Xml;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Xml
  34. {
  35. [TestFixture]
  36. public class XmlDataDocumentTest2 : Assertion
  37. {
  38. string xml = "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";
  39. [Test]
  40. [ExpectedException (typeof (ArgumentException))]
  41. public void TestCtorNullArgs ()
  42. {
  43. new XmlDataDocument (null);
  44. }
  45. [Test]
  46. public void TestDefaultCtor ()
  47. {
  48. XmlDataDocument doc = new XmlDataDocument ();
  49. AssertNotNull (doc.DataSet);
  50. AssertEquals ("NewDataSet", doc.DataSet.DataSetName);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (InvalidOperationException))]
  54. public void TestMultipleLoadError ()
  55. {
  56. DataSet ds = new DataSet ();
  57. ds.ReadXml (new XmlTextReader (xml, XmlNodeType.Document, null));
  58. // If there is already data element, Load() fails.
  59. XmlDataDocument doc = new XmlDataDocument (ds);
  60. doc.LoadXml (xml);
  61. }
  62. [Test]
  63. public void TestMultipleLoadNoError ()
  64. {
  65. DataSet ds = new DataSet ();
  66. DataTable dt = new DataTable ();
  67. dt.Columns.Add ("col1");
  68. ds.Tables.Add (dt);
  69. XmlDataDocument doc = new XmlDataDocument (ds);
  70. doc.LoadXml (xml);
  71. }
  72. [Test]
  73. [ExpectedException (typeof (ArgumentException))]
  74. public void TestMultipleDataDocFromDataSet ()
  75. {
  76. DataSet ds = new DataSet ();
  77. XmlDataDocument doc = new XmlDataDocument (ds);
  78. XmlDataDocument doc2 = new XmlDataDocument (ds);
  79. }
  80. [Test]
  81. public void TestLoadXml ()
  82. {
  83. XmlDataDocument doc = new XmlDataDocument ();
  84. doc.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");
  85. }
  86. [Test]
  87. public void TestCreateElementAndRow ()
  88. {
  89. DataSet ds = new DataSet ("set");
  90. DataTable dt = new DataTable ("tab1");
  91. dt.Columns.Add ("col1");
  92. dt.Columns.Add ("col2");
  93. ds.Tables.Add (dt);
  94. DataTable dt2 = new DataTable ("child");
  95. dt2.Columns.Add ("ref");
  96. dt2.Columns.Add ("val");
  97. ds.Tables.Add (dt2);
  98. DataRelation rel = new DataRelation ("rel",
  99. dt.Columns [0], dt2.Columns [0]);
  100. rel.Nested = true;
  101. ds.Relations.Add (rel);
  102. XmlDataDocument doc = new XmlDataDocument (ds);
  103. doc.LoadXml ("<set><tab1><col1>1</col1><col2/><child><ref>1</ref><val>aaa</val></child></tab1></set>");
  104. AssertEquals (1, ds.Tables [0].Rows.Count);
  105. AssertEquals (1, ds.Tables [1].Rows.Count);
  106. // document element - no mapped row
  107. XmlElement el = doc.DocumentElement;
  108. AssertNull (doc.GetRowFromElement (el));
  109. // tab1 element - has mapped row
  110. el = el.FirstChild as XmlElement;
  111. DataRow row = doc.GetRowFromElement (el);
  112. AssertNotNull (row);
  113. AssertEquals (DataRowState.Added, row.RowState);
  114. // col1 - it is column. no mapped row
  115. el = el.FirstChild as XmlElement;
  116. row = doc.GetRowFromElement (el);
  117. AssertNull (row);
  118. // col2 - it is column. np mapped row
  119. el = el.NextSibling as XmlElement;
  120. row = doc.GetRowFromElement (el);
  121. AssertNull (row);
  122. // child - has mapped row
  123. el = el.NextSibling as XmlElement;
  124. row = doc.GetRowFromElement (el);
  125. AssertNotNull (row);
  126. AssertEquals (DataRowState.Added, row.RowState);
  127. // created (detached) table 1 element (used later)
  128. el = doc.CreateElement ("tab1");
  129. row = doc.GetRowFromElement (el);
  130. AssertEquals (DataRowState.Detached, row.RowState);
  131. AssertEquals (1, dt.Rows.Count); // not added yet
  132. // adding a node before setting EnforceConstraints
  133. // raises an error
  134. try {
  135. doc.DocumentElement.AppendChild (el);
  136. Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");
  137. } catch (InvalidOperationException) {
  138. }
  139. // try again...
  140. ds.EnforceConstraints = false;
  141. AssertEquals (1, dt.Rows.Count); // not added yet
  142. doc.DocumentElement.AppendChild (el);
  143. AssertEquals (2, dt.Rows.Count); // added
  144. row = doc.GetRowFromElement (el);
  145. AssertEquals (DataRowState.Added, row.RowState); // changed
  146. // Irrelevant element
  147. XmlElement el2 = doc.CreateElement ("hoge");
  148. row = doc.GetRowFromElement (el2);
  149. AssertNull (row);
  150. // created table 2 element (used later)
  151. el = doc.CreateElement ("child");
  152. row = doc.GetRowFromElement (el);
  153. AssertEquals (DataRowState.Detached, row.RowState);
  154. // Adding it to irrelevant element performs no row state change.
  155. AssertEquals (1, dt2.Rows.Count); // not added yet
  156. el2.AppendChild (el);
  157. AssertEquals (1, dt2.Rows.Count); // still not added
  158. row = doc.GetRowFromElement (el);
  159. AssertEquals (DataRowState.Detached, row.RowState); // still detached here
  160. }
  161. }
  162. }