Update mcs/class/System.Core/System/TimeZoneInfo.cs
[mono.git] / mcs / class / System.Transactions / Test / IntResourceManager.cs
1 //
2 // ResourceManager representing an integer, used by other test cases
3 //
4 // Author:
5 //      Ankit Jain      <JAnkit@novell.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.Collections.Generic;
12 using System.Text;
13 using System.Transactions;
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Transactions
17 {
18     public class IntResourceManager
19     {
20         public IntResourceManager (int value) 
21         { 
22             actual = value;
23             guid = Guid.NewGuid ();
24         }
25
26         private int actual;
27         private int tmpValue;
28         private Transaction transaction = null;
29
30         public int NumPrepare = 0;
31         public int NumRollback = 0;
32         public int NumCommit = 0;
33         public int NumInDoubt = 0;
34         public int NumSingle = 0;
35
36         public bool FailPrepare = false;
37         public bool FailWithException = false;
38         public bool IgnorePrepare = false;
39
40         public bool Volatile = true;
41         public bool IgnoreSPC = false;
42         public bool FailSPC = false;
43         public bool FailCommit = false;
44         public bool UseSingle = false;
45
46         Guid guid;
47
48         public int Actual {
49             get { return actual; }
50         }
51
52         public int Value {
53             get { return transaction == null ? actual : tmpValue; }
54             set
55             {
56                 if (Transaction.Current == null) {
57                     /* Not in a transaction */
58                     actual = value;
59                     return;
60                 }
61                 /* FIXME: Do what in this case? */
62                 if (transaction != null)
63                     Console.WriteLine ("WARNING: Setting value more than once");
64
65                 if (transaction != Transaction.Current) {
66                     transaction = Transaction.Current;
67                     
68                     if (UseSingle) {
69                         SinglePhaseNotification enlistment = new SinglePhaseNotification ( this );
70                         if ( Volatile )
71                             transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
72                         else
73                             transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
74                     } else {
75                         EnlistmentNotification enlistment = new EnlistmentNotification ( this );
76                         if ( Volatile )
77                             transaction.EnlistVolatile ( enlistment, EnlistmentOptions.None );
78                         else
79                             transaction.EnlistDurable ( guid, enlistment, EnlistmentOptions.None );
80                     }
81                 }
82                 tmpValue = value;
83             }
84         }
85
86         public void Commit ()
87         {
88             actual = tmpValue;
89             transaction = null;
90         }
91
92         public void Rollback ()
93         {
94             transaction = null;
95         }
96
97         public  void CheckSPC ( string msg )
98         {
99             Check ( 1, 0, 0, 0, 0, msg );
100         }
101
102         public void Check2PC ( string msg)
103         {
104             Check ( 0, 1, 1, 0, 0, msg );
105         }
106
107         public void Check ( int s, int p, int c, int r, int d, string msg )
108         {
109             Assert.AreEqual ( s, NumSingle, msg + ": NumSingle" );
110             Assert.AreEqual ( p, NumPrepare, msg + ": NumPrepare" );
111             Assert.AreEqual ( c, NumCommit, msg + ": NumCommit" );
112             Assert.AreEqual ( r, NumRollback, msg + ": NumRollback" );
113             Assert.AreEqual ( d, NumInDoubt, msg + ": NumInDoubt" );
114         }
115        
116         /* Used for volatile RMs */
117         public void Check ( int p, int c, int r, int d, string msg )
118         {
119             Check ( 0, p, c, r, d, msg );
120         }
121     }
122
123     public class EnlistmentNotification : IEnlistmentNotification {
124         protected IntResourceManager resource;
125
126         public EnlistmentNotification ( IntResourceManager resource )
127         {
128             this.resource = resource;
129         }
130
131         public void Prepare ( PreparingEnlistment preparingEnlistment )
132         {
133             resource.NumPrepare++;
134             if ( resource.IgnorePrepare )
135                 return;
136
137             if ( resource.FailPrepare ) {
138                 if (resource.FailWithException)
139                     preparingEnlistment.ForceRollback ( new NotSupportedException () );
140                 else
141                     preparingEnlistment.ForceRollback ();
142             } else {
143                 preparingEnlistment.Prepared ();
144             }
145         }
146
147         public void Commit ( Enlistment enlistment )
148         {
149             resource.NumCommit++;
150             if ( resource.FailCommit )
151                 return;
152
153             resource.Commit ();
154             enlistment.Done ();
155         }
156
157         public void Rollback ( Enlistment enlistment )
158         {
159             resource.NumRollback++;
160             resource.Rollback ();
161         }
162
163         public void InDoubt ( Enlistment enlistment )
164         {
165             resource.NumInDoubt++;
166             throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
167         }
168
169     }
170
171     public class SinglePhaseNotification : EnlistmentNotification, ISinglePhaseNotification 
172     {
173         public SinglePhaseNotification ( IntResourceManager resource )
174             : base ( resource )
175         {
176         }
177
178         public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment )
179         {
180             resource.NumSingle++;
181             if ( resource.IgnoreSPC )
182                 return;
183
184             if ( resource.FailSPC ) {
185                 if ( resource.FailWithException )
186                     enlistment.Aborted ( new NotSupportedException () );
187                 else
188                     enlistment.Aborted ();
189             }
190             else {
191                 resource.Commit ();
192                 enlistment.Committed ();
193             }
194
195         }
196
197     }
198 }
199