1 17 package org.alfresco.repo.transaction; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.transaction.UserTransaction ; 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 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 49 { 50 serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); 51 transactionService = serviceRegistry.getTransactionService(); 52 } 53 54 public void testTransactionId() throws Exception 55 { 56 TransactionService transactionService = serviceRegistry.getTransactionService(); 58 UserTransaction txn = transactionService.getUserTransaction(); 59 assertNull("Thread shouldn't have a txn ID", AlfrescoTransactionSupport.getTransactionId()); 60 61 txn.begin(); 63 String txnId = AlfrescoTransactionSupport.getTransactionId(); 64 assertNotNull("Expected thread to have a txn id", txnId); 65 66 String txnIdCheck = AlfrescoTransactionSupport.getTransactionId(); 68 assertEquals("Transaction ID changed on same thread", txnId, txnIdCheck); 69 70 { 72 UserTransaction txnInner = transactionService.getNonPropagatingUserTransaction(); 73 74 String txnIdInner = AlfrescoTransactionSupport.getTransactionId(); 75 assertEquals("Inner transaction not started, so txn ID should not change", txnId, txnIdInner); 76 77 txnInner.begin(); 79 txnIdInner = AlfrescoTransactionSupport.getTransactionId(); 81 assertNotSame("Inner txn ID must be different from outer txn ID", txnIdInner, txnId); 82 83 txnInner.rollback(); 85 txnIdCheck = AlfrescoTransactionSupport.getTransactionId(); 86 assertEquals("Txn ID not popped inner txn completion", txnId, txnIdCheck); 87 } 88 89 txn.rollback(); 91 assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId()); 92 93 txn = transactionService.getUserTransaction(); 95 txn.begin(); 96 txnIdCheck = AlfrescoTransactionSupport.getTransactionId(); 97 assertNotSame("New transaction has same ID", txnId, txnIdCheck); 98 99 txn.rollback(); 101 assertNull("Thread shouldn't have a txn ID after rollback", AlfrescoTransactionSupport.getTransactionId()); 102 } 103 104 public void testListener() throws Exception 105 { 106 final List <String > strings = new ArrayList <String >(1); 107 108 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 UserTransaction txn = transactionService.getUserTransaction(); 135 txn.begin(); 136 137 AlfrescoTransactionSupport.bindListener(listener); 139 140 AlfrescoTransactionSupport.flush(); 142 assertTrue("flush not called on listener", strings.contains("flush")); 143 144 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 156 public void testPreCommitListenerBinding() throws Exception 157 { 158 final String beforeCommit = "beforeCommit"; 159 final String afterCommitInner = "afterCommit - inner"; 160 final String afterCommitOuter = "afterCommit = outer"; 161 162 final List <String > testList = new ArrayList <String >(1); 164 testList.add(beforeCommit); 165 testList.add(afterCommitInner); 166 testList.add(afterCommitOuter); 167 168 final TransactionListener listener = new TransactionListenerAdapter() 169 { 170 @Override 171 public int hashCode() 172 { 173 return 100; 175 } 176 @Override 177 public void beforeCommit(boolean readOnly) 178 { 179 testList.remove(beforeCommit); 180 TransactionListener postCommitListener = new TransactionListenerAdapter() 181 { 182 @Override 183 public void afterCommit() 184 { 185 testList.remove(afterCommitInner); 186 } 187 }; 188 AlfrescoTransactionSupport.bindListener(postCommitListener); 190 } 191 @Override 192 public void afterCommit() 193 { 194 testList.remove(afterCommitOuter); 195 } 196 }; 197 final TransactionListener dummyListener = new TransactionListenerAdapter() 198 { 199 @Override 200 public int hashCode() 201 { 202 return 200; 204 } 205 }; 206 TransactionWork<Object > bindWork = new TransactionWork<Object >() 208 { 209 public Object doWork() throws Exception 210 { 211 AlfrescoTransactionSupport.bindListener(dummyListener); 213 AlfrescoTransactionSupport.bindListener(listener); 214 return null; 215 } 216 }; 217 TransactionUtil.executeInNonPropagatingUserTransaction(transactionService, bindWork); 219 220 assertTrue("Expected callbacks not all processed: " + testList, testList.size() == 0); 222 } 223 } 224 | Popular Tags |