[jboss-cvs] jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb ...
Norman Richards
norman.richards at jboss.com
Wed Nov 1 13:14:22 EST 2006
User: nrichards
Date: 06/11/01 13:14:22
Added: jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb
EJBTestCase.java EJBTestRunner.java
EJBTestRunnerBean.java EJBTestRunnerHome.java
RemoteAssertionFailedError.java
RemoteTestException.java
Log:
modified for j2ee guide
Revision Changes Path
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/EJBTestCase.java
Index: EJBTestCase.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import junit.framework.TestResult;
/**
* An ejb test case is an extension to test case where the test is executed
* in the ejb server's virtual machine.
*
* Two new methods setUpEJB and tearDownEJB have been added. These methods
* work just like setUp and tearDown except they run in a sepperate transaction.
* The execution order is as follows:
* <pre>
* 1. setUpEJB (TX 1)
* 2. run (TX 2)
* 2.1. runBare
* 2.1.1 setUp
* 2.1.2 <your test method>
* 2.1.3 tearDown
* 3. ejbTearDown (TX 2)
* </pre>
*
* For an ejb test case to run successfully, the following must be setup:
* <pre>
* 1. The ejb test case class must be availabe to the client vm.
* 2. The ejb test case class must be availabe to the EJBTestRunner bean
* on the server.
* 3. The EJBTestRunnerHome must be bound to "ejb/EJBTestRunner" in the
* jndi context obtained from new InitialContext();
* 4. The EJBTestRunner bean must be configured as specified in the
* EJBTestRunner javadoc.
* </pre>
*
* @see EJBTestRunner
* @see junit.framework.TestCase
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
public class EJBTestCase extends TestCase
{
private boolean serverSide = false;
protected Properties props;
/**
* Constructs a test case that will run the method with the specified name.
* @param methodName the name of the method that will executed when this
* test is run
*/
public EJBTestCase(String methodName)
{
super(methodName);
}
/**
* Sets the flag that is used to determine if the class
* is running on the server side.
* @param serverSide boolean flag that this class uses to determine
* if it's running on the server side.
*/
public void setServerSide(boolean serverSide)
{
this.serverSide = serverSide;
}
/**
* Is this class running on the server side?
* @return true if this class is running on the server side
*/
public boolean isServerSide()
{
return serverSide;
}
/** Allow EJBTestCase subclasses to override the EJBRunnerHome JNDI name
* @return The JNDI name of the EJBRunnerHome home interface binding. The
* default is "ejb/EJBTestRunner"
*/
public String getEJBRunnerJndiName()
{
return "ejb/EJBTestRunner";
}
/**
* @return the properties associated with the test case
*/
public Properties getProps()
{
return props;
}
/**
* @param props the properties associated with the test case
*/
public void setProps(Properties props)
{
this.props = props;
}
public void run(TestResult result)
{
ClassLoader oldClassLoader = null;
try
{
// If we are on the server side, set the thread context class loader
// to the class loader that loaded this class. This fixes problems
// with the current implementation of the JUnit gui test runners class
// reloading logic. The gui relods the test classes with each run but
// does not set the context class loader so calls to Class.forName load
// the class in the wrong class loader.
if (!isServerSide())
{
oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader());
}
super.run(result);
}
finally
{
// be a good citizen, reset the context loader
if (oldClassLoader != null)
{
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
}
public void runBare() throws Throwable
{
if (!isServerSide())
{
// We're not on the server side yet, invoke the test on the serverside.
EJBTestRunner testRunner = null;
try
{
testRunner = getEJBTestRunner();
if( props != null )
testRunner.run(getClass().getName(), getName(), props);
else
testRunner.run(getClass().getName(), getName());
}
catch (RemoteTestException e)
{
// if the remote test exception is from an assertion error
// rethrow it with a sub class of AssertionFailedError so it is
// picked up as a failure and not an error.
// The server has to throw sub classes of Error because that is the
// allowable scope of application exceptions. So
// AssertionFailedError which is an instance of error has to be
// wrapped in an exception.
Throwable remote = e.getRemoteThrowable();
if (remote instanceof AssertionFailedError)
{
throw new RemoteAssertionFailedError(
(AssertionFailedError) remote, e.getRemoteStackTrace());
}
throw e;
}
finally
{
// be a good citizen, drop my ref to the session bean.
if (testRunner != null)
{
testRunner.remove();
}
}
}
else
{
// We're on the server side so, invoke the test the usual way.
super.runBare();
}
}
/** Sets up the ejb test case. This method is called before
* each test is executed and is run in a private transaction.
* @param props the properties passed in from the client
* @throws Exception if a problem occures
*/
public void setUpEJB(Properties props) throws Exception
{
this.props = props;
}
/** Tears down the ejb test case. This method is called after
* each test is executed and is run in a private transaction.
* @param props the properties passed in from the client
* @throws Exception if a problem occures
*/
public void tearDownEJB(Properties props) throws Exception
{
}
/**
* Looks up the ejb test runner home in JNDI (at "ejb/EJBTestRunner")
* and creates a new runner.
* @throws Exception if any problem happens
*/
private EJBTestRunner getEJBTestRunner() throws Exception
{
InitialContext jndiContext = new InitialContext();
// Get a reference from this to the Bean's Home interface
String name = getEJBRunnerJndiName();
Object ref = jndiContext.lookup(name);
EJBTestRunnerHome runnerHome = (EJBTestRunnerHome)
PortableRemoteObject.narrow(ref, EJBTestRunnerHome.class);
// create the test runner
return runnerHome.create();
}
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/EJBTestRunner.java
Index: EJBTestRunner.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.EJBObject;
/**
* The remote interface of the server side test runner. The EJBTestClient calls
* run with the names of the test class and test method to execute. Then run
* calls setUpEJB, runTestCase, and tearDownEJB in sepperate transactions. In
* order for the the tests to run as expected by the client the EJBTestRunner
* bean must be setup exactly as follows in the ejb-jar.xml file:
* <pre>
* <?xml version="1.0"?>
* <!DOCTYPE ejb-jar PUBLIC
* "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
* "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd">
* <ejb-jar>
* <enterprise-beans>
* <session>
* <description>JUnit Session Bean Test Runner</description>
* <ejb-name>EJBTestRunnerEJB</ejb-name>
* <home>net.sourceforge.junitejb.EJBTestRunnerHome</home>
* <remote>net.sourceforge.junitejb.EJBTestRunner</remote>
* <ejb-class>net.sourceforge.junitejb.EJBTestRunnerBean</ejb-class>
* <session-type>Stateless</session-type>
* <transaction-type>Bean</transaction-type>
* </session>
* </enterprise-beans>
* </ejb-jar>
* </pre>
*
* Additionally, the home interface must be bount to the jndi name:
* "ejb/EJBTestRunner"
*
* It is recomended that the test classes and the classes of JUnitEJB be
* packaged into a single jar.
*
* @see EJBTestCase
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
public interface EJBTestRunner extends EJBObject
{
/** Runs the specified test method on the specified class by calling
* run(className, methodName, props) with props built from the java:comp/env
* bindings.
*
* @param className the name of the test class
* @param methodName the name of the test method
* @throws RemoteTestException If any throwable is thrown during execution
* of the method, it is wrapped with a RemoteTestException and rethrown.
*/
public void run(String className, String methodName)
throws RemoteTestException, RemoteException;
/**
* Runs the specified test method on the specified class.
* @param className the name of the test class
* @param methodName the name of the test method
* @param props any properties passed in from the client for use by the
* server side tests
* @throws RemoteTestException If any throwable is thrown during execution
* of the method, it is wrapped with a RemoteTestException and rethrown.
*/
public void run(String className, String methodName, Properties props)
throws RemoteTestException, RemoteException;
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/EJBTestRunnerBean.java
Index: EJBTestRunnerBean.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import java.lang.reflect.Constructor;
import java.util.Properties;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Binding;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.transaction.Status;
import javax.transaction.SystemException;
/**
* Implementation of the ejb test runner.
*
* @see EJBTestRunner
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
public class EJBTestRunnerBean implements SessionBean
{
transient private SessionContext ctx;
private String runnerJndiName;
/** Run the specified test method on the given class name using a Properties
* map built from all java:comp/env entries.
*
* @param className the name of the test class
* @param methodName the name of the test method
* @throws RemoteTestException If any throwable is thrown during
* execution of the method, it is wrapped with a RemoteTestException and
* rethrown.
*/
public void run(String className, String methodName)
throws RemoteTestException
{
Properties props = new Properties();
try
{
InitialContext ctx = new InitialContext();
NamingEnumeration bindings = ctx.listBindings("java:comp/env");
while( bindings.hasMore() )
{
Binding binding = (Binding) bindings.next();
String name = binding.getName();
String value = binding.getObject().toString();
props.setProperty(name, value);
}
}
catch(NamingException e)
{
throw new RemoteTestException(e);
}
run(className, methodName, props);
}
/** Run the specified test method on the given class name
*
* @param className the name of the test class
* @param methodName the name of the test method
* @param props
* @throws RemoteTestException If any throwable is thrown during
* execution of the method, it is wrapped with a RemoteTestException and
* rethrown.
*/
public void run(String className, String methodName, Properties props)
throws RemoteTestException
{
EJBTestCase testCase = getTestInstance(className, methodName);
setUpEJB(testCase, props);
RemoteTestException exception = null;
try
{
runTestCase(testCase);
}
catch (RemoteTestException e)
{
exception = e;
}
finally
{
try
{
tearDownEJB(testCase, props);
}
catch (RemoteTestException e)
{
// favor the run exception if one was thrown
if (exception != null)
{
exception = e;
}
}
if (exception != null)
{
throw exception;
}
}
}
/**
* Runs the setUpEJB method on the specified test case
* @param testCase the actual test case that will be run
* @throws RemoteTestException If any throwable is thrown during execution
* of the method, it is wrapped with a RemoteTestException and rethrown.
*/
private void setUpEJB(EJBTestCase testCase, Properties props)
throws RemoteTestException
{
try
{
ctx.getUserTransaction().begin();
try
{
testCase.setUpEJB(props);
}
catch (Throwable e)
{
throw new RemoteTestException(e);
}
if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
{
ctx.getUserTransaction().commit();
}
}
catch (Throwable e)
{
try
{
ctx.getUserTransaction().rollback();
}
catch (SystemException unused)
{
// eat the exception we are exceptioning out anyway
}
if (e instanceof RemoteTestException)
{
throw (RemoteTestException) e;
}
throw new RemoteTestException(e);
}
}
/**
* Runs the test method on the specified test case
* @param testCase the actual test case that will be run
* @throws RemoteTestException If any throwable is thrown during execution
* of the method, it is wrapped with a RemoteTestException and rethrown.
*/
private void runTestCase(EJBTestCase testCase) throws RemoteTestException
{
try
{
ctx.getUserTransaction().begin();
try
{
testCase.runBare();
}
catch (Throwable e)
{
throw new RemoteTestException(e);
}
if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
{
ctx.getUserTransaction().commit();
}
}
catch (Throwable e)
{
try
{
ctx.getUserTransaction().rollback();
}
catch (SystemException unused)
{
// eat the exception we are exceptioning out anyway
}
if (e instanceof RemoteTestException)
{
throw (RemoteTestException) e;
}
throw new RemoteTestException(e);
}
}
/**
* Runs the tearDownEJB method on the specified test case
* @param testCase the actual test case that will be run
* @throws RemoteTestException If any throwable is thrown during execution
* of the method, it is wrapped with a RemoteTestException and rethrown.
*/
private void tearDownEJB(EJBTestCase testCase, Properties props)
throws RemoteTestException
{
try
{
ctx.getUserTransaction().begin();
try
{
testCase.tearDownEJB(props);
}
catch (Throwable e)
{
throw new RemoteTestException(e);
}
if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
{
ctx.getUserTransaction().commit();
}
}
catch (Throwable e)
{
try
{
ctx.getUserTransaction().rollback();
}
catch (SystemException unused)
{
// eat the exception we are exceptioning out anyway
}
if (e instanceof RemoteTestException)
{
throw (RemoteTestException) e;
}
throw new RemoteTestException(e);
}
}
/**
* Gets a instance of the test class with the specified class name and
* initialized to execute the specified method.
*
* @param className the name of the test class
* @param methodName the name of the test method
* @return a new instance of the test class with the specified class name and
* initialized to execute the specified method.
*/
private EJBTestCase getTestInstance(String className, String methodName)
{
Class testClass = null;
try
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
testClass = loader.loadClass(className);
}
catch (ClassNotFoundException e)
{
throw new EJBException("Test class not found : " + className);
}
Constructor constructor = null;
try
{
constructor = testClass.getConstructor(new Class[]{String.class});
}
catch (Exception e)
{
e.printStackTrace();
throw new EJBException("Test class does not have a constructor " +
"which has a single String argument.");
}
try
{
EJBTestCase testCase =
(EJBTestCase) constructor.newInstance(new Object[]{methodName});
testCase.setServerSide(true);
return testCase;
}
catch (Exception e)
{
e.printStackTrace();
throw new EJBException("Cannot instantiate test class: " +
testClass.getName());
}
}
public void ejbCreate()
{
}
public void ejbRemove()
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void setSessionContext(SessionContext ctx)
{
this.ctx = ctx;
}
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/EJBTestRunnerHome.java
Index: EJBTestRunnerHome.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
/**
* Remote home interface of the ejb test runner.
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @version $Revision: 1.1 $
*/
public interface EJBTestRunnerHome extends EJBHome
{
/**
* Creates an ejb test runner.
* @return a new EJBTestRunner
*/
public EJBTestRunner create() throws RemoteException, CreateException;
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/RemoteAssertionFailedError.java
Index: RemoteAssertionFailedError.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import junit.framework.AssertionFailedError;
/**
* RemoteAssertionFailedError is the client-side view of an assertion
* failed error on the server.
*
* All throwables caught on the server are wrapped with a RemoteTestException
* and rethrown. On the client side the exception is caught, and if the
* server side exception is an instance of AssertionFailedError, it is
* wrapped with an instance of this class and rethrown. That makes the
* exception an instance of AssertionFailedError so it is reconized as
* a failure and not an Error.
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @version $Revision: 1.1 $
*/
public class RemoteAssertionFailedError extends AssertionFailedError
{
private AssertionFailedError remoteAssertionFailedError;
private String remoteStackTrace;
/**
* Constructs a remote assertion failed error based on the specified
* AssertionFailedError and remote stack trace.
* @param e the AssertionFailedError that was thrown on the server side
* @param remoteStackTrace the stack trace of the assertion failed error
* exactly as it appeared on the server side
*/
public RemoteAssertionFailedError(
AssertionFailedError e,
String remoteStackTrace)
{
remoteAssertionFailedError = e;
this.remoteStackTrace = remoteStackTrace;
}
/**
* Gets the message exactly as it appeared on server side.
* @return the message exactly as it appeared on server side
*/
public String getMessage()
{
return remoteAssertionFailedError.getMessage();
}
/**
* Prints the stack trace exactly as it appeared on the server side.
* @param ps the PrintStream on which the stack trace is printed
*/
public void printStackTrace(java.io.PrintStream ps)
{
ps.print(remoteStackTrace);
}
/**
* Prints the stack trace exactly as it appeared on the server side.
*/
public void printStackTrace()
{
printStackTrace(System.err);
}
/**
* Prints the stack trace exactly as it appeared on the server side.
* @param pw the PrintWriter on which the stack trace is printed
*/
public void printStackTrace(java.io.PrintWriter pw)
{
pw.print(remoteStackTrace);
}
/**
* Gets the assertion failed error object from the server side.
* Note: the stack trace of this object is not available because
* exceptions don't seralize the stack trace. Use
* getRemoteStackTrace to get the stack trace as it appeared
* on the server.
* @retun the assertion failed error object from the server side.
*/
public AssertionFailedError getRemoteAssertionFailedError()
{
return remoteAssertionFailedError;
}
/**
* Gets the stack trace exactly as it appeared on the server side.
* @return the stack trace exactly as it appeared on the server side
*/
public String getRemoteStackTrace()
{
return remoteStackTrace;
}
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/test/util/ejb/RemoteTestException.java
Index: RemoteTestException.java
===================================================================
/*
* JUnitEJB
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.util.ejb;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* RemoteTestException is the client-side view of a throwable on the server.
*
* All throwables caught on the server are wrapped with a RemoteTestException
* and rethrown. On the client side the exception is caught, and if the
* server side exception is an instance of AssertionFailedError, it is
* wrapped with a RemoteAssertionFailedError and rethrown. That makes the
* exception an instance of AssertionFailedError so it is reconized as
* a failure and not an Error.
*
* @author <a href="mailto:dain at daingroup.com">Dain Sundstrom</a>
* @version $Revision: 1.1 $
*/
public class RemoteTestException extends Exception
{
private Throwable remoteThrowable;
private String remoteStackTrace;
/**
* Constructs a remote test exception that wrapps the the specified
* throwable.
* @param e the Throwable that was thrown on the server side
*/
public RemoteTestException(Throwable e)
{
remoteThrowable = e;
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
e.printStackTrace(writer);
StringBuffer buffer = stringWriter.getBuffer();
remoteStackTrace = buffer.toString();
}
/**
* Gets the message exactly as it appeared on server side.
* @return the message exactly as it appeared on server side
*/
public String getMessage()
{
return remoteThrowable.getMessage();
}
/**
* Prints the stack trace exactly as it appeared on the server side.
* @param ps the PrintStream on which the stack trace is printed
*/
public void printStackTrace(java.io.PrintStream ps)
{
ps.print(remoteStackTrace);
}
/**
* Prints the stack trace exactly as it appeared on the server side.
*/
public void printStackTrace()
{
printStackTrace(System.err);
}
/**
* Prints the stack trace exactly as it appeared on the server side.
* @param pw the PrintWriter on which the stack trace is printed
*/
public void printStackTrace(java.io.PrintWriter pw)
{
pw.print(remoteStackTrace);
}
/**
* Gets the throwable object from the server side.
* Note: the stack trace of this object is not available because
* exceptions don't seralize the stack trace. Use
* getRemoteStackTrace to get the stack trace as it appeared
* on the server.
* @return the Throwable object from the server side.
*/
public Throwable getRemoteThrowable()
{
return remoteThrowable;
}
/**
* Gets the stack trace exactly as it appeared on the server side.
* @return the stack trace exactly as it appeared on the server side
*/
public String getRemoteStackTrace()
{
return remoteStackTrace;
}
}
More information about the jboss-cvs-commits
mailing list