Skip to content

Package: SpringTestSupport

SpringTestSupport

nameinstructionbranchcomplexitylinemethod
SpringTestSupport(SpringTestSupport.LifeCycle, String[])
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
SpringTestSupport(String[])
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
afterClass()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
afterMethod()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
beforeClass()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
beforeMethod()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
closeSpringContext()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createSpringContext()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 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/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.commons.test;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Arrays;
31: import org.springframework.context.support.ClassPathXmlApplicationContext;
32: import it.tidalwave.role.ContextManager;
33: import lombok.extern.slf4j.Slf4j;
34: import org.testng.annotations.AfterClass;
35: import org.testng.annotations.AfterMethod;
36: import org.testng.annotations.BeforeClass;
37: import org.testng.annotations.BeforeMethod;
38:
39: /***********************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: @Slf4j
45: public class SpringTestSupport
46: {
47: protected enum LifeCycle
48: {
49: AROUND_METHOD, AROUND_CLASS
50: }
51:
52: protected ClassPathXmlApplicationContext context;
53:
54: private final LifeCycle lifeCycle;
55:
56: @Nonnull
57: private final String[] configLocations;
58:
59: protected SpringTestSupport (@Nonnull final LifeCycle lifeCycle, @Nonnull final String ... configLocations)
60: {
61: this.lifeCycle = lifeCycle;
62: this.configLocations = configLocations;
63: }
64:
65: protected SpringTestSupport (@Nonnull final String ... configLocations)
66: {
67: this(LifeCycle.AROUND_METHOD, configLocations);
68: }
69:
70: @BeforeMethod
71: public final void beforeMethod()
72: {
73:• if (lifeCycle == LifeCycle.AROUND_METHOD)
74: {
75: createSpringContext();
76: }
77: }
78:
79: @AfterMethod(timeOut = 60000)
80: public final void afterMethod()
81: {
82:• if (lifeCycle == LifeCycle.AROUND_METHOD)
83: {
84: closeSpringContext();
85: }
86: }
87:
88: @BeforeClass
89: public final void beforeClass()
90: {
91:• if (lifeCycle == LifeCycle.AROUND_CLASS)
92: {
93: createSpringContext();
94: }
95: }
96:
97: @AfterClass(timeOut = 60000)
98: public final void afterClass()
99: {
100:• if (lifeCycle == LifeCycle.AROUND_CLASS)
101: {
102: closeSpringContext();
103: }
104: }
105:
106: private void createSpringContext()
107: {
108: log.info("Spring configuration locations: {}", Arrays.toString(configLocations));
109: context = new ClassPathXmlApplicationContext(configLocations);
110: log.info(">>>> bean names: {}", Arrays.toString(context.getBeanDefinitionNames()));
111: }
112:
113: private void closeSpringContext()
114: {
115: log.info("Closing Spring context...");
116: context.close();
117: context = null; // don't keep in memory useless stuff
118: ContextManager.Locator.set(null);
119: }
120: }