SpringTestHelper.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util.test;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.function.Consumer;
  35. import org.springframework.context.ApplicationContext;
  36. import org.springframework.context.support.GenericApplicationContext;
  37. import org.springframework.context.support.GenericXmlApplicationContext;
  38. import org.springframework.core.env.MapPropertySource;
  39. import org.springframework.core.env.StandardEnvironment;
  40. import lombok.extern.slf4j.Slf4j;

  41. /***********************************************************************************************************************
  42.  *
  43.  * A facility that provides some common tasks for testing, such as creating a Spring context and manipulating files.
  44.  *
  45.  * @author  Fabrizio Giudici
  46.  * @since 3.2-ALPHA-18
  47.  *
  48.  **********************************************************************************************************************/
  49. @Slf4j
  50. public class SpringTestHelper extends BaseTestHelper
  51.   {
  52.     /*******************************************************************************************************************
  53.      *
  54.      ******************************************************************************************************************/
  55.     public SpringTestHelper (@Nonnull final Object test)
  56.       {
  57.         super(test);
  58.       }

  59.     /*******************************************************************************************************************
  60.      *
  61.      * Creates a Spring context configured with the given files. A further configuration file is appended, named
  62.      * {@code test-class-simple-name/TestBeans.xml}.
  63.      *
  64.      * @param   configurationFiles  the configuration files
  65.      * @return                      the Spring {@link ApplicationContext}
  66.      *
  67.      ******************************************************************************************************************/
  68.     @Nonnull
  69.     public ApplicationContext createSpringContext (@Nonnull final String ... configurationFiles)
  70.       {
  71.         return createSpringContext(Collections.emptyMap(), configurationFiles);
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      * Creates a Spring context configured with the given files. A further configuration file is appended, named
  76.      * {@code test-class-simple-name/TestBeans.xml}.
  77.      *
  78.      * @param   properties          the properties
  79.      * @param   configurationFiles  the configuration files
  80.      * @return                      the Spring {@link ApplicationContext}
  81.      *
  82.      ******************************************************************************************************************/
  83.     @Nonnull
  84.     public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
  85.                                                    @Nonnull final String ... configurationFiles)
  86.       {
  87.         return createSpringContext(properties, context -> {}, new ArrayList<>(List.of(configurationFiles)));
  88.       }

  89.     /*******************************************************************************************************************
  90.      *
  91.      * Creates a Spring context configured with the given files. A further configuration file is appended, named
  92.      * {@code test-class-simple-name/TestBeans.xml}.
  93.      *
  94.      * @param   configurationFiles  the configuration files
  95.      * @param   modifier            a processor to modify the contents of the context
  96.      * @return                      the Spring {@link ApplicationContext}
  97.      *
  98.      ******************************************************************************************************************/
  99.     @Nonnull
  100.     public ApplicationContext createSpringContext (@Nonnull final Consumer<? super GenericApplicationContext> modifier,
  101.                                                    @Nonnull final String ... configurationFiles)
  102.       {
  103.         return createSpringContext(Collections.emptyMap(), modifier, configurationFiles);
  104.       }

  105.     /*******************************************************************************************************************
  106.      *
  107.      * Creates a Spring context configured with the given files. A further configuration file is appended, named
  108.      * {@code test-class-simple-name/TestBeans.xml}.
  109.      *
  110.      * @param   properties          the properties
  111.      * @param   modifier            a processor to modify the contents of the context
  112.      * @param   configurationFiles  the configuration files
  113.      * @return                      the Spring {@link ApplicationContext}
  114.      *
  115.      ******************************************************************************************************************/
  116.     @Nonnull
  117.     public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
  118.                                                    @Nonnull final Consumer<? super GenericApplicationContext> modifier,
  119.                                                    @Nonnull final String ... configurationFiles)
  120.       {
  121.         return createSpringContext(properties, modifier, new ArrayList<>(List.of(configurationFiles)));
  122.       }

  123.     /*******************************************************************************************************************
  124.      *
  125.      ******************************************************************************************************************/
  126.     @Nonnull
  127.     private ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
  128.                                                     @Nonnull final Consumer<? super GenericApplicationContext> modifier,
  129.                                                     @Nonnull final Collection<? super String> configurationFiles)
  130.       {
  131.         configurationFiles.add(test.getClass().getSimpleName() + "/TestBeans.xml");

  132.         final var environment = new StandardEnvironment();
  133.         environment.getPropertySources().addFirst(new MapPropertySource("test", properties));
  134.         final var context = new GenericXmlApplicationContext();
  135.         context.setEnvironment(environment);
  136.         context.load(configurationFiles.toArray(new String[0]));
  137.         modifier.accept(context);
  138.         context.refresh();
  139.         log.info("Beans: {}", List.of(context.getBeanFactory().getBeanDefinitionNames()));

  140.         return context;
  141.       }
  142.   }