1 22 package org.jboss.test.util.ejb; 23 24 import java.lang.reflect.Constructor ; 25 import java.util.Properties ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.SessionBean ; 28 import javax.ejb.SessionContext ; 29 import javax.naming.Binding ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.naming.NamingEnumeration ; 33 import javax.transaction.Status ; 34 import javax.transaction.SystemException ; 35 36 45 public class EJBTestRunnerBean implements SessionBean 46 { 47 transient private SessionContext ctx; 48 private String runnerJndiName; 49 50 59 public void run(String className, String methodName) 60 throws RemoteTestException 61 { 62 Properties props = new Properties (); 63 try 64 { 65 InitialContext ctx = new InitialContext (); 66 NamingEnumeration bindings = ctx.listBindings("java:comp/env"); 67 while( bindings.hasMore() ) 68 { 69 Binding binding = (Binding ) bindings.next(); 70 String name = binding.getName(); 71 String value = binding.getObject().toString(); 72 props.setProperty(name, value); 73 } 74 } 75 catch(NamingException e) 76 { 77 throw new RemoteTestException(e); 78 } 79 run(className, methodName, props); 80 } 81 82 91 public void run(String className, String methodName, Properties props) 92 throws RemoteTestException 93 { 94 EJBTestCase testCase = getTestInstance(className, methodName); 95 96 setUpEJB(testCase, props); 97 98 RemoteTestException exception = null; 99 try 100 { 101 runTestCase(testCase); 102 } 103 catch (RemoteTestException e) 104 { 105 exception = e; 106 } 107 finally 108 { 109 try 110 { 111 tearDownEJB(testCase, props); 112 } 113 catch (RemoteTestException e) 114 { 115 if (exception != null) 117 { 118 exception = e; 119 } 120 } 121 if (exception != null) 122 { 123 throw exception; 124 } 125 } 126 } 127 128 134 private void setUpEJB(EJBTestCase testCase, Properties props) 135 throws RemoteTestException 136 { 137 try 138 { 139 ctx.getUserTransaction().begin(); 140 try 141 { 142 testCase.setUpEJB(props); 143 } 144 catch (Throwable e) 145 { 146 throw new RemoteTestException(e); 147 } 148 if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE) 149 { 150 ctx.getUserTransaction().commit(); 151 } 152 } 153 catch (Throwable e) 154 { 155 try 156 { 157 ctx.getUserTransaction().rollback(); 158 } 159 catch (SystemException unused) 160 { 161 } 163 if (e instanceof RemoteTestException) 164 { 165 throw (RemoteTestException) e; 166 } 167 throw new RemoteTestException(e); 168 } 169 } 170 171 177 private void runTestCase(EJBTestCase testCase) throws RemoteTestException 178 { 179 try 180 { 181 ctx.getUserTransaction().begin(); 182 try 183 { 184 testCase.runBare(); 185 } 186 catch (Throwable e) 187 { 188 throw new RemoteTestException(e); 189 } 190 if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE) 191 { 192 ctx.getUserTransaction().commit(); 193 } 194 } 195 catch (Throwable e) 196 { 197 try 198 { 199 ctx.getUserTransaction().rollback(); 200 } 201 catch (SystemException unused) 202 { 203 } 205 if (e instanceof RemoteTestException) 206 { 207 throw (RemoteTestException) e; 208 } 209 throw new RemoteTestException(e); 210 } 211 } 212 213 219 private void tearDownEJB(EJBTestCase testCase, Properties props) 220 throws RemoteTestException 221 { 222 223 try 224 { 225 ctx.getUserTransaction().begin(); 226 try 227 { 228 testCase.tearDownEJB(props); 229 } 230 catch (Throwable e) 231 { 232 throw new RemoteTestException(e); 233 } 234 if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE) 235 { 236 ctx.getUserTransaction().commit(); 237 } 238 } 239 catch (Throwable e) 240 { 241 try 242 { 243 ctx.getUserTransaction().rollback(); 244 } 245 catch (SystemException unused) 246 { 247 } 249 if (e instanceof RemoteTestException) 250 { 251 throw (RemoteTestException) e; 252 } 253 throw new RemoteTestException(e); 254 } 255 } 256 257 266 private EJBTestCase getTestInstance(String className, String methodName) 267 { 268 Class testClass = null; 269 try 270 { 271 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 272 testClass = loader.loadClass(className); 273 } 274 catch (ClassNotFoundException e) 275 { 276 throw new EJBException ("Test class not found : " + className); 277 } 278 279 Constructor constructor = null; 280 try 281 { 282 constructor = testClass.getConstructor(new Class []{String .class}); 283 } 284 catch (Exception e) 285 { 286 throw new EJBException ("Test class does not have a constructor " + 287 "which has a single String argument.", e); 288 } 289 290 try 291 { 292 EJBTestCase testCase = 293 (EJBTestCase) constructor.newInstance(new Object []{methodName}); 294 testCase.setServerSide(true); 295 return testCase; 296 } 297 catch (Exception e) 298 { 299 throw new EJBException ("Cannot instantiate test class: " + 300 testClass.getName(), e); 301 } 302 } 303 304 public void ejbCreate() 305 { 306 } 307 308 public void ejbRemove() 309 { 310 } 311 312 public void ejbActivate() 313 { 314 } 315 316 public void ejbPassivate() 317 { 318 } 319 320 public void setSessionContext(SessionContext ctx) 321 { 322 this.ctx = ctx; 323 } 324 } 325 | Popular Tags |