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
- 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.private static class
ServiceLocatorTestRule.Visitor
A very, very special-purposeClassVisitorImpl
suitable only for use by theServiceLocatorTestRule
class to determine efficiently whether a givenClass
is annotated withService
or not.
-
Field Summary
Fields Modifier and Type Field Description private org.junit.runner.Description
description
TheDescription
describing the JUnit test method currently executing.private ServiceLocatorTestRule.ServiceLocatorIsolation
isolation
TheServiceLocatorTestRule.ServiceLocatorIsolation
designating whether theServiceLocator
associated with thisServiceLocatorTestRule
is set up for each test method or shared among them.private ServiceLocator
serviceLocator
TheServiceLocator
in effect for the currently executing JUnit test method.private T
test
The test instance instantiating thisServiceLocatorTestRule
.private boolean
verbose
Aboolean
indicating whether thisServiceLocatorTestRule
is configured to output additional information toSystem.out
.
-
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 Static 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.private static void
bind(DynamicConfiguration configuration, java.io.BufferedReader reader)
Reads the HK2 locator file represented by the suppliedBufferedReader
and binds theDescriptor
s it represents.private void
bind(DynamicConfiguration configuration, java.lang.reflect.AnnotatedElement element)
Retrieves anyAnnotation
s found on the suppliedAnnotatedElement
and calls thebind(DynamicConfiguration, Collection)
method supplying them as the second parameter.private static void
bind(DynamicConfiguration configuration, java.lang.String locatorResourceName)
Using the contextClassLoader
, gets all classpath resources with the suppliedlocatorResourceName
, and iterates through them, calling thebind(DynamicConfiguration, URL)
method for each one.private static void
bind(DynamicConfiguration configuration, java.net.URL locatorResource)
Opens anInputStream
to the suppliedURL
, wraps aBufferedReader
around it, and calls thebind(DynamicConfiguration, BufferedReader)
method.private void
bind(DynamicConfiguration configuration, java.util.Collection<? extends java.lang.annotation.Annotation> annotations)
Recursively examines theAnnotation
s supplied looking for instances of theClasses
,InhabitantFiles
andPackages
annotations, and, using the binding methods on the suppliedDynamicConfiguration
bindsDescriptor
s for those "reachable" classes into the configuration.private void
bind(DynamicConfiguration configuration, java.util.Set<java.lang.String> packageNames)
Binds allClass
es "reachable" from the supplied packages.private void
bind(DynamicConfiguration configuration, Classes classes)
Binds allClass
es "reachable" from the suppliedClasses
annotation.private static void
bind(DynamicConfiguration configuration, InhabitantFiles inhabitantFiles)
Gathers all classpath resource names found in the suppliedInhabitantFiles
annotation representing HK2 locator files and calls thebind(DynamicConfiguration, String)
method for each such entry found.private void
bind(DynamicConfiguration configuration, Packages packages)
Binds allClass
es "reachable" from packages described by the suppliedPackages
annotation.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
.private static java.util.Set<java.lang.Class<?>>
getClassesFromDirectory(java.util.Set<java.lang.String> packageNames, java.io.File directory, boolean verbose)
Given aSet
of package names and aFile
designating an existing directory in the classpath that can be read, returns aSet
ofService
-annotatedClass
es that can be found there that belong to one of the supplied package names.private java.util.Set<java.lang.Class<?>>
getClassesFromPackages(java.util.Set<java.lang.String> packageNames)
Given aSet
of package names, returns aSet
ofService
-annotatedClass
es found in those packages, whether they are located inZipFile
s or directories on the classpath.private java.util.Set<java.lang.Class<?>>
getClassesFromZipFile(java.util.Set<java.lang.String> packageNames, java.util.zip.ZipFile zipFile)
Given aZipFile
(which is usually a.jar
file) containing class files and aSet
of package names, searches it to findService
-annotatedClass
es inside it that belong to one of the supplied package names and returns aSet
of suchClass
es.protected java.lang.String
getClasspath()
Returns aString
representing the classpath to use to search for classes.private org.junit.runner.Description
getDescription()
Returns theDescription
describing the currently executing JUnit test method.ServiceLocatorTestRule.ServiceLocatorIsolation
getServiceLocatorIsolation()
Returns theServiceLocatorTestRule.ServiceLocatorIsolation
for thisServiceLocatorTestRule
.protected java.lang.String
getServiceLocatorName(org.junit.runner.Description testDescription)
Returns a name for aServiceLocator
that is appropriate for the suppliedDescription
.private static boolean
isBlacklisted(java.lang.annotation.Annotation annotation)
Returnstrue
if the suppliedAnnotation
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method.private static boolean
isBlacklisted(java.lang.Class<? extends java.lang.annotation.Annotation> c)
Returnstrue
if the suppliedClass
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method.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
.
-
-
-
Field Detail
-
test
private final T test
The test instance instantiating thisServiceLocatorTestRule
.This field is never
null
.
-
isolation
private final ServiceLocatorTestRule.ServiceLocatorIsolation isolation
TheServiceLocatorTestRule.ServiceLocatorIsolation
designating whether theServiceLocator
associated with thisServiceLocatorTestRule
is set up for each test method or shared among them.This field is never
null
.
-
verbose
private final boolean verbose
Aboolean
indicating whether thisServiceLocatorTestRule
is configured to output additional information toSystem.out
.- See Also:
isVerbose()
-
description
private org.junit.runner.Description description
TheDescription
describing the JUnit test method currently executing.This field may be
null
.- See Also:
apply(Statement, Description)
-
serviceLocator
private ServiceLocator serviceLocator
TheServiceLocator
in effect for the currently executing JUnit test method.This field may be
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:
java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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
-
getDescription
private final org.junit.runner.Description getDescription()
Returns theDescription
describing the currently executing JUnit test method.This method may return
null
.- Returns:
- the
Description
describing the currently executing JUnit test method, ornull
-
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 java.io.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:
java.io.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 java.io.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:
java.lang.AssertionError
- if eitherserviceLocator
ortestDescription
isnull
java.io.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
-
getClassesFromPackages
private final java.util.Set<java.lang.Class<?>> getClassesFromPackages(java.util.Set<java.lang.String> packageNames) throws java.io.IOException
Given aSet
of package names, returns aSet
ofService
-annotatedClass
es found in those packages, whether they are located inZipFile
s or directories on the classpath.This method never returns
null
.- Parameters:
packageNames
- the names of packages to search; may benull
in which case an emptySet
will be returned- Returns:
- a non-
null
Set
ofClass
es - Throws:
java.io.IOException
- if there was any kind of error during package searching or file reading- See Also:
getClassesFromDirectory(Set, File, boolean)
,getClassesFromZipFile(Set, ZipFile)
-
getClassesFromZipFile
private final java.util.Set<java.lang.Class<?>> getClassesFromZipFile(java.util.Set<java.lang.String> packageNames, java.util.zip.ZipFile zipFile) throws java.io.IOException
Given aZipFile
(which is usually a.jar
file) containing class files and aSet
of package names, searches it to findService
-annotatedClass
es inside it that belong to one of the supplied package names and returns aSet
of suchClass
es.- Parameters:
packageNames
- aSet
of package names; may benull
in which case an emptySet
will be returnedzipFile
- aZipFile
hopefully containing class files; may benull
in which case an emptySet
will be returned- Returns:
- a non-
null
Set
ofService
-annotatedClass
es - Throws:
java.io.IOException
- if an error occurred reading theZipFile
-
getClasspath
protected java.lang.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
-
bind
private final void bind(DynamicConfiguration configuration, java.lang.reflect.AnnotatedElement element) throws java.io.IOException
Retrieves anyAnnotation
s found on the suppliedAnnotatedElement
and calls thebind(DynamicConfiguration, Collection)
method supplying them as the second parameter.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
element
- theAnnotatedElement
that has annotations; may benull
in which case no action is performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading class or locator files- See Also:
bind(DynamicConfiguration, Collection)
-
bind
private final void bind(DynamicConfiguration configuration, java.util.Collection<? extends java.lang.annotation.Annotation> annotations) throws java.io.IOException
Recursively examines theAnnotation
s supplied looking for instances of theClasses
,InhabitantFiles
andPackages
annotations, and, using the binding methods on the suppliedDynamicConfiguration
bindsDescriptor
s for those "reachable" classes into the configuration.Recursively means that a user-defined
Annotation
might itself be annotated withClasses
; in such a case theClasses
annotation's contents will be found and processed. This effectively allows stereotyping theClasses
,InhabitantFiles
andPackages
annotations.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
annotations
- aCollection
ofAnnotation
s; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading class or locator files- See Also:
bind(DynamicConfiguration, Classes)
,bind(DynamicConfiguration, InhabitantFiles)
,bind(DynamicConfiguration, Packages)
-
bind
private final void bind(DynamicConfiguration configuration, Classes classes) throws java.io.IOException
Binds allClass
es "reachable" from the suppliedClasses
annotation.Each
Class
found as a member of the return value of theClasses.value()
method is bound (even if it is an implementation ofFactory
) and in turn is examined forAnnotation
s on itself.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
classes
- aClasses
; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading class files or locator files- See Also:
DynamicConfiguration.addActiveDescriptor(Class)
,DynamicConfiguration.addActiveFactoryDescriptor(Class)
-
bind
private final void bind(DynamicConfiguration configuration, Packages packages) throws java.io.IOException
Binds allClass
es "reachable" from packages described by the suppliedPackages
annotation.This method calls the
bind(DynamicConfiguration, Set)
method, passing it aSet
formed from the suppliedPackages
annotation's list of package names as the second parameter.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
packages
- aPackages
; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading class files or locator files- See Also:
bind(DynamicConfiguration, Set)
-
bind
private final void bind(DynamicConfiguration configuration, java.util.Set<java.lang.String> packageNames) throws java.io.IOException
Binds allClass
es "reachable" from the supplied packages.This method calls the
getClassesFromPackages(Set)
method, and, for eachClass
returned, if it is an implementation ofFactory
then twoDescriptor
s are added to theDynamicConfiguration
in the form of aFactoryDescriptors
addition by way of theDynamicConfiguration.addActiveFactoryDescriptor(Class)
method, or, if not, then a singleDescriptor
is added to theDynamicConfiguration
.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
packageNames
- aSet
of package names; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading class files or locator files- See Also:
getClassesFromPackages(Set)
,DynamicConfiguration.addActiveDescriptor(Class)
,DynamicConfiguration.addActiveFactoryDescriptor(Class)
-
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:
java.lang.AssertionError
- iftestDescription
isnull
-
getServiceLocatorName
protected java.lang.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:
java.lang.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
-
getClassesFromDirectory
private static final java.util.Set<java.lang.Class<?>> getClassesFromDirectory(java.util.Set<java.lang.String> packageNames, java.io.File directory, boolean verbose) throws java.io.IOException
Given aSet
of package names and aFile
designating an existing directory in the classpath that can be read, returns aSet
ofService
-annotatedClass
es that can be found there that belong to one of the supplied package names.This method never returns
null
.- Parameters:
packageNames
- aSet
of package names; may benull
in which case an emptySet
will be returneddirectory
- aFile
designating a directory; an emptySet
will be returned unless the directory so designated exists and is readableverbose
- whether additional information should be output- Returns:
- a non-
null
Set
ofService
-annotatedClass
es - Throws:
java.io.IOException
- if there was a problem reading files
-
isBlacklisted
private static final boolean isBlacklisted(java.lang.annotation.Annotation annotation)
Returnstrue
if the suppliedAnnotation
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method.- Parameters:
annotation
- theAnnotation
to consider; may benull
in which casetrue
will be returned- Returns:
true
if the suppliedAnnotation
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method- See Also:
isBlacklisted(Class)
-
isBlacklisted
private static final boolean isBlacklisted(java.lang.Class<? extends java.lang.annotation.Annotation> c)
Returnstrue
if the suppliedClass
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method.- Parameters:
c
- theClass
to consider; may benull
in which casetrue
will be returned- Returns:
true
if the suppliedClass
isnull
or designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)
method;false
otherwise
-
bind
private static final void bind(DynamicConfiguration configuration, InhabitantFiles inhabitantFiles) throws java.io.IOException
Gathers all classpath resource names found in the suppliedInhabitantFiles
annotation representing HK2 locator files and calls thebind(DynamicConfiguration, String)
method for each such entry found.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
inhabitantFiles
- anInhabitantFiles
; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading locator files- See Also:
bind(DynamicConfiguration, String)
-
bind
private static final void bind(DynamicConfiguration configuration, java.lang.String locatorResourceName) throws java.io.IOException
Using the contextClassLoader
, gets all classpath resources with the suppliedlocatorResourceName
, and iterates through them, calling thebind(DynamicConfiguration, URL)
method for each one.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
locatorResourceName
- the name of a classpath resource identifying one or more HK2 locator files; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading locator files- See Also:
ClassLoader.getResources(String)
,Thread.getContextClassLoader()
,bind(DynamicConfiguration, URL)
-
bind
private static final void bind(DynamicConfiguration configuration, java.net.URL locatorResource) throws java.io.IOException
Opens anInputStream
to the suppliedURL
, wraps aBufferedReader
around it, and calls thebind(DynamicConfiguration, BufferedReader)
method.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
locatorResource
- aURL
to an HK2 locator resource; may benull
in which case no action will be taken- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading locator files- See Also:
bind(DynamicConfiguration, BufferedReader)
-
bind
private static final void bind(DynamicConfiguration configuration, java.io.BufferedReader reader) throws java.io.IOException
Reads the HK2 locator file represented by the suppliedBufferedReader
and binds theDescriptor
s it represents.- Parameters:
configuration
- aDynamicConfiguration
that will perform the binding; must not benull
reader
- aBufferedReader
reading a stream of descriptor information as parseable by theDescriptorImpl.readObject(BufferedReader)
method; may benull
in which case no action will be performed- Throws:
java.lang.AssertionError
- ifconfiguration
isnull
java.io.IOException
- if there was a problem reading locator files- See Also:
DescriptorImpl.readObject(BufferedReader)
,DynamicConfiguration.bind(Descriptor)
-
-