1 // DataTableCollectionTest.cs - NUnit Test Cases for for testing the DataTableCollection
4 // Punit Kumar Todi ( punit_todi@da-iict.org )
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 NUnit.Framework;
36 namespace MonoTests.System.Data
40 public class DataTableCollectionTest : Assertion {
41 // common variables here
42 private DataSet [] _dataset;
43 private DataTable [] _tables;
46 public void GetReady()
48 // setting up dataset && tables
49 _dataset = new DataSet[2];
50 _tables = new DataTable[2];
51 _dataset[0] = new DataSet();
52 _dataset[1] = new DataSet();
53 _tables[0] = new DataTable("Books");
54 _tables[0].Columns.Add("id",typeof(int));
55 _tables[0].Columns.Add("name",typeof(String));
56 _tables[0].Columns.Add("author",typeof(String));
58 _tables[1] = new DataTable("Category");
59 _tables[1].Columns.Add("id",typeof(int));
60 _tables[1].Columns.Add("desc",typeof(String));
66 _dataset[0].Tables.Clear();
67 _dataset[1].Tables.Clear();
72 DataTableCollection tbcol = _dataset[0].Tables;
73 tbcol.Add(_tables[0]);
77 foreach( DataTable table in tbcol )
79 AssertEquals("test#1",_tables[i].TableName,table.TableName);
81 foreach( DataColumn column in table.Columns )
83 AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
89 tbcol.Add(_tables[1]);
91 foreach( DataTable table in tbcol )
93 AssertEquals("test#3",_tables[i].TableName,table.TableName);
95 foreach( DataColumn column in table.Columns )
97 AssertEquals("test#4",_tables[i].Columns[j].ColumnName,column.ColumnName);
105 [ExpectedException(typeof(ArgumentNullException))]
106 public void AddException1()
108 DataTableCollection tbcol = _dataset[0].Tables;
114 [ExpectedException(typeof(ArgumentException))]
115 public void AddException2()
117 /* table already exist in the collection */
118 DataTableCollection tbcol = _dataset[0].Tables;
119 tbcol.Add(_tables[0]);
120 tbcol.Add(_tables[0]);
124 [ExpectedException(typeof(DuplicateNameException))]
125 public void AddException3()
127 DataTableCollection tbcol = _dataset[0].Tables;
128 tbcol.Add(new DataTable("SameTableName"));
129 tbcol.Add(new DataTable("SameTableName"));
133 [ExpectedException(typeof(DuplicateNameException))]
134 public void AddException4()
136 DataTableCollection tbcol = _dataset[0].Tables;
137 tbcol.Add("SameTableName");
138 tbcol.Add("SameTableName");
144 DataTableCollection tbcol = _dataset[0].Tables;
145 tbcol.Add(_tables[0]);
146 AssertEquals("test#1",1, tbcol.Count);
147 tbcol.Add(_tables[1]);
148 AssertEquals("test#2",2, tbcol.Count);
152 public void AddRange()
154 DataTableCollection tbcol = _dataset[0].Tables;
156 /* _tables is array of type DataTable defined in Setup */
157 tbcol.AddRange(_tables);
160 foreach( DataTable table in tbcol )
162 AssertEquals("test#1",_tables[i].TableName,table.TableName);
164 foreach( DataColumn column in table.Columns )
166 AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
174 public void CanRemove()
176 DataTableCollection tbcol = _dataset[0].Tables;
178 /* _tables is array of DataTables defined in Setup */
179 tbcol.AddRange(_tables);
180 DataTable tbl = null;
181 /* checking for a recently input table, expecting true */
182 AssertEquals("test#1",true,tbcol.CanRemove(_tables[0]));
183 /* trying to check with a null reference, expecting false */
184 AssertEquals("test#2",false,tbcol.CanRemove(tbl));
185 /* trying to check with a table that does not exist in collection, expecting false */
186 AssertEquals("test#3",false,tbcol.CanRemove(new DataTable("newTable")));
192 DataTableCollection tbcol = _dataset[0].Tables;
194 /* _tables is array of DataTables defined in Setup */
195 tbcol.AddRange(_tables);
197 /* removing a recently added table */
198 int count = tbcol.Count;
199 tbcol.Remove(_tables[0]);
200 AssertEquals("test#1",count-1,tbcol.Count);
201 DataTable tbl = null;
202 /* removing a null reference. must generate an Exception */
206 Fail("Err:: tbcol.Rmove(null) must fail");
210 AssertEquals ("test#2", typeof (ArgumentNullException), e.GetType());
212 /* removing a table that is not there in collection */
215 tbcol.Remove(new DataTable("newTable"));
216 Fail("Err:: cannot remove a table that is not there in collection");
220 AssertEquals ("test#3", typeof (ArgumentException), e.GetType());
227 DataTableCollection tbcol = _dataset[0].Tables;
228 tbcol.Add(_tables[0]);
230 AssertEquals("Test#1",0,tbcol.Count);
232 tbcol.AddRange(new DataTable[] {_tables[0],_tables[1]});
234 AssertEquals("Test#2",0,tbcol.Count);
237 public void Contains()
239 DataTableCollection tbcol = _dataset[0].Tables;
241 /* _tables is array of DataTables defined in Setup */
242 tbcol.AddRange(_tables);
244 /* checking for a recently input table, expecting true */
245 AssertEquals("test#1",true,tbcol.Contains(_tables[0].TableName));
246 /* trying to check with a empty string, expecting false */
247 AssertEquals("test#2",false,tbcol.Contains(tblname));
248 /* trying to check for a table that donot exist, expecting false */
249 AssertEquals("test#3",false,tbcol.Contains("InvalidTableName"));
255 DataTableCollection tbcol = _dataset[0].Tables;
261 DataTable [] array = new DataTable[4];
262 /* copying to the beginning of the array */
263 tbcol.CopyTo(array,0);
264 AssertEquals ("test#01", 4, array.Length);
265 AssertEquals ("test#02", "Table1", array[0].TableName);
266 AssertEquals ("test#03", "Table2", array[1].TableName);
267 AssertEquals ("test#04", "Table3", array[2].TableName);
268 AssertEquals ("test#05", "Table4", array[3].TableName);
270 /* copying with in a array */
271 DataTable [] array1 = new DataTable[6];
272 tbcol.CopyTo(array1,2);
273 AssertEquals("test#06",null,array1[0]);
274 AssertEquals("test#07",null,array1[1]);
275 AssertEquals("test#08","Table1",array1[2].TableName);
276 AssertEquals("test#09","Table2",array1[3].TableName);
277 AssertEquals("test#10","Table3",array1[4].TableName);
278 AssertEquals("test#11","Table4",array1[5].TableName);
283 DataTableCollection tbcol1 = _dataset[0].Tables;
284 DataTableCollection tbcol2 = _dataset[1].Tables;
285 DataTableCollection tbcol3;
286 tbcol1.Add(_tables[0]);
287 tbcol2.Add(_tables[1]);
290 AssertEquals("test#1",true,tbcol1.Equals(tbcol1));
291 AssertEquals("test#2",true,tbcol1.Equals(tbcol3));
292 AssertEquals("test#3",true,tbcol3.Equals(tbcol1));
294 AssertEquals("test#4",false,tbcol1.Equals(tbcol2));
295 AssertEquals("test#5",false,tbcol2.Equals(tbcol1));
297 AssertEquals("test#6",true,Object.Equals(tbcol1,tbcol3));
298 AssertEquals("test#7",true,Object.Equals(tbcol1,tbcol1));
299 AssertEquals("test#8",false,Object.Equals(tbcol1,tbcol2));
302 public void IndexOf()
304 DataTableCollection tbcol = _dataset[0].Tables;
305 tbcol.Add(_tables[0]);
309 AssertEquals("test#1",0,tbcol.IndexOf(_tables[0]));
310 AssertEquals("test#2",-1,tbcol.IndexOf(_tables[1]));
311 AssertEquals("test#3",1,tbcol.IndexOf("table1"));
312 AssertEquals("test#4",2,tbcol.IndexOf("table2"));
314 AssertEquals("test#5",0,tbcol.IndexOf(tbcol[0]));
315 AssertEquals("test#6",1,tbcol.IndexOf(tbcol[1]));
316 AssertEquals("test#7",-1,tbcol.IndexOf("_noTable_"));
317 DataTable tb = new DataTable("new_table");
318 AssertEquals("test#8",-1,tbcol.IndexOf(tb));
322 public void RemoveAt()
324 DataTableCollection tbcol = _dataset[0].Tables;
325 tbcol.Add(_tables[0]);
331 Fail("the index was out of bound: must have failed");
333 catch(IndexOutOfRangeException e)
339 Fail("the index was out of bound: must have failed");
341 catch(IndexOutOfRangeException e)
345 AssertEquals ("test#5", 1, tbcol.Count);
347 AssertEquals ("test#6", 0, tbcol.Count);
352 [Ignore ("Does not work with TARGET_JVM")]
354 public void ToStringTest()
356 DataTableCollection tbcol = _dataset[0].Tables;
360 AssertEquals("test#1","System.Data.DataTableCollection",tbcol.ToString());
364 public void TableDataSetNamespaces ()
366 DataTable dt = new DataTable ("dt1");
367 AssertEquals ("#1-1", String.Empty, dt.Namespace);
368 AssertNull ("#1-2", dt.DataSet);
370 DataSet ds1 = new DataSet ("ds1");
372 AssertEquals ("#2-1", String.Empty, dt.Namespace);
373 AssertEquals ("#2-2", ds1, dt.DataSet);
375 ds1.Namespace = "ns1";
376 AssertEquals ("#3", "ns1", dt.Namespace);
378 // back to null again
379 ds1.Tables.Remove (dt);
380 AssertEquals ("#4-1", String.Empty, dt.Namespace);
381 AssertNull ("#4-2", dt.DataSet);
383 // This table is being added to _already namespaced_
385 dt = new DataTable ("dt2");
388 AssertEquals ("#5-1", "ns1", dt.Namespace);
389 AssertEquals ("#5-2", ds1, dt.DataSet);
391 ds1.Tables.Remove (dt);
392 AssertEquals ("#6-1", String.Empty, dt.Namespace);
393 AssertNull ("#6-2", dt.DataSet);
395 DataSet ds2 = new DataSet ("ds2");
396 ds2.Namespace = "ns2";
398 AssertEquals ("#7-1", "ns2", dt.Namespace);
399 AssertEquals ("#7-2", ds2, dt.DataSet);