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