|
24 | 24 |
|
25 | 25 | import org.easymock.classextension.EasyMock; |
26 | 26 | import org.junit.Test; |
| 27 | +import org.springframework.aop.scope.ScopedProxyUtils; |
27 | 28 | import org.springframework.web.context.WebApplicationContext; |
28 | 29 |
|
29 | 30 | @SuppressWarnings("unchecked") |
@@ -145,6 +146,74 @@ public void testSpringMultipleNamesForClass() throws Exception |
145 | 146 |
|
146 | 147 | } |
147 | 148 |
|
| 149 | + @Test |
| 150 | + public void testSpringScopedProxyNames() throws Exception |
| 151 | + { |
| 152 | + |
| 153 | + // WebApplicationContext that knows multiple names for our test class |
| 154 | + WebApplicationContext appContext = EasyMock.createMock(WebApplicationContext.class); |
| 155 | + String scopedProxyName = ScopedProxyUtils.getTargetBeanName("springManagedBean"); |
| 156 | + EasyMock.expect(appContext.getBeanNamesForType((Class) SpringManagedBean.class)).andReturn( |
| 157 | + new String[] { "springManagedBean", scopedProxyName }).once(); |
| 158 | + EasyMock.replay(appContext); |
| 159 | + |
| 160 | + // Simple mock of ServletContext containing the WebApplicationContext |
| 161 | + ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class); |
| 162 | + EasyMock.expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) |
| 163 | + .andReturn(appContext).once(); |
| 164 | + EasyMock.replay(servletContext); |
| 165 | + |
| 166 | + // ClassLoader that knows WebApplicationContext |
| 167 | + ClassLoader classLoader = EasyMock.createNiceMock(ClassLoader.class); |
| 168 | + EasyMock.expect(classLoader.loadClass(WebApplicationContext.class.getName())).andReturn( |
| 169 | + (Class) WebApplicationContext.class).once(); |
| 170 | + EasyMock.expect(classLoader.loadClass(ScopedProxyUtils.class.getName())).andReturn( |
| 171 | + (Class) ScopedProxyUtils.class).once(); |
| 172 | + EasyMock.replay(classLoader); |
| 173 | + |
| 174 | + // initialize resolver |
| 175 | + SpringBeanNameResolver resolver = new SpringBeanNameResolver(); |
| 176 | + boolean initCompleted = resolver.init(servletContext, classLoader); |
| 177 | + assertTrue(initCompleted); |
| 178 | + |
| 179 | + // Verify: Successful resolving |
| 180 | + assertEquals("springManagedBean", resolver.getBeanName(SpringManagedBean.class)); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testSpringScopedProxyNamesWithoutAOP() throws Exception |
| 186 | + { |
| 187 | + |
| 188 | + // WebApplicationContext that knows multiple names for our test class |
| 189 | + WebApplicationContext appContext = EasyMock.createMock(WebApplicationContext.class); |
| 190 | + String scopedProxyName = ScopedProxyUtils.getTargetBeanName("springManagedBean"); |
| 191 | + EasyMock.expect(appContext.getBeanNamesForType((Class) SpringManagedBean.class)).andReturn( |
| 192 | + new String[] { "springManagedBean", scopedProxyName }).once(); |
| 193 | + EasyMock.replay(appContext); |
| 194 | + |
| 195 | + // Simple mock of ServletContext containing the WebApplicationContext |
| 196 | + ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class); |
| 197 | + EasyMock.expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) |
| 198 | + .andReturn(appContext).once(); |
| 199 | + EasyMock.replay(servletContext); |
| 200 | + |
| 201 | + // ClassLoader that knows WebApplicationContext |
| 202 | + ClassLoader classLoader = EasyMock.createNiceMock(ClassLoader.class); |
| 203 | + EasyMock.expect(classLoader.loadClass(WebApplicationContext.class.getName())).andReturn( |
| 204 | + (Class) WebApplicationContext.class).once(); |
| 205 | + EasyMock.replay(classLoader); |
| 206 | + |
| 207 | + // initialize resolver |
| 208 | + SpringBeanNameResolver resolver = new SpringBeanNameResolver(); |
| 209 | + boolean initCompleted = resolver.init(servletContext, classLoader); |
| 210 | + assertTrue(initCompleted); |
| 211 | + |
| 212 | + // Verify: unknown because of multiple known names |
| 213 | + assertNull(resolver.getBeanName(SpringManagedBean.class)); |
| 214 | + |
| 215 | + } |
| 216 | + |
148 | 217 | /** |
149 | 218 | * Test bean |
150 | 219 | */ |
|
0 commit comments