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