Class ServiceLocatorTestRule<T>
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- org.jvnet.hk2.testing.junit.ServiceLocatorTestRule<T>
-
- Type Parameters:
T
- the type of JUnit test thisServiceLocatorTestRule
is related to; consider making it an instance ofBinder
- All Implemented Interfaces:
Binder
,org.junit.rules.TestRule
public class ServiceLocatorTestRule<T> extends org.junit.rules.ExternalResource implements Binder
AnExternalResource
(and aBinder
) that sets up and tears down an HK2ServiceLocator
on a per-test-class or per-test-method basis.- Since:
- 2.4.0-b33
- Author:
- Laird Nelson
- See Also:
ExternalResource
,Binder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ServiceLocatorTestRule.ServiceLocatorIsolation
Anenum
describing possibleServiceLocator
isolation levels for JUnit tests.
-
Constructor Summary
Constructors Constructor Description ServiceLocatorTestRule(T test)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation in non-verbose mode.ServiceLocatorTestRule(T test, boolean verbose)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation with the supplied verbosity.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation
in non-verbose mode.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
after()
Shuts down and cleans up theServiceLocator
created by thisServiceLocatorTestRule
.org.junit.runners.model.Statement
apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
Overrides theExternalResource.apply(Statement, Description)
method to save the suppliedDescription
so that other methods in this class can refer to it.void
before()
When necessary, calls thecreateServiceLocator(Description)
,configureServiceLocator(ServiceLocator, Description)
andperformDependencyInjection(ServiceLocator, Object)
methods in order to set up aServiceLocator
appropriate for the current JUnit test.void
bind(DynamicConfiguration dynamicConfiguration)
Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)
method in case subclasses wish to add services to theServiceLocator
being configured.protected void
configureServiceLocator(ServiceLocator serviceLocator, org.junit.runner.Description testDescription)
Configures the suppliedServiceLocator
for use by the test described by the suppliedDescription
.protected ServiceLocator
createServiceLocator(org.junit.runner.Description testDescription)
Creates and returns aServiceLocator
suitable for the JUnit test described by the suppliedDescription
.protected String
getClasspath()
Returns aString
representing the classpath to use to search for classes.ServiceLocatorTestRule.ServiceLocatorIsolation
getServiceLocatorIsolation()
Returns theServiceLocatorTestRule.ServiceLocatorIsolation
for thisServiceLocatorTestRule
.protected String
getServiceLocatorName(org.junit.runner.Description testDescription)
Returns a name for aServiceLocator
that is appropriate for the suppliedDescription
.boolean
isVerbose()
protected void
performDependencyInjection(ServiceLocator serviceLocator, T test)
Performs HK2 dependency injection on thisServiceLocatorTestRule
and the supplied test instance, using the suppliedServiceLocator
as needed.void
shutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)
Blindly calls theServiceLocator.shutdown()
andServiceLocatorFactory.destroy(ServiceLocator)
methods on thisServiceLocatorTestRule
's associatedServiceLocator
if it is discovered to be non-null
.
-
-
-
Constructor Detail
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation in non-verbose mode.- Parameters:
test
- the JUnit test; must not benull
- Throws:
AssertionError
- iftest
isnull
- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, boolean verbose)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation with the supplied verbosity.- Parameters:
test
- the JUnit test; must not benull
verbose
- whether thisServiceLocatorTestRule
should output additional information toSystem.out
- Throws:
AssertionError
- iftest
isnull
- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation
in non-verbose mode.- Parameters:
test
- the JUnit test; must not benull
isolation
- theServiceLocatorTestRule.ServiceLocatorIsolation
; ifnull
thenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
will be used instead- Throws:
AssertionError
- iftest
isnull
- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose)
Creates a newServiceLocatorTestRule
on behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation
.- Parameters:
test
- the JUnit test; must not benull
isolation
- theServiceLocatorTestRule.ServiceLocatorIsolation
; ifnull
thenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
will be used insteadverbose
- whether thisServiceLocatorTestRule
should output additional information toSystem.out
- Throws:
AssertionError
- iftest
isnull
-
-
Method Detail
-
getServiceLocatorIsolation
public final ServiceLocatorTestRule.ServiceLocatorIsolation getServiceLocatorIsolation()
Returns theServiceLocatorTestRule.ServiceLocatorIsolation
for thisServiceLocatorTestRule
.This method never returns
null
.- Returns:
- the
ServiceLocatorTestRule.ServiceLocatorIsolation
for thisServiceLocatorTestRule
; nevernull
-
isVerbose
public final boolean isVerbose()
- Returns:
true
if thisServiceLocatorTestRule
should be verbose;false
otherwise- See Also:
ServiceLocatorUtilities.dumpAllDescriptors(ServiceLocator, PrintStream)
-
before
public void before() throws IOException
When necessary, calls thecreateServiceLocator(Description)
,configureServiceLocator(ServiceLocator, Description)
andperformDependencyInjection(ServiceLocator, Object)
methods in order to set up aServiceLocator
appropriate for the current JUnit test.- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
IOException
- if theconfigureServiceLocator(ServiceLocator, Description)
method threw anIOException
- See Also:
createServiceLocator(Description)
,configureServiceLocator(ServiceLocator, Description)
,performDependencyInjection(ServiceLocator, Object)
-
configureServiceLocator
protected void configureServiceLocator(ServiceLocator serviceLocator, org.junit.runner.Description testDescription) throws IOException
Configures the suppliedServiceLocator
for use by the test described by the suppliedDescription
.This implementation:
- Adds the
ErrorServiceImpl
class to the suppliedServiceLocator
so that exceptions will be thrown from tests - Removes, via
DynamicConfiguration.addUnbindFilter(Filter)
, allDescriptor
s that haveDescription.class
as one of their contracts - Adds a constant
descriptor in
Singleton
scope with a name equal to the return value of theDescription.getDisplayName()
method for the suppliedDescription
so that the current test can inject theDescription
for the current method if it wishes - Adds any classes found in an optional
Classes
annotation decorating the test class—if they are assignable toFactory.class
then they are added as factories - Adds any classes found in any packages listed in an optional
Packages
annotation decorating the test class, provided they are annotated withService
- Reads any locator files listed in an optional
InhabitantFiles
annotation decorating the test class and adds the services listed therein - Calls the
bind(DynamicConfiguration)
method - Calls the
Binder.bind(DynamicConfiguration)
method on the test if it is in fact an instance ofBinder
- Parameters:
serviceLocator
- theServiceLocator
to configure; must not benull
testDescription
- theDescription
describing the particular test being run; must not benull
- Throws:
AssertionError
- if eitherserviceLocator
ortestDescription
isnull
IOException
- if there was an error looking for classes in packages or finding or reading locator files
- Adds the
-
bind
public void bind(DynamicConfiguration dynamicConfiguration)
Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)
method in case subclasses wish to add services to theServiceLocator
being configured.Overrides of this method must not invoke the
DynamicConfiguration.commit()
method.The default implementation of this method does nothing.
- Specified by:
bind
in interfaceBinder
- Parameters:
dynamicConfiguration
- theDynamicConfiguration
to manipulate in order to add or remove services from theServiceLocator
being configured; must not benull
-
getClasspath
protected String getClasspath()
Returns aString
representing the classpath to use to search for classes.This method never returns
null
.Overrides of this method are permitted to return
null
.The default implementation of this method returns the value of the
java.class.path
system property, or in the almost certainly catastrophic case where this is not set, the empty string.- Returns:
- a
String
representing the classpath, ornull
-
performDependencyInjection
protected void performDependencyInjection(ServiceLocator serviceLocator, T test)
Performs HK2 dependency injection on thisServiceLocatorTestRule
and the supplied test instance, using the suppliedServiceLocator
as needed.The default implementation of this method does the following:
- Calls
ServiceLocator.inject(Object)
on the suppliedServiceLocator
, if it is non-null
, passing it thisServiceLocatorTestRule
- Calls
ServiceLocator.postConstruct(Object)
on the suppliedServiceLocator
, if it is non-null
, passing it thisServiceLocatorTestRule
- Calls
ServiceLocator.inject(Object)
on the suppliedServiceLocator
, if it is non-null
, passing it the suppliedtest
instance, if it is non-null
- Calls
ServiceLocator.postConstruct(Object)
on the suppliedServiceLocator
, if it is non-null
, passing it the suppliedtest
instance, if it is non-null
This method is guaranteed to be called after the
configureServiceLocator(ServiceLocator, Description)
method with the sameServiceLocator
.- Parameters:
serviceLocator
- theServiceLocator
to use to perform injection; may benull
in which case no action will be takentest
- the test instance to inject; may benull
- See Also:
bind(DynamicConfiguration)
,configureServiceLocator(ServiceLocator, Description)
- Calls
-
createServiceLocator
protected ServiceLocator createServiceLocator(org.junit.runner.Description testDescription)
Creates and returns aServiceLocator
suitable for the JUnit test described by the suppliedDescription
.This method never returns
null
.Overrides of this method must not return
null
.- Parameters:
testDescription
- theDescription
describing the currently executing JUnit test; must not benull
- Returns:
- a non-
null
ServiceLocator
- Throws:
AssertionError
- iftestDescription
isnull
-
getServiceLocatorName
protected String getServiceLocatorName(org.junit.runner.Description testDescription)
Returns a name for aServiceLocator
that is appropriate for the suppliedDescription
.This method never returns
null
.Overrides of this method must not return
null
.- Parameters:
testDescription
- theDescription
describing the currently executing JUnit test; must not benull
- Returns:
- a non-
null
String
that will be used as a name for aServiceLocator
- Throws:
AssertionError
- iftestDescription
isnull
, or if itsgetClassName()
method returnsnull
or if itsgetMethodName()
method returnsnull
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
Overrides theExternalResource.apply(Statement, Description)
method to save the suppliedDescription
so that other methods in this class can refer to it.This method never returns
null
.Overrides of this method must not return
null
.- Specified by:
apply
in interfaceorg.junit.rules.TestRule
- Overrides:
apply
in classorg.junit.rules.ExternalResource
- Parameters:
statement
- theStatement
thisServiceLocatorTestRule
will modify; passed unchanged to theExternalResource.apply(Statement, Description)
methoddescription
- theDescription
describing the current JUnit test; must not benull
; passed unchanged to theExternalResource.apply(Statement, Description)
method- Returns:
- the return value of the
ExternalResource.apply(Statement, Description)
method; nevernull
-
after
public void after()
Shuts down and cleans up theServiceLocator
created by thisServiceLocatorTestRule
.This method performs the following actions in order:
- Calls the
ServiceLocator.preDestroy(Object)
method on the existingServiceLocator
, passing it the test instance supplied at construction time - Calls the
ServiceLocator.preDestroy(Object)
method on the existingServiceLocator
, passing it thisServiceLocatorTestRule
- If the
ServiceLocator
isolation level isPER_TEST
, calls theshutdownAndDestroyServiceLocator(Description)
method with aDescription
instance that describes the JUnit test method that just executed
- Overrides:
after
in classorg.junit.rules.ExternalResource
- See Also:
shutdownAndDestroyServiceLocator(Description)
- Calls the
-
shutdownAndDestroyServiceLocator
public void shutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)
Blindly calls theServiceLocator.shutdown()
andServiceLocatorFactory.destroy(ServiceLocator)
methods on thisServiceLocatorTestRule
's associatedServiceLocator
if it is discovered to be non-null
.- Parameters:
testDescription
- aDescription
describing the currently executing JUnit test, if any; ignored by this method's default implementation; may benull
-
-