BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Data.OracleClient / Test / System.Data.OracleClient.jvm / OracleDataAdapter / OracleDataAdapter_RowUpdating.cs
1 // 
2 // Copyright (c) 2006 Mainsoft Co.
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23
24 using System;
25 using System.Data;
26 using System.Data.Common;
27 using System.Data.OracleClient;
28
29 using MonoTests.System.Data.Utils;
30
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Data.OracleClient
35 {
36         [TestFixture]
37         public class OracleDataAdapter_RowUpdating : ADONetTesterClass
38         {
39                 public static void Main()
40                 {
41                         OracleDataAdapter_RowUpdating tc = new OracleDataAdapter_RowUpdating();
42                         Exception exp = null;
43                         try
44                         {
45                                 tc.BeginTest("OracleDataAdapter_RowUpdating");
46                                 tc.run();
47                         }
48                         catch(Exception ex)
49                         {
50                                 exp = ex;
51                         }
52                         finally
53                         {
54                                 tc.EndTest(exp);
55                         }
56                 }
57
58
59                 //public TestClass():base(true){}
60
61                 //Activate this constructor to log Failures to a log file
62                 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
63
64
65                 //Activate this constructor to log All to a log file
66                 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
67
68                 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
69
70
71                 int EventCounter = 0;
72                 DataRow drInsert,drDelete,drUpdate;
73                 [Test]
74                 public void run()
75                 {
76                         Exception exp = null;
77
78                         OracleDataAdapter oleDBda = new OracleDataAdapter();
79                         oleDBda.SelectCommand = new OracleCommand("",new OracleConnection());
80
81                         base.OracleDataAdapter_BuildUpdateCommands(ref oleDBda);                
82                         // --------- get data from DB -----------------
83                         DataSet ds = base.PrepareDBData_Update((DbDataAdapter)oleDBda);
84                         // add event handler
85                         oleDBda.RowUpdating +=new OracleRowUpdatingEventHandler(oleDBda_RowUpdating);
86                         
87                         //insert ,delete, update
88                         drInsert = ds.Tables[0].NewRow();
89                         drInsert.ItemArray = new object[] {9991,"Ofer","Borshtein","Insert"};
90                         drDelete = ds.Tables[0].Rows.Find(9992);
91                         drUpdate = ds.Tables[0].Rows.Find(9993);
92                 
93                         ds.Tables[0].Rows.Add(drInsert);
94                         drDelete.Delete();
95                         drUpdate["Title"] = "Jack the ripper"; 
96
97                         //execute update to db, will raise events
98                         oleDBda.Update(ds);
99
100                         try
101                         {
102                                 BeginCase("EventCounter ");
103                                 Compare(EventCounter ,3);
104                         }
105                         catch(Exception ex)     {exp = ex;}
106                         finally {EndCase(exp); exp = null;}
107                 
108                         oleDBda.RowUpdating -=new OracleRowUpdatingEventHandler(oleDBda_RowUpdating);
109                                 
110                         //close connection
111                         if (  ((IDbDataAdapter)oleDBda).SelectCommand.Connection.State != ConnectionState.Closed )
112                                 ((IDbDataAdapter)oleDBda).SelectCommand.Connection.Close();
113                 }
114
115                 private void oleDBda_RowUpdating(object sender, OracleRowUpdatingEventArgs e)
116                 {
117                         Exception exp = null;
118                         switch (e.StatementType)
119                         {
120                                 case StatementType.Insert: 
121                                         try
122                                         {
123                                                 BeginCase("RowInsert");
124                                                 Compare(drInsert ,e.Row );
125                                         }
126                                         catch(Exception ex)     {exp = ex;}
127                                         finally {EndCase(exp); exp = null;}
128                                         EventCounter++;
129                                         break;
130                                 case StatementType.Delete:
131                                         try
132                                         {
133                                                 BeginCase("RowDelete");
134                                                 Compare(drDelete ,e.Row );
135                                         }
136                                         catch(Exception ex)     {exp = ex;}
137                                         finally {EndCase(exp); exp = null;}
138                                         EventCounter++;
139                                         break;
140                                 case StatementType.Update:
141                                         try
142                                         {
143                                                 BeginCase("RowUpdate");
144                                                 Compare(drUpdate ,e.Row );
145                                         }
146                                         catch(Exception ex)     {exp = ex;}
147                                         finally {EndCase(exp); exp = null;}
148                                         EventCounter++;
149                                         break;
150                         }
151                 }
152         }
153 }