Class TestBeanContainer

java.lang.Object
org.silverpeas.kernel.test.TestBeanContainer
All Implemented Interfaces:
BeanContainer

public class TestBeanContainer extends Object implements BeanContainer
Implementation of the BeanContainer interface dedicated to the unit tests implying and requiring an IoC/IoD container. It plays like a simple smart stub of a true IoC container.

It provides additional methods allowing to register in the container the classes of beans or to put directly into the container the beans to manage. If a class is put into the container, then both the instantiation and its initialization is managed by the container. If a bean is explicitly put into the container, then only its initialization is taken in charge by the container. The initialization of a bean consists of its dependency resolution and of the invocation of its PostConstruct annotated method (in case there is one). When the container is cleared, an invocation of the @PreDestroy annotated method is invoked for each bean before to be cleared (in case they have one). The dependency injection is done for all fields annotated with either Inject or Resource annotations. This implementation of the BeanContainer is built upon some assumptions to be kept simple and usable in unit tests:

  • Only one bean is managed for a given type and for all the parents of this type.
  • Only the default constructor without parameters is supported. For classes with a constructor having the parameters to be resolved by the container, it is recommended to calls it explicitly in the test and to put the created instance directly into the container.
  • The beans aren't proxied, meaning no resolution of the parameters is performed when a method of a managed bean is invoked. This has to be taken in charge by the test itself.
  • The Provider annotation isn't supported
  • Any specific IoC/IoD annotations and extensions aren't supported
Author:
mmoquillon
  • Constructor Details

    • TestBeanContainer

      public TestBeanContainer()
  • Method Details

    • getBeanByName

      public <T> Optional<T> getBeanByName(String name)
      Specified by:
      getBeanByName in interface BeanContainer
    • getBeanByType

      public <T> Optional<T> getBeanByType(Class<T> type, Annotation... qualifiers)
      Specified by:
      getBeanByType in interface BeanContainer
    • getAllBeansByType

      public <T> Set<T> getAllBeansByType(Class<T> type, Annotation... qualifiers)
      Specified by:
      getAllBeansByType in interface BeanContainer
    • putBean

      public <T> void putBean(T bean, String name)
      Puts directly into the container the specified bean and with the given name so that the bean could be retrieved later with that name.
      Type Parameters:
      T - the concrete type of the bean
      Parameters:
      bean - the bean to manage.
      name - the name inder which the bean will be put into the container.
    • putBean

      public <T> void putBean(Class<T> beanType, String name)
      Asks to manage the specified class of beans under the given name so that the beans could be retrieved later with that name.
      Type Parameters:
      T - the concrete type of the beans
      Parameters:
      beanType - the type of beans to manage.
      name - the name with which the beans will be created.
    • putBean

      public <T> void putBean(Class<T> beanType, Class<? super T> type, Annotation... qualifiers)
      Asks to manage the specified class of beans for the given type and with the specified qualifiers.
      Parameters:
      beanType - the class of beans to put into the container at instantiation time. Only one bean will be created on demand.
      type - the type for which the beans will be created and retrieved later.
      qualifiers - zero, one or more annotations qualifying the beans put into the container in order to be retrieved later with these qualifiers.
    • putBean

      public <T> void putBean(T bean, Class<T> type, Annotation... qualifiers)
      Puts directly into the container the specified bean for the given type and with the specified qualifiers.
      Type Parameters:
      T - the concrete type of the bean.
      Parameters:
      bean - the bean to manage.
      type - the type for which the bean will be retrieved later.
      qualifiers - zero, one or more annotations qualifying the bean put into the container in order to be retrieved later with these qualifiers.
    • clear

      public void clear()
      Clears the container. The @PreDestroy annotated method of each managed bean (if such a method exists) will be invoked before being wiped out.