Importing tests from Mainsoft
[mono.git] / mcs / class / System.Data / Test / System.Data.Tests.Mainsoft / System.Data / DataTable / DataTable_GetChanges_D.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30
31
32 using System;
33 using System.Data;
34
35 using GHTUtils;
36 using GHTUtils.Base;
37
38 namespace tests.system_data_dll.System_Data
39 {
40 [TestFixture] public class DataTable_GetChanges_D : GHTBase
41 {
42         [Test] public void Main()
43         {
44                 DataTable_GetChanges_D tc = new DataTable_GetChanges_D();
45                 Exception exp = null;
46                 try
47                 {
48                         tc.BeginTest("DataTable_GetChanges_D");
49                         tc.run();
50                 }
51                 catch(Exception ex)
52                 {
53                         exp = ex;
54                 }
55                 finally
56                 {
57                         tc.EndTest(exp);
58                 }
59         }
60
61         //Activate This Construntor to log All To Standard output
62         //public TestClass():base(true){}
63
64         //Activate this constructor to log Failures to a log file
65         //public TestClass(System.IO.TextWriter tw):base(tw, false){}
66
67
68         //Activate this constructor to log All to a log file
69         //public TestClass(System.IO.TextWriter tw):base(tw, true){}
70
71         //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
72
73         public void run()
74         {
75                 Exception exp = null;
76                 
77                 
78                 DataTable dt1,dt2 = GHTUtils.DataProvider.CreateParentDataTable();
79                 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
80                 dt2.Columns[0].DefaultValue=7;
81
82                 //make some changes
83                 dt2.Rows[0].Delete(); //DataRowState.Deleted 
84                 dt2.Rows[1].Delete(); //DataRowState.Deleted 
85                 dt2.Rows[2].BeginEdit();
86                 dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified 
87                 dt2.Rows[2].EndEdit();
88
89                 dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added 
90
91                 // *********** Checking GetChanges - DataRowState.Deleted ************
92                 dt1=null;
93                 dt1 = dt2.GetChanges(DataRowState.Deleted);
94                 CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
95                 DataRow[] drArr1,drArr2;
96                 drArr1 = dt1.Select("","",DataViewRowState.Deleted );
97                 drArr2 = dt2.Select("","",DataViewRowState.Deleted );
98
99                 for (int i=0; i<drArr1.Length ; i++)
100                 {
101                         try
102                         {
103                                 BeginCase(String.Format("GetChanges(Deleted) - Data [ParentId]{0}" ,i));
104                                 Compare(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId", DataRowVersion.Original]);
105                         }
106                         catch(Exception ex)     {exp = ex;}
107                         finally {EndCase(exp); exp = null;}
108                         try
109                         {
110                                 BeginCase(String.Format("GetChanges(Deleted) - Data [String1]{0}" ,i));
111                                 Compare(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1", DataRowVersion.Original]);
112                         }
113                         catch(Exception ex)     {exp = ex;}
114                         finally {EndCase(exp); exp = null;}
115                         try
116                         {
117                                 BeginCase(String.Format("GetChanges(Deleted) - Data [String2]{0}" ,i));
118                                 Compare(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2", DataRowVersion.Original]);
119                         }
120                         catch(Exception ex)     {exp = ex;}
121                         finally {EndCase(exp); exp = null;}
122                 }
123
124                 // *********** Checking GetChanges - DataRowState.Modified ************
125                 dt1=null;
126                 dt1 = dt2.GetChanges(DataRowState.Modified);
127                 CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
128                 drArr1 = dt1.Select("","");
129                 drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
130
131                 for (int i=0; i<drArr1.Length ; i++)
132                 {
133                         try
134                         {
135                                 BeginCase(String.Format("GetChanges(Modified) - Data [ParentId]{0}" ,i));
136                                 Compare(drArr1[i]["ParentId"],drArr2[i]["ParentId"]);
137                         }
138                         catch(Exception ex)     {exp = ex;}
139                         finally {EndCase(exp); exp = null;}
140                         try
141                         {
142                                 BeginCase(String.Format("GetChanges(Modified) - Data [String1]{0}" ,i));
143                                 Compare(drArr1[i]["String1"],drArr2[i]["String1"]);
144                         }
145                         catch(Exception ex)     {exp = ex;}
146                         finally {EndCase(exp); exp = null;}
147                         try
148                         {
149                                 BeginCase(String.Format("GetChanges(Modified) - Data [String2]{0}" ,i));
150                                 Compare(drArr1[i]["String2" ],drArr2[i]["String2"]);
151                         }
152                         catch(Exception ex)     {exp = ex;}
153                         finally {EndCase(exp); exp = null;}
154                 }
155
156                 // *********** Checking GetChanges - DataRowState.Added ************
157                 dt1=null;
158                 dt1 = dt2.GetChanges(DataRowState.Added);
159                 CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
160                 drArr1 = dt1.Select("","");
161                 drArr2 = dt2.Select("","",DataViewRowState.Added );
162
163                 for (int i=0; i<drArr1.Length ; i++)
164                 {
165                         try
166                         {
167                                 BeginCase(String.Format("GetChanges(Added) - Data [ParentId]{0}" ,i));
168                                 Compare(drArr1[i]["ParentId"],drArr2[i]["ParentId"]);
169                         }
170                         catch(Exception ex)     {exp = ex;}
171                         finally {EndCase(exp); exp = null;}
172                         try
173                         {
174                                 BeginCase(String.Format("GetChanges(Added) - Data [String1]{0}" ,i));
175                                 Compare(drArr1[i]["String1"],drArr2[i]["String1"]);
176                         }
177                         catch(Exception ex)     {exp = ex;}
178                         finally {EndCase(exp); exp = null;}
179                         try
180                         {
181                                 BeginCase(String.Format("GetChanges(Added) - Data [String2]{0}" ,i));
182                                 Compare(drArr1[i]["String2" ],drArr2[i]["String2"]);
183                         }
184                         catch(Exception ex)     {exp = ex;}
185                         finally {EndCase(exp); exp = null;}
186                 }
187
188                 // *********** Checking GetChanges - DataRowState.Unchanged  ************
189                 dt1=null;
190                 dt1 = dt2.GetChanges(DataRowState.Unchanged);
191                 CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
192                 drArr1 = dt1.Select("","");
193                 drArr2 = dt2.Select("","",DataViewRowState.Unchanged  );
194
195                 for (int i=0; i<drArr1.Length ; i++)
196                 {
197                         try
198                         {
199                                 BeginCase(String.Format("GetChanges(Unchanged) - Data [ParentId]{0}" ,i));
200                                 Compare(drArr1[i]["ParentId"],drArr2[i]["ParentId"]);
201                         }
202                         catch(Exception ex)     {exp = ex;}
203                         finally {EndCase(exp); exp = null;}
204                         try
205                         {
206                                 BeginCase(String.Format("GetChanges(Unchanged) - Data [String1]{0}" ,i));
207                                 Compare(drArr1[i]["String1"],drArr2[i]["String1"]);
208                         }
209                         catch(Exception ex)     {exp = ex;}
210                         finally {EndCase(exp); exp = null;}
211                         try
212                         {
213                                 BeginCase(String.Format("GetChanges(Unchanged) - Data [String2]{0}" ,i));
214                                 Compare(drArr1[i]["String2" ],drArr2[i]["String2"]);
215                         }
216                         catch(Exception ex)     {exp = ex;}
217                         finally {EndCase(exp); exp = null;}
218                 }
219
220
221         }
222
223         private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
224         {
225                 Exception exp = null;
226                 for (int i=0; i<dt2.Constraints.Count; i++)
227                 {
228                         try
229                         {
230                                 BeginCase(String.Format("GetChanges - Constraints[{0}] - {1}",i,Description));
231                                 Compare( dt1.Constraints[i].ConstraintName ,dt2.Constraints[i].ConstraintName );
232                         }
233                         catch(Exception ex)     {exp = ex;}
234                         finally {EndCase(exp); exp = null;}
235                 }
236                 
237                 for (int i=0; i<dt2.Columns.Count; i++)
238                 {
239                         try
240                         {
241                                 BeginCase(String.Format("GetChanges - Columns[{0}].ColumnName - {1}",i,Description));
242                                 Compare( dt1.Columns[i].ColumnName  ,dt2.Columns[i].ColumnName );
243                         }
244                         catch(Exception ex)     {exp = ex;}
245                         finally {EndCase(exp); exp = null;}
246
247                         try
248                         {
249                                 BeginCase(String.Format("GetChanges - Columns[{0}].DataType {1}",i,Description));
250                                 Compare( dt1.Columns[i].DataType ,dt2.Columns[i].DataType );
251                         }
252                         catch(Exception ex)     {exp = ex;}
253                         finally {EndCase(exp); exp = null;}
254                 }
255         }
256
257 }
258 }