01: /**
02: *
03: */package org.acegisecurity.config;
04:
05: import java.lang.reflect.Field;
06: import java.util.Map;
07:
08: import junit.framework.TestCase;
09:
10: import org.acegisecurity.AuthenticationManager;
11: import org.acegisecurity.ui.rememberme.RememberMeProcessingFilter;
12: import org.acegisecurity.ui.rememberme.RememberMeServices;
13: import org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices;
14: import org.acegisecurity.userdetails.UserDetailsService;
15: import org.springframework.beans.PropertyValue;
16: import org.springframework.beans.factory.config.BeanDefinition;
17: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
18: import org.springframework.beans.factory.support.RootBeanDefinition;
19: import org.springframework.context.ApplicationContext;
20: import org.springframework.context.support.ClassPathXmlApplicationContext;
21: import org.springframework.util.ReflectionUtils;
22:
23: /**
24: * @author Vishal Puri
25: *
26: */
27: public class RememberMeServicesBeanDefinitionParserTests extends
28: TestCase {
29:
30: public void testFilterConfiguration() {
31: ApplicationContext context = new ClassPathXmlApplicationContext(
32: "org/acegisecurity/config/remember-me-defaults.xml");
33: ConfigurableListableBeanFactory bf = (ConfigurableListableBeanFactory) context
34: .getAutowireCapableBeanFactory();
35: String[] names = bf
36: .getBeanNamesForType(RememberMeProcessingFilter.class);
37: assertEquals(1, names.length);
38: RootBeanDefinition definition = (RootBeanDefinition) bf
39: .getBeanDefinition(names[0]);
40: assertEquals(definition.getBeanClass(),
41: RememberMeProcessingFilter.class);
42: }
43:
44: public void testRuntimeAutoDetectionOfDependencies()
45: throws Exception {
46: ApplicationContext context = new ClassPathXmlApplicationContext(
47: "org/acegisecurity/config/remember-me-autodetect.xml");
48: ConfigurableListableBeanFactory factory = (ConfigurableListableBeanFactory) context
49: .getAutowireCapableBeanFactory();
50: Map map = factory
51: .getBeansOfType(RememberMeProcessingFilter.class);
52: RememberMeProcessingFilter filter = (RememberMeProcessingFilter) map
53: .values().iterator().next();
54: RememberMeServices services = filter.getRememberMeServices();
55: assertNotNull(services);
56: TokenBasedRememberMeServices rememberMeServices = (TokenBasedRememberMeServices) services;
57: UserDetailsService ud = rememberMeServices
58: .getUserDetailsService();
59: assertNotNull(ud);
60: Field field = filter.getClass().getDeclaredField(
61: "authenticationManager");
62: ReflectionUtils.makeAccessible(field);
63: Object obj = field.get(filter);
64: assertNotNull(
65: "authentication manager should not have been null", obj);
66: assertTrue(obj instanceof AuthenticationManager);
67: }
68:
69: }
|