1 22 package org.jboss.test.util.ejb; 23 24 25 import java.util.Properties ; 26 import javax.naming.InitialContext ; 27 import javax.rmi.PortableRemoteObject ; 28 29 import junit.framework.AssertionFailedError; 30 import junit.framework.TestCase; 31 import junit.framework.TestResult; 32 33 68 public class EJBTestCase extends TestCase 69 { 70 private boolean serverSide = false; 71 protected Properties props; 72 73 78 public EJBTestCase(String methodName) 79 { 80 super(methodName); 81 } 82 83 89 public void setServerSide(boolean serverSide) 90 { 91 this.serverSide = serverSide; 92 } 93 94 98 public boolean isServerSide() 99 { 100 return serverSide; 101 } 102 103 107 public String getEJBRunnerJndiName() 108 { 109 return "ejb/EJBTestRunner"; 110 } 111 112 115 public Properties getProps() 116 { 117 return props; 118 } 119 122 public void setProps(Properties props) 123 { 124 this.props = props; 125 } 126 127 public void run(TestResult result) 128 { 129 ClassLoader oldClassLoader = null; 130 try 131 { 132 if (!isServerSide()) 139 { 140 oldClassLoader = Thread.currentThread().getContextClassLoader(); 141 Thread.currentThread().setContextClassLoader( 142 getClass().getClassLoader()); 143 } 144 145 super.run(result); 146 } 147 finally 148 { 149 if (oldClassLoader != null) 151 { 152 Thread.currentThread().setContextClassLoader(oldClassLoader); 153 } 154 } 155 } 156 157 public void runBare() throws Throwable 158 { 159 if (!isServerSide()) 160 { 161 EJBTestRunner testRunner = null; 163 try 164 { 165 testRunner = getEJBTestRunner(); 166 if( props != null ) 167 testRunner.run(getClass().getName(), getName(), props); 168 else 169 testRunner.run(getClass().getName(), getName()); 170 } 171 catch (RemoteTestException e) 172 { 173 Throwable remote = e.getRemoteThrowable(); 181 if (remote instanceof AssertionFailedError) 182 { 183 throw new RemoteAssertionFailedError( 184 (AssertionFailedError) remote, e.getRemoteStackTrace()); 185 } 186 throw e; 187 } 188 finally 189 { 190 if (testRunner != null) 192 { 193 testRunner.remove(); 194 } 195 } 196 } 197 else 198 { 199 super.runBare(); 201 } 202 } 203 204 209 public void setUpEJB(Properties props) throws Exception 210 { 211 this.props = props; 212 } 213 214 219 public void tearDownEJB(Properties props) throws Exception 220 { 221 } 222 223 228 private EJBTestRunner getEJBTestRunner() throws Exception 229 { 230 InitialContext jndiContext = new InitialContext (); 231 232 String name = getEJBRunnerJndiName(); 234 Object ref = jndiContext.lookup(name); 235 EJBTestRunnerHome runnerHome = (EJBTestRunnerHome) 236 PortableRemoteObject.narrow(ref, EJBTestRunnerHome.class); 237 238 return runnerHome.create(); 240 } 241 } 242 | Popular Tags |