2009-10-19 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Transactions / Test / IntResourceManager.cs
blobaf41c9f3cba9aded4e1253072266c4762193e26c
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 //
10 using System;
11 using System.Collections.Generic;
12 using System.Text;
13 using System.Transactions;
14 using NUnit.Framework;
16 namespace MonoTests.System.Transactions
18 public class IntResourceManager
20 public IntResourceManager (int value)
22 actual = value;
23 guid = Guid.NewGuid ();
26 private int actual;
27 private int tmpValue;
28 private Transaction transaction = null;
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;
36 public bool FailPrepare = false;
37 public bool FailWithException = false;
38 public bool IgnorePrepare = false;
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;
46 Guid guid;
48 public int Actual {
49 get { return actual; }
52 public int Value {
53 get { return transaction == null ? actual : tmpValue; }
54 set
56 if (Transaction.Current == null) {
57 /* Not in a transaction */
58 actual = value;
59 return;
61 /* FIXME: Do what in this case? */
62 if (transaction != null)
63 Console.WriteLine ("WARNING: Setting value more than once");
65 if (transaction != Transaction.Current) {
66 transaction = Transaction.Current;
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 );
82 tmpValue = value;
86 public void Commit ()
88 actual = tmpValue;
89 transaction = null;
92 public void Rollback ()
94 transaction = null;
97 public void CheckSPC ( string msg )
99 Check ( 1, 0, 0, 0, 0, msg );
102 public void Check2PC ( string msg)
104 Check ( 0, 1, 1, 0, 0, msg );
107 public void Check ( int s, int p, int c, int r, int d, string msg )
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" );
116 /* Used for volatile RMs */
117 public void Check ( int p, int c, int r, int d, string msg )
119 Check ( 0, p, c, r, d, msg );
123 public class EnlistmentNotification : IEnlistmentNotification {
124 protected IntResourceManager resource;
126 public EnlistmentNotification ( IntResourceManager resource )
128 this.resource = resource;
131 public void Prepare ( PreparingEnlistment preparingEnlistment )
133 resource.NumPrepare++;
134 if ( resource.IgnorePrepare )
135 return;
137 if ( resource.FailPrepare ) {
138 if (resource.FailWithException)
139 preparingEnlistment.ForceRollback ( new NotSupportedException () );
140 else
141 preparingEnlistment.ForceRollback ();
142 } else {
143 preparingEnlistment.Prepared ();
147 public void Commit ( Enlistment enlistment )
149 resource.NumCommit++;
150 if ( resource.FailCommit )
151 return;
153 resource.Commit ();
154 enlistment.Done ();
157 public void Rollback ( Enlistment enlistment )
159 resource.NumRollback++;
160 resource.Rollback ();
163 public void InDoubt ( Enlistment enlistment )
165 resource.NumInDoubt++;
166 throw new Exception ( "IntResourceManager.InDoubt is not implemented." );
171 public class SinglePhaseNotification : EnlistmentNotification, ISinglePhaseNotification
173 public SinglePhaseNotification ( IntResourceManager resource )
174 : base ( resource )
178 public void SinglePhaseCommit ( SinglePhaseEnlistment enlistment )
180 resource.NumSingle++;
181 if ( resource.IgnoreSPC )
182 return;
184 if ( resource.FailSPC ) {
185 if ( resource.FailWithException )
186 enlistment.Aborted ( new NotSupportedException () );
187 else
188 enlistment.Aborted ();
190 else {
191 resource.Commit ();
192 enlistment.Committed ();