KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > taskman > TransactionTest


1 package org.sapia.taskman;
2
3 import junit.framework.TestCase;
4
5 import org.sapia.taskman.transaction.Transaction;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * @author Yanick Duchesne
12  *
13  * <dl>
14  * <dt><b>Copyright: </b>
15  * <dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open
16  * Source Software </a>. All Rights Reserved.</dd>
17  * </dt>
18  * <dt><b>License: </b>
19  * <dd>Read the license.txt file of the jar or visit the <a
20  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
21  * OSS web site</dd>
22  * </dt>
23  * </dl>
24  */

25 public class TransactionTest extends TestCase {
26   public TransactionTest(String JavaDoc name) {
27     super(name);
28   }
29
30   public void testSyncTx() throws Exception JavaDoc {
31     TaskManager tm = new TaskManager("test");
32     tm.setDaemon(true);
33     tm.start();
34
35     TestTask t1 = new TestTask();
36     TestTask t2 = new TestTask();
37     TestTask t3 = new TestTask();
38     TestTransactionListener listener = new TestTransactionListener();
39     Transaction tx = tm.newTransaction(listener);
40     tm.execSyncTask("t1", t1);
41     tm.execSyncTask("t2", t2);
42     tm.execSyncTask("t3", t3);
43     tx.commit();
44     Thread.sleep(3000);
45     tm.shutdown();
46     super.assertTrue("Task 1 not executed", t1.execCount == 1);
47     super.assertTrue("Task 2 not executed", t2.execCount == 1);
48     super.assertTrue("Task 3 not executed", t3.execCount == 1);
49     super.assertTrue("onBegin not called on Tx listener", listener.begin);
50     super.assertTrue("onCompleted not called on Tx listener",
51         listener.completed);
52     super.assertTrue("Tx should have been successfull", listener.success);
53     super.assertTrue("Tx should not have generated errors", !listener.error);
54     super.assertTrue("Thread should not be in Tx", !tm.isInTransaction());
55   }
56
57   public void testSyncErrTx() throws Exception JavaDoc { TaskManager tm = new
58     TaskManager("test");
59     tm.setDaemon(true);
60     tm.start();
61     TestTask t1 = new TestTask();
62     Task t2 = new TestTxErrTask();
63     TestTask t3 = new TestTask();
64     TestTransactionListener listener = new TestTransactionListener();
65     Transaction tx = tm.newTransaction(listener);
66     tm.execSyncTask("t1", t1);
67     tm.execSyncTask("t2", t2);
68     tm.execSyncTask("t3", t3);
69     tx.commit();
70     Thread.sleep(3000);
71     tm.shutdown();
72     super.assertTrue("Task 1 not executed", t1.execCount == 1);
73     super.assertTrue("Task 3 should not have been executed", t3.execCount == 0);
74     super.assertTrue("onBegin not called on Tx listener", listener.begin);
75     super.assertTrue("onCompleted not called on Tx listener", listener.completed);
76     super.assertTrue("Tx should not have been successfull", !listener.success);
77     super.assertTrue("Tx should have generated errors", listener.error); super.assertTrue("Thread should not be in Tx", !tm.isInTransaction());
78   }
79     
80   public void testNestedTx() throws Exception JavaDoc {
81     TaskManager tm = new TaskManager("test");
82     tm.setDaemon(true);
83     tm.start();
84     CompositeTestTask task = new CompositeTestTask();
85     List JavaDoc tasks = new ArrayList JavaDoc();
86     ComponentTestTask ct;
87     TestCounter counter = new TestCounter();
88     int i = 0;
89     for (; i < 5; i++) {
90       ct = new ComponentTestTask(i, counter);
91       task.addTask(ct);
92       tasks.add(ct);
93     }
94     TestTransactionListener listener = new TestTransactionListener();
95     Transaction tx = tm.newTransaction(listener);
96     ComponentTestTask last = new ComponentTestTask(i++, counter);
97     tm.execSyncTask("parent", task);
98     tm.execSyncTask("last", last);
99     tx.commit();
100     Thread.sleep(5000);
101     tm.shutdown();
102     super.assertEquals(last.index, last.currentCount);
103   }
104 }
105
Popular Tags