DataTableReadWriteXmlTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Author:
  2. // Patrick Earl <[email protected]>
  3. //
  4. // Copyright (c) 2006
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #if NET_2_0
  26. using System;
  27. using System.Data;
  28. using System.IO;
  29. using System.Text.RegularExpressions;
  30. using System.Xml;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Data
  33. {
  34. [TestFixture]
  35. public class DataTableReadWriteXmlTest
  36. {
  37. void StandardizeXmlFormat(ref string xml)
  38. {
  39. XmlDocument doc = new XmlDocument();
  40. doc.LoadXml(xml);
  41. StringWriter sw = new StringWriter();
  42. doc.Save(sw);
  43. xml = sw.ToString();
  44. }
  45. void GenerateTestData(out DataSet ds,
  46. out DataTable dtMainInDS,
  47. out DataTable dtChildInDS,
  48. out DataTable dtMain)
  49. {
  50. ds = new DataSet("MyDataSet");
  51. // Create a primary table and populate it with some data. Make a
  52. // copy of the primary table and put it into the dataset.
  53. dtMain = new DataTable("Main");
  54. dtMain.Columns.Add(new DataColumn("ID", typeof(int)));
  55. dtMain.Columns.Add(new DataColumn("Data", typeof(string)));
  56. DataRow row = dtMain.NewRow();
  57. row["ID"] = 1;
  58. row["Data"] = "One";
  59. dtMain.Rows.Add(row);
  60. row = dtMain.NewRow();
  61. row["ID"] = 2;
  62. row["Data"] = "Two";
  63. dtMain.Rows.Add(row);
  64. row = dtMain.NewRow();
  65. row["ID"] = 3;
  66. row["Data"] = "Three";
  67. dtMain.Rows.Add(row);
  68. dtMainInDS = dtMain.Copy();
  69. ds.Tables.Add(dtMainInDS);
  70. // Create a child table. Make a copy of the child table and put
  71. // it into the dataset.
  72. dtChildInDS = new DataTable("Child");
  73. dtChildInDS.Columns.Add(new DataColumn("ID", typeof(int)));
  74. dtChildInDS.Columns.Add(new DataColumn("PID", typeof(int)));
  75. dtChildInDS.Columns.Add(new DataColumn("ChildData", typeof(string)));
  76. row = dtChildInDS.NewRow();
  77. row["ID"] = 1;
  78. row["PID"] = 1;
  79. row["ChildData"] = "Parent1Child1";
  80. dtChildInDS.Rows.Add(row);
  81. row = dtChildInDS.NewRow();
  82. row["ID"] = 2;
  83. row["PID"] = 1;
  84. row["ChildData"] = "Parent1Child2";
  85. dtChildInDS.Rows.Add(row);
  86. row = dtChildInDS.NewRow();
  87. row["ID"] = 3;
  88. row["PID"] = 2;
  89. row["ChildData"] = "Parent2Child3";
  90. dtChildInDS.Rows.Add(row);
  91. ds.Tables.Add(dtChildInDS);
  92. // Set up the relation in the dataset.
  93. ds.Relations.Add(new DataRelation("MainToChild",
  94. dtMainInDS.Columns["ID"],
  95. dtChildInDS.Columns["PID"]));
  96. }
  97. [Test]
  98. public void TestWriteXml()
  99. {
  100. DataSet ds;
  101. DataTable dtMainInDS, dtChildInDS, dtMain;
  102. GenerateTestData(out ds,
  103. out dtMainInDS,
  104. out dtChildInDS,
  105. out dtMain);
  106. StringWriter sw = new StringWriter();
  107. // Get XML for DataSet writes.
  108. sw.GetStringBuilder().Length = 0;
  109. ds.WriteXml(sw);
  110. string xmlDSNone = sw.ToString();
  111. sw.GetStringBuilder().Length = 0;
  112. ds.WriteXml(sw, XmlWriteMode.DiffGram);
  113. string xmlDSDiffGram = sw.ToString();
  114. sw.GetStringBuilder().Length = 0;
  115. ds.WriteXml(sw, XmlWriteMode.WriteSchema);
  116. string xmlDSWriteSchema = sw.ToString();
  117. // Get XML for recursive DataTable writes of the same data as in
  118. // the DataSet.
  119. sw.GetStringBuilder().Length = 0;
  120. dtMainInDS.WriteXml(sw, true);
  121. string xmlDTNone = sw.ToString();
  122. sw.GetStringBuilder().Length = 0;
  123. dtMainInDS.WriteXml(sw, XmlWriteMode.DiffGram, true);
  124. string xmlDTDiffGram = sw.ToString();
  125. sw.GetStringBuilder().Length = 0;
  126. dtMainInDS.WriteXml(sw, XmlWriteMode.WriteSchema, true);
  127. string xmlDTWriteSchema = sw.ToString();
  128. // The schema XML written by the DataTable call has an extra element
  129. // in the element for the dataset schema definition. We remove that
  130. // extra attribute and then check to see if the rest of the xml is
  131. // identical.
  132. XmlDocument doc = new XmlDocument();
  133. doc.LoadXml(xmlDTWriteSchema);
  134. XmlNode node = doc.DocumentElement.FirstChild.FirstChild;
  135. XmlAttribute a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
  136. Assert.IsNotNull(a, "Test#01");
  137. Assert.AreEqual("Main", a.Value, "Test#02");
  138. node.Attributes.Remove(a);
  139. sw.GetStringBuilder().Length = 0;
  140. doc.Save(sw);
  141. xmlDTWriteSchema = sw.ToString();
  142. StandardizeXmlFormat(ref xmlDSWriteSchema);
  143. Assert.AreEqual(xmlDSNone, xmlDTNone, "Test#03");
  144. Assert.AreEqual(xmlDSDiffGram, xmlDTDiffGram, "Test#04");
  145. Assert.AreEqual(xmlDSWriteSchema, xmlDTWriteSchema, "Test#05");
  146. // Now that we've tested writing tables (including children),
  147. // we will go on to test the cases where the hierarchy flag
  148. // is false. For this, we will test one table inside the
  149. // dataset and one table outside the dataset.
  150. // First, we fix our test DataSet to only have a single table
  151. // with no relations. Then, we go about comparing the XML.
  152. // Get XML for DataSet writes.
  153. ds.Tables[1].Constraints.Remove(ds.Tables[1].Constraints[0]);
  154. ds.Tables[0].Constraints.Remove(ds.Tables[0].Constraints[0]);
  155. ds.Tables[0].ChildRelations.Remove("MainToChild");
  156. ds.Tables.Remove("Child");
  157. sw.GetStringBuilder().Length = 0;
  158. ds.WriteXml(sw);
  159. xmlDSNone = sw.ToString();
  160. sw.GetStringBuilder().Length = 0;
  161. ds.WriteXml(sw, XmlWriteMode.DiffGram);
  162. xmlDSDiffGram = sw.ToString();
  163. sw.GetStringBuilder().Length = 0;
  164. ds.WriteXml(sw, XmlWriteMode.WriteSchema);
  165. xmlDSWriteSchema = sw.ToString();
  166. // Get all the DataTable.WriteXml results.
  167. sw.GetStringBuilder().Length = 0;
  168. dtMainInDS.WriteXml(sw);
  169. string xmlDTNoneInDS = sw.ToString();
  170. sw.GetStringBuilder().Length = 0;
  171. dtMainInDS.WriteXml(sw, XmlWriteMode.DiffGram);
  172. string xmlDTDiffGramInDS = sw.ToString();
  173. sw.GetStringBuilder().Length = 0;
  174. dtMainInDS.WriteXml(sw, XmlWriteMode.WriteSchema);
  175. string xmlDTWriteSchemaInDS = sw.ToString();
  176. sw.GetStringBuilder().Length = 0;
  177. dtMain.WriteXml(sw);
  178. string xmlDTNoneNoDS = sw.ToString();
  179. sw.GetStringBuilder().Length = 0;
  180. dtMain.WriteXml(sw, XmlWriteMode.DiffGram);
  181. string xmlDTDiffGramNoDS = sw.ToString();
  182. sw.GetStringBuilder().Length = 0;
  183. dtMain.WriteXml(sw, XmlWriteMode.WriteSchema);
  184. string xmlDTWriteSchemaNoDS = sw.ToString();
  185. Assert.AreEqual(xmlDSNone, xmlDTNoneInDS, "Test#06");
  186. // The only difference between the xml output from inside the
  187. // dataset and the xml output from outside the dataset is that
  188. // there's a fake <DocumentElement> tag surrounding tbe table
  189. // in the second case. We replace it with the name of the
  190. // dataset for testing purposes.
  191. doc.LoadXml(xmlDTNoneNoDS);
  192. Assert.AreEqual("DocumentElement", doc.DocumentElement.Name, "Test#07");
  193. sw.GetStringBuilder().Length = 0;
  194. doc.Save(sw);
  195. xmlDTNoneNoDS = sw.ToString();
  196. xmlDTNoneNoDS = xmlDTNoneNoDS.Replace("<DocumentElement>", "<MyDataSet>");
  197. xmlDTNoneNoDS = xmlDTNoneNoDS.Replace("</DocumentElement>", "</MyDataSet>");
  198. StandardizeXmlFormat(ref xmlDSNone);
  199. Assert.AreEqual(xmlDSNone, xmlDTNoneNoDS, "Test#08");
  200. // Now check the DiffGram.
  201. Assert.AreEqual(xmlDSDiffGram, xmlDTDiffGramInDS, "Test#09");
  202. doc.LoadXml(xmlDTDiffGramNoDS);
  203. Assert.AreEqual("DocumentElement", doc.DocumentElement.FirstChild.Name, "Test#10");
  204. xmlDTDiffGramNoDS = xmlDTDiffGramNoDS.Replace("<DocumentElement>", "<MyDataSet>");
  205. xmlDTDiffGramNoDS = xmlDTDiffGramNoDS.Replace("</DocumentElement>", "</MyDataSet>");
  206. Assert.AreEqual(xmlDSDiffGram, xmlDTDiffGramNoDS, "Test#11");
  207. // Finally we check the WriteSchema version of the data. First
  208. // we remove the extra "msdata:MainDataTable" attribute from
  209. // the schema declaration part of the DataTable xml.
  210. doc = new XmlDocument();
  211. doc.LoadXml(xmlDTWriteSchemaInDS);
  212. node = doc.DocumentElement.FirstChild.FirstChild;
  213. a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
  214. Assert.IsNotNull(a, "Test#12");
  215. Assert.AreEqual("Main", a.Value, "Test#13");
  216. node.Attributes.Remove(a);
  217. sw.GetStringBuilder().Length = 0;
  218. doc.Save(sw);
  219. xmlDTWriteSchemaInDS = sw.ToString();
  220. StandardizeXmlFormat(ref xmlDSWriteSchema);
  221. Assert.AreEqual(xmlDSWriteSchema, xmlDTWriteSchemaInDS, "Test#14");
  222. // Remove the extra "msdata:MainDataTable" for the other test case.
  223. // Also make sure we have "NewDataSet" in the appropriate locations.
  224. doc = new XmlDocument();
  225. doc.LoadXml(xmlDTWriteSchemaNoDS);
  226. node = doc.DocumentElement.FirstChild.FirstChild;
  227. a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
  228. Assert.IsNotNull(a, "Test#15");
  229. Assert.AreEqual("Main", a.Value, "Test#16");
  230. node.Attributes.Remove(a);
  231. sw.GetStringBuilder().Length = 0;
  232. doc.Save(sw);
  233. Assert.AreEqual("NewDataSet", doc.DocumentElement.Name, "Test#17");
  234. Assert.AreEqual("NewDataSet", doc.DocumentElement.FirstChild.Attributes["id"].Value, "Test#18");
  235. Assert.AreEqual("NewDataSet", doc.DocumentElement.FirstChild.FirstChild.Attributes["name"].Value, "Test#19");
  236. xmlDTWriteSchemaNoDS = sw.ToString();
  237. xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("<NewDataSet>","<MyDataSet>");
  238. xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("</NewDataSet>","</MyDataSet>");
  239. xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("\"NewDataSet\"","\"MyDataSet\"");
  240. Assert.AreEqual(xmlDSWriteSchema, xmlDTWriteSchemaNoDS, "Test#20");
  241. }
  242. [Test]
  243. [ExpectedException (typeof (NotImplementedException))]
  244. #if TARGET_JVM
  245. [Ignore ("Should review the test")]
  246. #endif
  247. public void TestReadXml()
  248. {
  249. // For reading, DataTable.ReadXml only supports reading in xml with
  250. // the schema included. This means that we can only read in XML
  251. // that was generated with the WriteSchema flag.
  252. DataSet ds;
  253. DataTable dtMainInDS, dtChildInDS, dtMain;
  254. GenerateTestData(out ds,
  255. out dtMainInDS,
  256. out dtChildInDS,
  257. out dtMain);
  258. StringWriter sw = new StringWriter();
  259. // Get XML for recursive DataTable writes of the same data as in
  260. // the DataSet.
  261. sw.GetStringBuilder().Length = 0;
  262. dtMainInDS.WriteXml(sw, true);
  263. string xmlDTNone = sw.ToString();
  264. sw.GetStringBuilder().Length = 0;
  265. dtMainInDS.WriteXml(sw, XmlWriteMode.DiffGram, true);
  266. string xmlDTDiffGram = sw.ToString();
  267. sw.GetStringBuilder().Length = 0;
  268. dtMainInDS.WriteXml(sw, XmlWriteMode.WriteSchema, true);
  269. string xmlMultiTable = sw.ToString();
  270. sw.GetStringBuilder().Length = 0;
  271. dtMain.WriteXml(sw, XmlWriteMode.WriteSchema);
  272. string xmlSingleTable = sw.ToString();
  273. DataTable newdt = new DataTable();
  274. try {
  275. newdt.ReadXml(new StringReader(xmlDTNone));
  276. Assert.Fail("Test#01");
  277. } catch(InvalidOperationException) {
  278. // DataTable does not support schema inference from Xml.
  279. }
  280. try {
  281. newdt.ReadXml(new StringReader(xmlDTDiffGram));
  282. Assert.Fail("Test#02");
  283. } catch(InvalidOperationException) {
  284. // DataTable does not support schema inference from Xml.
  285. }
  286. DataTable multiTable = new DataTable();
  287. multiTable.ReadXml(new StringReader(xmlMultiTable));
  288. // Do some simple checks to see if the main dataset was created
  289. // and if there are relationships present.
  290. Assert.AreEqual("MyDataSet", multiTable.DataSet.DataSetName, "Test#03");
  291. Assert.AreEqual(1, multiTable.ChildRelations.Count, "Test#04");
  292. Assert.AreEqual(1, multiTable.Constraints.Count, "Test#05");
  293. // Write the table back out and check to see that the XML is
  294. // the same as before.
  295. sw.GetStringBuilder().Length = 0;
  296. multiTable.WriteXml(sw, XmlWriteMode.WriteSchema, true);
  297. string xmlMultiTableCheck = sw.ToString();
  298. Assert.AreEqual(xmlMultiTable, xmlMultiTableCheck, "Test#06");
  299. DataTable singleTable = new DataTable();
  300. singleTable.ReadXml(new StringReader(xmlSingleTable));
  301. // Do some simple checks on the table.
  302. Assert.IsNull(singleTable.DataSet, "Test#07");
  303. Assert.AreEqual("Main", singleTable.TableName, "Test#08");
  304. // Write the table out and check if it's the same.
  305. sw.GetStringBuilder().Length = 0;
  306. singleTable.WriteXml(sw, XmlWriteMode.WriteSchema);
  307. string xmlSingleTableCheck = sw.ToString();
  308. Assert.AreEqual(xmlSingleTable, xmlSingleTableCheck, "Test#09");
  309. }
  310. }
  311. }
  312. #endif