1 22 package org.jboss.test.dtmusertx.test; 23 24 import javax.naming.Context ; 25 import javax.rmi.PortableRemoteObject ; 26 import javax.transaction.Status ; 27 import javax.transaction.UserTransaction ; 28 29 import junit.framework.Test; 30 31 import org.jboss.test.JBossTestCase; 32 import org.jboss.test.dtmusertx.interfaces.StatefulSession; 33 import org.jboss.test.dtmusertx.interfaces.StatefulSessionHome; 34 35 43 public class DTMUserTransactionUnitTestCase 44 extends JBossTestCase 45 { 46 public DTMUserTransactionUnitTestCase (String name) 47 throws java.io.IOException 48 { 49 super(name); 50 } 51 52 54 public void testUserTx() 55 throws Exception 56 { 57 getLog().debug("+++ testUsrTx"); 58 59 getLog().debug("Obtain home interface"); 60 Context ctx = getInitialContext(); 62 Object ref = ctx.lookup("dtmusertx/StatefulSessionBean"); 63 StatefulSessionHome home = 64 (StatefulSessionHome) PortableRemoteObject.narrow( 65 ref, StatefulSessionHome.class); 66 StatefulSession bean = home.create("testUserTx"); 67 68 bean.setCounter(100); 69 getLog().debug("Try to instantiate a UserTransaction"); 70 UserTransaction userTx = (UserTransaction )ctx.lookup("UserTransaction"); 71 userTx.begin(); 72 bean.incCounter(); 73 bean.incCounter(); 74 userTx.commit(); 75 int counter = bean.getCounter(); 76 assertTrue("counter == 102", counter == 102); 77 78 bean.setCounter(100); 79 userTx.begin(); 80 bean.incCounter(); 81 bean.incCounter(); 82 userTx.rollback(); 83 counter = bean.getCounter(); 84 assertTrue("counter == 100", counter == 100); 85 86 bean.remove(); 87 } 88 89 public void testSetRollbackOnly() 90 throws Exception 91 { 92 getLog().debug("+++ testSetRollbackOnly"); 93 94 getLog().debug("Obtain home interface"); 95 Context ctx = getInitialContext(); 97 Object ref = ctx.lookup("dtmusertx/StatefulSessionBean"); 98 StatefulSessionHome home = 99 (StatefulSessionHome) PortableRemoteObject.narrow( 100 ref, StatefulSessionHome.class); 101 StatefulSession bean = home.create("testUserTx"); 102 103 bean.setCounter(100); 104 getLog().debug("Try to instantiate a UserTransaction"); 105 UserTransaction userTx = (UserTransaction )ctx.lookup("UserTransaction"); 106 bean.setCounter(100); 107 try 108 { 109 userTx.begin(); 110 bean.incCounter(); 111 bean.incCounter(); 112 userTx.setRollbackOnly(); 113 userTx.commit(); 114 getLog().debug("Should not get here!"); 115 fail("RollbackException should have been thrown"); 116 } 117 catch(javax.transaction.RollbackException e) 118 { 119 getLog().debug("Expected exception: " + e); 120 } 121 assertTrue("userTx.getStatus() == Status.STATUS_NO_TRANSACTION", 122 userTx.getStatus() == Status.STATUS_NO_TRANSACTION); 123 assertTrue("counter == 100", bean.getCounter() == 100); 124 125 try 126 { 127 userTx.begin(); 128 bean.incCounter(); 129 userTx.setRollbackOnly(); 130 bean.incCounter(); userTx.commit(); 133 getLog().debug("Should not get here!"); 134 fail("Either TransactionRolledbackException or " + 135 "RollbackException should have been thrown"); 136 } 137 catch (javax.transaction.TransactionRolledbackException e) 138 { 139 getLog().debug("This does not happen in JBoss: " + e); 148 userTx.rollback(); } 151 catch(javax.transaction.RollbackException e) 152 { 153 getLog().debug("Expected exception: " + e); 154 } 155 assertTrue("userTx.getStatus() == Status.STATUS_NO_TRANSACTION", 156 userTx.getStatus() == Status.STATUS_NO_TRANSACTION); 157 assertTrue("counter == 100", bean.getCounter() == 100); 158 159 bean.remove(); 160 } 161 162 public void testTxMandatory() 163 throws Exception 164 { 165 getLog().debug("+++ testTxMandatory"); 166 167 getLog().debug("Obtain home interface"); 168 Context ctx = getInitialContext(); 170 Object ref = ctx.lookup("dtmusertx/StatefulSessionBean"); 171 StatefulSessionHome home = 172 (StatefulSessionHome) PortableRemoteObject.narrow( 173 ref, StatefulSessionHome.class); 174 StatefulSession bean = home.create("testTxMandatory"); 175 getLog().debug("Call txMandatoryMethod without a UserTransaction"); 176 try 177 { 178 bean.txMandatoryMethod("without a UserTransaction"); 179 getLog().debug("Should not get here!"); 180 fail("TransactionRequiredException should have been thrown"); 181 } 182 catch (javax.transaction.TransactionRequiredException e) 183 { 184 getLog().debug("Expected exception: " + e); 185 } 186 getLog().debug("Begin UserTransaction"); 187 UserTransaction userTx = (UserTransaction )ctx.lookup("UserTransaction"); 188 userTx.begin(); 189 bean.txMandatoryMethod("within a UserTransaction"); 190 getLog().debug("Commit UserTransaction"); 191 userTx.commit(); 192 bean.remove(); 193 } 194 195 public static Test suite() throws Exception 196 { 197 return getDeploySetup(DTMUserTransactionUnitTestCase.class, 198 "dtmusertx.jar"); 199 } 200 201 } 202 | Popular Tags |