1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.database;
29
30 import javax.transaction.Status;
31 import javax.transaction.UserTransaction;
32
33 import junit.framework.TestCase;
34
35 import org.apache.log4j.BasicConfigurator;
36 import org.jcontainer.dna.Logger;
37 import org.jcontainer.dna.impl.Log4JLogger;
38 import org.objectledge.context.Context;
39 import org.picocontainer.Startable;
40
41 /***
42 *
43 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
44 * @version $Id: JotmTransactionTest.java,v 1.8 2005/09/25 12:03:49 rafal Exp $
45 */
46 public class JotmTransactionTest extends TestCase
47 {
48 private Context context;
49
50 private Logger log;
51
52 private Transaction transaction;
53
54 private Transaction.GuardValve valve;
55
56 /***
57 * Constructor for JotmTransactionTest.
58 * @param arg0
59 */
60 public JotmTransactionTest(String arg0)
61 {
62 super(arg0);
63 }
64
65 public void setUp()
66 throws Exception
67 {
68 BasicConfigurator.resetConfiguration();
69 BasicConfigurator.configure();
70 log = new Log4JLogger(org.apache.log4j.Logger.getLogger(JotmTransactionTest.class));
71 context = new Context();
72 context.clearAttributes();
73 transaction = new JotmTransaction(2, 120, context, log, null);
74 valve = new Transaction.GuardValve(transaction, log);
75 }
76
77 public void tearDown()
78 {
79 ((Startable)transaction).stop();
80 }
81
82 public void testGetUserTransaction()
83 throws Exception
84 {
85 UserTransaction ut = transaction.getUserTransaction();
86 assertEquals(Status.STATUS_NO_TRANSACTION, ut.getStatus());
87 ut.begin();
88 assertEquals(Status.STATUS_ACTIVE, ut.getStatus());
89 ut.commit();
90 assertEquals(Status.STATUS_NO_TRANSACTION, ut.getStatus());
91 }
92
93 public void testCommit()
94 throws Exception
95 {
96 boolean controller = transaction.begin();
97 assertTrue(controller);
98 transaction.commit(controller);
99 }
100
101 public void testRollback()
102 throws Exception
103 {
104 boolean controller = transaction.begin();
105 assertTrue(controller);
106 transaction.rollback(controller);
107 }
108
109 public void testNestedCommit()
110 throws Exception
111 {
112 boolean controller1 = transaction.begin();
113 assertTrue(controller1);
114 boolean controller2 = transaction.begin();
115 assertFalse(controller2);
116 transaction.commit(controller2);
117 transaction.commit(controller1);
118 valve.process(context);
119 }
120
121 public void testNestedRollback()
122 throws Exception
123 {
124 boolean controller1 = transaction.begin();
125 assertTrue(controller1);
126 boolean controller2 = transaction.begin();
127 assertFalse(controller2);
128 transaction.rollback(controller2);
129 transaction.rollback(controller1);
130 }
131
132 public void testNestedMixed()
133 throws Exception
134 {
135 boolean controller1 = transaction.begin();
136 assertTrue(controller1);
137 boolean controller2 = transaction.begin();
138 assertFalse(controller2);
139 transaction.rollback(controller2);
140 try
141 {
142 transaction.commit(controller1);
143 fail("exception expected");
144 }
145 catch(Exception e)
146 {
147 assertEquals("commit failed", e.getMessage());
148 }
149 }
150
151 public void testGuardValve()
152 throws Exception
153 {
154 boolean controller1 = transaction.begin();
155 assertTrue(controller1);
156 boolean controller2 = transaction.begin();
157 assertFalse(controller2);
158 transaction.commit(controller2);
159 valve.process(context);
160 }
161
162 public void testGuardValveNoTracing()
163 throws Exception
164 {
165 transaction = new JotmTransaction(0, 120, context, log, null);
166 valve = new Transaction.GuardValve(transaction, log);
167 boolean controller1 = transaction.begin();
168 assertTrue(controller1);
169 boolean controller2 = transaction.begin();
170 assertFalse(controller2);
171 transaction.commit(controller2);
172 valve.process(context);
173 }
174 }