1 22 package org.jboss.ejb3.test.asynchronous.unit; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.util.Collection ; 26 import javax.ejb.EJBAccessException ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.ObjectName ; 29 import javax.transaction.RollbackException ; 30 import javax.transaction.UserTransaction ; 31 import org.jboss.aspects.asynch.AsynchProvider; 32 import org.jboss.aspects.asynch.Future; 33 import org.jboss.ejb3.Ejb3Registry; 34 import org.jboss.ejb3.JBossProxy; 35 import org.jboss.ejb3.asynchronous.Asynch; 36 import org.jboss.ejb3.test.asynchronous.SecuredStatelessRemote; 37 import org.jboss.ejb3.test.asynchronous.ServiceRemote; 38 import org.jboss.ejb3.test.asynchronous.StatefulClusteredRemote; 39 import org.jboss.ejb3.test.asynchronous.StatefulRemote; 40 import org.jboss.ejb3.test.asynchronous.StatelessClusteredRemote; 41 import org.jboss.ejb3.test.asynchronous.StatelessRemote; 42 import org.jboss.ejb3.test.asynchronous.TxSessionRemote; 43 import org.jboss.logging.Logger; 44 import org.jboss.security.SecurityAssociation; 45 import org.jboss.security.SimplePrincipal; 46 import org.jboss.test.JBossTestCase; 47 import junit.framework.Test; 48 49 53 public class AsynchronousTestCase extends JBossTestCase 54 { 55 private static final Logger log = Logger.getLogger(AsynchronousTestCase.class); 56 57 static boolean deployed = false; 58 static int test = 0; 59 60 public AsynchronousTestCase(String name) 61 { 62 super(name); 63 } 64 65 public void testSLRemoteAsynchronous() throws Exception 66 { 67 StatelessRemote tester = 68 (StatelessRemote) getInitialContext().lookup("StatelessBean/remote"); 69 assertEquals("Wrong return for stateless remote", 11, tester.method(11)); 70 71 StatelessRemote asynchTester = (StatelessRemote)Asynch.getAsynchronousProxy(tester); 72 assertEquals("Wrong return value for stateless remote", 0, asynchTester.method(12)); 73 Future future = Asynch.getFutureResult(asynchTester); 74 int ret = (Integer ) future.get(); 75 assertEquals("Wrong async return value for stateless remote", ret, 12); 76 } 77 78 public void testSLClusteredAsynchronous() throws Exception 79 { 80 StatelessClusteredRemote tester = 81 (StatelessClusteredRemote) getInitialContext().lookup("StatelessClusteredBean/remote"); 82 assertEquals("Wrong return for stateless clustered", 21, tester.method(21)); 83 84 StatelessClusteredRemote asynchTester = (StatelessClusteredRemote)((JBossProxy)tester).getAsynchronousProxy(); 85 assertEquals("Wrong return value for stateless clustered", 0, asynchTester.method(22)); 86 AsynchProvider ap = (AsynchProvider) asynchTester; 87 Future future = ap.getFuture(); 88 int ret = (Integer ) future.get(); 89 assertEquals("Wrong async return value for stateless clustered", ret, 22); 90 } 91 92 public void testSLLocalAsynchrounous() throws Exception 93 { 94 MBeanServerConnection server = getServer(); 95 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=asynchronous"); 96 Object [] params = {}; 97 String [] sig = {}; 98 server.invoke(testerName, "testSLLocalAsynchronous", params, sig); 99 } 100 101 public void testSFRemoteAsynchronous() throws Exception 102 { 103 StatefulRemote tester = 104 (StatefulRemote) getInitialContext().lookup("StatefulBean/remote"); 105 assertEquals("Wrong return for stateful remote", 31, tester.method(31)); 106 107 StatefulRemote asynchTester = (StatefulRemote)((JBossProxy)tester).getAsynchronousProxy(); 108 assertEquals("Wrong return value for stateful remote", 0, asynchTester.method(32)); 109 AsynchProvider ap = (AsynchProvider) asynchTester; 110 Future future = ap.getFuture(); 111 int ret = (Integer ) future.get(); 112 assertEquals("Wrong async return value for stateful remote", ret, 32); 113 } 114 115 public void testSFClusteredAsynchronous() throws Exception 116 { 117 StatefulClusteredRemote tester = 118 (StatefulClusteredRemote) getInitialContext().lookup("StatefulClusteredBean/remote"); 119 assertEquals("Wrong return for stateful clustered", 41, tester.method(41)); 120 121 StatefulClusteredRemote asynchTester = (StatefulClusteredRemote)((JBossProxy)tester).getAsynchronousProxy(); 122 assertEquals("Wrong return value for stateful clustered", 0, asynchTester.method(42)); 123 AsynchProvider ap = (AsynchProvider) asynchTester; 124 Future future = ap.getFuture(); 125 int ret = (Integer ) future.get(); 126 assertEquals("Wrong async return value for stateful clustered", ret, 42); 127 } 128 129 public void testSFLocalAsynchrounous() throws Exception 130 { 131 MBeanServerConnection server = getServer(); 132 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=asynchronous"); 133 Object [] params = {}; 134 String [] sig = {}; 135 server.invoke(testerName, "testSFLocalAsynchronous", params, sig); 136 } 137 138 public void testServiceRemoteAsynchronous() throws Exception 139 { 140 ServiceRemote tester = 141 (ServiceRemote) getInitialContext().lookup("ServiceBean/remote"); 142 assertEquals("Wrong return for service remote", 51, tester.method(51)); 143 144 ServiceRemote asynchTester = (ServiceRemote)((JBossProxy)tester).getAsynchronousProxy(); 145 assertEquals("Wrong return value for service remote", 0, asynchTester.method(52)); 146 AsynchProvider ap = (AsynchProvider) asynchTester; 147 Future future = ap.getFuture(); 148 int ret = (Integer ) future.get(); 149 assertEquals("Wrong async return value for service remote", ret, 52); 150 } 151 152 public void testServiceLocalAsynchrounous() throws Exception 153 { 154 MBeanServerConnection server = getServer(); 155 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=asynchronous"); 156 Object [] params = {}; 157 String [] sig = {}; 158 server.invoke(testerName, "testServiceLocalAsynchronous", params, sig); 159 } 160 161 public void testAsynchSecurity() throws Exception 162 { 163 164 SecuredStatelessRemote tester = 165 (SecuredStatelessRemote) getInitialContext().lookup("SecuredStatelessBean/remote"); 166 SecuredStatelessRemote asynchTester = (SecuredStatelessRemote)((JBossProxy)tester).getAsynchronousProxy(); 167 AsynchProvider ap = (AsynchProvider)asynchTester; 168 169 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 170 SecurityAssociation.setCredential("password".toCharArray()); 171 172 asynchTester.method(61); 173 Object ret = getReturnOrException(ap); 174 assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException ); 175 176 asynchTester.uncheckedMethod(62); 177 ret = getReturnOrException(ap); 178 assertEquals("Wrong value", 62, ret); 179 180 asynchTester.excludedMethod(63); 181 ret = getReturnOrException(ap); 182 assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException ); 183 184 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 185 SecurityAssociation.setCredential("password".toCharArray()); 186 187 asynchTester.method(64); 188 ret = getReturnOrException(ap); 189 assertEquals("Wrong return for authorised method", 64, ret); 190 191 SecurityAssociation.setPrincipal(new SimplePrincipal("nosuchuser")); 192 SecurityAssociation.setCredential("password".toCharArray()); 193 194 asynchTester.method(65); 195 ret = getReturnOrException(ap); 196 assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException ); 197 } 198 199 public void testRemoteAsynchTransaction() throws Exception 200 { 201 TxSessionRemote tester = (TxSessionRemote) getInitialContext().lookup("TxSessionBean/remote"); 202 TxSessionRemote asynchTester = (TxSessionRemote)((JBossProxy)tester).getAsynchronousProxy(); 203 AsynchProvider ap = (AsynchProvider) asynchTester; 204 UserTransaction tx = (UserTransaction )getInitialContext().lookup("UserTransaction"); 205 206 tx.begin(); 208 tester.createFruit("apple", false); 209 tester.createFruit("pear", false); 210 tester.createFruit("tomato", false); 211 212 asynchTester.createVeg("Potato", false); 213 waitForProvider(ap); 214 asynchTester.createVeg("Turnip", false); 215 waitForProvider(ap); 216 asynchTester.createVeg("Carrot", false); 217 waitForProvider(ap); 218 tx.commit(); 219 220 tx.begin(); 221 Collection entries = tester.getEntries(); 222 tx.commit(); 223 assertEquals("Wrong number of entries", 6, entries.size()); 224 225 tx.begin(); 227 tester.cleanAll(); 228 tx.commit(); 229 230 tx.begin(); 231 entries = tester.getEntries(); 232 tx.commit(); 233 assertEquals("Wrong number of entries", 0, entries.size()); 234 235 tx.begin(); 237 tester.createFruit("apple", false); 238 tester.createFruit("pear", false); 239 tester.createFruit("tomato", false); 240 241 asynchTester.createVeg("Potato", false); 242 waitForProvider(ap); 243 asynchTester.createVeg("Turnip", false); 244 waitForProvider(ap); 245 asynchTester.createVeg("Carrot", false); 246 waitForProvider(ap); 247 tx.rollback(); 248 249 tx.begin(); 250 entries = tester.getEntries(); 251 tx.commit(); 252 253 assertEquals("Wrong number of entries", 0, entries.size()); 254 255 tx.begin(); 257 tester.createFruit("apple", false); 258 tester.createFruit("pear", false); 259 tester.createFruit("tomato", true); 260 261 asynchTester.createVeg("Potato", false); 262 waitForProvider(ap); 263 asynchTester.createVeg("Turnip", false); 264 waitForProvider(ap); 265 asynchTester.createVeg("Carrot", true); 266 waitForProvider(ap); 267 268 boolean rollbackException = false; 269 try 270 { 271 tx.commit(); 272 } 273 catch(RollbackException e) 274 { 275 rollbackException = true; 276 } 277 278 assertTrue("RollbackException not picked up", rollbackException); 279 280 tx.begin(); 281 entries = tester.getEntries(); 282 tx.commit(); 283 assertEquals("Wrong number of entries", 0, entries.size()); 284 } 285 286 public void testLocalAsynchTransaction() throws Exception 287 { 288 MBeanServerConnection server = getServer(); 289 ObjectName testerName = new ObjectName ("jboss.ejb3:service=Tester,test=asynchronous"); 290 Object [] params = {}; 291 String [] sig = {}; 292 server.invoke(testerName, "testLocalAsynchTransaction", params, sig); 293 294 } 295 296 private Object getReturnOrException(AsynchProvider provider)throws Exception 297 { 298 try 299 { 300 Future future = provider.getFuture(); 301 302 waitForFuture(future); 303 return future.get(); 304 } 305 catch(InvocationTargetException e) 306 { 307 return e.getCause(); 308 } 309 } 310 311 private void waitForProvider(AsynchProvider provider) throws InterruptedException 312 { 313 Future future = provider.getFuture(); 314 waitForFuture(future); 315 } 316 317 private void waitForFuture(Future future) throws InterruptedException 318 { 319 while (!future.isDone()) 320 { 321 Thread.sleep(100); 322 } 323 } 324 325 public static Test suite() throws Exception 326 { 327 return getDeploySetup(AsynchronousTestCase.class, "asynchronous-test.sar, asynchronous-test.jar"); 328 } 329 330 331 } 332 | Popular Tags |