1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import org .junit .After ;
22
22
import org .junit .Test ;
23
23
24
+ import org .springframework .beans .factory .annotation .Autowired ;
24
25
import org .springframework .cache .Cache ;
25
26
import org .springframework .cache .CacheManager ;
26
27
import org .springframework .cache .CacheTestUtils ;
33
34
import org .springframework .context .annotation .Bean ;
34
35
import org .springframework .context .annotation .Configuration ;
35
36
import org .springframework .context .annotation .Import ;
37
+ import org .springframework .core .env .Environment ;
38
+ import org .springframework .mock .env .MockEnvironment ;
36
39
40
+ import static org .junit .Assert .*;
37
41
import static org .springframework .cache .CacheTestUtils .*;
38
42
39
43
/**
@@ -45,13 +49,15 @@ public class EnableCachingIntegrationTests {
45
49
46
50
private ConfigurableApplicationContext context ;
47
51
52
+
48
53
@ After
49
54
public void closeContext () {
50
55
if (this .context != null ) {
51
56
this .context .close ();
52
57
}
53
58
}
54
59
60
+
55
61
@ Test
56
62
public void fooServiceWithInterface () {
57
63
this .context = new AnnotationConfigApplicationContext (FooConfig .class );
@@ -77,57 +83,91 @@ private void fooGetSimple(FooService service) {
77
83
}
78
84
79
85
@ Test
80
- public void beanCondition () {
86
+ public void beanConditionOff () {
81
87
this .context = new AnnotationConfigApplicationContext (BeanConditionConfig .class );
82
- Cache cache = getCache ();
83
88
FooService service = this .context .getBean (FooService .class );
89
+ Cache cache = getCache ();
84
90
85
91
Object key = new Object ();
86
92
service .getWithCondition (key );
87
93
assertCacheMiss (key , cache );
94
+ service .getWithCondition (key );
95
+ assertCacheMiss (key , cache );
96
+
97
+ assertEquals (2 , this .context .getBean (BeanConditionConfig .Bar .class ).count );
98
+ }
99
+
100
+ @ Test
101
+ public void beanConditionOn () {
102
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
103
+ ctx .setEnvironment (new MockEnvironment ().withProperty ("bar.enabled" , "true" ));
104
+ ctx .register (BeanConditionConfig .class );
105
+ ctx .refresh ();
106
+ this .context = ctx ;
107
+
108
+ FooService service = this .context .getBean (FooService .class );
109
+ Cache cache = getCache ();
110
+
111
+ Object key = new Object ();
112
+ Object value = service .getWithCondition (key );
113
+ assertCacheHit (key , value , cache );
114
+ value = service .getWithCondition (key );
115
+ assertCacheHit (key , value , cache );
116
+
117
+ assertEquals (2 , this .context .getBean (BeanConditionConfig .Bar .class ).count );
88
118
}
89
119
90
120
private Cache getCache () {
91
121
return this .context .getBean (CacheManager .class ).getCache ("testCache" );
92
122
}
93
123
124
+
94
125
@ Configuration
95
126
static class SharedConfig extends CachingConfigurerSupport {
127
+
96
128
@ Override
97
129
@ Bean
98
130
public CacheManager cacheManager () {
99
131
return CacheTestUtils .createSimpleCacheManager ("testCache" );
100
132
}
101
133
}
102
134
135
+
103
136
@ Configuration
104
137
@ Import (SharedConfig .class )
105
138
@ EnableCaching
106
139
static class FooConfig {
140
+
107
141
@ Bean
108
142
public FooService fooService () {
109
143
return new FooServiceImpl ();
110
144
}
111
145
}
112
146
147
+
113
148
@ Configuration
114
149
@ Import (SharedConfig .class )
115
150
@ EnableCaching (proxyTargetClass = true )
116
151
static class FooConfigCglib {
152
+
117
153
@ Bean
118
154
public FooService fooService () {
119
155
return new FooServiceImpl ();
120
156
}
121
157
}
122
158
159
+
123
160
private interface FooService {
161
+
124
162
Object getSimple (Object key );
125
163
126
164
Object getWithCondition (Object key );
127
165
}
128
166
167
+
129
168
@ CacheConfig (cacheNames = "testCache" )
130
169
private static class FooServiceImpl implements FooService {
170
+
131
171
private final AtomicLong counter = new AtomicLong ();
132
172
133
173
@ Override
@@ -143,24 +183,33 @@ public Object getWithCondition(Object key) {
143
183
}
144
184
}
145
185
186
+
146
187
@ Configuration
147
188
@ Import (FooConfig .class )
148
189
@ EnableCaching
149
190
static class BeanConditionConfig {
150
191
192
+ @ Autowired
193
+ Environment env ;
194
+
151
195
@ Bean
152
196
public Bar bar () {
153
- return new Bar (false );
197
+ return new Bar (Boolean . valueOf ( env . getProperty ( "bar.enabled" )) );
154
198
}
155
199
200
+
156
201
static class Bar {
202
+
203
+ public int count ;
204
+
157
205
private final boolean enabled ;
158
206
159
207
public Bar (boolean enabled ) {
160
208
this .enabled = enabled ;
161
209
}
162
210
163
211
public boolean isEnabled () {
212
+ this .count ++;
164
213
return this .enabled ;
165
214
}
166
215
}
0 commit comments