Package org.silverpeas.kernel.test
Class DependencyResolver
java.lang.Object
org.silverpeas.kernel.test.DependencyResolver
A resolver of dependencies on managed beans in a given object itself managed by the underlying
IoC container. This class can be extended to provide a custom dependency resolution for specific
IoD system like Spring DI or CDI. By default, it provides a simple resolution mechanism in which
when a dependency isn't found in the
BeanContainer then it is
mocked. The new implementation will be loaded by the Java SPI when a dependency resolver will be
demanded.- Author:
- mmoquillon
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DependencyResolverget()Gets an instance of a dependency resolver.final voidResolves the dependencies on others managed bean of the specified object.resolveCustomDependency(Field dependency, Annotation... qualifiers) Applies a custom dependency resolution mechanism.protected ObjectresolveDependency(Field dependency, Annotation... qualifiers) Applies the default dependency resolution mechanism.
-
Constructor Details
-
DependencyResolver
public DependencyResolver()
-
-
Method Details
-
get
Gets an instance of a dependency resolver. If at least one custom dependency resolver is found by the Java SPI, the first one will be got and returned.- Returns:
- a
DependencyResolverobject.
-
resolve
public final void resolve(Object bean) throws MultipleCandidateException, SilverpeasReflectionException Resolves the dependencies on others managed bean of the specified object. The dependencies on others managed beans are figured out by the annotationInjectapplied on the fields of the bean. The resolution is performed as follow: for each field annotated withInject:- invoke the
resolveCustomDependency(Field, Annotation...)method for custom dependency resolution. - if no bean is found by the custom dependency resolver, then invoke the
resolveDependency(Field, Annotation...)default method.
- Parameters:
bean- the bean for which the dependencies on others managed bean have to be resolved.- Throws:
MultipleCandidateException- if there is more than one managed bean satisfying a dependency when only one is requiredSilverpeasReflectionException- if the dependencies cannot be satisfied.
- invoke the
-
resolveCustomDependency
protected Optional<Object> resolveCustomDependency(@NonNull Field dependency, @NonNull Annotation... qualifiers) throws MultipleCandidateException, SilverpeasReflectionException Applies a custom dependency resolution mechanism. Does nothing by default.- Parameters:
dependency- the dependency to resolve. The field shouldn't be valued by the method.qualifiers- a set of qualifiers the managed bean to look for has to satisfy.- Returns:
- optionally a bean responding successfully to the expectations. By default, nothing.
- Throws:
MultipleCandidateException- if there is more than one managed bean satisfying a dependency.SilverpeasReflectionException- if the dependency cannot be set.
-
resolveDependency
protected Object resolveDependency(@NonNull Field dependency, @NonNull Annotation... qualifiers) throws MultipleCandidateException, SilverpeasReflectionException Applies the default dependency resolution mechanism. It looks for a bean in the IoC container that satisfies the type of the field and the specified qualifiers on the field. If no such a bean is found, then the field is mocked.- Parameters:
dependency- the dependency to resolve. The field shouldn't be valued by the method.qualifiers- a set of qualifiers the managed bean to look for has to satisfy.- Returns:
- either the managed bean that satisfies the expectations or a mock.
- Throws:
MultipleCandidateException- if there is more than one managed bean satisfying a dependency.SilverpeasReflectionException- if the dependency cannot be set.
-