1 22 package org.jboss.test; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.StringTokenizer ; 27 28 import javax.management.MBeanServerConnection ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 import javax.naming.InitialContext ; 32 33 import org.jboss.logging.Logger; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 51 public class JBossTestCase 52 extends AbstractTestCaseWithSetup 53 { 54 55 58 protected static Exception deploymentException = null; 59 60 protected JBossTestServices delegate; 61 62 protected Logger log; 63 64 68 public static AbstractTestDelegate getDelegate(Class clazz) throws Exception 69 { 70 AbstractTestDelegate delegate = new JBossTestServices(clazz); 71 return delegate; 72 } 73 74 80 public JBossTestCase(String name) 81 { 82 super(name); 83 } 84 85 91 protected void setUp() throws Exception 92 { 93 super.setUp(); 94 log = getLog(); 95 delegate = (JBossTestServices) AbstractTestSetup.delegate; 96 } 97 98 102 protected void tearDown() throws Exception 103 { 104 if (delegate != null) 105 delegate.tearDown(); 106 } 107 108 112 @Override 113 public Logger getLog() 114 { 115 Logger theLog; 116 if( delegate == null ) 117 { 118 theLog = Logger.getLogger(getClass()); 119 } 120 else 121 { 122 theLog = super.getLog(); 123 } 124 return theLog; 125 } 126 127 public void resetDelegate() 128 { 129 try 130 { 131 delegate.reinit(); 132 } 133 catch(Exception e) 134 { 135 log.error("Failed to init delegate", e); 136 } 137 } 138 139 141 142 150 public void serverFound() throws Exception 151 { 152 if (deploymentException != null) 153 throw deploymentException; 154 assertTrue("Server was not found", getServer() != null); 155 } 156 157 159 165 protected InitialContext getInitialContext() throws Exception 166 { 167 return delegate.getInitialContext(); 168 } 169 170 176 protected MBeanServerConnection getServer() throws Exception 177 { 178 return delegate.getServer(); 179 } 180 181 187 protected ObjectName getDeployerName() throws MalformedObjectNameException 188 { 189 return delegate.getDeployerName(); 190 } 191 192 193 203 protected URL getDeployURL(final String filename) throws MalformedURLException 204 { 205 return delegate.getDeployURL(filename); 206 } 207 208 217 protected String getResourceURL(final String resource) throws MalformedURLException 218 { 219 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 220 URL resURL = loader.getResource(resource); 221 return resURL != null ? resURL.toString() : null; 222 } 223 224 225 236 protected Object invoke(ObjectName name, String method, Object [] args, String [] sig) throws Exception 237 { 238 return delegate.invoke(name, method, args, sig); 239 } 240 241 248 protected void deploy(String name) throws Exception 249 { 250 delegate.deploy(name); 251 } 252 253 260 protected void redeploy(String name) throws Exception 261 { 262 delegate.redeploy(name); 263 } 264 265 272 protected void undeploy(String name) throws Exception 273 { 274 delegate.undeploy(name); 275 } 276 277 285 public static Test getDeploySetup(final Class clazz, final Test test, final String jarNames) throws Exception 286 { 287 JBossTestSetup wrapper = new JBossTestSetup(clazz, test) 288 { 289 protected void setUp() throws Exception 290 { 291 super.setUp(); 292 deploymentException = null; 293 try 294 { 295 this.delegate.init(); 296 297 if (this.delegate.isSecure()) 298 this.delegate.login(); 299 300 if (jarNames == null) return; 301 302 StringTokenizer st = new StringTokenizer (jarNames, ", "); 304 while (st != null && st.hasMoreTokens()) 305 { 306 String jarName = st.nextToken(); 307 this.redeploy(jarName); 308 this.getLog().debug("deployed package: " + jarName); 309 } 310 } 311 catch (Exception ex) 312 { 313 deploymentException = ex; 315 } 316 } 317 318 protected void tearDown() throws Exception 319 { 320 if (jarNames == null) return; 322 StringTokenizer st = new StringTokenizer (jarNames, ", "); 324 String [] depoyments = new String [st.countTokens()]; 325 for (int i = depoyments.length - 1; i >= 0; i--) 326 depoyments[i] = st.nextToken(); 327 for (int i = 0; i < depoyments.length; i++) 328 { 329 String jarName = depoyments[i]; 330 this.undeploy(jarName); 331 this.getLog().debug("undeployed package: " + jarName); 332 } 333 334 if (this.delegate.isSecure()) 335 this.delegate.logout(); 336 super.tearDown(); 337 } 338 }; 339 return wrapper; 340 } 341 342 public static Test getDeploySetup(final Test test, final String jarName) 343 throws Exception 344 { 345 return getDeploySetup(JBossTestCase.class, test, jarName); 346 } 347 public static Test getDeploySetup(final Class clazz, final String jarName) 348 throws Exception 349 { 350 TestSuite suite = new TestSuite(); 351 suite.addTest(new TestSuite(clazz)); 352 return getDeploySetup(clazz, suite, jarName); 353 } 354 355 protected String getJndiURL() 356 { 357 return delegate.getJndiURL(); 358 } 359 360 protected String getJndiInitFactory() 361 { 362 return delegate.getJndiInitFactory(); 363 } 364 365 protected int getThreadCount() 366 { 367 return delegate.getThreadCount(); 368 } 369 370 protected int getIterationCount() 371 { 372 return delegate.getIterationCount(); 373 } 374 375 protected int getBeanCount() 376 { 377 return delegate.getBeanCount(); 378 } 379 380 386 public String getServerHost() 387 { 388 return delegate.getServerHost(); 389 } 390 391 protected void flushAuthCache() throws Exception 392 { 393 flushAuthCache("other"); 394 } 395 396 protected void flushAuthCache(String domain) throws Exception 397 { 398 delegate.flushAuthCache(domain); 399 } 400 401 404 protected void restartDBPool() throws Exception 405 { 406 delegate.restartDBPool(); 407 } 408 409 protected void sleep(long interval) throws InterruptedException 410 { 411 synchronized (this) 412 { 413 wait(interval); 414 } 415 } 416 } 417 | Popular Tags |