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 the License.
12 * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.util.test;
27
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 *
44 * A facility that provides some common tasks for testing, such as creating a Spring context and manipulating files.
45 *
46 * @author Fabrizio Giudici
47 * @since 3.2-ALPHA-18
48 *
49 **************************************************************************************************************************************************************/
50 @Slf4j
51 public class SpringTestHelper extends BaseTestHelper
52 {
53 /***********************************************************************************************************************************************************
54 *
55 **********************************************************************************************************************************************************/
56 public SpringTestHelper (@Nonnull final Object test)
57 {
58 super(test);
59 }
60
61 /***********************************************************************************************************************************************************
62 * Creates a Spring context configured with the given files. A further configuration file is appended, named
63 * {@code test-class-simple-name/TestBeans.xml}.
64 *
65 * @param configurationFiles the configuration files
66 * @return the Spring {@link ApplicationContext}
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 @Nonnull
83 public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
84 @Nonnull final String ... configurationFiles)
85 {
86 return createSpringContext(properties, context -> {}, new ArrayList<>(List.of(configurationFiles)));
87 }
88
89 /***********************************************************************************************************************************************************
90 * Creates a Spring context configured with the given files. A further configuration file is appended, named
91 * {@code test-class-simple-name/TestBeans.xml}.
92 *
93 * @param configurationFiles the configuration files
94 * @param modifier a processor to modify the contents of the context
95 * @return the Spring {@link ApplicationContext}
96 **********************************************************************************************************************************************************/
97 @Nonnull
98 public ApplicationContext createSpringContext (@Nonnull final Consumer<? super GenericApplicationContext> modifier,
99 @Nonnull final String ... configurationFiles)
100 {
101 return createSpringContext(Collections.emptyMap(), modifier, configurationFiles);
102 }
103
104 /***********************************************************************************************************************************************************
105 * Creates a Spring context configured with the given files. A further configuration file is appended, named
106 * {@code test-class-simple-name/TestBeans.xml}.
107 *
108 * @param properties the properties
109 * @param modifier a processor to modify the contents of the context
110 * @param configurationFiles the configuration files
111 * @return the Spring {@link ApplicationContext}
112 **********************************************************************************************************************************************************/
113 @Nonnull
114 public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
115 @Nonnull final Consumer<? super GenericApplicationContext> modifier,
116 @Nonnull final String ... configurationFiles)
117 {
118 return createSpringContext(properties, modifier, new ArrayList<>(List.of(configurationFiles)));
119 }
120
121 /***********************************************************************************************************************************************************
122 *
123 **********************************************************************************************************************************************************/
124 @Nonnull
125 private ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
126 @Nonnull final Consumer<? super GenericApplicationContext> modifier,
127 @Nonnull final Collection<? super String> configurationFiles)
128 {
129 configurationFiles.add(test.getClass().getSimpleName() + "/TestBeans.xml");
130
131 final var environment = new StandardEnvironment();
132 environment.getPropertySources().addFirst(new MapPropertySource("test", properties));
133 final var context = new GenericXmlApplicationContext();
134 context.setEnvironment(environment);
135 context.load(configurationFiles.toArray(new String[0]));
136 modifier.accept(context);
137 context.refresh();
138 log.info("Beans: {}", List.of(context.getBeanFactory().getBeanDefinitionNames()));
139
140 return context;
141 }
142 }