-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
TypeCachingMockBytecodeGeneratorTest.java
227 lines (189 loc) · 8.76 KB
/
TypeCachingMockBytecodeGeneratorTest.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.internal.creation.bytebuddy.MockFeatures.withMockFeatures;
import static org.mockitoutil.ClassLoaders.inMemoryClassLoader;
import static org.mockitoutil.SimpleClassGenerator.makeMarkerInterface;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;
import java.util.WeakHashMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;
import org.mockito.mock.SerializableMode;
import org.mockitoutil.VmArgAssumptions;
public class TypeCachingMockBytecodeGeneratorTest {
@Before
public void ensure_disable_gc_is_activated() throws Exception {
VmArgAssumptions.assumeVmArgNotPresent("-XX:+DisableExplicitGC");
}
@Test
public void ensure_cache_is_cleared_if_no_reference_to_classloader_and_classes()
throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache =
inMemoryClassLoader()
.withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
.build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator =
new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Class<?> the_mock_type =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
Answers.RETURNS_DEFAULTS));
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
Reference<Object> typeReference =
new PhantomReference<Object>(the_mock_type, referenceQueue);
// when
classloader_with_life_shorter_than_cache = is_no_more_referenced();
the_mock_type = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(referenceQueue.poll()).isEqualTo(typeReference);
}
@Test
public void ensure_cache_returns_same_instance() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache =
inMemoryClassLoader()
.withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
.build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator =
new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Class<?> the_mock_type =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
Answers.RETURNS_DEFAULTS));
Class<?> other_mock_type =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
Answers.RETURNS_DEFAULTS));
assertThat(other_mock_type).isSameAs(the_mock_type);
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
Reference<Object> typeReference =
new PhantomReference<Object>(the_mock_type, referenceQueue);
// when
classloader_with_life_shorter_than_cache = is_no_more_referenced();
the_mock_type = is_no_more_referenced();
other_mock_type = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(referenceQueue.poll()).isEqualTo(typeReference);
}
@Test
public void ensure_cache_returns_different_instance_serializableMode() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache =
inMemoryClassLoader()
.withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
.build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator =
new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Class<?> the_mock_type =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
Answers.RETURNS_DEFAULTS));
Class<?> other_mock_type =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.BASIC,
false,
Answers.RETURNS_DEFAULTS));
assertThat(other_mock_type).isNotSameAs(the_mock_type);
}
@Test
public void ensure_cache_returns_same_instance_defaultAnswer() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache =
inMemoryClassLoader()
.withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
.build();
TypeCachingBytecodeGenerator cachingMockBytecodeGenerator =
new TypeCachingBytecodeGenerator(new SubclassBytecodeGenerator(), true);
Answers[] answers = Answers.values();
Set<Class<?>> classes = Collections.newSetFromMap(new IdentityHashMap<>());
classes.add(
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
null)));
for (Answers answer : answers) {
Class<?> klass =
cachingMockBytecodeGenerator.mockClass(
withMockFeatures(
classloader_with_life_shorter_than_cache.loadClass("foo.Bar"),
Collections.<Class<?>>emptySet(),
SerializableMode.NONE,
false,
answer));
assertThat(classes.add(klass)).isFalse();
}
assertThat(classes).hasSize(1);
}
@Test
public void
validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references()
throws Exception {
// given
WeakHashMap<ClassLoader, Object> cache = new WeakHashMap<ClassLoader, Object>();
ClassLoader short_lived_classloader =
inMemoryClassLoader()
.withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar"))
.build();
cache.put(
short_lived_classloader,
new HoldingAReference(
new WeakReference<Class<?>>(short_lived_classloader.loadClass("foo.Bar"))));
assertThat(cache).hasSize(1);
// when
short_lived_classloader = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(cache).isEmpty();
}
static class HoldingAReference {
final WeakReference<Class<?>> a;
HoldingAReference(WeakReference<Class<?>> a) {
this.a = a;
}
}
private static <T> T is_no_more_referenced() {
return null;
}
private static void ensure_gc_happened() throws InterruptedException {
// wait in order to make sure the GC happened
Thread.sleep(500);
}
}