KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > transaction > AlfrescoTransactionSupportTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.transaction;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.transaction.UserTransaction JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
27 import org.alfresco.service.ServiceRegistry;
28 import org.alfresco.service.transaction.TransactionService;
29 import org.alfresco.util.ApplicationContextHelper;
30 import org.springframework.context.ApplicationContext;
31
32 /**
33  * Tests integration between our <tt>UserTransaction</tt> implementation and
34  * our <tt>TransactionManager</tt>.
35  *
36  * @see org.alfresco.repo.transaction.AlfrescoTransactionManager
37  * @see org.alfresco.util.transaction.SpringAwareUserTransaction
38  *
39  * @author Derek Hulley
40  */

41 public class AlfrescoTransactionSupportTest extends TestCase
42 {
43     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
44
45     private ServiceRegistry serviceRegistry;
46     TransactionService transactionService;
47     
48     public void setUp() throws Exception JavaDoc
49     {
50         serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
51         transactionService = serviceRegistry.getTransactionService();
52     }
53     
54     public void testTransactionId() throws Exception JavaDoc
55     {
56         // get a user transaction
57
TransactionService transactionService = serviceRegistry.getTransactionService();
58         UserTransaction JavaDoc txn = transactionService.getUserTransaction();
59         assertNull("Thread shouldn't have a txn ID", AlfrescoTransactionSupport.getTransactionId());
60         
61         // begine the txn
62
txn.begin();
63         String JavaDoc txnId = AlfrescoTransactionSupport.getTransactionId();
64         assertNotNull("Expected thread to have a txn id", txnId);
65         
66         // check that the txn id doesn't change
67
String JavaDoc txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
68         assertEquals("Transaction ID changed on same thread", txnId, txnIdCheck);
69         
70         // begin a new, inner transaction
71
{
72             UserTransaction JavaDoc txnInner = transactionService.getNonPropagatingUserTransaction();
73             
74             String JavaDoc txnIdInner = AlfrescoTransactionSupport.getTransactionId();
75             assertEquals("Inner transaction not started, so txn ID should not change", txnId, txnIdInner);
76             
77             // begin the nested txn
78
txnInner.begin();
79             // check the ID for the outer transaction
80
txnIdInner = AlfrescoTransactionSupport.getTransactionId();
81             assertNotSame("Inner txn ID must be different from outer txn ID", txnIdInner, txnId);
82             
83             // rollback the nested txn
84
txnInner.rollback();
85             txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
86             assertEquals("Txn ID not popped inner txn completion", txnId, txnIdCheck);
87         }
88         
89         // rollback
90
txn.rollback();
91         assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId());
92         
93         // start a new transaction
94
txn = transactionService.getUserTransaction();
95         txn.begin();
96         txnIdCheck = AlfrescoTransactionSupport.getTransactionId();
97         assertNotSame("New transaction has same ID", txnId, txnIdCheck);
98         
99         // rollback
100
txn.rollback();
101         assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId());
102     }
103     
104     public void testListener() throws Exception JavaDoc
105     {
106         final List JavaDoc<String JavaDoc> strings = new ArrayList JavaDoc<String JavaDoc>(1);
107
108         // anonymous inner class to test it
109
TransactionListener listener = new TransactionListener()
110         {
111             public void flush()
112             {
113                 strings.add("flush");
114             }
115             public void beforeCommit(boolean readOnly)
116             {
117                 strings.add("beforeCommit");
118             }
119             public void beforeCompletion()
120             {
121                 strings.add("beforeCompletion");
122             }
123             public void afterCommit()
124             {
125                 strings.add("afterCommit");
126             }
127             public void afterRollback()
128             {
129                 strings.add("afterRollback");
130             }
131         };
132         
133         // begin a transaction
134
UserTransaction JavaDoc txn = transactionService.getUserTransaction();
135         txn.begin();
136         
137         // register it
138
AlfrescoTransactionSupport.bindListener(listener);
139
140         // test flush
141
AlfrescoTransactionSupport.flush();
142         assertTrue("flush not called on listener", strings.contains("flush"));
143         
144         // test commit
145
txn.commit();
146         assertTrue("beforeCommit not called on listener", strings.contains("beforeCommit"));
147         assertTrue("beforeCompletion not called on listener", strings.contains("beforeCompletion"));
148         assertTrue("afterCommit not called on listener", strings.contains("afterCommit"));
149     }
150     
151     /**
152      * Tests the condition whereby a listener can cause failure by attempting to bind itself to
153      * the transaction in the pre-commit callback. This is caused by the listener set being
154      * modified during calls to the listeners.
155      */

156     public void testPreCommitListenerBinding() throws Exception JavaDoc
157     {
158         final String JavaDoc beforeCommit = "beforeCommit";
159         final String JavaDoc afterCommitInner = "afterCommit - inner";
160         final String JavaDoc afterCommitOuter = "afterCommit = outer";
161         
162         // the listeners will play with this
163
final List JavaDoc<String JavaDoc> testList = new ArrayList JavaDoc<String JavaDoc>(1);
164         testList.add(beforeCommit);
165         testList.add(afterCommitInner);
166         testList.add(afterCommitOuter);
167         
168         final TransactionListener listener = new TransactionListenerAdapter()
169         {
170             @Override JavaDoc
171             public int hashCode()
172             {
173                 // force this listener to be first in the bound set
174
return 100;
175             }
176             @Override JavaDoc
177             public void beforeCommit(boolean readOnly)
178             {
179                 testList.remove(beforeCommit);
180                 TransactionListener postCommitListener = new TransactionListenerAdapter()
181                 {
182                     @Override JavaDoc
183                     public void afterCommit()
184                     {
185                         testList.remove(afterCommitInner);
186                     }
187                 };
188                 // register bogus on the transaction
189
AlfrescoTransactionSupport.bindListener(postCommitListener);
190             }
191             @Override JavaDoc
192             public void afterCommit()
193             {
194                 testList.remove(afterCommitOuter);
195             }
196         };
197         final TransactionListener dummyListener = new TransactionListenerAdapter()
198         {
199             @Override JavaDoc
200             public int hashCode()
201             {
202                 // force the dummy listener to be AFTER the binding listener
203
return 200;
204             }
205         };
206         // start a transaction
207
TransactionWork<Object JavaDoc> bindWork = new TransactionWork<Object JavaDoc>()
208         {
209             public Object JavaDoc doWork() throws Exception JavaDoc
210             {
211                 // just bind the listener to the transaction
212
AlfrescoTransactionSupport.bindListener(dummyListener);
213                 AlfrescoTransactionSupport.bindListener(listener);
214                 return null;
215             }
216         };
217         // kick it all off
218
TransactionUtil.executeInNonPropagatingUserTransaction(transactionService, bindWork);
219         
220         // make sure that the binding all worked
221
assertTrue("Expected callbacks not all processed: " + testList, testList.size() == 0);
222     }
223 }
224
Popular Tags