Class HK2Runner
- java.lang.Object
-
- org.jvnet.hk2.testing.junit.HK2Runner
-
public class HK2Runner extends java.lang.Object
This class should be extended by test classes in order to get an automatically filled in ServiceLocator. By default the testLocator will inspect the package of the test to find any classes annotated with @Service. The locator will also be able to do second-chance advertisement of services that were injected. The default ServiceLocator will also have an error handler that causes any classloading failure to get rethrown up to the lookup call, since this can sometimes cause confusion.The behavior of HK2Runner can be customized by annotating the class extending HK2Runner with
Packages
,Classes
,Excludes
orInhabitantFiles
.Packages
gives the names of packages that will automatically be scanned for classes that should be added to testLocator as services.Classes
gives an explicit set of classes that should be added to testLocator as services.Excludes
gives a set of services that should not be automatically added to the testLocator.InhabitantFiles
gives a set of inhabitant files to load in the classpath of the test.This behavior can be customized by overriding the before method of the test and calling one of the
initialize(String, List, List)
methods. The annotations listed above are overridden by any values passed to the initialize methods
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CLASS_PATH_PROP
private static java.lang.String
DOT_CLASS
protected ServiceLocator
testLocator
Test classes can use this service locator as their private test locatorprivate boolean
verbose
The verbosity of this runner
-
Constructor Summary
Constructors Constructor Description HK2Runner()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addClassIfService(java.io.InputStream is, java.util.Set<java.lang.String> excludes)
private void
addServicesFromDefault(DynamicConfiguration config, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
private void
addServicesFromPackage(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.util.Set<java.lang.String> excludes)
private void
addServicesFromPathDirectory(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.io.File directory, java.util.Set<java.lang.String> excludes)
private void
addServicesFromPathElement(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.lang.String element, java.util.Set<java.lang.String> excludes)
private void
addServicesFromPathJar(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.io.File jar, java.util.Set<java.lang.String> excludes)
void
before()
This will generate the default testLocator for this test class, which will search the package of the test itself for classes annotated with @Service.private static java.lang.String
convertToFileFormat(java.lang.String clazzFormat)
private java.util.List<java.lang.Class<?>>
getDefaultClazzes()
private java.util.Set<java.lang.String>
getDefaultExcludes()
private java.util.Set<java.lang.String>
getDefaultLocatorFiles()
private java.util.List<java.lang.String>
getDefaultPackages()
void
initialize()
This method initializes the service locator with services.protected void
initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.protected void
initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes, java.util.Set<java.lang.String> excludes)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.protected void
initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.private void
internalAddServicesFromDefault(DynamicConfiguration config, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
private void
internalAddServicesFromPackage(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.util.Set<java.lang.String> excludes)
private void
readResources(java.util.Enumeration<java.net.URL> resources, java.util.Set<java.lang.String> excludes, DynamicConfiguration config)
protected void
setVerbosity(boolean verbose)
-
-
-
Field Detail
-
CLASS_PATH_PROP
private static final java.lang.String CLASS_PATH_PROP
- See Also:
- Constant Field Values
-
DOT_CLASS
private static final java.lang.String DOT_CLASS
- See Also:
- Constant Field Values
-
testLocator
protected ServiceLocator testLocator
Test classes can use this service locator as their private test locator
-
verbose
private boolean verbose
The verbosity of this runner
-
-
Method Detail
-
before
public void before()
This will generate the default testLocator for this test class, which will search the package of the test itself for classes annotated with @Service.
-
initialize
public void initialize()
-
initialize
protected void initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.- Parameters:
name
- The name of the service locator to create. If there is already a service locator of this name then the remaining fields will be ignored and the existing locator with this name will be returned. May not be nullpackages
- The list of packages (in "." format, i.e. "com.acme.test.services") that we should hunt through the classpath for in order to find services. If null this is considered to be the empty setclazzes
- A set of classes that should be analyzed as services, whether they declare @Service or not. If null this is considered to be the empty set
-
initialize
protected void initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes, java.util.Set<java.lang.String> excludes)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.- Parameters:
name
- The name of the service locator to create. If there is already a service locator of this name then the remaining fields will be ignored and the existing locator with this name will be returned. May not be nullpackages
- The list of packages (in "." format, i.e. "com.acme.test.services") that we should hunt through the classpath for in order to find services. If null this is considered to be the empty setclazzes
- A set of classes that should be analyzed as services, whether they declare @Service or not. If null this is considered to be the empty setexcludes
- A set of implementations that should be excluded from being added. This list is NOT checked against the clazzes list (the explicit include wins), but instead against the set of things coming from packages or from the hk2-locator/default file
-
getDefaultPackages
private java.util.List<java.lang.String> getDefaultPackages()
-
getDefaultClazzes
private java.util.List<java.lang.Class<?>> getDefaultClazzes()
-
getDefaultExcludes
private java.util.Set<java.lang.String> getDefaultExcludes()
-
getDefaultLocatorFiles
private java.util.Set<java.lang.String> getDefaultLocatorFiles()
-
initialize
protected void initialize(java.lang.String name, java.util.List<java.lang.String> packages, java.util.List<java.lang.Class<?>> clazzes, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
This method initializes the service locator with services from the given list of packages (in "." format) and with the set of classes given.- Parameters:
name
- The name of the service locator to create. If there is already a service locator of this name then the remaining fields will be ignored and the existing locator with this name will be returned. May not be nullpackages
- The list of packages (in "." format, i.e. "com.acme.test.services") that we should hunt through the classpath for in order to find services. If null this is considered to be the empty setclazzes
- A set of classes that should be analyzed as services, whether they declare @Service or not. If null this is considered to be the empty setexcludes
- A set of implementations that should be excluded from being added. This list is NOT checked against the clazzes list (the explicit include wins), but instead against the set of things coming from packages or from the hk2-locator/default filelocatorFiles
- A set of locator inhabitant files to search the classpath for to load. If this value is null then only META-INF/hk2-locator/default files on the classpath will be searched. If this value is an empty set then no inhabitant files will be loaded. If this value contains values those will be searched as resources from the jars in the classpath to load the registry with
-
setVerbosity
protected void setVerbosity(boolean verbose)
-
addServicesFromDefault
private void addServicesFromDefault(DynamicConfiguration config, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
-
readResources
private void readResources(java.util.Enumeration<java.net.URL> resources, java.util.Set<java.lang.String> excludes, DynamicConfiguration config)
-
internalAddServicesFromDefault
private void internalAddServicesFromDefault(DynamicConfiguration config, java.util.Set<java.lang.String> excludes, java.util.Set<java.lang.String> locatorFiles)
-
addServicesFromPackage
private void addServicesFromPackage(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.util.Set<java.lang.String> excludes)
-
internalAddServicesFromPackage
private void internalAddServicesFromPackage(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.util.Set<java.lang.String> excludes)
-
addServicesFromPathElement
private void addServicesFromPathElement(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.lang.String element, java.util.Set<java.lang.String> excludes)
-
addServicesFromPathDirectory
private void addServicesFromPathDirectory(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.io.File directory, java.util.Set<java.lang.String> excludes)
-
addServicesFromPathJar
private void addServicesFromPathJar(DynamicConfiguration config, java.util.List<java.lang.String> packages, java.io.File jar, java.util.Set<java.lang.String> excludes)
-
addClassIfService
private void addClassIfService(java.io.InputStream is, java.util.Set<java.lang.String> excludes) throws java.io.IOException
- Throws:
java.io.IOException
-
convertToFileFormat
private static java.lang.String convertToFileFormat(java.lang.String clazzFormat)
-
-