-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractSpringTest.java
169 lines (145 loc) · 5.85 KB
/
AbstractSpringTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.test.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.jetbrains.annotations.Nullable;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import com.evolveum.midpoint.tools.testng.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* Base test class for tests integrated with Spring providing {@link MidpointTestMixin} implementation.
* Can be extended by any unit test class that would otherwise extend
* {@link AbstractTestNGSpringContextTests}.
*/
public abstract class AbstractSpringTest extends AbstractTestNGSpringContextTests
implements MidpointTestMixin {
/**
* Hides parent's logger, but that one is from commons-logging and we don't want that.
*/
protected final Trace logger = TraceManager.getTrace(getClass());
// region perf-test support
private TestMonitor testMonitor;
/** Called only by tests that need it, implements performance mixin interface. */
public TestMonitor createTestMonitor() {
testMonitor = new TestMonitor();
return testMonitor;
}
/** Called only by tests that need it, implements performance mixin interface. */
public void destroyTestMonitor() {
testMonitor = null;
}
/** Called only by tests that need it, implements performance mixin interface. */
public TestMonitor testMonitor() {
return testMonitor;
}
// see the comment in PerformanceTestMethodMixin for explanation
@BeforeMethod
public void initTestMethodMonitor() {
if (this instanceof PerformanceTestMethodMixin) {
createTestMonitor();
}
}
// see the comment in PerformanceTestMethodMixin for explanation
@AfterMethod
public void dumpMethodReport(Method method) {
if (this instanceof PerformanceTestMethodMixin) {
((PerformanceTestMethodMixin) this).dumpReport(
getClass().getSimpleName() + "#" + method.getName());
}
}
// see the comment in PerformanceTestClassMixin for explanation
@BeforeClass
public void initTestClassMonitor() {
if (this instanceof PerformanceTestClassMixin) {
createTestMonitor();
}
}
// see the comment in PerformanceTestClassMixin for explanation
@AfterClass
public void dumpClassReport() {
if (this instanceof PerformanceTestClassMixin) {
((PerformanceTestClassMixin) this).dumpReport(getClass().getSimpleName());
}
}
// endregion
@BeforeClass
public void displayTestClassTitle() {
displayTestTitle("Initializing TEST CLASS: " + getClass().getName());
}
@AfterClass
public void displayTestClassFooter() {
displayTestFooter("Finishing with TEST CLASS: " + getClass().getName());
}
@BeforeMethod
public void startTestContext(ITestResult testResult) throws Exception {
SimpleMidpointTestContext context = SimpleMidpointTestContext.create(testResult);
displayTestTitle(context.getTestName());
}
@AfterMethod
public void finishTestContext(ITestResult testResult) {
SimpleMidpointTestContext context = SimpleMidpointTestContext.get();
displayTestFooter(context.getTestName(), testResult);
SimpleMidpointTestContext.destroy();
}
@Override
public Trace logger() {
return logger;
}
@Override
@Nullable
public MidpointTestContext getTestContext() {
return SimpleMidpointTestContext.get();
}
/**
* This method null all fields which are not static, final or primitive type.
* <p>
* All this is just to make GC work during DirtiesContext after every test class,
* because test class instances are not GCed immediately.
* If they hold autowired fields like sessionFactory (for example
* through SqlRepositoryService impl), their memory footprint is getting big.
* This can manifest as failed test initialization because of OOM in modules like model-intest.
* Strangely, this will not fail the Jenkins build (but makes it much slower).
* <p>
* Note that this does not work for components injected through constructor into
* final fields - if we need this cleanup, make the field non-final.
*/
@AfterClass(alwaysRun = true, dependsOnMethods = "dumpClassReport")
protected void clearClassFields() throws Exception {
logger.trace("Clearing all fields for test class {}", getClass().getName());
clearClassFields(this, getClass());
}
private void clearClassFields(Object object, Class<?> forClass) throws Exception {
if (forClass.getSuperclass() != null) {
clearClassFields(object, forClass.getSuperclass());
}
for (Field field : forClass.getDeclaredFields()) {
if (Modifier.isFinal(field.getModifiers())
|| Modifier.isStatic(field.getModifiers())
|| field.getType().isPrimitive()) {
continue;
}
nullField(object, field);
}
}
private void nullField(Object obj, Field field) throws Exception {
logger.info("Setting {} to null on {}.", field.getName(), obj.getClass().getSimpleName());
boolean accessible = field.isAccessible();
if (!accessible) {
field.setAccessible(true);
}
field.set(obj, null);
field.setAccessible(accessible);
}
}