(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / Test / System.Data / DataTableCollectionTest.cs
blobb0fa4ac1bedc7158d88414ca6eda5236412d9e8e
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
8 //
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:
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.
32 using NUnit.Framework;
33 using System;
34 using System.Data;
36 namespace MonoTests.System.Data
39 [TestFixture]
40 public class DataTableCollectionTest : Assertion {
41 // common variables here
42 private DataSet [] _dataset;
43 private DataTable [] _tables;
45 [SetUp]
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));
62 // clean up code here
63 [TearDown]
64 public void Clean()
66 _dataset[0].Tables.Clear();
67 _dataset[1].Tables.Clear();
69 [Test]
70 public void Add()
72 DataTableCollection tbcol = _dataset[0].Tables;
73 tbcol.Add(_tables[0]);
74 int i,j;
75 i=0;
77 foreach( DataTable table in tbcol )
79 AssertEquals("test#1",_tables[i].TableName,table.TableName);
80 j=0;
81 foreach( DataColumn column in table.Columns )
83 AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
84 j++;
86 i++;
89 tbcol.Add(_tables[1]);
90 i=0;
91 foreach( DataTable table in tbcol )
93 AssertEquals("test#3",_tables[i].TableName,table.TableName);
94 j=0;
95 foreach( DataColumn column in table.Columns )
97 AssertEquals("test#4",_tables[i].Columns[j].ColumnName,column.ColumnName);
98 j++;
100 i++;
104 [Test]
105 [ExpectedException(typeof(ArgumentNullException))]
106 public void AddException1()
108 DataTableCollection tbcol = _dataset[0].Tables;
109 DataTable tb = null;
110 tbcol.Add(tb);
113 [Test]
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]);
123 [Test]
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"));
132 [Test]
133 [ExpectedException(typeof(DuplicateNameException))]
134 public void AddException4()
136 DataTableCollection tbcol = _dataset[0].Tables;
137 tbcol.Add("SameTableName");
138 tbcol.Add("SameTableName");
141 [Test]
142 public void Count()
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);
151 [Test]
152 public void AddRange()
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 )
162 AssertEquals("test#1",_tables[i].TableName,table.TableName);
163 j=0;
164 foreach( DataColumn column in table.Columns )
166 AssertEquals("test#2",_tables[i].Columns[j].ColumnName,column.ColumnName);
167 j++;
169 i++;
173 [Test]
174 public void CanRemove()
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")));
189 [Test]
190 public void Remove()
192 DataTableCollection tbcol = _dataset[0].Tables;
193 tbcol.Clear();
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 */
205 tbcol.Remove(tbl);
206 Fail("Err:: tbcol.Rmove(null) must fail");
208 catch(Exception e)
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");
218 catch(Exception e)
220 AssertEquals ("test#3", typeof (ArgumentException), e.GetType());
224 [Test]
225 public void Clear()
227 DataTableCollection tbcol = _dataset[0].Tables;
228 tbcol.Add(_tables[0]);
229 tbcol.Clear();
230 AssertEquals("Test#1",0,tbcol.Count);
232 tbcol.AddRange(new DataTable[] {_tables[0],_tables[1]});
233 tbcol.Clear();
234 AssertEquals("Test#2",0,tbcol.Count);
236 [Test]
237 public void Contains()
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"));
252 [Test]
253 public void CopyTo()
255 DataTableCollection tbcol = _dataset[0].Tables;
256 tbcol.Add("Table1");
257 tbcol.Add("Table2");
258 tbcol.Add("Table3");
259 tbcol.Add("Table4");
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);
280 [Test]
281 public void Equals()
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;
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));
301 [Test]
302 public void IndexOf()
304 DataTableCollection tbcol = _dataset[0].Tables;
305 tbcol.Add(_tables[0]);
306 tbcol.Add("table1");
307 tbcol.Add("table2");
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));
321 [Test]
322 public void RemoveAt()
324 DataTableCollection tbcol = _dataset[0].Tables;
325 tbcol.Add(_tables[0]);
326 tbcol.Add("table1");
330 tbcol.RemoveAt(-1);
331 Fail("the index was out of bound: must have failed");
333 catch(Exception e)
335 AssertEquals ("test#1", typeof (ArgumentException), e.GetType ());
336 AssertEquals ("test#2", "There is no row at position -1.", e.Message);
340 tbcol.RemoveAt(101);
341 Fail("the index was out of bound: must have failed");
343 catch(Exception e)
345 AssertEquals ("test#3", typeof (ArgumentException), e.GetType ());
346 AssertEquals ("test#4", "There is no row at position 101.", e.Message);
348 tbcol.RemoveAt (1);
349 AssertEquals ("test#5", 1, tbcol.Count);
350 tbcol.RemoveAt (0);
351 AssertEquals ("test#6", 0, tbcol.Count);
354 [Test]
355 public void ToStringTest()
357 DataTableCollection tbcol = _dataset[0].Tables;
358 tbcol.Add("Table1");
359 tbcol.Add("Table2");
360 tbcol.Add("Table3");
361 AssertEquals("test#1","System.Data.DataTableCollection",tbcol.ToString());
364 [Test]
365 public void TableDataSetNamespaces ()
367 DataTable dt = new DataTable ("dt1");
368 AssertEquals ("#1-1", String.Empty, dt.Namespace);
369 AssertNull ("#1-2", dt.DataSet);
371 DataSet ds1 = new DataSet ("ds1");
372 ds1.Tables.Add (dt);
373 AssertEquals ("#2-1", String.Empty, dt.Namespace);
374 AssertEquals ("#2-2", ds1, dt.DataSet);
376 ds1.Namespace = "ns1";
377 AssertEquals ("#3", "ns1", dt.Namespace);
379 // back to null again
380 ds1.Tables.Remove (dt);
381 AssertEquals ("#4-1", String.Empty, dt.Namespace);
382 AssertNull ("#4-2", dt.DataSet);
384 // This table is being added to _already namespaced_
385 // dataset.
386 dt = new DataTable ("dt2");
388 ds1.Tables.Add (dt);
389 AssertEquals ("#5-1", "ns1", dt.Namespace);
390 AssertEquals ("#5-2", ds1, dt.DataSet);
392 ds1.Tables.Remove (dt);
393 AssertEquals ("#6-1", String.Empty, dt.Namespace);
394 AssertNull ("#6-2", dt.DataSet);
396 DataSet ds2 = new DataSet ("ds2");
397 ds2.Namespace = "ns2";
398 ds2.Tables.Add (dt);
399 AssertEquals ("#7-1", "ns2", dt.Namespace);
400 AssertEquals ("#7-2", ds2, dt.DataSet);