PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data/Test/System.Data/DataTableCollectionTest.cs

https://github.com/iainlane/mono
C# | 402 lines | 313 code | 39 blank | 50 comment | 0 complexity | 47df93df74463a1060ca4952a7a4f91f MD5 | raw file
  1. // DataTableCollectionTest.cs - NUnit Test Cases for for testing the DataTableCollection
  2. // class
  3. // Author:
  4. // Punit Kumar Todi ( punit_todi@da-iict.org )
  5. //
  6. // (C) Punit Todi
  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 NUnit.Framework;
  30. using System;
  31. using System.Data;
  32. namespace MonoTests.System.Data
  33. {
  34. [TestFixture]
  35. public class DataTableCollectionTest : Assertion {
  36. // common variables here
  37. private DataSet [] _dataset;
  38. private DataTable [] _tables;
  39. [SetUp]
  40. public void GetReady()
  41. {
  42. // setting up dataset && tables
  43. _dataset = new DataSet[2];
  44. _tables = new DataTable[2];
  45. _dataset[0] = new DataSet();
  46. _dataset[1] = new DataSet();
  47. _tables[0] = new DataTable("Books");
  48. _tables[0].Columns.Add("id",typeof(int));
  49. _tables[0].Columns.Add("name",typeof(String));
  50. _tables[0].Columns.Add("author",typeof(String));
  51. _tables[1] = new DataTable("Category");
  52. _tables[1].Columns.Add("id",typeof(int));
  53. _tables[1].Columns.Add("desc",typeof(String));
  54. }
  55. // clean up code here
  56. [TearDown]
  57. public void Clean()
  58. {
  59. _dataset[0].Tables.Clear();
  60. _dataset[1].Tables.Clear();
  61. }
  62. [Test]
  63. public void Add()
  64. {
  65. DataTableCollection tbcol = _dataset[0].Tables;
  66. tbcol.Add(_tables[0]);
  67. int i,j;
  68. i=0;
  69. foreach( DataTable table in tbcol )
  70. {
  71. AssertEquals("test#1",_tables[i].TableName,table.TableName);
  72. j=0;
  73. foreach( DataColumn column in table.Columns )
  74. {
  75. AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
  76. j++;
  77. }
  78. i++;
  79. }
  80. tbcol.Add(_tables[1]);
  81. i=0;
  82. foreach( DataTable table in tbcol )
  83. {
  84. AssertEquals("test#3",_tables[i].TableName,table.TableName);
  85. j=0;
  86. foreach( DataColumn column in table.Columns )
  87. {
  88. AssertEquals("test#4",_tables[i].Columns[j].ColumnName,column.ColumnName);
  89. j++;
  90. }
  91. i++;
  92. }
  93. }
  94. [Test]
  95. [ExpectedException(typeof(ArgumentNullException))]
  96. public void AddException1()
  97. {
  98. DataTableCollection tbcol = _dataset[0].Tables;
  99. DataTable tb = null;
  100. tbcol.Add(tb);
  101. }
  102. [Test]
  103. [ExpectedException(typeof(ArgumentException))]
  104. public void AddException2()
  105. {
  106. /* table already exist in the collection */
  107. DataTableCollection tbcol = _dataset[0].Tables;
  108. tbcol.Add(_tables[0]);
  109. tbcol.Add(_tables[0]);
  110. }
  111. [Test]
  112. [ExpectedException(typeof(DuplicateNameException))]
  113. public void AddException3()
  114. {
  115. DataTableCollection tbcol = _dataset[0].Tables;
  116. tbcol.Add(new DataTable("SameTableName"));
  117. tbcol.Add(new DataTable("SameTableName"));
  118. }
  119. [Test]
  120. [ExpectedException(typeof(DuplicateNameException))]
  121. public void AddException4()
  122. {
  123. DataTableCollection tbcol = _dataset[0].Tables;
  124. tbcol.Add("SameTableName");
  125. tbcol.Add("SameTableName");
  126. }
  127. [Test]
  128. public void Count()
  129. {
  130. DataTableCollection tbcol = _dataset[0].Tables;
  131. tbcol.Add(_tables[0]);
  132. AssertEquals("test#1",1, tbcol.Count);
  133. tbcol.Add(_tables[1]);
  134. AssertEquals("test#2",2, tbcol.Count);
  135. }
  136. [Test]
  137. public void AddRange()
  138. {
  139. DataTableCollection tbcol = _dataset[0].Tables;
  140. tbcol.Clear();
  141. /* _tables is array of type DataTable defined in Setup */
  142. tbcol.AddRange(_tables);
  143. int i,j;
  144. i=0;
  145. foreach( DataTable table in tbcol )
  146. {
  147. AssertEquals("test#1",_tables[i].TableName,table.TableName);
  148. j=0;
  149. foreach( DataColumn column in table.Columns )
  150. {
  151. AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
  152. j++;
  153. }
  154. i++;
  155. }
  156. }
  157. [Test]
  158. public void CanRemove()
  159. {
  160. DataTableCollection tbcol = _dataset[0].Tables;
  161. tbcol.Clear();
  162. /* _tables is array of DataTables defined in Setup */
  163. tbcol.AddRange(_tables);
  164. DataTable tbl = null;
  165. /* checking for a recently input table, expecting true */
  166. AssertEquals("test#1",true,tbcol.CanRemove(_tables[0]));
  167. /* trying to check with a null reference, expecting false */
  168. AssertEquals("test#2",false,tbcol.CanRemove(tbl));
  169. /* trying to check with a table that does not exist in collection, expecting false */
  170. AssertEquals("test#3",false,tbcol.CanRemove(new DataTable("newTable")));
  171. }
  172. [Test]
  173. public void Remove()
  174. {
  175. DataTableCollection tbcol = _dataset[0].Tables;
  176. tbcol.Clear();
  177. /* _tables is array of DataTables defined in Setup */
  178. tbcol.AddRange(_tables);
  179. /* removing a recently added table */
  180. int count = tbcol.Count;
  181. tbcol.Remove(_tables[0]);
  182. AssertEquals("test#1",count-1,tbcol.Count);
  183. DataTable tbl = null;
  184. /* removing a null reference. must generate an Exception */
  185. try
  186. {
  187. tbcol.Remove(tbl);
  188. Fail("Err:: tbcol.Rmove(null) must fail");
  189. }
  190. catch(Exception e)
  191. {
  192. AssertEquals ("test#2", typeof (ArgumentNullException), e.GetType());
  193. }
  194. /* removing a table that is not there in collection */
  195. try
  196. {
  197. tbcol.Remove(new DataTable("newTable"));
  198. Fail("Err:: cannot remove a table that is not there in collection");
  199. }
  200. catch(Exception e)
  201. {
  202. AssertEquals ("test#3", typeof (ArgumentException), e.GetType());
  203. }
  204. }
  205. [Test]
  206. public void Clear()
  207. {
  208. DataTableCollection tbcol = _dataset[0].Tables;
  209. tbcol.Add(_tables[0]);
  210. tbcol.Clear();
  211. AssertEquals("Test#1",0,tbcol.Count);
  212. tbcol.AddRange(new DataTable[] {_tables[0],_tables[1]});
  213. tbcol.Clear();
  214. AssertEquals("Test#2",0,tbcol.Count);
  215. }
  216. [Test]
  217. public void Contains()
  218. {
  219. DataTableCollection tbcol = _dataset[0].Tables;
  220. tbcol.Clear();
  221. /* _tables is array of DataTables defined in Setup */
  222. tbcol.AddRange(_tables);
  223. string tblname = "";
  224. /* checking for a recently input table, expecting true */
  225. AssertEquals("test#1",true,tbcol.Contains(_tables[0].TableName));
  226. /* trying to check with a empty string, expecting false */
  227. AssertEquals("test#2",false,tbcol.Contains(tblname));
  228. /* trying to check for a table that donot exist, expecting false */
  229. AssertEquals("test#3",false,tbcol.Contains("InvalidTableName"));
  230. }
  231. [Test]
  232. public void CopyTo()
  233. {
  234. DataTableCollection tbcol = _dataset[0].Tables;
  235. tbcol.Add("Table1");
  236. tbcol.Add("Table2");
  237. tbcol.Add("Table3");
  238. tbcol.Add("Table4");
  239. DataTable [] array = new DataTable[4];
  240. /* copying to the beginning of the array */
  241. tbcol.CopyTo(array,0);
  242. AssertEquals ("test#01", 4, array.Length);
  243. AssertEquals ("test#02", "Table1", array[0].TableName);
  244. AssertEquals ("test#03", "Table2", array[1].TableName);
  245. AssertEquals ("test#04", "Table3", array[2].TableName);
  246. AssertEquals ("test#05", "Table4", array[3].TableName);
  247. /* copying with in a array */
  248. DataTable [] array1 = new DataTable[6];
  249. tbcol.CopyTo(array1,2);
  250. AssertEquals("test#06",null,array1[0]);
  251. AssertEquals("test#07",null,array1[1]);
  252. AssertEquals("test#08","Table1",array1[2].TableName);
  253. AssertEquals("test#09","Table2",array1[3].TableName);
  254. AssertEquals("test#10","Table3",array1[4].TableName);
  255. AssertEquals("test#11","Table4",array1[5].TableName);
  256. }
  257. [Test]
  258. public void Equals()
  259. {
  260. DataTableCollection tbcol1 = _dataset[0].Tables;
  261. DataTableCollection tbcol2 = _dataset[1].Tables;
  262. DataTableCollection tbcol3;
  263. tbcol1.Add(_tables[0]);
  264. tbcol2.Add(_tables[1]);
  265. tbcol3 = tbcol1;
  266. AssertEquals("test#1",true,tbcol1.Equals(tbcol1));
  267. AssertEquals("test#2",true,tbcol1.Equals(tbcol3));
  268. AssertEquals("test#3",true,tbcol3.Equals(tbcol1));
  269. AssertEquals("test#4",false,tbcol1.Equals(tbcol2));
  270. AssertEquals("test#5",false,tbcol2.Equals(tbcol1));
  271. AssertEquals("test#6",true,Object.Equals(tbcol1,tbcol3));
  272. AssertEquals("test#7",true,Object.Equals(tbcol1,tbcol1));
  273. AssertEquals("test#8",false,Object.Equals(tbcol1,tbcol2));
  274. }
  275. [Test]
  276. public void IndexOf()
  277. {
  278. DataTableCollection tbcol = _dataset[0].Tables;
  279. tbcol.Add(_tables[0]);
  280. tbcol.Add("table1");
  281. tbcol.Add("table2");
  282. AssertEquals("test#1",0,tbcol.IndexOf(_tables[0]));
  283. AssertEquals("test#2",-1,tbcol.IndexOf(_tables[1]));
  284. AssertEquals("test#3",1,tbcol.IndexOf("table1"));
  285. AssertEquals("test#4",2,tbcol.IndexOf("table2"));
  286. AssertEquals("test#5",0,tbcol.IndexOf(tbcol[0]));
  287. AssertEquals("test#6",1,tbcol.IndexOf(tbcol[1]));
  288. AssertEquals("test#7",-1,tbcol.IndexOf("_noTable_"));
  289. DataTable tb = new DataTable("new_table");
  290. AssertEquals("test#8",-1,tbcol.IndexOf(tb));
  291. }
  292. [Test]
  293. public void RemoveAt()
  294. {
  295. DataTableCollection tbcol = _dataset[0].Tables;
  296. tbcol.Add(_tables[0]);
  297. tbcol.Add("table1");
  298. try
  299. {
  300. tbcol.RemoveAt(-1);
  301. Fail("the index was out of bound: must have failed");
  302. }
  303. catch(IndexOutOfRangeException e)
  304. {
  305. }
  306. try
  307. {
  308. tbcol.RemoveAt(101);
  309. Fail("the index was out of bound: must have failed");
  310. }
  311. catch(IndexOutOfRangeException e)
  312. {
  313. }
  314. tbcol.RemoveAt (1);
  315. AssertEquals ("test#5", 1, tbcol.Count);
  316. tbcol.RemoveAt (0);
  317. AssertEquals ("test#6", 0, tbcol.Count);
  318. }
  319. [Test]
  320. #if TARGET_JVM
  321. [Ignore ("Does not work with TARGET_JVM")]
  322. #endif
  323. public void ToStringTest()
  324. {
  325. DataTableCollection tbcol = _dataset[0].Tables;
  326. tbcol.Add("Table1");
  327. tbcol.Add("Table2");
  328. tbcol.Add("Table3");
  329. AssertEquals("test#1","System.Data.DataTableCollection",tbcol.ToString());
  330. }
  331. [Test]
  332. public void TableDataSetNamespaces ()
  333. {
  334. DataTable dt = new DataTable ("dt1");
  335. AssertEquals ("#1-1", String.Empty, dt.Namespace);
  336. AssertNull ("#1-2", dt.DataSet);
  337. DataSet ds1 = new DataSet ("ds1");
  338. ds1.Tables.Add (dt);
  339. AssertEquals ("#2-1", String.Empty, dt.Namespace);
  340. AssertEquals ("#2-2", ds1, dt.DataSet);
  341. ds1.Namespace = "ns1";
  342. AssertEquals ("#3", "ns1", dt.Namespace);
  343. // back to null again
  344. ds1.Tables.Remove (dt);
  345. AssertEquals ("#4-1", String.Empty, dt.Namespace);
  346. AssertNull ("#4-2", dt.DataSet);
  347. // This table is being added to _already namespaced_
  348. // dataset.
  349. dt = new DataTable ("dt2");
  350. ds1.Tables.Add (dt);
  351. AssertEquals ("#5-1", "ns1", dt.Namespace);
  352. AssertEquals ("#5-2", ds1, dt.DataSet);
  353. ds1.Tables.Remove (dt);
  354. AssertEquals ("#6-1", String.Empty, dt.Namespace);
  355. AssertNull ("#6-2", dt.DataSet);
  356. DataSet ds2 = new DataSet ("ds2");
  357. ds2.Namespace = "ns2";
  358. ds2.Tables.Add (dt);
  359. AssertEquals ("#7-1", "ns2", dt.Namespace);
  360. AssertEquals ("#7-2", ds2, dt.DataSet);
  361. }
  362. }
  363. }