T
- the type of JUnit test this ServiceLocatorTestRule
is related to; consider making it an
instance of Binder
public class ServiceLocatorTestRule<T> extends org.junit.rules.ExternalResource implements Binder
ExternalResource
(and a Binder
) that sets up and
tears down an HK2 ServiceLocator
on a per-test-class or
per-test-method basis.ExternalResource
,
Binder
Modifier and Type | Class and Description |
---|---|
static class |
ServiceLocatorTestRule.ServiceLocatorIsolation
An
enum describing possible ServiceLocator
isolation levels for JUnit tests. |
Constructor and Description |
---|
ServiceLocatorTestRule(T test)
Creates a new
ServiceLocatorTestRule on behalf of the
supplied JUnit test in ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation in non-verbose mode. |
ServiceLocatorTestRule(T test,
boolean verbose)
Creates a new
ServiceLocatorTestRule on behalf of the
supplied JUnit test in ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation with the supplied verbosity. |
ServiceLocatorTestRule(T test,
ServiceLocatorTestRule.ServiceLocatorIsolation isolation)
Creates a new
ServiceLocatorTestRule on behalf of the
supplied JUnit test in the given ServiceLocatorTestRule.ServiceLocatorIsolation
in non-verbose mode. |
ServiceLocatorTestRule(T test,
ServiceLocatorTestRule.ServiceLocatorIsolation isolation,
boolean verbose)
Creates a new
ServiceLocatorTestRule on behalf of the
supplied JUnit test in the given ServiceLocatorTestRule.ServiceLocatorIsolation . |
Modifier and Type | Method and Description |
---|---|
void |
after()
Shuts down and cleans up the
ServiceLocator created by
this ServiceLocatorTestRule . |
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement statement,
org.junit.runner.Description description)
Overrides the
ExternalResource.apply(Statement,
Description) method to save the supplied Description so
that other methods in this class can refer to it. |
void |
before()
When necessary, calls the
createServiceLocator(Description) , configureServiceLocator(ServiceLocator, Description) and performDependencyInjection(ServiceLocator, Object) methods in
order to set up a ServiceLocator appropriate for the
current JUnit test. |
void |
bind(DynamicConfiguration dynamicConfiguration)
Called at the appropriate time by the
configureServiceLocator(ServiceLocator, Description) method in
case subclasses wish to add services to the ServiceLocator being configured. |
protected void |
configureServiceLocator(ServiceLocator serviceLocator,
org.junit.runner.Description testDescription)
Configures the supplied
ServiceLocator for use by the
test described by the supplied Description . |
protected ServiceLocator |
createServiceLocator(org.junit.runner.Description testDescription)
Creates and returns a
ServiceLocator suitable for the
JUnit test described by the supplied Description . |
protected String |
getClasspath()
Returns a
String representing the classpath to use to
search for classes. |
ServiceLocatorTestRule.ServiceLocatorIsolation |
getServiceLocatorIsolation()
Returns the
ServiceLocatorTestRule.ServiceLocatorIsolation for this ServiceLocatorTestRule . |
protected String |
getServiceLocatorName(org.junit.runner.Description testDescription)
Returns a name for a
ServiceLocator that is appropriate
for the supplied Description . |
boolean |
isVerbose()
|
protected void |
performDependencyInjection(ServiceLocator serviceLocator,
T test)
Performs HK2 dependency injection on this
ServiceLocatorTestRule and the supplied test instance, using the
supplied ServiceLocator as needed. |
void |
shutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)
Blindly calls the
ServiceLocator.shutdown() and ServiceLocatorFactory.destroy(ServiceLocator) methods on this
ServiceLocatorTestRule 's associated ServiceLocator if it is discovered to be non-null . |
public ServiceLocatorTestRule(T test)
ServiceLocatorTestRule
on behalf of the
supplied JUnit test in ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation in non-verbose mode.test
- the JUnit test; must not be null
AssertionError
- if test
is null
ServiceLocatorTestRule(Object, ServiceLocatorIsolation,
boolean)
public ServiceLocatorTestRule(T test, boolean verbose)
ServiceLocatorTestRule
on behalf of the
supplied JUnit test in ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
isolation with the supplied verbosity.test
- the JUnit test; must not be null
verbose
- whether this ServiceLocatorTestRule
should
output additional information to System.out
AssertionError
- if test
is null
ServiceLocatorTestRule(Object, ServiceLocatorIsolation,
boolean)
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation)
ServiceLocatorTestRule
on behalf of the
supplied JUnit test in the given ServiceLocatorTestRule.ServiceLocatorIsolation
in non-verbose mode.test
- the JUnit test; must not be null
isolation
- the ServiceLocatorTestRule.ServiceLocatorIsolation
; if null
then ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
will be used
insteadAssertionError
- if test
is null
ServiceLocatorTestRule(Object, ServiceLocatorIsolation,
boolean)
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose)
ServiceLocatorTestRule
on behalf of the
supplied JUnit test in the given ServiceLocatorTestRule.ServiceLocatorIsolation
.test
- the JUnit test; must not be null
isolation
- the ServiceLocatorTestRule.ServiceLocatorIsolation
; if null
then ServiceLocatorTestRule.ServiceLocatorIsolation.PER_TEST
will be used
insteadverbose
- whether this ServiceLocatorTestRule
should
output additional information to System.out
AssertionError
- if test
is null
public final ServiceLocatorTestRule.ServiceLocatorIsolation getServiceLocatorIsolation()
ServiceLocatorTestRule.ServiceLocatorIsolation
for this ServiceLocatorTestRule
.
This method never returns null
.
ServiceLocatorTestRule.ServiceLocatorIsolation
for this ServiceLocatorTestRule
; never null
public final boolean isVerbose()
true
if this ServiceLocatorTestRule
should be verbose; false
otherwiseServiceLocatorUtilities.dumpAllDescriptors(ServiceLocator,
PrintStream)
public void before() throws IOException
createServiceLocator(Description)
, configureServiceLocator(ServiceLocator, Description)
and performDependencyInjection(ServiceLocator, Object)
methods in
order to set up a ServiceLocator
appropriate for the
current JUnit test.before
in class org.junit.rules.ExternalResource
IOException
- if the configureServiceLocator(ServiceLocator, Description)
method
threw an IOException
createServiceLocator(Description)
,
configureServiceLocator(ServiceLocator, Description)
,
performDependencyInjection(ServiceLocator, Object)
protected void configureServiceLocator(ServiceLocator serviceLocator, org.junit.runner.Description testDescription) throws IOException
ServiceLocator
for use by the
test described by the supplied Description
.
This implementation:
ErrorServiceImpl
class to the supplied
ServiceLocator
so that exceptions will be thrown from
testsDynamicConfiguration.addUnbindFilter(Filter)
, all Descriptor
s that have Description.class
as
one of their contractsSingleton
scope with a name equal to the return value of the
Description.getDisplayName()
method for the supplied
Description
so that the current test can inject the
Description
for the current method if it wishesClasses
annotation
decorating the test class—if they are assignable to Factory.class
then they are added as
factoriesPackages
annotation decorating the test class, provided
they are annotated with Service
InhabitantFiles
annotation decorating the test class and adds
the services listed thereinbind(DynamicConfiguration)
methodBinder.bind(DynamicConfiguration)
method on
the test if it is in fact an instance of Binder
serviceLocator
- the ServiceLocator
to configure;
must not be null
testDescription
- the Description
describing the
particular test being run; must not be null
AssertionError
- if either serviceLocator
or
testDescription
is null
IOException
- if there was an error looking for classes
in packages or finding or reading locator filespublic void bind(DynamicConfiguration dynamicConfiguration)
configureServiceLocator(ServiceLocator, Description)
method in
case subclasses wish to add services to the ServiceLocator
being configured.
Overrides of this method must not invoke the DynamicConfiguration.commit()
method.
The default implementation of this method does nothing.
bind
in interface Binder
dynamicConfiguration
- the DynamicConfiguration
to
manipulate in order to add or remove services from the ServiceLocator
being configured; must not be null
protected String getClasspath()
String
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.
String
representing the classpath, or null
protected void performDependencyInjection(ServiceLocator serviceLocator, T test)
ServiceLocatorTestRule
and the supplied test instance, using the
supplied ServiceLocator
as needed.
The default implementation of this method does the following:
ServiceLocator.inject(Object)
on the supplied
ServiceLocator
, if it is non-null
, passing it
this ServiceLocatorTestRule
ServiceLocator.postConstruct(Object)
on the
supplied ServiceLocator
, if it is non-null
,
passing it this ServiceLocatorTestRule
ServiceLocator.inject(Object)
on the supplied
ServiceLocator
, if it is non-null
, passing it the
supplied test
instance, if it is non-null
ServiceLocator.postConstruct(Object)
on the
supplied ServiceLocator
, if it is non-null
,
passing it the supplied test
instance, if it is
non-null
This method is guaranteed to be called after the configureServiceLocator(ServiceLocator, Description)
method
with the same ServiceLocator
.
serviceLocator
- the ServiceLocator
to use to
perform injection; may be null
in which case no action
will be takentest
- the test instance to inject; may be null
bind(DynamicConfiguration)
,
configureServiceLocator(ServiceLocator, Description)
protected ServiceLocator createServiceLocator(org.junit.runner.Description testDescription)
ServiceLocator
suitable for the
JUnit test described by the supplied Description
.
This method never returns null
.
Overrides of this method must not return null
.
testDescription
- the Description
describing the
currently executing JUnit test; must not be null
null
ServiceLocator
AssertionError
- if testDescription
is null
protected String getServiceLocatorName(org.junit.runner.Description testDescription)
ServiceLocator
that is appropriate
for the supplied Description
.
This method never returns null
.
Overrides of this method must not return null
.
testDescription
- the Description
describing the
currently executing JUnit test; must not be null
null
String
that will be used as a
name for a ServiceLocator
AssertionError
- if testDescription
is null
, or if its getClassName()
method returns null
or if its getMethodName()
method returns
null
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
ExternalResource.apply(Statement,
Description)
method to save the supplied Description
so
that other methods in this class can refer to it.
This method never returns null
.
Overrides of this method must not return null
.
apply
in interface org.junit.rules.TestRule
apply
in class org.junit.rules.ExternalResource
statement
- the Statement
this ServiceLocatorTestRule
will modify; passed unchanged to the
ExternalResource.apply(Statement, Description)
methoddescription
- the Description
describing the current
JUnit test; must not be null
; passed unchanged to the
ExternalResource.apply(Statement, Description)
methodExternalResource.apply(Statement, Description)
method; never
null
public void after()
ServiceLocator
created by
this ServiceLocatorTestRule
.
This method performs the following actions in order:
ServiceLocator.preDestroy(Object)
method on
the existing ServiceLocator
, passing it the test instance supplied at
construction timeServiceLocator.preDestroy(Object)
method on
the existing ServiceLocator
, passing it this ServiceLocatorTestRule
ServiceLocator
isolation level is PER_TEST
, calls the shutdownAndDestroyServiceLocator(Description)
method with a
Description
instance that describes the JUnit test method
that just executedafter
in class org.junit.rules.ExternalResource
shutdownAndDestroyServiceLocator(Description)
public void shutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)
ServiceLocator.shutdown()
and ServiceLocatorFactory.destroy(ServiceLocator)
methods on this
ServiceLocatorTestRule
's associated ServiceLocator
if it is discovered to be non-null
.testDescription
- a Description
describing the
currently executing JUnit test, if any; ignored by this method's
default implementation; may be null
Copyright © 2009-2017, Oracle and/or its affiliates. All Rights Reserved.