1 16 17 package org.springframework.jca.cci; 18 19 import javax.resource.ResourceException ; 20 import javax.resource.cci.Connection ; 21 import javax.resource.cci.ConnectionFactory ; 22 import javax.resource.cci.Interaction ; 23 import javax.resource.cci.InteractionSpec ; 24 import javax.resource.cci.LocalTransaction ; 25 import javax.resource.cci.Record ; 26 27 import junit.framework.TestCase; 28 import org.easymock.MockControl; 29 30 import org.springframework.dao.DataRetrievalFailureException; 31 import org.springframework.jca.cci.core.CciTemplate; 32 import org.springframework.transaction.TransactionStatus; 33 import org.springframework.transaction.support.TransactionCallback; 34 import org.springframework.transaction.support.TransactionCallbackWithoutResult; 35 import org.springframework.transaction.support.TransactionSynchronizationManager; 36 import org.springframework.transaction.support.TransactionTemplate; 37 38 41 public class CciLocalTransactionTests extends TestCase { 42 43 48 public void testLocalTransactionCommit() throws ResourceException { 49 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 50 final ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 51 MockControl connectionControl = MockControl.createControl(Connection .class); 52 Connection connection = (Connection ) connectionControl.getMock(); 53 MockControl interactionControl = MockControl.createControl(Interaction .class); 54 Interaction interaction = (Interaction ) interactionControl.getMock(); 55 MockControl localTransactionControl = MockControl.createControl(LocalTransaction .class); 56 LocalTransaction localTransaction = (LocalTransaction ) localTransactionControl.getMock(); 57 MockControl recordControl = MockControl.createControl(Record .class); 58 final Record record = (Record ) recordControl.getMock(); 59 60 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 61 final InteractionSpec interactionSpec = (InteractionSpec ) interactionSpecControl.getMock(); 62 63 connectionFactory.getConnection(); 64 connectionFactoryControl.setReturnValue(connection, 1); 65 66 connection.getLocalTransaction(); 67 connectionControl.setReturnValue(localTransaction, 1); 68 69 localTransaction.begin(); 70 localTransactionControl.setVoidCallable(1); 71 72 connection.createInteraction(); 73 connectionControl.setReturnValue(interaction); 74 75 interaction.execute(interactionSpec, record, record); 76 interactionControl.setReturnValue(true, 1); 77 78 interaction.close(); 79 interactionControl.setVoidCallable(1); 80 81 connection.getLocalTransaction(); 82 connectionControl.setReturnValue(localTransaction); 83 84 localTransaction.commit(); 85 localTransactionControl.setVoidCallable(1); 86 87 connection.close(); 88 connectionControl.setVoidCallable(1); 89 90 connectionFactoryControl.replay(); 91 connectionControl.replay(); 92 localTransactionControl.replay(); 93 interactionControl.replay(); 94 95 org.springframework.jca.cci.connection.CciLocalTransactionManager tm = new org.springframework.jca.cci.connection.CciLocalTransactionManager(); 96 tm.setConnectionFactory(connectionFactory); 97 TransactionTemplate tt = new TransactionTemplate(tm); 98 99 tt.execute(new TransactionCallbackWithoutResult() { 100 protected void doInTransactionWithoutResult(TransactionStatus status) { 101 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory)); 102 CciTemplate ct = new CciTemplate(connectionFactory); 103 ct.execute(interactionSpec, record, record); 104 } 105 }); 106 107 connectionFactoryControl.verify(); 108 connectionControl.verify(); 109 interactionControl.verify(); 110 localTransactionControl.verify(); 111 } 112 113 118 public void testLocalTransactionRollback() throws ResourceException { 119 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 120 final ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 121 MockControl connectionControl = MockControl.createControl(Connection .class); 122 Connection connection = (Connection ) connectionControl.getMock(); 123 MockControl interactionControl = MockControl.createControl(Interaction .class); 124 Interaction interaction = (Interaction ) interactionControl.getMock(); 125 MockControl localTransactionControl = MockControl.createControl(LocalTransaction .class); 126 LocalTransaction localTransaction = (LocalTransaction ) localTransactionControl.getMock(); 127 MockControl recordControl = MockControl.createControl(Record .class); 128 final Record record = (Record ) recordControl.getMock(); 129 130 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 131 final InteractionSpec interactionSpec = (InteractionSpec ) interactionSpecControl.getMock(); 132 133 connectionFactory.getConnection(); 134 connectionFactoryControl.setReturnValue(connection); 135 136 connection.getLocalTransaction(); 137 connectionControl.setReturnValue(localTransaction); 138 139 localTransaction.begin(); 140 localTransactionControl.setVoidCallable(1); 141 142 connection.createInteraction(); 143 connectionControl.setReturnValue(interaction); 144 145 interaction.execute(interactionSpec, record, record); 146 interactionControl.setReturnValue(true, 1); 147 148 interaction.close(); 149 interactionControl.setVoidCallable(1); 150 151 connection.getLocalTransaction(); 152 connectionControl.setReturnValue(localTransaction); 153 154 localTransaction.rollback(); 155 localTransactionControl.setVoidCallable(1); 156 157 connection.close(); 158 connectionControl.setVoidCallable(1); 159 160 connectionFactoryControl.replay(); 161 connectionControl.replay(); 162 localTransactionControl.replay(); 163 interactionControl.replay(); 164 165 org.springframework.jca.cci.connection.CciLocalTransactionManager tm = new org.springframework.jca.cci.connection.CciLocalTransactionManager(); 166 tm.setConnectionFactory(connectionFactory); 167 TransactionTemplate tt = new TransactionTemplate(tm); 168 169 try { 170 Object result = tt.execute(new TransactionCallback() { 171 public Object doInTransaction(TransactionStatus status) { 172 assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory)); 173 CciTemplate ct = new CciTemplate(connectionFactory); 174 ct.execute(interactionSpec, record, record); 175 throw new DataRetrievalFailureException("error"); 176 } 177 }); 178 } 179 catch (Exception ex) { 180 } 181 182 connectionFactoryControl.verify(); 183 connectionControl.verify(); 184 interactionControl.verify(); 185 localTransactionControl.verify(); 186 } 187 } 188 | Popular Tags |