org.mockcentral.server
Class MockCentralServer

java.lang.Object
  extended by org.mockcentral.server.MockCentralServer

public class MockCentralServer
extends java.lang.Object

The class used to access the Mock Objects and Proxy instances created within the application. Implemented as a Singleton, this class provides a number of alternate init methods, which require either an InputStream, File or file path String to locate the configuration xml data. Overloaded versions allow the specification of runtime variables and a boolean indicating whether or not the InitialContextFactory.getInitialContext() method should return an instance of MockCentralTestingContext to facilitate JNDI lookups of Mock Objects.

Any entries in the runtime variables map become available to the Mock Object building process: if a MethodExpectationBean for a Mock Object specifies a "variable" value type, the bean's value will be used to look up an Object from the map, which will then be assigned as the indicated method parameter or return value. This can be useful if your code under test accesses runtime data (such as the current date or a value obtained from an external service) or if you simply want to use a prebuilt Object of any kind for a method signature part.

The MockCentralTestingContext can be used to automatically bind Mock Objects under JNDI names for use by code under test: every Mock Object definition which specifies a jndiName value becomes bindable, and will be loaded into the Context when the bindFixtureProxies method is invoked. JNDI names must be unique within single Fixture instances but may be duplicated across multiple ones.

Author:
David Hutchinson
See Also:
Fixture, MockCentralTestingContext

Method Summary
 void addFixture(Fixture fixture)
          Adds a new Fixture to the map.
 void bindFixtureProxies()
          If using the MockCentralTestingContext, gets the current Fixture and binds all of the Proxy Objects into the Context which have supplied JNDI names.
 void bindFixtureProxies(java.lang.String fixtureName)
          If using the MockCentralTestingContext, gets the named Fixture from the map and binds all of its Proxy Objects into the Context which have supplied JNDI names.
 javax.naming.Context getContext()
          Returns the MockCentralTestingContext instance.
static MockCentralServer getInstance()
          Returns the Singleton instance.
 java.lang.Object getProxy(java.lang.String mockObjectName)
          Returns the Proxy for the Mock Object specified by the argument.
 java.lang.Object getProxy(java.lang.String fixtureName, java.lang.String mockObjectName)
          Returns the Proxy for the Mock Object specified by the arguments.
 void init(java.io.File file)
          Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.io.File file, boolean useTestingContext)
          Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.io.File file)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.io.File file, boolean useTestingContext)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.io.InputStream input)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.io.InputStream input, boolean useTestingContext)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.lang.String path)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap, java.lang.String path, boolean useTestingContext)
          Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void init(java.io.InputStream input)
          Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.io.InputStream input, boolean useTestingContext)
          Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void init(java.lang.String path)
          Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.
 void init(java.lang.String path, boolean useTestingContext)
          Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.
 void verifyMock(java.lang.String mockObjectName)
          Gets the indicated Mock Object and verifies its expectations.
 void verifyMock(java.lang.String fixtureName, java.lang.String mockObjectName)
          Gets the indicated Mock Object from the indicated Fixture and verifies its expectations.
 void verifyMocks()
          Gets all the Mock Objects belonging to the current Fixture and verifies their expectations.
 void verifyMocks(java.lang.String fixtureName)
          Gets all the Mock Objects belonging to the Fixture defined by the fixtureName parameter and verifies their expectations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static MockCentralServer getInstance()
Returns the Singleton instance.

Returns:
the Singleton instance.

init

public void init(java.lang.String path)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
path - the path to the xml config file.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.io.File file)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
file - the xml config file.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.io.InputStream input)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
input - the InputStream containing the data.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.lang.String path)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
variablesMap - the map of runtime variables.
path - the path to the xml config file.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.io.File file)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
variablesMap - the map of runtime variables.
file - the xml config file.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.io.InputStream input)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances and invokes the MockDataLoader class to parse the configuration file.

Parameters:
variablesMap - the map of runtime variables.
input - the InputStream containing the data.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.lang.String path,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
path - the path to the xml config file.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.io.File file,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
file - the xml config file.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.io.InputStream input,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
input - the InputStream containing the data.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.lang.String path,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
variablesMap - the map of runtime variables.
path - the path to the xml config file.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.io.File file,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
variablesMap - the map of runtime variables.
file - the xml config file.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

init

public void init(java.util.HashMap<java.lang.String,java.lang.Object> variablesMap,
                 java.io.InputStream input,
                 boolean useTestingContext)
          throws MockCentralServerException
Initializes the application; stores the map of runtime variables, creates the map to store the Fixture instances, invokes the MockDataLoader class to parse the configuration file and sets up the MockCentralTestingContext.

Parameters:
variablesMap - the map of runtime variables.
input - the InputStream containing xml config file contents.
useTestingContext - flag indicating whether to instruct the InitialContextFactory to return an instance of the MockCentralTextingContext class to facilitate jndi lookups of Mock Objects.
Throws:
MockCentralServerException - if initialization failed.

getContext

public javax.naming.Context getContext()
                                throws MockCentralServerException
Returns the MockCentralTestingContext instance.

Returns:
the Context instance.
Throws:
MockCentralServerException - if the Context could not be returned.

addFixture

public void addFixture(Fixture fixture)
                throws MockCentralServerException
Adds a new Fixture to the map. This method is invoked by the org.apache.commons.digester.Digester when the configuration file is parsed, and should not be called by clients.

Parameters:
fixture - the Fixture to add to the map.
Throws:
MockCentralServerException

bindFixtureProxies

public void bindFixtureProxies()
                        throws MockCentralServerException
If using the MockCentralTestingContext, gets the current Fixture and binds all of the Proxy Objects into the Context which have supplied JNDI names. This method is valid only for configurations that specify only one Fixture instance.

Throws:
MockCentralServerException - if the server was not initialized to use the testing Context or if there was no Fixture found with the supplied name.

bindFixtureProxies

public void bindFixtureProxies(java.lang.String fixtureName)
                        throws MockCentralServerException
If using the MockCentralTestingContext, gets the named Fixture from the map and binds all of its Proxy Objects into the Context which have supplied JNDI names.

Parameters:
fixtureName - the name of the Fixture to load.
Throws:
MockCentralServerException - if the server was not initialized to use the testing Context or if there was no Fixture found with the supplied name.

getProxy

public java.lang.Object getProxy(java.lang.String mockObjectName)
                          throws UnknownProxyException,
                                 MockCentralServerException
Returns the Proxy for the Mock Object specified by the argument. This method is valid only for configurations that specify only one Fixture instance.

Parameters:
mockObjectName - the name under which the Proxy is stored.
Returns:
the Proxy specified by the argument.
Throws:
UnknownProxyException - if the Proxy could not be located.
MockCentralServerException - if there are multiple Fixture instances defined in the configuration file.

getProxy

public java.lang.Object getProxy(java.lang.String fixtureName,
                                 java.lang.String mockObjectName)
                          throws UnknownProxyException
Returns the Proxy for the Mock Object specified by the arguments.

Parameters:
fixtureName - the name of the Fixture containing the Proxy.
mockObjectName - the name under which the Proxy is stored.
Returns:
the Proxy specified by the arguments.
Throws:
UnknownProxyException - if the Proxy could not be located.

verifyMocks

public void verifyMocks()
                 throws MockCentralServerException
Gets all the Mock Objects belonging to the current Fixture and verifies their expectations. This method is valid only for configurations that specify only one Fixture instance.

Throws:
MockCentralServerException - if there is more than one Fixture defined in the configuration.

verifyMocks

public void verifyMocks(java.lang.String fixtureName)
                 throws MockCentralServerException
Gets all the Mock Objects belonging to the Fixture defined by the fixtureName parameter and verifies their expectations.

Parameters:
fixtureName - the name of the Fixture containing the Mock Objects to be verified.
Throws:
MockCentralServerException - if the indicated Fixture does not exist.

verifyMock

public void verifyMock(java.lang.String mockObjectName)
                throws UnknownProxyException,
                       MockCentralServerException
Gets the indicated Mock Object and verifies its expectations. This method is valid only for configurations that specify only one Fixture instance.

Parameters:
mockObjectName - the name of the Mock Object.
Throws:
UnknownProxyException - if the Mock Object's Proxy could not be located.
MockCentralServerException - if there are multiple fixtures definged in the configuration file.

verifyMock

public void verifyMock(java.lang.String fixtureName,
                       java.lang.String mockObjectName)
                throws UnknownProxyException
Gets the indicated Mock Object from the indicated Fixture and verifies its expectations.

Parameters:
fixtureName - the name of the Fixture containing the Mock Object.
mockObjectName - the name of the Mock Object.
Throws:
UnknownProxyException - if the Mock Object's Proxy could not be located.