1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.tests.core.aop;
19
20 import static org.junit.Assert.assertEquals;
21
22 import org.junit.Test;
23 import org.springframework.context.ApplicationContext;
24
25 import ch.elca.el4j.core.context.ModuleApplicationContext;
26
27
28
29
30
31
32
33
34
35 public class ExclusiveBeanNameAutoProxyCreatorTest {
36
37
38 public static final String PROXIED = "proxied";
39
40
41 private static final String BEAN_A = "beanA";
42
43
44 private static final String BEAN_B = "beanB";
45
46
47 private static final String FOO = "foo";
48
49
50 private static final String BAR = "bar";
51
52
53 private static final String FOOBAR = "foobar";
54
55
56 private static final String[] BEAN_NAMES = {
57 BEAN_A, BEAN_B, FOO, BAR, FOOBAR
58 };
59
60
61 private static final String EMPTY_EXCLUSIVE_CONFIG
62 = "classpath:scenarios/core/aop/emptyExclusiveList.xml";
63
64
65 private static final String EXCLUDE_ALL_CONFIG
66 = "classpath:scenarios/core/aop/excludeAll.xml";
67
68
69 private static final String INCLUDE_ALL_BUT_FOOBAR_CONFIG
70 = "classpath:scenarios/core/aop/includeAllButFoobar.xml";
71
72
73 private static final String INCLUDE_ALL_BUT_FOO_PREFIX_CONFIG
74 = "classpath:scenarios/core/aop/includeAllButFooPrefix.xml";
75
76
77 private static final String INCLUDE_ALL_BUT_BAR_SUFFIX_CONFIG
78 = "classpath:scenarios/core/aop/includeAllButBarSuffix.xml";
79
80
81
82
83
84 private static final String
85 INCLUDE_ALL_STARTING_WITH_BEAN_BUT_SUFFIX_A_CONFIG
86 = "classpath:scenarios/core/aop/"
87 + "includeAllStartingWithBeanButSuffixA.xml";
88
89
90
91
92 @Test
93 public void testIncludesOnly() {
94 ApplicationContext appCtx = createAppContext(EMPTY_EXCLUSIVE_CONFIG);
95
96 for (int i = 0; i < BEAN_NAMES.length; i++) {
97 assertAdvised(appCtx, BEAN_NAMES[i]);
98 }
99 }
100
101
102
103
104 @Test
105 public void testExcludeAll() {
106 ApplicationContext appCtx = createAppContext(EXCLUDE_ALL_CONFIG);
107
108 for (int i = 0; i < BEAN_NAMES.length; i++) {
109 assertNotAdvised(appCtx, BEAN_NAMES[i]);
110 }
111 }
112
113
114
115
116
117 @Test
118 public void testIncludeAllButFoobar() {
119 ApplicationContext appCtx = createAppContext(
120 INCLUDE_ALL_BUT_FOOBAR_CONFIG);
121
122 assertAdvised(appCtx, BEAN_A);
123 assertAdvised(appCtx, BEAN_B);
124 assertAdvised(appCtx, FOO);
125 assertAdvised(appCtx, BAR);
126 assertNotAdvised(appCtx, FOOBAR);
127 }
128
129
130
131
132
133
134 @Test
135 public void testIncludeAllButFooPrefix() {
136 ApplicationContext appCtx = createAppContext(
137 INCLUDE_ALL_BUT_FOO_PREFIX_CONFIG);
138
139 assertAdvised(appCtx, BEAN_A);
140 assertAdvised(appCtx, BEAN_B);
141 assertNotAdvised(appCtx, FOO);
142 assertAdvised(appCtx, BAR);
143 assertNotAdvised(appCtx, FOOBAR);
144 }
145
146
147
148
149
150 @Test
151 public void testIncludeAllButBarSuffix() {
152 ApplicationContext appCtx = createAppContext(
153 INCLUDE_ALL_BUT_BAR_SUFFIX_CONFIG);
154
155 assertAdvised(appCtx, BEAN_A);
156 assertAdvised(appCtx, BEAN_B);
157 assertAdvised(appCtx, FOO);
158 assertNotAdvised(appCtx, BAR);
159 assertNotAdvised(appCtx, FOOBAR);
160 }
161
162
163
164
165
166 @Test
167 public void testIncludeAllStartingWithBeanButSuffixA() {
168 ApplicationContext appCtx = createAppContext(
169 INCLUDE_ALL_STARTING_WITH_BEAN_BUT_SUFFIX_A_CONFIG);
170
171 assertNotAdvised(appCtx, BEAN_A);
172 assertAdvised(appCtx, BEAN_B);
173 assertNotAdvised(appCtx, FOO);
174 assertNotAdvised(appCtx, BAR);
175 assertNotAdvised(appCtx, FOOBAR);
176 }
177
178
179
180
181
182
183
184
185
186
187 protected void assertAdvised(ApplicationContext appCtx, String beanName) {
188 Bean bean = (Bean) appCtx.getBean(beanName);
189 assertEquals("Bean '" + beanName + "' has not been advised.",
190 PROXIED, bean.getBeanName());
191 }
192
193
194
195
196
197
198
199
200
201
202 protected void assertNotAdvised(
203 ApplicationContext appCtx, String beanName) {
204 Bean bean = (Bean) appCtx.getBean(beanName);
205 assertEquals("Bean '" + beanName + "' has not been advised.",
206 beanName, bean.getBeanName());
207 }
208
209
210
211
212
213
214
215
216
217 private ApplicationContext createAppContext(String config) {
218 return new ModuleApplicationContext(config, false);
219 }
220 }