1 17 package org.alfresco.repo.transaction; 18 19 import javax.transaction.RollbackException ; 20 import javax.transaction.Status ; 21 import javax.transaction.UserTransaction ; 22 23 import junit.framework.TestCase; 24 25 import org.alfresco.service.cmr.repository.NodeService; 26 import org.alfresco.service.cmr.repository.StoreRef; 27 import org.alfresco.util.ApplicationContextHelper; 28 import org.springframework.context.ApplicationContext; 29 import org.springframework.dao.InvalidDataAccessApiUsageException; 30 import org.springframework.transaction.PlatformTransactionManager; 31 32 37 public class TransactionComponentTest extends TestCase 38 { 39 private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 40 41 private PlatformTransactionManager transactionManager; 42 private TransactionComponent transactionComponent; 43 private NodeService nodeService; 44 45 public void setUp() throws Exception 46 { 47 transactionManager = (PlatformTransactionManager) ctx.getBean("transactionManager"); 48 transactionComponent = new TransactionComponent(); 49 transactionComponent.setTransactionManager(transactionManager); 50 transactionComponent.setAllowWrite(true); 51 52 nodeService = (NodeService) ctx.getBean("dbNodeService"); 53 } 54 55 public void testPropagatingTxn() throws Exception 56 { 57 UserTransaction txnOuter = transactionComponent.getUserTransaction(); 59 txnOuter.begin(); 60 String txnIdOuter = AlfrescoTransactionSupport.getTransactionId(); 61 62 UserTransaction txnInner = transactionComponent.getUserTransaction(); 64 txnInner.begin(); 65 String txnIdInner = AlfrescoTransactionSupport.getTransactionId(); 66 67 assertEquals("Txn ID not propagated", txnIdOuter, txnIdInner); 69 70 txnInner.rollback(); 72 73 assertEquals("Inner txn not marked rolled back", Status.STATUS_ROLLEDBACK, txnInner.getStatus()); 75 assertEquals("Outer txn not marked for rolled back", Status.STATUS_MARKED_ROLLBACK, txnOuter.getStatus()); 76 77 try 78 { 79 txnOuter.commit(); 80 fail("Outer txn not marked for rollback"); 81 } 82 catch (RollbackException e) 83 { 84 txnOuter.rollback(); 86 } 87 } 88 89 public void testNonPropagatingTxn() throws Exception 90 { 91 UserTransaction txnOuter = transactionComponent.getUserTransaction(); 93 txnOuter.begin(); 94 String txnIdOuter = AlfrescoTransactionSupport.getTransactionId(); 95 96 UserTransaction txnInner = transactionComponent.getNonPropagatingUserTransaction(); 98 txnInner.begin(); 99 String txnIdInner = AlfrescoTransactionSupport.getTransactionId(); 100 101 assertNotSame("Txn ID not propagated", txnIdOuter, txnIdInner); 103 104 txnInner.rollback(); 106 107 txnOuter.commit(); 109 } 110 111 public void testReadOnlyTxn() throws Exception 112 { 113 transactionComponent.setAllowWrite(false); 115 116 UserTransaction txn = transactionComponent.getUserTransaction(); 117 txn.begin(); 118 119 try 121 { 122 nodeService.createStore( 123 StoreRef.PROTOCOL_WORKSPACE, 124 getName() + "_" + System.currentTimeMillis()); 125 txn.commit(); 126 fail("Read-only transaction wasn't detected"); 127 } 128 catch (InvalidDataAccessApiUsageException e) 129 { 130 int i = 0; 131 } 133 } 134 } 135 | Popular Tags |