2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DataTableMappingCollectionTest.cs
1 // DataTableMappingCollectionTest.cs - NUnit Test Cases for Testing the 
2 // DataTableMappingCollection class
3 //
4 // Author: Ameya Sailesh Gargesh (ameya_13@yahoo.com)
5 //
6 // (C) Ameya Gargesh
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 using System.Data.Common;
36 namespace MonoTests.System.Data.Common
37 {
38         [TestFixture]
39         public class DataTableMappingCollectionTest : Assertion
40         {
41                 DataTableMappingCollection tableMapCollection;
42                 DataTableMapping [] tabs;
43                 [SetUp]
44                 public void GetReady()
45                 {
46                         tabs=new DataTableMapping[5];
47                         tabs[0]=new DataTableMapping("sourceCustomers","dataSetCustomers");
48                         tabs[1]=new DataTableMapping("sourceEmployees","dataSetEmployees");
49                         tabs[2]=new DataTableMapping("sourceBooks","dataSetBooks");
50                         tabs[3]=new DataTableMapping("sourceStore","dataSetStore");
51                         tabs[4]=new DataTableMapping("sourceInventory","dataSetInventory");
52                         tableMapCollection=new DataTableMappingCollection();
53                 }
54                 
55                 [TearDown]
56                 public void Clean()
57                 {
58                         tableMapCollection.Clear();
59                 }
60                 
61                 [Test]
62                 public void Add()
63                 {
64                         int t=tableMapCollection.Add((Object)tabs[0]);
65                         AssertEquals("test1",0,t);
66                         bool eq1=tabs[0].Equals(tableMapCollection[0]);
67                         AssertEquals("test2",true,eq1);
68                         AssertEquals("test3",1,tableMapCollection.Count);
69                         DataTableMapping tab2;
70                         tab2=tableMapCollection.Add("sourceEmployees","dataSetEmployees");
71                         bool eq2=tab2.Equals(tableMapCollection[1]);
72                         AssertEquals("test4",true,eq2);
73                         AssertEquals("test5",2,tableMapCollection.Count);
74                 }
75                 
76                 [Test]
77                 [ExpectedException(typeof(InvalidCastException))]
78                 public void AddException1()
79                 {
80                         DataTableMappingCollection c=new DataTableMappingCollection();
81                         tableMapCollection.Add((Object)c);
82                 }
83                 
84                 [Test]
85                 public void AddRange()
86                 {
87                         tableMapCollection.Add(new DataTableMapping("sourceFactory","dataSetFactory"));
88                         AssertEquals("test1",1,tableMapCollection.Count);
89                         tableMapCollection.AddRange(tabs);
90                         AssertEquals("test2",6,tableMapCollection.Count);
91                         bool eq;
92                         eq=tabs[0].Equals(tableMapCollection[1]);
93                         AssertEquals("test3",true,eq);
94                         eq=tabs[1].Equals(tableMapCollection[2]);
95                         AssertEquals("test4",true,eq);
96                         eq=tabs[0].Equals(tableMapCollection[0]);
97                         AssertEquals("test5",false,eq);
98                         eq=tabs[1].Equals(tableMapCollection[0]);
99                         AssertEquals("test6",false,eq);                 
100                 }
101                 
102                 [Test]
103                 public void Clear()
104                 {
105                         DataTableMapping tab1=new DataTableMapping("sourceSuppliers","dataSetSuppliers");
106                         tableMapCollection.Add((Object)tab1);
107                         AssertEquals("test1",1,tableMapCollection.Count);
108                         tableMapCollection.Clear();
109                         AssertEquals("test2",0,tableMapCollection.Count);
110                         tableMapCollection.AddRange(tabs);
111                         AssertEquals("test3",5,tableMapCollection.Count);
112                         tableMapCollection.Clear();
113                         AssertEquals("test4",0,tableMapCollection.Count);
114                 }
115                 
116                 [Test]
117                 public void Contains()
118                 {
119                         DataTableMapping tab1=new DataTableMapping("sourceCustomers","dataSetCustomers");
120                         tableMapCollection.AddRange(tabs);
121                         bool eq;
122                         eq=tableMapCollection.Contains((Object)tabs[0]);
123                         AssertEquals("test1",true,eq);
124                         eq=tableMapCollection.Contains((Object)tabs[1]);
125                         AssertEquals("test2",true,eq);
126                         eq=tableMapCollection.Contains((Object)tab1);
127                         AssertEquals("test3",false,eq);
128                         eq=tableMapCollection.Contains(tabs[0].SourceTable);
129                         AssertEquals("test4",true,eq);
130                         eq=tableMapCollection.Contains(tabs[1].SourceTable);
131                         AssertEquals("test5",true,eq);
132                         eq=tableMapCollection.Contains(tab1.SourceTable);
133                         AssertEquals("test6",true,eq);
134                         eq=tableMapCollection.Contains(tabs[0].DataSetTable);
135                         AssertEquals("test7",false,eq);
136                         eq=tableMapCollection.Contains(tabs[1].DataSetTable);
137                         AssertEquals("test8",false,eq);
138                         eq=tableMapCollection.Contains(tab1.DataSetTable);
139                         AssertEquals("test9",false,eq);
140                 }
141                 
142                 [Test]
143                 public void CopyTo()
144                 {
145                         DataTableMapping [] tabcops=new DataTableMapping[5];
146                         tableMapCollection.AddRange(tabs);
147                         tableMapCollection.CopyTo(tabcops,0);
148                         bool eq;
149                         for (int i=0;i<5;i++)
150                         {
151                                 eq=tableMapCollection[i].Equals(tabcops[i]);
152                                 AssertEquals("test1"+i,true,eq);
153                         }
154                         tabcops=null;
155                         tabcops=new DataTableMapping[7];
156                         tableMapCollection.CopyTo(tabcops,2);
157                         for (int i=0;i<5;i++)
158                         {
159                                 eq=tableMapCollection[i].Equals(tabcops[i+2]);
160                                 AssertEquals("test2"+i,true,eq);
161                         }
162                         eq=tableMapCollection[0].Equals(tabcops[0]);
163                         AssertEquals("test31",false,eq);
164                         eq=tableMapCollection[0].Equals(tabcops[1]);
165                         AssertEquals("test32",false,eq);
166                 }
167                 
168                 [Test]
169                 public void Equals()
170                 {
171 //                      DataTableMappingCollection collect2=new DataTableMappingCollection();
172                         tableMapCollection.AddRange(tabs);
173 //                      collect2.AddRange(tabs);
174                         DataTableMappingCollection copy1;
175                         copy1=tableMapCollection;
176                         
177 //                      AssertEquals("test1",false,tableMapCollection.Equals(collect2));
178                         AssertEquals("test2",true,tableMapCollection.Equals(copy1));
179 //                      AssertEquals("test3",false,collect2.Equals(tableMapCollection));
180                         AssertEquals("test4",true,copy1.Equals(tableMapCollection));
181 //                      AssertEquals("test5",false,collect2.Equals(copy1));
182                         AssertEquals("test6",true,copy1.Equals(tableMapCollection));
183                         AssertEquals("test7",true,tableMapCollection.Equals(tableMapCollection));
184 //                      AssertEquals("test8",true,collect2.Equals(collect2));
185                         AssertEquals("test9",true,copy1.Equals(copy1));
186                         
187 //                      AssertEquals("test10",false,Object.Equals(collect2,tableMapCollection));
188                         AssertEquals("test11",true,Object.Equals(copy1,tableMapCollection));
189 //                      AssertEquals("test12",false,Object.Equals(tableMapCollection,collect2));
190                         AssertEquals("test13",true,Object.Equals(tableMapCollection,copy1));
191 //                      AssertEquals("test14",false,Object.Equals(copy1,collect2));
192                         AssertEquals("test15",true,Object.Equals(tableMapCollection,copy1));
193                         AssertEquals("test16",true,Object.Equals(tableMapCollection,tableMapCollection));
194 //                      AssertEquals("test17",true,Object.Equals(collect2,collect2));
195                         AssertEquals("test18",true,Object.Equals(copy1,copy1));
196 //                      AssertEquals("test10",false,Object.Equals(tableMapCollection,collect2));
197                         AssertEquals("test11",true,Object.Equals(tableMapCollection,copy1));
198 //                      AssertEquals("test12",false,Object.Equals(collect2,tableMapCollection));
199                         AssertEquals("test13",true,Object.Equals(copy1,tableMapCollection));
200 //                      AssertEquals("test14",false,Object.Equals(collect2,copy1));
201                         AssertEquals("test15",true,Object.Equals(copy1,tableMapCollection));
202                 }
203                 
204                 [Test]
205                 public void GetByDataSetTable()
206                 {
207                         tableMapCollection.AddRange(tabs);
208                         bool eq;
209                         DataTableMapping tab1;
210                         tab1=tableMapCollection.GetByDataSetTable("dataSetCustomers");
211                         eq=(tab1.DataSetTable.Equals("dataSetCustomers") && tab1.SourceTable.Equals("sourceCustomers"));
212                         AssertEquals("test1",true,eq);
213                         tab1=tableMapCollection.GetByDataSetTable("dataSetEmployees");
214                         eq=(tab1.DataSetTable.Equals("dataSetEmployees") && tab1.SourceTable.Equals("sourceEmployees"));
215                         AssertEquals("test2",true,eq);
216                                                 
217                         tab1=tableMapCollection.GetByDataSetTable("datasetcustomers");
218                         eq=(tab1.DataSetTable.Equals("dataSetCustomers") && tab1.SourceTable.Equals("sourceCustomers"));
219                         AssertEquals("test3",true,eq);
220                         tab1=tableMapCollection.GetByDataSetTable("datasetemployees");
221                         eq=(tab1.DataSetTable.Equals("dataSetEmployees") && tab1.SourceTable.Equals("sourceEmployees"));
222                         AssertEquals("test4",true,eq);
223                         
224                 }
225                 
226                 [Test]
227                 public void GetTableMappingBySchemaAction()
228                 {
229                         tableMapCollection.AddRange(tabs);
230                         bool eq;
231                         DataTableMapping tab1;
232                         tab1=DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection,"sourceCustomers","dataSetCustomers",MissingMappingAction.Passthrough);
233                         eq=(tab1.DataSetTable.Equals("dataSetCustomers") && tab1.SourceTable.Equals("sourceCustomers"));
234                         AssertEquals("test1",true,eq);
235                         tab1=DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection,"sourceEmployees","dataSetEmployees",MissingMappingAction.Passthrough);
236                         eq=(tab1.DataSetTable.Equals("dataSetEmployees") && tab1.SourceTable.Equals("sourceEmployees"));
237                         AssertEquals("test2",true,eq);
238                         
239                         tab1=DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection,"sourceData","dataSetData",MissingMappingAction.Passthrough);
240                         eq=(tab1.DataSetTable.Equals("sourceData") && tab1.SourceTable.Equals("dataSetData"));
241                         AssertEquals("test3",false, eq);
242                         eq=tableMapCollection.Contains(tab1);
243                         AssertEquals("test4",false,eq);
244                         tab1=DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection,"sourceData","dataSetData",MissingMappingAction.Ignore);
245                         AssertEquals("test5",null,tab1);
246                 }
247                 
248                 [Test]
249                 [ExpectedException(typeof(InvalidOperationException))]
250                 public void GetTableMappingBySchemaActionException1()
251                 {
252                         DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection,"sourceCustomers","dataSetCustomers",MissingMappingAction.Error);
253                 }
254                 
255                 [Test]
256                 public void IndexOf()
257                 {
258                         tableMapCollection.AddRange(tabs);
259                         int ind;
260                         ind=tableMapCollection.IndexOf(tabs[0]);
261                         AssertEquals("test1",0,ind);
262                         ind=tableMapCollection.IndexOf(tabs[1]);
263                         AssertEquals("test2",1,ind);
264                                         
265                         ind=tableMapCollection.IndexOf(tabs[0].SourceTable);
266                         AssertEquals("test3",0,ind);
267                         ind=tableMapCollection.IndexOf(tabs[1].SourceTable);
268                         AssertEquals("test4",1,ind);                    
269                 }
270                 
271                 [Test]
272                 public void IndexOfDataSetTable()
273                 {
274                         tableMapCollection.AddRange(tabs);
275                         int ind;
276                         ind=tableMapCollection.IndexOfDataSetTable(tabs[0].DataSetTable);
277                         AssertEquals("test1",0,ind);
278                         ind=tableMapCollection.IndexOfDataSetTable(tabs[1].DataSetTable);
279                         AssertEquals("test2",1,ind);
280                                                 
281                         ind=tableMapCollection.IndexOfDataSetTable("datasetcustomers");
282                         AssertEquals("test3",0,ind);
283                         ind=tableMapCollection.IndexOfDataSetTable("datasetemployees");
284                         AssertEquals("test4",1,ind);
285                                                 
286                         ind=tableMapCollection.IndexOfDataSetTable("sourcedeter");
287                         AssertEquals("test5",-1,ind);
288                 }
289                 
290                 [Test]
291                 public void Insert()
292                 {
293                         tableMapCollection.AddRange(tabs);
294                         DataTableMapping mymap=new DataTableMapping("sourceTestAge","datatestSetAge");
295                         tableMapCollection.Insert(3,mymap);
296                         int ind=tableMapCollection.IndexOfDataSetTable("datatestSetAge");
297                         AssertEquals("test1",3,ind);
298                 }
299                 
300                 [Test]
301                 [Ignore ("This test is invalid; a mapping in a mapcollection must be identical.")]
302                 public void Remove()
303                 {
304                         tableMapCollection.AddRange(tabs);
305                         DataTableMapping mymap=new DataTableMapping("sourceCustomers","dataSetCustomers");
306                         tableMapCollection.Add(mymap);
307                         tableMapCollection.Remove((Object)mymap);
308                         bool eq=tableMapCollection.Contains((Object)mymap);
309                         AssertEquals("test1",false,eq);
310                 }
311                 
312                 [Test]
313                 [ExpectedException(typeof(InvalidCastException))]
314                 public void RemoveException1()
315                 {
316                         String te="testingdata";
317                         tableMapCollection.AddRange(tabs);
318                         tableMapCollection.Remove(te);
319                 }
320                 
321                 [Test]
322                 [ExpectedException(typeof(ArgumentException))]
323                 public void RemoveException2()
324                 {
325                         tableMapCollection.AddRange(tabs);
326                         DataTableMapping mymap=new DataTableMapping("sourceAge","dataSetAge");
327                         tableMapCollection.Remove(mymap);
328                 }
329                 
330                 [Test]
331                 public void RemoveAt()
332                 {
333                         tableMapCollection.AddRange(tabs);
334                         bool eq;
335                         tableMapCollection.RemoveAt(0);
336                         eq=tableMapCollection.Contains(tabs[0]);
337                         AssertEquals("test1",false,eq);
338                         eq=tableMapCollection.Contains(tabs[1]);
339                         AssertEquals("test2",true,eq);
340                                                 
341                         tableMapCollection.RemoveAt("sourceEmployees");
342                         eq=tableMapCollection.Contains(tabs[1]);
343                         AssertEquals("test3",false,eq);
344                         eq=tableMapCollection.Contains(tabs[2]);
345                         AssertEquals("test4",true,eq);                  
346                 }
347                 
348                 [Test]
349                 [ExpectedException(typeof(IndexOutOfRangeException))]
350                 public void RemoveAtException1()
351                 {
352                         tableMapCollection.RemoveAt(3);
353                 }
354                 
355                 [Test]
356                 [ExpectedException(typeof(IndexOutOfRangeException))]
357                 public void RemoveAtException2()
358                 {
359                         tableMapCollection.RemoveAt("sourceAge");
360                 }
361                 
362                 [Test]
363 #if TARGET_JVM
364                 [Ignore ("Does not work with TARGET_JVM")]
365 #endif
366                 public void ToStringTest()
367                 {
368                         AssertEquals("test1","System.Data.Common.DataTableMappingCollection",tableMapCollection.ToString());
369                 }
370         }
371 }