Skip to content

Package: SpringTestHelper

SpringTestHelper

nameinstructionbranchcomplexitylinemethod
SpringTestHelper(Object)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createSpringContext(Consumer, String[])
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createSpringContext(Map, Consumer, Collection)
M: 48 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
createSpringContext(Map, Consumer, String[])
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createSpringContext(Map, String[])
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createSpringContext(String[])
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$createSpringContext$0(GenericApplicationContext)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Collections;
33: import java.util.List;
34: import java.util.Map;
35: import java.util.function.Consumer;
36: import org.springframework.context.ApplicationContext;
37: import org.springframework.context.support.GenericApplicationContext;
38: import org.springframework.context.support.GenericXmlApplicationContext;
39: import org.springframework.core.env.MapPropertySource;
40: import org.springframework.core.env.StandardEnvironment;
41: import lombok.extern.slf4j.Slf4j;
42:
43: /***********************************************************************************************************************
44: *
45: * A facility that provides some common tasks for testing, such as creating a Spring context and manipulating files.
46: *
47: * @author Fabrizio Giudici
48: * @since 3.2-ALPHA-18
49: *
50: **********************************************************************************************************************/
51: @Slf4j
52: public class SpringTestHelper extends BaseTestHelper
53: {
54: /*******************************************************************************************************************
55: *
56: ******************************************************************************************************************/
57: public SpringTestHelper (@Nonnull final Object test)
58: {
59: super(test);
60: }
61:
62: /*******************************************************************************************************************
63: *
64: * Creates a Spring context configured with the given files. A further configuration file is appended, named
65: * {@code test-class-simple-name/TestBeans.xml}.
66: *
67: * @param configurationFiles the configuration files
68: * @return the Spring {@link ApplicationContext}
69: *
70: ******************************************************************************************************************/
71: @Nonnull
72: public ApplicationContext createSpringContext (@Nonnull final String ... configurationFiles)
73: {
74: return createSpringContext(Collections.emptyMap(), configurationFiles);
75: }
76:
77: /*******************************************************************************************************************
78: *
79: * Creates a Spring context configured with the given files. A further configuration file is appended, named
80: * {@code test-class-simple-name/TestBeans.xml}.
81: *
82: * @param properties the properties
83: * @param configurationFiles the configuration files
84: * @return the Spring {@link ApplicationContext}
85: *
86: ******************************************************************************************************************/
87: @Nonnull
88: public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
89: @Nonnull final String ... configurationFiles)
90: {
91: return createSpringContext(properties, context -> {}, new ArrayList<>(List.of(configurationFiles)));
92: }
93:
94: /*******************************************************************************************************************
95: *
96: * Creates a Spring context configured with the given files. A further configuration file is appended, named
97: * {@code test-class-simple-name/TestBeans.xml}.
98: *
99: * @param configurationFiles the configuration files
100: * @param modifier a processor to modify the contents of the context
101: * @return the Spring {@link ApplicationContext}
102: *
103: ******************************************************************************************************************/
104: @Nonnull
105: public ApplicationContext createSpringContext (@Nonnull final Consumer<? super GenericApplicationContext> modifier,
106: @Nonnull final String ... configurationFiles)
107: {
108: return createSpringContext(Collections.emptyMap(), modifier, configurationFiles);
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * Creates a Spring context configured with the given files. A further configuration file is appended, named
114: * {@code test-class-simple-name/TestBeans.xml}.
115: *
116: * @param properties the properties
117: * @param modifier a processor to modify the contents of the context
118: * @param configurationFiles the configuration files
119: * @return the Spring {@link ApplicationContext}
120: *
121: ******************************************************************************************************************/
122: @Nonnull
123: public ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
124: @Nonnull final Consumer<? super GenericApplicationContext> modifier,
125: @Nonnull final String ... configurationFiles)
126: {
127: return createSpringContext(properties, modifier, new ArrayList<>(List.of(configurationFiles)));
128: }
129:
130: /*******************************************************************************************************************
131: *
132: ******************************************************************************************************************/
133: @Nonnull
134: private ApplicationContext createSpringContext (@Nonnull final Map<String, Object> properties,
135: @Nonnull final Consumer<? super GenericApplicationContext> modifier,
136: @Nonnull final Collection<? super String> configurationFiles)
137: {
138: configurationFiles.add(test.getClass().getSimpleName() + "/TestBeans.xml");
139:
140: final var environment = new StandardEnvironment();
141: environment.getPropertySources().addFirst(new MapPropertySource("test", properties));
142: final var context = new GenericXmlApplicationContext();
143: context.setEnvironment(environment);
144: context.load(configurationFiles.toArray(new String[0]));
145: modifier.accept(context);
146: context.refresh();
147: log.info("Beans: {}", List.of(context.getBeanFactory().getBeanDefinitionNames()));
148:
149: return context;
150: }
151: }