/
ScopingTests.java
377 lines (288 loc) · 9.67 KB
/
ScopingTests.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation.configuration;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.context.support.GenericApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests that scopes are properly supported by using a custom Scope implementations
* and scoped proxy {@link Bean} declarations.
*
* @author Costin Leau
* @author Chris Beams
*/
class ScopingTests {
public static String flag = "1";
private static final String SCOPE = "my scope";
private CustomScope customScope;
private GenericApplicationContext ctx;
@BeforeEach
void setUp() {
customScope = new CustomScope();
ctx = createContext(ScopedConfigurationClass.class);
}
@AfterEach
void tearDown() {
if (ctx != null) {
ctx.close();
}
}
private GenericApplicationContext createContext(Class<?> configClass) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
if (customScope != null) {
beanFactory.registerScope(SCOPE, customScope);
}
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(beanFactory);
ctx.refresh();
return ctx;
}
@Test
void testScopeOnClasses() {
genericTestScope("scopedClass");
}
@Test
void testScopeOnInterfaces() {
genericTestScope("scopedInterface");
}
private void genericTestScope(String beanName) {
String message = "scope is ignored";
Object bean1 = ctx.getBean(beanName);
Object bean2 = ctx.getBean(beanName);
assertThat(bean2).as(message).isSameAs(bean1);
Object bean3 = ctx.getBean(beanName);
assertThat(bean3).as(message).isSameAs(bean1);
// make the scope create a new object
customScope.createNewScope = true;
Object newBean1 = ctx.getBean(beanName);
assertThat(newBean1).as(message).isNotSameAs(bean1);
Object sameBean1 = ctx.getBean(beanName);
assertThat(sameBean1).as(message).isSameAs(newBean1);
// make the scope create a new object
customScope.createNewScope = true;
Object newBean2 = ctx.getBean(beanName);
assertThat(newBean2).as(message).isNotSameAs(newBean1);
// make the scope create a new object .. again
customScope.createNewScope = true;
Object newBean3 = ctx.getBean(beanName);
assertThat(newBean3).as(message).isNotSameAs(newBean2);
}
@Test
void testSameScopeOnDifferentBeans() {
Object beanAInScope = ctx.getBean("scopedClass");
Object beanBInScope = ctx.getBean("scopedInterface");
assertThat(beanBInScope).isNotSameAs(beanAInScope);
customScope.createNewScope = true;
Object newBeanAInScope = ctx.getBean("scopedClass");
Object newBeanBInScope = ctx.getBean("scopedInterface");
assertThat(newBeanBInScope).isNotSameAs(newBeanAInScope);
assertThat(beanAInScope).isNotSameAs(newBeanAInScope);
assertThat(beanBInScope).isNotSameAs(newBeanBInScope);
}
@Test
void testRawScopes() {
String beanName = "scopedProxyInterface";
// get hidden bean
Object bean = ctx.getBean("scopedTarget." + beanName);
boolean condition = bean instanceof ScopedObject;
assertThat(condition).isFalse();
}
@Test
void testScopedProxyConfiguration() {
TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedInterfaceDep");
ITestBean spouse = singleton.getSpouse();
boolean condition = spouse instanceof ScopedObject;
assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
String beanName = "scopedProxyInterface";
String scopedBeanName = "scopedTarget." + beanName;
// get hidden bean
assertThat(spouse.getName()).isEqualTo(flag);
ITestBean spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
// the scope proxy has kicked in
assertThat(spouseFromBF).isNotSameAs(spouse);
// create a new bean
customScope.createNewScope = true;
// get the bean again from the BF
spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
// make sure the name has been updated
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
assertThat(spouseFromBF).isNotSameAs(spouse);
// get the bean again
spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
@Test
void testScopedProxyConfigurationWithClasses() {
TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedClassDep");
ITestBean spouse = singleton.getSpouse();
boolean condition = spouse instanceof ScopedObject;
assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
String beanName = "scopedProxyClass";
String scopedBeanName = "scopedTarget." + beanName;
// get hidden bean
assertThat(spouse.getName()).isEqualTo(flag);
TestBean spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
// the scope proxy has kicked in
assertThat(spouseFromBF).isNotSameAs(spouse);
// create a new bean
customScope.createNewScope = true;
flag = "boo";
// get the bean again from the BF
spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
// make sure the name has been updated
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
assertThat(spouseFromBF).isNotSameAs(spouse);
// get the bean again
spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
static class Foo {
public Foo() {
}
public void doSomething() {
}
}
static class Bar {
private final Foo foo;
public Bar(Foo foo) {
this.foo = foo;
}
public Foo getFoo() {
return foo;
}
}
@Configuration
public static class InvalidProxyOnPredefinedScopesConfiguration {
@Bean @Scope(proxyMode=ScopedProxyMode.INTERFACES)
public Object invalidProxyOnPredefinedScopes() {
return new Object();
}
}
@Configuration
public static class ScopedConfigurationClass {
@Bean
@MyScope
public TestBean scopedClass() {
TestBean tb = new TestBean();
tb.setName(flag);
return tb;
}
@Bean
@MyScope
public ITestBean scopedInterface() {
TestBean tb = new TestBean();
tb.setName(flag);
return tb;
}
@Bean
@MyProxiedScope
public ITestBean scopedProxyInterface() {
TestBean tb = new TestBean();
tb.setName(flag);
return tb;
}
@MyProxiedScope
public TestBean scopedProxyClass() {
TestBean tb = new TestBean();
tb.setName(flag);
return tb;
}
@Bean
public TestBean singletonWithScopedClassDep() {
TestBean singleton = new TestBean();
singleton.setSpouse(scopedProxyClass());
return singleton;
}
@Bean
public TestBean singletonWithScopedInterfaceDep() {
TestBean singleton = new TestBean();
singleton.setSpouse(scopedProxyInterface());
return singleton;
}
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope(SCOPE)
@interface MyScope {
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Bean
@Scope(value=SCOPE, proxyMode=ScopedProxyMode.TARGET_CLASS)
@interface MyProxiedScope {
}
/**
* Simple scope implementation which creates object based on a flag.
* @author Costin Leau
* @author Chris Beams
*/
static class CustomScope implements org.springframework.beans.factory.config.Scope {
public boolean createNewScope = true;
private Map<String, Object> beans = new HashMap<>();
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
if (createNewScope) {
beans.clear();
// reset the flag back
createNewScope = false;
}
Object bean = beans.get(name);
// if a new object is requested or none exists under the current
// name, create one
if (bean == null) {
beans.put(name, objectFactory.getObject());
}
return beans.get(name);
}
@Override
public String getConversationId() {
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
throw new IllegalStateException("Not supposed to be called");
}
@Override
public Object remove(String name) {
return beans.remove(name);
}
@Override
public Object resolveContextualObject(String key) {
return null;
}
}
}