public class TestJNDIContext implements Context(Code)
Mock JNDI context that permits String names to be bound to objects.
It is intended to simulate JNDI factories in web containers and
application servers, and provides only the bare minimum functions.
This class contains a static member InitialContextFactory class
called Factory that installs a "root" instance of TestJNDIContext
as the initial context for the entire JVM. Additional contexts can
be bound by supplying pairs of Strings and Objects using
TestJNDIContext.bind(String,Object) . Objects are looked up and retrieved
using
TestJNDIContext.lookup(String) . All other methods in this class no-op.
For example, simulating a JNDI DataSource requires us to first
establish an initial context of java:comp/env. Inside
the initial context, we bind our data source:
Context initCtx = new InitialContext();
initCtx.bind( "java:comp/env", new TestJNDIContext() );
Context ctx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = new TestJDBCDataSource();
ctx.bind( "jdbc/UserDatabase", ds);
initialize() Static factory method that creates a new TestJNDIContext and
configures the JVM so that the new InitialContext()
always returns this context.
Constructs a new mock JNDI context. Note that the instance has no
relationship to the JVM's initial context per se.
To configure the JVM so that it always returns a TestJNDIContext
instance, see
TestJNDIContext.initialize() .