KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.transaction.RollbackException JavaDoc;
20 import javax.transaction.Status JavaDoc;
21 import javax.transaction.UserTransaction JavaDoc;
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 /**
33  * @see org.alfresco.repo.transaction.TransactionComponent
34  *
35  * @author Derek Hulley
36  */

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 JavaDoc
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 JavaDoc
56     {
57         // start a transaction
58
UserTransaction JavaDoc txnOuter = transactionComponent.getUserTransaction();
59         txnOuter.begin();
60         String JavaDoc txnIdOuter = AlfrescoTransactionSupport.getTransactionId();
61         
62         // start a propagating txn
63
UserTransaction JavaDoc txnInner = transactionComponent.getUserTransaction();
64         txnInner.begin();
65         String JavaDoc txnIdInner = AlfrescoTransactionSupport.getTransactionId();
66         
67         // the txn IDs should be the same
68
assertEquals("Txn ID not propagated", txnIdOuter, txnIdInner);
69         
70         // rollback the inner
71
txnInner.rollback();
72         
73         // check both transactions' status
74
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 JavaDoc e)
83         {
84             // expected
85
txnOuter.rollback();
86         }
87     }
88     
89     public void testNonPropagatingTxn() throws Exception JavaDoc
90     {
91         // start a transaction
92
UserTransaction JavaDoc txnOuter = transactionComponent.getUserTransaction();
93         txnOuter.begin();
94         String JavaDoc txnIdOuter = AlfrescoTransactionSupport.getTransactionId();
95         
96         // start a propagating txn
97
UserTransaction JavaDoc txnInner = transactionComponent.getNonPropagatingUserTransaction();
98         txnInner.begin();
99         String JavaDoc txnIdInner = AlfrescoTransactionSupport.getTransactionId();
100         
101         // the txn IDs should be different
102
assertNotSame("Txn ID not propagated", txnIdOuter, txnIdInner);
103         
104         // rollback the inner
105
txnInner.rollback();
106
107         // outer should commit without problems
108
txnOuter.commit();
109     }
110     
111     public void testReadOnlyTxn() throws Exception JavaDoc
112     {
113         // start a read-only transaction
114
transactionComponent.setAllowWrite(false);
115         
116         UserTransaction JavaDoc txn = transactionComponent.getUserTransaction();
117         txn.begin();
118         
119         // do some writing
120
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             // expected
132
}
133     }
134 }
135
Popular Tags