Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[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 {
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                                 Assert.AreEqual(_tables[i].TableName,table.TableName,"test#1");
80                                 j=0;
81                         foreach( DataColumn column in table.Columns )
82                         {
83                                         Assert.AreEqual(_tables[i].Columns[j].ColumnName,column.ColumnName,"test#2");
84                                         j++;
85                         }
86                                 i++;
87                         }
88                         
89                         tbcol.Add(_tables[1]);
90                         i=0;
91                         foreach( DataTable table in tbcol )
92                         {
93                                 Assert.AreEqual(_tables[i].TableName,table.TableName,"test#3");
94                                 j=0;
95                         foreach( DataColumn column in table.Columns )
96                         {
97                                         Assert.AreEqual(_tables[i].Columns[j].ColumnName,column.ColumnName,"test#4");
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                         Assert.AreEqual(1,tbcol.Count, "test#1");
147                         tbcol.Add(_tables[1]);
148                         Assert.AreEqual(2,tbcol.Count, "test#2");
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                                 Assert.AreEqual(_tables[i].TableName,table.TableName,"test#1");
163                                 j=0;
164                         foreach( DataColumn column in table.Columns )
165                         {
166                                         Assert.AreEqual(_tables[i].Columns[j].ColumnName,column.ColumnName,"test#2");
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                         Assert.AreEqual(true,tbcol.CanRemove(_tables[0]),"test#1");
183                         /* trying to check with a null reference, expecting false */
184                         Assert.AreEqual(false,tbcol.CanRemove(tbl),"test#2");
185                         /* trying to check with a table that does not exist in collection, expecting false */
186                         Assert.AreEqual(false,tbcol.CanRemove(new DataTable("newTable")),"test#3");
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                         Assert.AreEqual(count-1,tbcol.Count,"test#1");
201                         DataTable tbl = null;
202                         /* removing a null reference. must generate an Exception */
203                         try
204                         {
205                                 tbcol.Remove(tbl);
206                                 Assert.Fail("Err:: tbcol.Rmove(null) must fail");
207                         }
208                         catch(Exception e)
209                         {
210                                 Assert.AreEqual (typeof (ArgumentNullException), e.GetType(), "test#2");
211                         }
212                         /* removing a table that is not there in collection */
213                         try
214                         {
215                                 tbcol.Remove(new DataTable("newTable"));
216                                 Assert.Fail("Err:: cannot remove a table that is not there in collection");
217                         }
218                         catch(Exception e)
219                         {
220                                 Assert.AreEqual (typeof (ArgumentException), e.GetType(), "test#3");
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                         Assert.AreEqual(0,tbcol.Count,"Test#1");
231                         
232                         tbcol.AddRange(new DataTable[] {_tables[0],_tables[1]});
233                         tbcol.Clear();
234                         Assert.AreEqual(0,tbcol.Count,"Test#2");
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                         Assert.AreEqual(true,tbcol.Contains(_tables[0].TableName),"test#1");
246                         /* trying to check with a empty string, expecting false */
247                         Assert.AreEqual(false,tbcol.Contains(tblname),"test#2");
248                         /* trying to check for a table that donot exist, expecting false */
249                         Assert.AreEqual(false,tbcol.Contains("InvalidTableName"),"test#3");
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                         Assert.AreEqual (4, array.Length, "test#01");
265                         Assert.AreEqual ("Table1", array[0].TableName, "test#02");
266                         Assert.AreEqual ("Table2", array[1].TableName, "test#03");
267                         Assert.AreEqual ("Table3", array[2].TableName, "test#04");
268                         Assert.AreEqual ("Table4", array[3].TableName, "test#05");
269
270                         /* copying with in a array */
271                         DataTable [] array1 = new DataTable[6];
272                         tbcol.CopyTo(array1,2);
273                         Assert.AreEqual(null,array1[0],"test#06");
274                         Assert.AreEqual(null,array1[1],"test#07");
275                         Assert.AreEqual("Table1",array1[2].TableName,"test#08");
276                         Assert.AreEqual("Table2",array1[3].TableName,"test#09");
277                         Assert.AreEqual("Table3",array1[4].TableName,"test#10");
278                         Assert.AreEqual("Table4",array1[5].TableName,"test#11");                        
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                         Assert.AreEqual(true,tbcol1.Equals(tbcol1),"test#1");
291                         Assert.AreEqual(true,tbcol1.Equals(tbcol3),"test#2");
292                         Assert.AreEqual(true,tbcol3.Equals(tbcol1),"test#3");
293                         
294                         Assert.AreEqual(false,tbcol1.Equals(tbcol2),"test#4");
295                         Assert.AreEqual(false,tbcol2.Equals(tbcol1),"test#5");
296                         
297                         Assert.AreEqual(true,Object.Equals(tbcol1,tbcol3),"test#6");
298                         Assert.AreEqual(true,Object.Equals(tbcol1,tbcol1),"test#7");
299                         Assert.AreEqual(false,Object.Equals(tbcol1,tbcol2),"test#8");
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                         Assert.AreEqual(0,tbcol.IndexOf(_tables[0]),"test#1");
310                         Assert.AreEqual(-1,tbcol.IndexOf(_tables[1]),"test#2");
311                         Assert.AreEqual(1,tbcol.IndexOf("table1"),"test#3");
312                         Assert.AreEqual(2,tbcol.IndexOf("table2"),"test#4");
313                         
314                         Assert.AreEqual(0,tbcol.IndexOf(tbcol[0]),"test#5");
315                         Assert.AreEqual(1,tbcol.IndexOf(tbcol[1]),"test#6");
316                         Assert.AreEqual(-1,tbcol.IndexOf("_noTable_"),"test#7");
317                         DataTable tb = new DataTable("new_table");
318                         Assert.AreEqual(-1,tbcol.IndexOf(tb),"test#8");
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                                 Assert.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                                 Assert.Fail("the index was out of bound: must have failed");
340                         }
341                         catch(IndexOutOfRangeException e)
342                         {
343                         }
344                         tbcol.RemoveAt (1);
345                         Assert.AreEqual (1, tbcol.Count, "test#5");
346                         tbcol.RemoveAt (0);
347                         Assert.AreEqual (0, tbcol.Count, "test#6");
348                 }
349
350                 [Test]
351                 public void ToStringTest()
352                 {
353                         DataTableCollection tbcol = _dataset[0].Tables;
354                         tbcol.Add("Table1");
355                         tbcol.Add("Table2");
356                         tbcol.Add("Table3");
357                         Assert.AreEqual("System.Data.DataTableCollection",tbcol.ToString(),"test#1");
358                 }
359
360                 [Test]
361                 public void TableDataSetNamespaces ()
362                 {
363                         DataTable dt = new DataTable ("dt1");
364                         Assert.AreEqual (String.Empty, dt.Namespace, "#1-1");
365                         Assert.IsNull (dt.DataSet, "#1-2");
366
367                         DataSet ds1 = new DataSet ("ds1");
368                         ds1.Tables.Add (dt);
369                         Assert.AreEqual (String.Empty, dt.Namespace, "#2-1");
370                         Assert.AreEqual (ds1, dt.DataSet, "#2-2");
371
372                         ds1.Namespace = "ns1";
373                         Assert.AreEqual ("ns1", dt.Namespace, "#3");
374
375                         // back to null again
376                         ds1.Tables.Remove (dt);
377                         Assert.AreEqual (String.Empty, dt.Namespace, "#4-1");
378                         Assert.IsNull (dt.DataSet, "#4-2");
379
380                         // This table is being added to _already namespaced_
381                         // dataset.
382                         dt = new DataTable ("dt2");
383
384                         ds1.Tables.Add (dt);
385                         Assert.AreEqual ("ns1", dt.Namespace, "#5-1");
386                         Assert.AreEqual (ds1, dt.DataSet, "#5-2");
387
388                         ds1.Tables.Remove (dt);
389                         Assert.AreEqual (String.Empty, dt.Namespace, "#6-1");
390                         Assert.IsNull (dt.DataSet, "#6-2");
391
392                         DataSet ds2 = new DataSet ("ds2");
393                         ds2.Namespace = "ns2";
394                         ds2.Tables.Add (dt);
395                         Assert.AreEqual ("ns2", dt.Namespace, "#7-1");
396                         Assert.AreEqual (ds2, dt.DataSet, "#7-2");
397                 }
398         }
399 }