* roottypes.cs: Rename from tree.cs.
[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
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using NUnit.Framework;
33 using System;
34 using System.Data;
35
36 namespace MonoTests.System.Data
37 {
38
39         [TestFixture]
40         public class DataTableCollectionTest : Assertion {
41                 // common variables here
42                 private DataSet [] _dataset;
43                 private DataTable [] _tables;
44                 
45                 [SetUp]
46                 public void GetReady() 
47                 {
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));
57                         
58                         _tables[1] = new DataTable("Category");
59                         _tables[1].Columns.Add("id",typeof(int));
60                         _tables[1].Columns.Add("desc",typeof(String));
61                 }
62                 // clean up code here
63                 [TearDown]
64                 public void Clean() 
65                 {
66                         _dataset[0].Tables.Clear();
67                         _dataset[1].Tables.Clear();
68                 }
69                 [Test]
70                 public void Add()
71                 {
72                         DataTableCollection tbcol = _dataset[0].Tables;
73                         tbcol.Add(_tables[0]);
74                         int i,j;
75                         i=0;
76                         
77                         foreach( DataTable table in tbcol )
78                         {
79                                 AssertEquals("test#1",_tables[i].TableName,table.TableName);
80                                 j=0;
81                         foreach( DataColumn column in table.Columns )
82                         {
83                                         AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
84                                         j++;
85                         }
86                                 i++;
87                         }
88                         
89                         tbcol.Add(_tables[1]);
90                         i=0;
91                         foreach( DataTable table in tbcol )
92                         {
93                                 AssertEquals("test#3",_tables[i].TableName,table.TableName);
94                                 j=0;
95                         foreach( DataColumn column in table.Columns )
96                         {
97                                         AssertEquals("test#4",_tables[i].Columns[j].ColumnName,column.ColumnName);
98                                         j++;
99                         }
100                                 i++;
101                         }
102                 }
103
104                 [Test]
105                 [ExpectedException(typeof(ArgumentNullException))]
106                 public void AddException1()
107                 {
108                         DataTableCollection tbcol = _dataset[0].Tables;
109                         DataTable tb = null;
110                         tbcol.Add(tb);
111                 }
112                 
113                 [Test]
114                 [ExpectedException(typeof(ArgumentException))]
115                 public void AddException2()
116                 {
117                         /* table already exist in the collection */
118                         DataTableCollection tbcol = _dataset[0].Tables;
119                         tbcol.Add(_tables[0]);
120                         tbcol.Add(_tables[0]);
121                 }
122                 
123                 [Test]
124                 [ExpectedException(typeof(DuplicateNameException))]
125                 public void AddException3()
126                 {
127                         DataTableCollection tbcol = _dataset[0].Tables;
128                         tbcol.Add(new DataTable("SameTableName"));
129                         tbcol.Add(new DataTable("SameTableName"));
130                 }
131                 
132                 [Test]
133                 [ExpectedException(typeof(DuplicateNameException))]
134                 public void AddException4()
135                 {
136                         DataTableCollection tbcol = _dataset[0].Tables;
137                         tbcol.Add("SameTableName");
138                         tbcol.Add("SameTableName");
139                 }
140
141                 [Test]
142                 public void Count() 
143                 {
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);
149                 }
150                 
151                 [Test]
152                 public void AddRange()
153                 {
154                         DataTableCollection tbcol = _dataset[0].Tables;
155                         tbcol.Clear();
156                         /* _tables is array of type DataTable defined in Setup */
157                         tbcol.AddRange(_tables);
158                         int i,j;
159                         i=0;
160                         foreach( DataTable table in tbcol )
161                         {
162                                 AssertEquals("test#1",_tables[i].TableName,table.TableName);
163                                 j=0;
164                         foreach( DataColumn column in table.Columns )
165                         {
166                                         AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
167                                         j++;
168                         }
169                                 i++;
170                         }
171                 }
172                 
173                 [Test]
174                 public void CanRemove()
175                 {
176                         DataTableCollection tbcol = _dataset[0].Tables;
177                         tbcol.Clear();
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")));
187                 }
188                 
189                 [Test]
190                 public void Remove()
191                 {
192                         DataTableCollection tbcol = _dataset[0].Tables;
193                         tbcol.Clear();
194                         /* _tables is array of DataTables defined in Setup */
195                         tbcol.AddRange(_tables);
196                         
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 */
203                         try
204                         {
205                                 tbcol.Remove(tbl);
206                                 Fail("Err:: tbcol.Rmove(null) must fail");
207                         }
208                         catch(Exception e)
209                         {
210                                 AssertEquals ("test#2", typeof (ArgumentNullException), e.GetType());
211                         }
212                         /* removing a table that is not there in collection */
213                         try
214                         {
215                                 tbcol.Remove(new DataTable("newTable"));
216                                 Fail("Err:: cannot remove a table that is not there in collection");
217                         }
218                         catch(Exception e)
219                         {
220                                 AssertEquals ("test#3", typeof (ArgumentException), e.GetType());
221                         }
222                         
223                 }
224                 [Test]
225                 public void Clear()
226                 {
227                         DataTableCollection tbcol = _dataset[0].Tables;
228                         tbcol.Add(_tables[0]);
229                         tbcol.Clear();
230                         AssertEquals("Test#1",0,tbcol.Count);
231                         
232                         tbcol.AddRange(new DataTable[] {_tables[0],_tables[1]});
233                         tbcol.Clear();
234                         AssertEquals("Test#2",0,tbcol.Count);
235                 }
236                 [Test]
237                 public void Contains()
238                 {
239                         DataTableCollection tbcol = _dataset[0].Tables;
240                         tbcol.Clear();
241                         /* _tables is array of DataTables defined in Setup */
242                         tbcol.AddRange(_tables);
243                         string tblname = "";
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"));
250                 }
251                 
252                 [Test]
253                 public void CopyTo()
254                 {
255                         DataTableCollection tbcol = _dataset[0].Tables;
256                         tbcol.Add("Table1");                    
257                         tbcol.Add("Table2");                    
258                         tbcol.Add("Table3");                    
259                         tbcol.Add("Table4");
260
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);
269
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);                   
279                 }
280                 [Test]
281                 public void Equals()
282                 {
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]);
288                         tbcol3 = tbcol1;
289                         
290                         AssertEquals("test#1",true,tbcol1.Equals(tbcol1));
291                         AssertEquals("test#2",true,tbcol1.Equals(tbcol3));
292                         AssertEquals("test#3",true,tbcol3.Equals(tbcol1));
293                         
294                         AssertEquals("test#4",false,tbcol1.Equals(tbcol2));
295                         AssertEquals("test#5",false,tbcol2.Equals(tbcol1));
296                         
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));
300                 }
301                 [Test]
302                 public void IndexOf()
303                 {
304                         DataTableCollection tbcol = _dataset[0].Tables;
305                         tbcol.Add(_tables[0]);
306                         tbcol.Add("table1");
307                         tbcol.Add("table2");
308                         
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"));
313                         
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));
319                         
320                 }
321                 [Test]
322                 public void RemoveAt()
323                 {
324                         DataTableCollection tbcol = _dataset[0].Tables;
325                         tbcol.Add(_tables[0]);
326                         tbcol.Add("table1");
327                         
328                         try
329                         {
330                                 tbcol.RemoveAt(-1);
331                                 Fail("the index was out of bound: must have failed");
332                         }
333                         catch(IndexOutOfRangeException e)
334                         {
335                         }
336                         try
337                         {
338                                 tbcol.RemoveAt(101);
339                                 Fail("the index was out of bound: must have failed");
340                         }
341                         catch(IndexOutOfRangeException e)
342                         {
343                         }
344                         tbcol.RemoveAt (1);
345                         AssertEquals ("test#5", 1, tbcol.Count);
346                         tbcol.RemoveAt (0);
347                         AssertEquals ("test#6", 0, tbcol.Count);
348                 }
349
350                 [Test]
351 #if TARGET_JVM
352                 [Ignore ("Does not work with TARGET_JVM")]
353 #endif
354                 public void ToStringTest()
355                 {
356                         DataTableCollection tbcol = _dataset[0].Tables;
357                         tbcol.Add("Table1");
358                         tbcol.Add("Table2");
359                         tbcol.Add("Table3");
360                         AssertEquals("test#1","System.Data.DataTableCollection",tbcol.ToString());
361                 }
362
363                 [Test]
364                 public void TableDataSetNamespaces ()
365                 {
366                         DataTable dt = new DataTable ("dt1");
367                         AssertEquals ("#1-1", String.Empty, dt.Namespace);
368                         AssertNull ("#1-2", dt.DataSet);
369
370                         DataSet ds1 = new DataSet ("ds1");
371                         ds1.Tables.Add (dt);
372                         AssertEquals ("#2-1", String.Empty, dt.Namespace);
373                         AssertEquals ("#2-2", ds1, dt.DataSet);
374
375                         ds1.Namespace = "ns1";
376                         AssertEquals ("#3", "ns1", dt.Namespace);
377
378                         // back to null again
379                         ds1.Tables.Remove (dt);
380                         AssertEquals ("#4-1", String.Empty, dt.Namespace);
381                         AssertNull ("#4-2", dt.DataSet);
382
383                         // This table is being added to _already namespaced_
384                         // dataset.
385                         dt = new DataTable ("dt2");
386
387                         ds1.Tables.Add (dt);
388                         AssertEquals ("#5-1", "ns1", dt.Namespace);
389                         AssertEquals ("#5-2", ds1, dt.DataSet);
390
391                         ds1.Tables.Remove (dt);
392                         AssertEquals ("#6-1", String.Empty, dt.Namespace);
393                         AssertNull ("#6-2", dt.DataSet);
394
395                         DataSet ds2 = new DataSet ("ds2");
396                         ds2.Namespace = "ns2";
397                         ds2.Tables.Add (dt);
398                         AssertEquals ("#7-1", "ns2", dt.Namespace);
399                         AssertEquals ("#7-2", ds2, dt.DataSet);
400                 }
401         }
402 }