org.mockejb
Class MockContainer

java.lang.Object
  extended byorg.mockejb.MockContainer

public class MockContainer
extends java.lang.Object

Provides methods to "deploy" EJBs. Most deploy methods simply create Home proxy and bind it to the JNDI. Since MockEjb is not a container in a true sense, the purpose of this class is to provide an abstraction that emulates EJB deployment.

Author:
Alexander Ananiev

Constructor Summary
MockContainer(javax.naming.Context context)
          Creates a new instance of the MockContainer for the given context, deletes all aspects from the AspectSystem and adds the system interceptors, such as ExceptionHandler.
 
Method Summary
 javax.jms.MessageListener createMessageBean(MockEjbObject ejbObject)
          Deprecated. use deploy instead
 void deploy(EntityBeanDescriptor descriptor)
          Deploys entity bean specified by the given descriptor.
 void deploy(MDBDescriptor descriptor)
          If "isAlreadyBound" is "false" in deployment descriptor, creates mock connection factory and destination and bind them to JNDI.
 void deploy(SessionBeanDescriptor descriptor)
          Deploys session bean specified by the given descriptor.
 EntityDatabase getEntityDatabase()
          Returns an instance of the EntityDatabase that will be used by the system interceptors (Finder Handlers) used by this instance of the container.
static MockUser getUser()
          Returns the user that was passed to the MockContainer.login method.
static boolean isSystemException(java.lang.Throwable throwable)
          Tests if the given throwable is the system exception in terms of the container.
protected  void loadEntityDatabase()
           
 void login(MockUser user)
          Simulates the login operation of the container.
protected  void setupDefaultInterceptors()
          Add the interceptors that should always be present to the AspectSystem.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockContainer

public MockContainer(javax.naming.Context context)
Creates a new instance of the MockContainer for the given context, deletes all aspects from the AspectSystem and adds the system interceptors, such as ExceptionHandler. Clears the EntityDatabase as well. Creates a default (anonymous) user which is used for all EJB operations unless "login" is called explicitly.

Parameters:
context - JNDI context to use for all "bind" operations
Method Detail

getUser

public static MockUser getUser()
Returns the user that was passed to the MockContainer.login method. Note that the user is stored as a ThreadLocal variable, so it is available to all classes. Returns the anonymous user if login was not called.

Returns:
the current user of the MockContainer

login

public void login(MockUser user)
Simulates the login operation of the container. This method does not perform any security checks. It simply stores the given MockUser object for further use by EJBContext's security-related methods.

Parameters:
user - user of the MockContainer

setupDefaultInterceptors

protected void setupDefaultInterceptors()
Add the interceptors that should always be present to the AspectSystem.


deploy

public void deploy(SessionBeanDescriptor descriptor)
            throws javax.naming.NamingException
Deploys session bean specified by the given descriptor. MockContainer creates the proxy implementing session bean home interface and binds it to the JNDI context using rebind() method. Clients can subsequently lookup the home and invoke create().

Parameters:
descriptor - descriptor of the session bean to deploy As of MockEJB 0.6, this method does not return MockEjbObject since the direct use of MockEjbObject is deprecated. AspectSystem should be used instead.
Throws:
javax.naming.NamingException

deploy

public void deploy(EntityBeanDescriptor descriptor)
            throws javax.naming.NamingException
Deploys entity bean specified by the given descriptor.

Throws:
javax.naming.NamingException

deploy

public void deploy(MDBDescriptor descriptor)
            throws javax.naming.NamingException,
                   javax.jms.JMSException
If "isAlreadyBound" is "false" in deployment descriptor, creates mock connection factory and destination and bind them to JNDI. Otherwise, assumes that the connection factory and destination were created previously and available from JNDI. The default is "false". Creates MDB and sets it to listen on the destination. Handles both queues and topics, depending on "isTopic" setting of the deployment descriptor. Note that mock JMS implementation is synchronous, in other words the message sent to the destination is delivered to MDB right away.

Parameters:
descriptor - deployment descriptor of the MDB specifying JNDI names of connection factory and destinations as well as the bean implementation object.
Throws:
javax.naming.NamingException - in case of problems binding to JNDI or retrieving objects from JNDI
javax.jms.JMSException - in case of problems with mock connection factory or destination.

getEntityDatabase

public EntityDatabase getEntityDatabase()
Returns an instance of the EntityDatabase that will be used by the system interceptors (Finder Handlers) used by this instance of the container.

Returns:
an instance of the entity database

createMessageBean

public javax.jms.MessageListener createMessageBean(MockEjbObject ejbObject)
Deprecated. use deploy instead

Creates message-driven bean. This method emulates create() method of the Session bean home interface. Since MDBs don't have home interface, MockContainer provides this service for MDB clients.

Parameters:
ejbObject - MockEjbObject of the message bean created by deployMessageBean()
Returns:
implementation of the MessageListener interface. The interface is implemented by a proxy provided by MockEjbObject.

isSystemException

public static boolean isSystemException(java.lang.Throwable throwable)
Tests if the given throwable is the system exception in terms of the container. Currently we consider all runtime exceptions and transaction-related exceptions system exceptions.
Note that the spec is vague on what is system and non-system exception, so this method might change in the future.

Parameters:
throwable - exception in question
Returns:
true if the given throwable is "system" exception

loadEntityDatabase

protected void loadEntityDatabase()