1 22 package org.jboss.test.dtm.test; 23 24 import org.jboss.test.JBossTestCase; 25 import org.jboss.test.dtm.interfaces.FrontEnd; 26 import org.jboss.test.dtm.interfaces.FrontEndHome; 27 import javax.naming.Context ; 28 import javax.rmi.PortableRemoteObject ; 29 import javax.transaction.RollbackException ; 30 import javax.transaction.TransactionRolledbackException ; 31 import javax.transaction.UserTransaction ; 32 33 import junit.framework.Test; 34 35 public class T09DTMXAUnitTestCase 36 extends JBossTestCase 37 { 38 public T09DTMXAUnitTestCase(String name) 39 throws java.io.IOException 40 { 41 super(name); 42 } 43 44 46 public void testCommittedTx() 47 throws Exception 48 { 49 getLog().debug("+++ testCommittedTx"); 50 51 Context ctx = getInitialContext(); 52 53 getLog().debug("Obtain UserTransaction instance"); 54 UserTransaction userTx = (UserTransaction ) ctx.lookup("UserTransaction"); 55 56 getLog().debug("Obtain home interface"); 57 Object objref = ctx.lookup("dtmtest/FrontEndEJB"); 58 FrontEndHome home = 59 (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class); 60 61 getLog().debug("Create FrontEnd bean"); 62 FrontEnd frontEnd = home.create("testCommittedTx"); 63 64 getLog().debug("Set balances"); 65 userTx.begin(); 66 frontEnd.setBalances(101, 102, 103); 67 userTx.commit(); 68 69 int balances[]; 70 71 getLog().debug("Get balances"); 72 userTx.begin(); 73 balances = frontEnd.getBalances(); 74 userTx.commit(); 75 76 assertTrue("first balance == 101", balances[0] == 101); 77 assertTrue("second balance == 102", balances[1] == 102); 78 assertTrue("third balance == 103", balances[2] == 103); 79 80 getLog().debug("Update balances"); 81 userTx.begin(); 82 frontEnd.addToBalances(100); 83 userTx.commit(); 84 85 getLog().debug("Check updated balances"); 86 userTx.begin(); 87 balances = frontEnd.getBalances(); 88 userTx.commit(); 89 90 assertTrue("first balance == 201", balances[0] == 201); 91 assertTrue("second balance == 202", balances[1] == 202); 92 assertTrue("third balance == 203", balances[2] == 203); 93 94 getLog().debug("Remove FrontEnd bean"); 95 frontEnd.remove(); 96 } 97 98 public void testRolledbackTx() 99 throws Exception 100 { 101 getLog().debug("+++ testRolledbackTx"); 102 103 Context ctx = getInitialContext(); 104 105 getLog().debug("Obtain UserTransaction instance"); 106 UserTransaction userTx = (UserTransaction ) ctx.lookup("UserTransaction"); 107 108 getLog().debug("Obtain home interface"); 109 Object objref = ctx.lookup("dtmtest/FrontEndEJB"); 110 FrontEndHome home = 111 (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class); 112 113 getLog().debug("Create FrontEnd bean"); 114 FrontEnd frontEnd = home.create("testRolledbackTx"); 115 116 getLog().debug("Set balances"); 117 userTx.begin(); 118 frontEnd.setBalances(101, 102, 103); 119 userTx.commit(); 120 121 int balances[]; 122 123 getLog().debug("Get balances"); 124 userTx.begin(); 125 balances = frontEnd.getBalances(); 126 userTx.commit(); 127 128 assertTrue("first balance == 101", balances[0] == 101); 129 assertTrue("second balance == 102", balances[1] == 102); 130 assertTrue("third balance == 103", balances[2] == 103); 131 132 getLog().debug("Update balances and rollback"); 133 userTx.begin(); 134 frontEnd.addToBalances(100); 135 userTx.rollback(); 136 137 getLog().debug("Check that all balances remain the same"); 138 userTx.begin(); 139 balances = frontEnd.getBalances(); 140 userTx.commit(); 141 142 assertTrue("first balance == 101", balances[0] == 101); 143 assertTrue("second balance == 102", balances[1] == 102); 144 assertTrue("third balance == 103", balances[2] == 103); 145 146 frontEnd.remove(); 147 } 148 149 public void testSetRollbackOnly() 150 throws Exception 151 { 152 getLog().debug("+++ testSetRollbackOnly"); 153 154 Context ctx = getInitialContext(); 155 156 getLog().debug("Obtain UserTransaction instance"); 157 UserTransaction userTx = (UserTransaction ) ctx.lookup("UserTransaction"); 158 159 getLog().debug("Obtain home interface"); 160 Object objref = ctx.lookup("dtmtest/FrontEndEJB"); 161 FrontEndHome home = 162 (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class); 163 164 getLog().debug("Create FrontEnd bean"); 165 FrontEnd frontEnd = home.create("testSetRollbackOnly"); 166 167 getLog().debug("Set balances"); 168 userTx.begin(); 169 frontEnd.setBalances(101, 102, 103); 170 userTx.commit(); 171 172 int balances[]; 173 174 getLog().debug("Get balances"); 175 userTx.begin(); 176 balances = frontEnd.getBalances(); 177 userTx.commit(); 178 179 assertTrue("first balance == 101", balances[0] == 101); 180 assertTrue("second balance == 102", balances[1] == 102); 181 assertTrue("third balance == 103", balances[2] == 103); 182 183 getLog().debug("Update balances, set rollback only and try to commit"); 184 try 185 { 186 userTx.begin(); 187 frontEnd.addToBalances(100); 188 userTx.setRollbackOnly(); 189 userTx.commit(); 190 fail("RollbackException should have been thrown"); 191 } 192 catch (RollbackException e) 193 { 194 getLog().debug("Expected exception: " + e); 195 } 196 197 getLog().debug("Check that all balances remain the same"); 198 userTx.begin(); 199 balances = frontEnd.getBalances(); 200 userTx.commit(); 201 202 assertTrue("first balance == 101", balances[0] == 101); 203 assertTrue("second balance == 102", balances[1] == 102); 204 assertTrue("third balance == 103", balances[2] == 103); 205 206 getLog().debug("Update balances, set rollback only and try to commit"); 207 try 208 { 209 userTx.begin(); 210 frontEnd.addToBalances(100); 211 userTx.setRollbackOnly(); 212 frontEnd.addToBalances(50); 213 userTx.commit(); 214 fail("RollbackException should have been thrown"); 215 } 216 catch (RollbackException e) 217 { 218 getLog().debug("Expected exception: " + e); 219 } 220 221 getLog().debug("Check that all balances remain the same"); 222 userTx.begin(); 223 balances = frontEnd.getBalances(); 224 userTx.commit(); 225 226 assertTrue("first balance == 101", balances[0] == 101); 227 assertTrue("second balance == 102", balances[1] == 102); 228 assertTrue("third balance == 103", balances[2] == 103); 229 230 frontEnd.remove(); 231 } 232 233 public void testSetRollbackOnlyAtTheFrontEnd() 234 throws Exception 235 { 236 getLog().debug("+++ testSetRollbackOnlyAtTheFrontEnd"); 237 238 Context ctx = getInitialContext(); 239 240 getLog().debug("Obtain UserTransaction instance"); 241 UserTransaction userTx = (UserTransaction ) ctx.lookup("UserTransaction"); 242 243 getLog().debug("Obtain home interface"); 244 Object objref = ctx.lookup("dtmtest/FrontEndEJB"); 245 FrontEndHome home = 246 (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class); 247 248 getLog().debug("Create FrontEnd bean"); 249 FrontEnd frontEnd = home.create("testSetRollbackOnlyAtTheFrontEnd"); 250 251 getLog().debug("Set balances"); 252 userTx.begin(); 253 frontEnd.setBalances(101, 102, 103); 254 userTx.commit(); 255 256 int balances[]; 257 258 getLog().debug("Get balances"); 259 userTx.begin(); 260 balances = frontEnd.getBalances(); 261 userTx.commit(); 262 263 assertTrue("first balance == 101", balances[0] == 101); 264 assertTrue("second balance == 102", balances[1] == 102); 265 assertTrue("third balance == 103", balances[2] == 103); 266 267 getLog().debug("Update balances, set rollback only at the front end, " + 268 "and try to commit"); 269 try 270 { 271 userTx.begin(); 272 frontEnd.addToBalances(100); 273 frontEnd.setRollbackOnly(); 274 frontEnd.addToBalances(50); 275 userTx.commit(); 276 fail("RollbackException should have been thrown"); 277 } 278 catch (RollbackException e) 279 { 280 getLog().debug("Expected exception: " + e); 281 } 282 283 getLog().debug("Check that all balances remain the same"); 284 userTx.begin(); 285 balances = frontEnd.getBalances(); 286 userTx.commit(); 287 288 assertTrue("first balance == 101", balances[0] == 101); 289 assertTrue("second balance == 102", balances[1] == 102); 290 assertTrue("third balance == 103", balances[2] == 103); 291 292 frontEnd.remove(); 293 } 294 295 public void testSetRollbackOnlyAtTheFirstAccount() 296 throws Exception 297 { 298 getLog().debug("+++ testSetRollbackOnlyAtTheFirstAccount"); 299 300 Context ctx = getInitialContext(); 301 302 getLog().debug("Obtain UserTransaction instance"); 303 UserTransaction userTx = (UserTransaction ) ctx.lookup("UserTransaction"); 304 305 getLog().debug("Obtain home interface"); 306 Object objref = ctx.lookup("dtmtest/FrontEndEJB"); 307 FrontEndHome home = 308 (FrontEndHome) PortableRemoteObject.narrow(objref, FrontEndHome.class); 309 310 getLog().debug("Create FrontEnd bean"); 311 FrontEnd frontEnd = home.create("testSetRollbackOnlyAtTheFirstAccount"); 312 313 getLog().debug("Set balances"); 314 userTx.begin(); 315 frontEnd.setBalances(101, 102, 103); 316 userTx.commit(); 317 318 int balances[]; 319 320 getLog().debug("Get balances"); 321 userTx.begin(); 322 balances = frontEnd.getBalances(); 323 userTx.commit(); 324 325 assertTrue("first balance == 101", balances[0] == 101); 326 assertTrue("second balance == 102", balances[1] == 102); 327 assertTrue("third balance == 103", balances[2] == 103); 328 329 getLog().debug("Update balances, set rollback only at " + 330 "the first account, and try to commit"); 331 try 332 { 333 userTx.begin(); 334 frontEnd.addToBalances(100); 335 frontEnd.tellFirstAccountToSetRollbackOnly(); 336 frontEnd.addToBalances(50); 337 userTx.commit(); 338 fail("TransactionRolledbackException should have been thrown"); 339 } 340 catch (TransactionRolledbackException e) 341 { 342 getLog().debug("Expected exception: " + e); 343 344 userTx.rollback(); 347 348 frontEnd = home.create("testSetRollbackOnlyAtTheFirstAccount", true); 351 } 352 353 getLog().debug("Check that all balances remain the same"); 354 userTx.begin(); 355 balances = frontEnd.getBalances(); 356 userTx.commit(); 357 358 assertTrue("first balance == 101", balances[0] == 101); 359 assertTrue("second balance == 102", balances[1] == 102); 360 assertTrue("third balance == 103", balances[2] == 103); 361 362 frontEnd.remove(); 363 } 364 365 public static Test suite() throws Exception 366 { 367 java.net.URL url 368 = ClassLoader.getSystemResource("host0.jndi.properties"); 369 java.util.Properties host0JndiProps = new java.util.Properties (); 370 host0JndiProps.load(url.openStream()); 371 372 java.util.Properties systemProps = System.getProperties(); 373 systemProps.putAll(host0JndiProps); 374 System.setProperties(systemProps); 375 376 return getDeploySetup(T09DTMXAUnitTestCase.class, 377 "dtmfrontend2entities.jar"); 378 } 379 380 } 381 | Popular Tags |