[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System.Transactions / Test / TransactionScopeTest.cs
1 //
2 // Unit tests for TransactionScope and Implicit/Explicit use of
3 // Transactions
4 //
5 // Author:
6 //      Ankit Jain      <JAnkit@novell.com>
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10
11 using System;
12 using NUnit.Framework;
13 using System.Transactions;
14
15 namespace MonoTests.System.Transactions
16 {
17         [TestFixture]
18         public class TransactionScopeTest
19         {
20
21                 [Test]
22                 public void TransactionScopeCommit ()
23                 {
24                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
25                         using (TransactionScope scope = new TransactionScope ()) {
26                                 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
27                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
28                                 
29                                 scope.Complete ();
30                         }
31                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
32                 }
33
34                 [Test]
35                 public void TransactionScopeAbort ()
36                 {
37                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
38                         IntResourceManager irm = new IntResourceManager (1);
39                         using (TransactionScope scope = new TransactionScope ()) {
40                                 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
41                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
42
43                                 irm.Value = 2;
44                                 /* Not completing scope here */
45                         }
46                         irm.Check ( 0, 0, 1, 0, "irm");
47                         Assert.AreEqual (1, irm.Value);
48                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (InvalidOperationException))]
53                 public void TransactionScopeCompleted1 ()
54                 {
55                         using (TransactionScope scope = new TransactionScope ()) {
56                                 scope.Complete ();
57                                 /* Can't access ambient transaction after scope.Complete */
58                                 TransactionStatus status = Transaction.Current.TransactionInformation.Status;
59                         }
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void TransactionScopeCompleted2 ()
65                 {
66                         using (TransactionScope scope = new TransactionScope ()) {
67                                 scope.Complete ();
68                                 Transaction.Current = Transaction.Current;
69                         }
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (InvalidOperationException))]
74                 public void TransactionScopeCompleted3 ()
75                 {
76                         using (TransactionScope scope = new TransactionScope ()) {
77                                 scope.Complete ();
78                                 scope.Complete ();
79                         }
80                 }
81
82                 #region NestedTransactionScope tests
83                 [Test]
84                 public void NestedTransactionScope1 ()
85                 {
86                         IntResourceManager irm = new IntResourceManager (1);
87
88                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
89                         using (TransactionScope scope = new TransactionScope ()) {
90                                 irm.Value = 2;
91
92                                 /* Complete this scope */
93                                 scope.Complete ();
94                         }
95
96                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
97                         /* Value = 2, got committed */
98                         Assert.AreEqual (irm.Value, 2, "#1");
99                         irm.Check ( 1, 1, 0, 0, "irm" );
100                 }
101
102                 [Test]
103                 public void NestedTransactionScope2 ()
104                 {
105                         IntResourceManager irm = new IntResourceManager (1);
106                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
107                         using (TransactionScope scope = new TransactionScope ()) {
108                                 irm.Value = 2;
109
110                                 /* Not-Completing this scope */
111                         }
112
113                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
114                         /* Value = 2, got rolledback */
115                         Assert.AreEqual (irm.Value, 1, "#2");
116                         irm.Check ( 0, 0, 1, 0, "irm" );
117                 }
118
119                 [Test]
120                 public void NestedTransactionScope3 ()
121                 {
122                         IntResourceManager irm = new IntResourceManager (1);
123                         IntResourceManager irm2 = new IntResourceManager (10);
124
125                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
126                         using (TransactionScope scope = new TransactionScope ()) {
127                                 irm.Value = 2;
128
129                                 using (TransactionScope scope2 = new TransactionScope ()) {
130                                         irm2.Value = 20;
131
132                                         scope2.Complete ();
133                                 }
134
135                                 scope.Complete ();
136                         }
137
138                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
139                         /* Both got committed */
140                         Assert.AreEqual (irm.Value, 2, "#3");
141                         Assert.AreEqual (irm2.Value, 20, "#4");
142                         irm.Check ( 1, 1, 0, 0, "irm" );
143                         irm2.Check ( 1, 1, 0, 0, "irm2" );
144                 }
145
146                 [Test]
147                 public void NestedTransactionScope4 ()
148                 {
149                         IntResourceManager irm = new IntResourceManager (1);
150                         IntResourceManager irm2 = new IntResourceManager (10);
151
152                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
153                         using (TransactionScope scope = new TransactionScope ()) {
154                                 irm.Value = 2;
155
156                                 using (TransactionScope scope2 = new TransactionScope ()) {
157                                         irm2.Value = 20;
158
159                                         /* Inner Tx not completed, Tx should get rolled back */
160                                         //scope2.Complete();
161                                 }
162                                 /* Both rolledback */
163                                 irm.Check ( 0, 0, 1, 0, "irm" );
164                                 irm2.Check ( 0, 0, 1, 0, "irm2" );
165                                 Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
166                                 //scope.Complete ();
167                         }
168
169                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
170
171                         Assert.AreEqual (irm.Value, 1, "#6");
172                         Assert.AreEqual (irm2.Value, 10, "#7");
173                         irm.Check ( 0, 0, 1, 0, "irm" );
174                 }
175
176                 [Test]
177                 public void NestedTransactionScope5 ()
178                 {
179                         IntResourceManager irm = new IntResourceManager (1);
180                         IntResourceManager irm2 = new IntResourceManager (10);
181
182                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
183                         using (TransactionScope scope = new TransactionScope ()) {
184                                 irm.Value = 2;
185
186                                 using (TransactionScope scope2 = new TransactionScope ()) {
187                                         irm2.Value = 20;
188                                         scope2.Complete ();
189                                 }
190
191                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
192                                 /* Not completing outer scope
193                                 scope.Complete (); */
194                         }
195
196                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
197
198                         Assert.AreEqual (irm.Value, 1, "#9");
199                         Assert.AreEqual (irm2.Value, 10, "#10");
200                         irm.Check ( 0, 0, 1, 0, "irm" );
201                         irm2.Check ( 0, 0, 1, 0, "irm2" );
202                 }
203
204                 [Test]
205                 public void NestedTransactionScope6 ()
206                 {
207                         IntResourceManager irm = new IntResourceManager (1);
208                         IntResourceManager irm2 = new IntResourceManager (10);
209
210                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
211                         using (TransactionScope scope = new TransactionScope ()) {
212                                 irm.Value = 2;
213
214                                 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
215                                         irm2.Value = 20;
216                                         scope2.Complete ();
217                                 }
218                                 /* vr2, committed */
219                                 irm2.Check ( 1, 1, 0, 0, "irm2" );
220                                 Assert.AreEqual (irm2.Value, 20);
221
222                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
223
224                                 scope.Complete ();
225                         }
226
227                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
228                         Assert.AreEqual (irm.Value, 2, "#12");
229                         irm.Check ( 1, 1, 0, 0, "irm" );
230                 }
231
232                 [Test]
233                 public void NestedTransactionScope7 ()
234                 {
235                         IntResourceManager irm = new IntResourceManager (1);
236                         IntResourceManager irm2 = new IntResourceManager (10);
237
238                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
239                         using (TransactionScope scope = new TransactionScope ()) {
240                                 irm.Value = 2;
241
242                                 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
243                                         irm2.Value = 20;
244                                         /* Not completing 
245                                          scope2.Complete();*/
246                                 }
247
248                                 /* irm2, rolled back*/
249                                 irm2.Check ( 0, 0, 1, 0, "irm2" );
250                                 Assert.AreEqual (irm2.Value, 10, "#13");
251
252                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
253
254                                 scope.Complete ();
255                         }
256
257                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
258                         /* ..But irm got committed */
259                         Assert.AreEqual (irm.Value, 2, "#15");
260                         irm.Check ( 1, 1, 0, 0, "irm" );
261                 }
262
263                 [Test]
264                 public void NestedTransactionScope8 ()
265                 {
266                         IntResourceManager irm = new IntResourceManager (1);
267                         IntResourceManager irm2 = new IntResourceManager (10);
268
269                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
270                         using (TransactionScope scope = new TransactionScope ()) {
271                                 irm.Value = 2;
272
273                                 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
274                                         /* Not transactional, so this WONT get committed */
275                                         irm2.Value = 20;
276                                         scope2.Complete ();
277                                 }
278                                 irm2.Check ( 0, 0, 0, 0, "irm2" );
279                                 Assert.AreEqual (20, irm2.Value, "#16");
280                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
281
282                                 scope.Complete ();
283                         }
284
285                         Assert.IsNull (Transaction.Current, "Ambient transaction exists");
286                         Assert.AreEqual (irm.Value, 2, "#18");
287                         irm.Check ( 1, 1, 0, 0, "irm" );
288                 }
289
290                 [Test]
291                 public void NestedTransactionScope8a ()
292                 {
293                         IntResourceManager irm = new IntResourceManager ( 1 );
294                         IntResourceManager irm2 = new IntResourceManager ( 10 );
295
296                         Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
297                         using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
298                                 irm.Value = 2;
299
300                                 using (TransactionScope scope2 = new TransactionScope ()) {
301                                         irm2.Value = 20;
302                                         scope2.Complete ();
303                                 }
304                                 irm2.Check ( 1, 1, 0, 0, "irm2" );
305                                 Assert.AreEqual ( 20, irm2.Value, "#16a" );
306
307                                 scope.Complete ();
308                         }
309
310                         Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
311                         Assert.AreEqual ( 2, irm.Value, "#18a" );
312                         irm.Check ( 0, 0, 0, 0, "irm" );
313                 }
314
315                 [Test]
316                 public void NestedTransactionScope9 ()
317                 {
318                         IntResourceManager irm = new IntResourceManager (1);
319                         IntResourceManager irm2 = new IntResourceManager (10);
320
321                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
322                         using (TransactionScope scope = new TransactionScope ()) {
323                                 irm.Value = 2;
324
325                                 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
326                                         /* Not transactional, so this WONT get committed */
327                                         irm2.Value = 4;
328                                         scope2.Complete ();
329                                 }
330                                 irm2.Check ( 0, 0, 0, 0, "irm2" );
331
332                                 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
333                                         irm.Value = 6;
334                                         scope3.Complete ();
335                                 }
336
337                                 /* vr's value has changed as the inner scope committed = 6 */
338                                 irm.Check ( 1, 1, 0, 0, "irm" );
339                                 Assert.AreEqual (irm.Value, 6, "#19");
340                                 Assert.AreEqual (irm.Actual, 6, "#20");
341                                 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
342
343                                 scope.Complete ();
344                         }
345
346                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
347                         Assert.AreEqual (irm.Value, 6, "#22");
348                         irm.Check ( 2, 2, 0, 0, "irm" );
349                 }
350
351                 [Test]
352                 [ExpectedException (typeof (TransactionAbortedException))]
353                 public void NestedTransactionScope10 ()
354                 {
355                         IntResourceManager irm = new IntResourceManager (1);
356                         bool failed = false;
357
358                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
359                         using (TransactionScope scope = new TransactionScope ()) {
360                                 irm.Value = 2;
361
362                                 using (TransactionScope scope2 = new TransactionScope ()) {
363                                         irm.Value = 4;
364                                         /* Not completing this, so the transaction will
365                                          * get aborted 
366                                         scope2.Complete (); */
367                                 }
368
369                                 using (TransactionScope scope3 = new TransactionScope ()) {
370                                         /* Aborted transaction cannot be used for another
371                                          * TransactionScope 
372                                          */
373                                         //Assert.Fail ("Should not reach here.");
374                                         failed = true;
375                                 }
376                         }
377                         Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
378                 }
379
380                 [Test]
381                 public void NestedTransactionScope12 ()
382                 {
383                         IntResourceManager irm = new IntResourceManager (1);
384
385                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
386                         using (TransactionScope scope = new TransactionScope ()) {
387                                 irm.Value = 2;
388
389                                 using (TransactionScope scope2 = new TransactionScope ()) {
390                                         irm.Value = 4;
391                                         /* Not completing this, so the transaction will
392                                          * get aborted 
393                                         scope2.Complete (); */
394                                 }
395
396                                 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
397                                         /* Using RequiresNew here, so outer transaction
398                                          * being aborted doesn't matter
399                                          */
400                                         scope3.Complete (); 
401                                 }
402                         }
403                 }
404
405                 [Test]
406                 [ExpectedException (typeof (TransactionAbortedException))]
407                 public void NestedTransactionScope13 ()
408                 {
409                         IntResourceManager irm = new IntResourceManager ( 1 );
410
411                         Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
412                         using ( TransactionScope scope = new TransactionScope () ) {
413                                 irm.Value = 2;
414
415                                 using ( TransactionScope scope2 = new TransactionScope () ) {
416                                         irm.Value = 4;
417                                         /* Not completing this, so the transaction will
418                                          * get aborted 
419                                         scope2.Complete (); */
420                                 }
421
422                                 scope.Complete ();
423                         }
424                 }
425                 #endregion
426
427                 /* Tests using IntResourceManager */
428
429                 [Test]
430                 public void RMFail1 ()
431                 {
432                         IntResourceManager irm = new IntResourceManager (1);
433                         IntResourceManager irm2 = new IntResourceManager (10);
434                         IntResourceManager irm3 = new IntResourceManager (12);
435
436                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
437                         try {
438                                 using (TransactionScope scope = new TransactionScope ()) {
439                                         irm.Value = 2;
440                                         irm2.Value = 20;
441                                         irm3.Value = 24;
442
443                                         /* Make second RM fail to prepare, this should throw
444                                          * TransactionAbortedException when the scope ends 
445                                          */
446                                         irm2.FailPrepare = true;
447                                         scope.Complete ();
448                                 }
449                         } catch (TransactionAbortedException) {
450                                 irm.Check ( 1, 0, 1, 0, "irm");
451                                 irm2.Check ( 1, 0, 0, 0, "irm2");
452                                 irm3.Check ( 0, 0, 1, 0, "irm3");
453                         }
454                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
455                 }
456
457                 [Test]
458                 public void RMFail2 ()
459                 {
460                         IntResourceManager irm = new IntResourceManager (1);
461                         IntResourceManager irm2 = new IntResourceManager (10);
462                         IntResourceManager irm3 = new IntResourceManager (12);
463
464                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
465                         try {
466                                 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
467                                         irm.Value = 2;
468                                         irm2.Value = 20;
469                                         irm3.Value = 24;
470
471                                         /* irm2 wont call Prepared or ForceRollback in
472                                          * its Prepare (), so TransactionManager will timeout
473                                          * waiting for it 
474                                          */
475                                         irm2.IgnorePrepare = true;
476                                         scope.Complete ();
477                                 }
478                         } catch (TransactionAbortedException e) {
479                                 /* FIXME: Not working right now.. no timeout exception thrown! */
480                                 
481                                 Assert.IsNotNull ( e.InnerException, "innerexception is null" );
482                                 Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
483
484                                 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
485                                 return;
486                         }
487
488                         Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
489                 }
490
491                 #region Explicit Transaction Tests
492
493                 [Test]
494                 public void ExplicitTransactionCommit ()
495                 {
496                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
497
498                         CommittableTransaction ct = new CommittableTransaction ();
499                         Transaction oldTransaction = Transaction.Current;
500                         Transaction.Current = ct;
501
502                         IntResourceManager irm = new IntResourceManager (1);
503                         irm.Value = 2;
504                         ct.Commit ();
505
506                         Assert.AreEqual (2, irm.Value, "#33");
507                         Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
508                         Transaction.Current = oldTransaction;
509                 }
510
511                 [Test]
512                 public void ExplicitTransactionRollback ()
513                 {
514                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
515
516                         CommittableTransaction ct = new CommittableTransaction ();
517                         Transaction oldTransaction = Transaction.Current;
518                         Transaction.Current = ct;
519
520                         IntResourceManager irm = new IntResourceManager (1);
521                         irm.Value = 2;
522                         Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
523                         ct.Rollback ();
524
525                         Assert.AreEqual (1, irm.Value, "#36");
526                         Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
527                         Transaction.Current = oldTransaction;
528                 }
529
530                 [Test]
531                 public void ExplicitTransaction1 ()
532                 {
533                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
534                         CommittableTransaction ct = new CommittableTransaction ();
535                         Transaction oldTransaction = Transaction.Current;
536
537                         Transaction.Current = ct;
538
539                         IntResourceManager irm = new IntResourceManager (1);
540
541                         irm.Value = 2;
542
543                         using (TransactionScope scope = new TransactionScope ()) {
544                                 Assert.AreEqual (ct, Transaction.Current, "#38");
545                                 irm.Value = 4;
546                                 scope.Complete ();
547                         }
548
549                         Assert.AreEqual (ct, Transaction.Current, "#39");
550                         Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
551                         Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
552
553                         ct.Commit ();
554                         Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
555                         Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
556                         Transaction.Current = oldTransaction;
557                 }
558
559                 [Test]
560                 public void ExplicitTransaction2 ()
561                 {
562                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
563                         CommittableTransaction ct = new CommittableTransaction ();
564                         Transaction oldTransaction = Transaction.Current;
565
566                         Transaction.Current = ct;
567
568                         IntResourceManager irm = new IntResourceManager (1);
569
570                         irm.Value = 2;
571                         using (TransactionScope scope = new TransactionScope ()) {
572                                 Assert.AreEqual (ct, Transaction.Current, "#44");
573
574                                 /* Not calling scope.Complete
575                                 scope.Complete ();*/
576                         }
577
578                         Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
579                         Assert.AreEqual (ct, Transaction.Current, "#46");
580                         Assert.AreEqual (1, irm.Actual, "#47");
581                         Assert.AreEqual (1, irm.NumRollback, "#48");
582                         irm.Check ( 0, 0, 1, 0, "irm" );
583                         Transaction.Current = oldTransaction;
584
585                         try {
586                                 ct.Commit ();
587                         } catch (TransactionAbortedException) {
588                                 return;
589                         }
590                         Assert.Fail ("Commit on an aborted transaction should fail");
591                 }
592
593                 [Test]
594                 public void ExplicitTransaction3 ()
595                 {
596                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
597                         CommittableTransaction ct = new CommittableTransaction ();
598                         Transaction oldTransaction = Transaction.Current;
599
600                         Transaction.Current = ct;
601
602                         IntResourceManager irm = new IntResourceManager (1);
603
604                         using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
605                                 Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
606
607                                 irm.Value = 3;
608                                 scope.Complete ();
609                         }
610
611                         irm.Value = 2;
612
613                         Assert.AreEqual (3, irm.Actual, "#50");
614
615                         Assert.AreEqual (ct, Transaction.Current, "#51");
616                         ct.Commit ();
617                         Assert.AreEqual (2, irm.Actual, "#52");
618                         Transaction.Current = oldTransaction;
619                 }
620
621                 [Test]
622                 public void ExplicitTransaction4 ()
623                 {
624                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
625                         CommittableTransaction ct = new CommittableTransaction ();
626                         Transaction oldTransaction = Transaction.Current;
627
628                         /* Not setting ambient transaction 
629                          Transaction.Current = ct; 
630                          */
631
632                         IntResourceManager irm = new IntResourceManager (1);
633
634                         using (TransactionScope scope = new TransactionScope (ct)) {
635                                 Assert.AreEqual (ct, Transaction.Current, "#53");
636
637                                 irm.Value = 2;
638                                 scope.Complete ();
639                         }
640
641                         Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
642                         Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
643                         Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
644
645                         ct.Commit ();
646                         Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
647                         Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
648
649                         irm.Check ( 1, 1, 0, 0, "irm");
650                 }
651
652                 [Test]
653                 public void ExplicitTransaction5 ()
654                 {
655                         Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
656                         CommittableTransaction ct = new CommittableTransaction ();
657                         Transaction oldTransaction = Transaction.Current;
658
659                         /* Not setting ambient transaction 
660                          Transaction.Current = ct; 
661                          */
662
663                         IntResourceManager irm = new IntResourceManager (1);
664
665                         using (TransactionScope scope = new TransactionScope (ct)) {
666                                 Assert.AreEqual (ct, Transaction.Current, "#59");
667
668                                 irm.Value = 2;
669
670                                 /* Not completing this scope
671                                 scope.Complete (); */
672                         }
673
674                         Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
675                         Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
676                         Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
677
678                         irm.Check ( 0, 0, 1, 0, "irm");
679                 }
680
681                 [Test]
682                 [ExpectedException (typeof (InvalidOperationException))]
683                 public void ExplicitTransaction6 ()
684                 {
685                         CommittableTransaction ct = new CommittableTransaction ();
686
687                         IntResourceManager irm = new IntResourceManager (1);
688                         irm.Value = 2;
689                         ct.Commit ();
690
691                         ct.Commit ();
692                 }
693
694                 [Test]
695                 [ExpectedException (typeof (InvalidOperationException))]
696                 public void ExplicitTransaction6a ()
697                 {
698                         CommittableTransaction ct = new CommittableTransaction ();
699
700                         IntResourceManager irm = new IntResourceManager ( 1 );
701                         irm.Value = 2;
702                         ct.Commit ();
703
704                         /* Using a already committed transaction in a new 
705                          * TransactionScope
706                          */
707                         TransactionScope scope = new TransactionScope ( ct );
708                 }
709
710                 [Test]
711                 public void ExplicitTransaction6b ()
712                 {
713                         CommittableTransaction ct = new CommittableTransaction ();
714
715                         IntResourceManager irm = new IntResourceManager ( 1 );
716                         
717                         Transaction.Current = ct; 
718
719                         TransactionScope scope1 = new TransactionScope ();
720                         /* Enlist */
721                         irm.Value = 2;
722
723                         scope1.Complete ();
724
725                         try {
726                                 ct.Commit ();
727                         } catch (TransactionAbortedException) {
728                                 irm.Check ( 0, 0, 1, 0, "irm" );
729                                 
730                                 scope1.Dispose ();
731                                 Transaction.Current = null;
732                                 return;
733                         }
734                         Assert.Fail ( "Commit should've failed" );
735                 }
736
737                 [Test]
738                 public void ExplicitTransaction6c ()
739                 {
740                         CommittableTransaction ct = new CommittableTransaction ();
741
742                         IntResourceManager irm = new IntResourceManager ( 1 );
743
744                         Transaction.Current = ct;
745
746                         TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
747                         /* Enlist */
748                         irm.Value = 2;
749
750                         TransactionScope scope2 = new TransactionScope ();
751                         try {
752                                 scope1.Dispose ();
753                         } catch (InvalidOperationException) {
754                                 /* Error: TransactionScope nested incorrectly */
755                                 irm.Check ( 0, 0, 1, 0, "irm" );
756                                 scope2.Dispose ();
757                                 Transaction.Current = null;
758                                 return;
759                         }
760
761                         Assert.Fail ("Commit should've failed");
762                 }
763
764                 [Test]
765                 public void ExplicitTransaction6d ()
766                 {
767                         CommittableTransaction ct = new CommittableTransaction ();
768
769                         IntResourceManager irm = new IntResourceManager ( 1 );
770
771                         Transaction.Current = ct;
772
773                         TransactionScope scope1 = new TransactionScope ();
774                         /* Enlist */
775                         irm.Value = 2;
776
777                         TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
778                         try {
779                                 scope1.Dispose ();
780                         } catch (InvalidOperationException) {
781                                 /* Error: TransactionScope nested incorrectly */
782                                 scope2.Dispose ();
783                                 Transaction.Current = null;
784                                 return;
785                         }
786
787                         Assert.Fail ( "Commit should've failed" );
788                 }
789
790                 [Test]
791                 public void ExplicitTransaction6e ()
792                 {
793                         CommittableTransaction ct = new CommittableTransaction ();
794
795                         IntResourceManager irm = new IntResourceManager ( 1 );
796
797                         Transaction.Current = ct;
798
799                         TransactionScope scope1 = new TransactionScope ();
800                         /* Enlist */
801                         irm.Value = 2;
802
803                         TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
804                         try {
805                                 scope1.Dispose ();
806                         } catch (InvalidOperationException) {
807                                 /* Error: TransactionScope nested incorrectly */
808                                 scope2.Dispose ();
809                                 Transaction.Current = null;
810                                 return;
811                         }
812
813                         Assert.Fail ( "Commit should've failed" );
814                 }
815
816                 [Test]
817                 [ExpectedException (typeof (TransactionException))]
818                 public void ExplicitTransaction7 ()
819                 {
820                         CommittableTransaction ct = new CommittableTransaction ();
821
822                         IntResourceManager irm = new IntResourceManager (1);
823                         irm.Value = 2;
824                         ct.Commit ();
825                         /* Cannot accept any new work now, so TransactionException */
826                         ct.Rollback ();
827                 }
828
829                 [Test]
830                 public void ExplicitTransaction8 ()
831                 {
832                         CommittableTransaction ct = new CommittableTransaction ();
833
834                         IntResourceManager irm = new IntResourceManager ( 1 );
835                         using ( TransactionScope scope = new TransactionScope (ct) ) {
836                                 irm.Value = 2;
837                                 /* FIXME: Why TransactionAbortedException ?? */
838                                 try {
839                                         ct.Commit ();
840                                 } catch ( TransactionAbortedException) {
841                                         irm.Check ( 0, 0, 1, 0, "irm" );
842                                         return;
843                                 }
844                                 Assert.Fail ( "Should not be reached" );
845                         }
846                 }
847
848                 [Test]
849                 public void ExplicitTransaction8a ()
850                 {
851                         CommittableTransaction ct = new CommittableTransaction ();
852
853                         IntResourceManager irm = new IntResourceManager ( 1 );
854                         using ( TransactionScope scope = new TransactionScope ( ct ) ) {
855                                 irm.Value = 2;
856                                 scope.Complete ();
857                                 /* FIXME: Why TransactionAbortedException ?? */
858                                 try {
859                                         ct.Commit ();
860                                 }
861                                 catch ( TransactionAbortedException) {
862                                         irm.Check ( 0, 0, 1, 0, "irm" );
863                                         return;
864                                 }
865                                 Assert.Fail ( "Should not be reached" );
866                         }
867                 }
868
869                 [Test]
870                 [ExpectedException (typeof (InvalidOperationException))]
871                 public void ExplicitTransaction9 ()
872                 {
873                         CommittableTransaction ct = new CommittableTransaction ();
874
875                         IntResourceManager irm = new IntResourceManager ( 1 );
876                         ct.BeginCommit ( null, null );
877                         ct.BeginCommit ( null, null );
878                 }
879
880                 [Test]
881                 public void ExplicitTransaction10 ()
882                 {
883                         CommittableTransaction ct = new CommittableTransaction ();
884
885                         IntResourceManager irm = new IntResourceManager ( 1 );
886                         Transaction.Current = ct;
887                         irm.Value = 2;
888
889                         TransactionScope scope = new TransactionScope ( ct );
890                         Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
891                         //scope.Complete ();
892                         //scope.Dispose ();
893                         try {
894                                 ct.Commit ();
895                         } catch ( TransactionAbortedException) {
896                                 irm.Check ( 0, 0, 1, 0, "irm" );
897                                 Transaction.Current = null;
898                                 return;
899                         }
900                         
901                         Transaction.Current = null;
902                         Assert.Fail ("Expected TransactionAbortedException");
903                 }
904
905                 [Test]
906                 public void ExplicitTransaction10a ()
907                 {
908                         CommittableTransaction ct = new CommittableTransaction ();
909
910                         IntResourceManager irm = new IntResourceManager ( 1 );
911                         Transaction.Current = ct;
912                         irm.Value = 2;
913                         Transaction.Current = null;
914
915                         TransactionScope scope = new TransactionScope ( ct );
916                         Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
917                         Transaction.Current = null;
918                         //scope2.Complete ();
919                         //scope2.Dispose ();
920                         try {
921                                 ct.Commit ();
922                         }
923                         catch ( TransactionAbortedException) {
924                                 irm.Check ( 0, 0, 1, 0, "irm" );
925                                 Transaction.Current = null;
926                                 return;
927                         }
928
929                         Transaction.Current = null;
930                         Assert.Fail ();
931                 }
932
933                 [Test]
934                 public void ExplicitTransaction10b ()
935                 {
936                         CommittableTransaction ct = new CommittableTransaction ();
937
938                         IntResourceManager irm = new IntResourceManager ( 1 );
939                         Transaction.Current = ct;
940                         irm.Value = 2;
941                         Transaction.Current = null;
942
943                         TransactionScope scope = new TransactionScope ( ct );
944                         Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
945                         //scope2.Complete ();
946                         //scope2.Dispose ();
947                         IAsyncResult ar = ct.BeginCommit ( null, null );
948                         try {
949                                 ct.EndCommit (ar);
950                         }
951                         catch ( TransactionAbortedException) {
952                                 irm.Check ( 0, 0, 1, 0, "irm" );
953                                 Transaction.Current = null;
954                                 return;
955                         }
956
957                         Transaction.Current = null;
958                         Assert.Fail ();
959                 }
960
961                 [Test]
962                 [ExpectedException ( typeof (ArgumentException))]
963                 public void ExplicitTransaction12 ()
964                 {
965                         CommittableTransaction ct = new CommittableTransaction ();
966
967                         IntResourceManager irm = new IntResourceManager ( 1 );
968                         irm.FailPrepare = true;
969                         ct.BeginCommit ( null, null );
970                         ct.EndCommit ( null );
971                 }
972
973                 [Test]
974                 public void ExplicitTransaction13 ()
975                 {
976                         CommittableTransaction ct = new CommittableTransaction ();
977                         IntResourceManager irm = new IntResourceManager ( 1 );
978
979                         Assert.IsNull ( Transaction.Current );
980                         Transaction.Current = ct;
981                         irm.Value = 2;
982                         irm.FailPrepare = true;
983
984                         try {
985                                 ct.Commit ();
986                         } catch ( TransactionAbortedException ) {
987                                 Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
988                                 try {
989                                         ct.BeginCommit ( null, null );
990                                 } catch (Exception) {
991                                         Transaction.Current = null;
992                                         return;
993                                 }
994                                 Assert.Fail ( "Should not be reached(2)" );
995                         }
996                         Assert.Fail ("Should not be reached");
997                 }
998
999                 [Test]
1000                 public void ExplicitTransaction14 ()
1001                 {
1002                         CommittableTransaction ct = new CommittableTransaction ();
1003                         IntResourceManager irm = new IntResourceManager ( 1 );
1004
1005                         Assert.IsNull ( Transaction.Current );
1006                         Transaction.Current = ct;
1007                         irm.Value = 2;
1008
1009                         ct.Commit ();
1010
1011                         Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
1012                         try {
1013                                 ct.BeginCommit ( null, null );
1014                         }
1015                         catch ( Exception) {
1016                                 Transaction.Current = null;
1017                                 return;
1018                         }
1019                         Assert.Fail ( "Should not be reached" );
1020                 }
1021
1022                 [Test]
1023                 public void ExplicitTransaction15 ()
1024                 {
1025                         CommittableTransaction ct = new CommittableTransaction ();
1026                         IntResourceManager irm = new IntResourceManager ( 1 );
1027                         IntResourceManager irm2 = new IntResourceManager ( 3 );
1028
1029                         Assert.IsNull ( Transaction.Current );
1030                         Transaction.Current = ct;
1031
1032                         try {
1033                                 using (TransactionScope scope = new TransactionScope ()) {
1034                                         irm.Value = 2;
1035                                         Transaction.Current = new CommittableTransaction ();
1036                                         irm2.Value = 6;
1037                                 }
1038                         } catch (InvalidOperationException) {
1039                                 irm.Check ( 0, 0, 1, 0, "irm" );
1040                                 irm2.Check ( 0, 0, 1, 0, "irm2" );
1041                                 Transaction.Current = null;
1042                                 return;
1043                         }
1044
1045                         Assert.Fail ( "Should not be reached" );
1046                 }
1047
1048                 [Test]
1049                 public void ExplicitTransaction16 ()
1050                 {
1051                         CommittableTransaction ct = new CommittableTransaction ();
1052                         IntResourceManager irm0 = new IntResourceManager ( 3 );
1053                         IntResourceManager irm = new IntResourceManager ( 1 );
1054
1055                         Assert.IsNull ( Transaction.Current );
1056
1057                         Transaction.Current = ct;
1058
1059                         irm.FailPrepare = true;
1060                         irm.FailWithException = true;
1061                         irm.Value = 2;
1062                         irm0.Value = 6;
1063
1064                         try {
1065                                 ct.Commit ();
1066                         } catch (TransactionAbortedException e) {
1067                                 Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
1068                                 Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
1069                                 irm.Check ( 1, 0, 0, 0, "irm" );
1070                                 irm0.Check ( 0, 0, 1, 0, "irm0" );
1071                                 Transaction.Current = null;
1072                                 return;
1073                         }
1074                          
1075                         Assert.Fail ( "Should not be reached" );
1076                 }
1077
1078                 #endregion
1079         }
1080
1081 }
1082