Package org.dbunit.database
Class InMemoryJndiContextFactory
java.lang.Object
org.dbunit.database.InMemoryJndiContextFactory
- All Implemented Interfaces:
InitialContextFactory
Test-only
InitialContextFactory backed by a simple in-memory name-to-object map, so
JndiDatabaseTester can be unit tested through a real InitialContext
without a real JNDI provider or an extra test dependency.
Register an object under a unique name with bind(Object), then
pass environment() (or an environment built from it) as the
environment argument of the
JndiDatabaseTester constructors so JNDI resolves this
factory. Bindings are held in static maps for the JVM's lifetime; call
unbind(String) from test cleanup (e.g. an @AfterEach
method) for every name bind(Object) returned, so a long test run
does not accumulate bound objects it no longer needs.
- Since:
- 3.4.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringBinds the given object under a fresh, unique name.static PropertiesReturns a JNDI environment that resolvesInitialContextto this factory.getInitialContext(Hashtable<?, ?> environment) Returns a proxyContextwhoselookup(String)resolves names againstBINDINGS(incrementing the matchingLOOKUP_COUNTSentry) and whoseclose()is a no-op; any otherContextmethod throwsUnsupportedOperationException.static intlookupCount(String name) Returns how many timesContext.lookup(String)has been called for the given bound name.static voidRemoves the given bound name and its lookup counter, so the previously bound object - and anything it holds, e.g. a testDataSource- becomes eligible for garbage collection.
-
Constructor Details
-
InMemoryJndiContextFactory
public InMemoryJndiContextFactory()
-
-
Method Details
-
bind
Binds the given object under a fresh, unique name.- Parameters:
value- The objectContext.lookup(String)should return.- Returns:
- The unique name the object was bound under.
-
unbind
Removes the given bound name and its lookup counter, so the previously bound object - and anything it holds, e.g. a testDataSource- becomes eligible for garbage collection. Call from test cleanup for every namebind(Object)returned.- Parameters:
name- A name previously returned bybind(Object).
-
lookupCount
Returns how many timesContext.lookup(String)has been called for the given bound name.- Parameters:
name- A name previously returned bybind(Object).- Returns:
- The number of lookups observed so far.
-
environment
Returns a JNDI environment that resolvesInitialContextto this factory.- Returns:
- A fresh, mutable
Propertiesinstance.
-
getInitialContext
Returns a proxyContextwhoselookup(String)resolves names againstBINDINGS(incrementing the matchingLOOKUP_COUNTSentry) and whoseclose()is a no-op; any otherContextmethod throwsUnsupportedOperationException.- Specified by:
getInitialContextin interfaceInitialContextFactory- Parameters:
environment- Ignored; this factory only ever serves the in-memory bindings.- Returns:
- The proxy
Context.
-