svn path=/trunk/mcs/; revision=39278
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / Test / FbTransactionTest.cs
1 /*
2  *  Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *     The contents of this file are subject to the Initial 
5  *     Developer's Public License Version 1.0 (the "License"); 
6  *     you may not use this file except in compliance with the 
7  *     License. You may obtain a copy of the License at 
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *     Software distributed under the License is distributed on 
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *     express or implied.  See the License for the specific 
13  *     language governing rights and limitations under the License.
14  * 
15  *  Copyright (c) 2002, 2004 Carlos Guzman Alvarez
16  *  All Rights Reserved.
17  */
18
19 using NUnit.Framework;
20 using System;
21 using System.Data;
22 using System.Text;
23 using FirebirdSql.Data.Firebird;
24
25 namespace FirebirdSql.Data.Firebird.Tests
26 {
27         [TestFixture]
28         public class FbTransactionTest : BaseTest 
29         {
30                 public FbTransactionTest() : base(false)
31                 {               
32                 }
33
34         [Test]
35         public void DisposeTest()
36         {
37             bool result = true;
38             try
39             {
40                 FbCommand cmd = new FbCommand("select * from test", this.Connection);
41                 cmd.Transaction = this.Connection.BeginTransaction(IsolationLevel.RepeatableRead);
42
43                 FbDataReader r = cmd.ExecuteReader();
44                 while (r.Read())
45                 {
46                 }
47                 r.Close();
48
49                 cmd.Transaction.Rollback();
50                 cmd.Transaction.Dispose();
51
52                 result = false;
53             }
54             catch
55             {
56             }
57             finally
58             {
59                 if (!result)
60                 {
61                     throw new Exception("Incorrect Dispose behavior");
62                 }
63             }
64         }
65
66         [Test]
67                 public void CommitTest()
68                 {                       
69                         Transaction = Connection.BeginTransaction();
70                         Transaction.Commit();
71                 }
72                 
73                 [Test]
74                 public void RollbackTest()
75                 {
76                         Transaction = Connection.BeginTransaction();
77                         Transaction.Rollback();
78                 }
79
80         [Test]
81                 public void SavePointTest()
82                 {
83                         FbCommand command = new FbCommand();
84
85                         Console.WriteLine("Iniciada nueva transaccion");
86                         
87                         Transaction = Connection.BeginTransaction("InitialSavePoint");
88                         
89                         command.Connection      = Connection;
90                         command.Transaction     = Transaction;
91
92                         command.CommandText = "insert into TEST (INT_FIELD) values (200) ";
93                         command.ExecuteNonQuery();                      
94
95                         Transaction.Save("FirstSavePoint");
96
97                         command.CommandText = "insert into TEST (INT_FIELD) values (201) ";
98                         command.ExecuteNonQuery();                      
99                         Transaction.Save("SecondSavePoint");
100
101                         command.CommandText = "insert into TEST (INT_FIELD) values (202) ";
102                         command.ExecuteNonQuery();                      
103                         Transaction.Rollback("InitialSavePoint");
104
105                         Transaction.Commit();
106                         command.Dispose();
107                 }
108
109                 [Test]
110                 public void AbortTransaction()
111                 {
112                         StringBuilder b1 = new StringBuilder();
113                         b1.AppendFormat("ALTER TABLE \"{0}\" drop \"INT_FIELD\"", "TEST");
114
115                         FbTransaction   transaction = null;
116                         FbCommand               command         = null;
117
118                         try
119                         {
120                                 transaction = this.Connection.BeginTransaction();
121
122                                 command = new FbCommand(b1.ToString(), this.Connection, transaction);
123                                 command.ExecuteNonQuery();
124
125                                 transaction.Commit();
126                                 transaction = null;
127                         }
128                         catch (Exception)
129                         {
130                                 transaction.Rollback();
131                                 transaction = null;
132                         }
133                         finally
134                         {
135                                 if (command != null)
136                                 {
137                                         command.Dispose();
138                                 }
139                         }
140                 }
141         }
142 }