package org.jboss.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
public class JBossTestCase
extends TestCase
{
protected Logger log;
protected static Exception deploymentException = null;
protected JBossTestServices delegate;
public JBossTestCase(String name)
{
super(name);
log = Logger.getLogger(getClass());
initDelegate();
}
public void initDelegate()
{
delegate = new JBossTestServices(getClass().getName());
try
{
delegate.init();
}
catch (Exception e)
{
log.error("Failed to init delegate", e);
}
}
public void testServerFound() throws Exception
{
if (deploymentException != null)
throw deploymentException;
assertTrue("Server was not found", getServer() != null);
}
protected InitialContext getInitialContext() throws Exception
{
return delegate.getInitialContext();
}
protected RMIAdaptor getServer() throws Exception
{
return delegate.getServer();
}
protected Logger getLog()
{
return log;
}
protected ObjectName getDeployerName() throws MalformedObjectNameException
{
return delegate.getDeployerName();
}
protected String getDeployURL(final String filename) throws MalformedURLException
{
return delegate.getDeployURL(filename);
}
protected String getResourceURL(final String resource) throws MalformedURLException
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resURL = loader.getResource(resource);
return resURL != null ? resURL.toString() : null;
}
protected Object invoke(ObjectName name, String method, Object[] args, String[] sig) throws Exception
{
return delegate.invoke(name, method, args, sig);
}
protected void deploy(String name) throws Exception
{
delegate.deploy(name);
}
protected void undeploy(String name) throws Exception
{
delegate.undeploy(name);
}
public static Test getDeploySetup(final Test test, final String jarNames)
throws Exception
{
JBossTestSetup wrapper = new JBossTestSetup(test)
{
protected void setUp() throws Exception
{
deploymentException = null;
try
{
this.delegate.init();
if (this.delegate.isSecure())
this.delegate.login();
if (jarNames == null) return;
StringTokenizer st = new StringTokenizer(jarNames, ", ");
while (st != null && st.hasMoreTokens())
{
String jarName = st.nextToken();
this.redeploy(jarName);
this.getLog().debug("deployed package: " + jarName);
}
}
catch (Exception ex)
{
deploymentException = ex;
}
}
protected void tearDown() throws Exception
{
if (jarNames == null) return;
StringTokenizer st = new StringTokenizer(jarNames, ", ");
String[] depoyments = new String[st.countTokens()];
for (int i = depoyments.length - 1; i >= 0; i--)
depoyments[i] = st.nextToken();
for (int i = 0; i < depoyments.length; i++)
{
String jarName = depoyments[i];
this.undeploy(jarName);
this.getLog().debug("undeployed package: " + jarName);
}
if (this.delegate.isSecure())
this.delegate.logout();
}
};
return wrapper;
}
public static Test getDeploySetup(final Class clazz, final String jarName)
throws Exception
{
TestSuite suite = new TestSuite();
suite.addTest(new TestSuite(clazz));
return getDeploySetup(suite, jarName);
}
protected String getJndiURL()
{
return delegate.getJndiURL();
}
protected String getJndiInitFactory()
{
return delegate.getJndiInitFactory();
}
protected int getThreadCount()
{
return delegate.getThreadCount();
}
protected int getIterationCount()
{
return delegate.getIterationCount();
}
protected int getBeanCount()
{
return delegate.getBeanCount();
}
public String getServerHost()
{
return delegate.getServerHost();
}
protected void flushAuthCache() throws Exception
{
flushAuthCache("other");
}
protected void flushAuthCache(String domain) throws Exception
{
delegate.flushAuthCache(domain);
}
protected void restartDBPool() throws Exception
{
delegate.restartDBPool();
}
protected void sleep(long interval) throws InterruptedException
{
synchronized (this)
{
wait(interval);
}
}
}