01: package org.acegisecurity.config;
02:
03: import junit.framework.TestCase;
04:
05: import org.acegisecurity.GrantedAuthority;
06: import org.acegisecurity.GrantedAuthorityImpl;
07: import org.acegisecurity.userdetails.User;
08: import org.acegisecurity.userdetails.UserDetailsService;
09: import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
10: import org.acegisecurity.userdetails.memory.UserMap;
11: import org.springframework.beans.PropertyValue;
12: import org.springframework.beans.factory.config.BeanDefinition;
13: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
14: import org.springframework.beans.factory.support.RootBeanDefinition;
15: import org.springframework.context.ApplicationContext;
16: import org.springframework.context.support.ClassPathXmlApplicationContext;
17:
18: /**
19: * @author Vishal Puri
20: *
21: */
22: public class PrincipalRepositoryNamespaceTests extends TestCase {
23:
24: public void testParserWithUserDefinition() {
25: ApplicationContext context = new ClassPathXmlApplicationContext(
26: "org/acegisecurity/config/principal-repository-user-map.xml");
27:
28: ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) context
29: .getAutowireCapableBeanFactory();
30:
31: String[] names = clbf
32: .getBeanNamesForType(UserDetailsService.class);
33: assertEquals(1, names.length);
34:
35: RootBeanDefinition definition = (RootBeanDefinition) clbf
36: .getBeanDefinition(names[0]);
37: assertEquals(InMemoryDaoImpl.class, definition.getBeanClass());
38:
39: UserMap map = new UserMap();
40:
41: GrantedAuthority[] authotities = {
42: new GrantedAuthorityImpl("ROLE_YO"),
43: new GrantedAuthorityImpl("ROLE_YOYO") };
44:
45: User user = new User("vishal", "nottellingya", true, true,
46: true, true, authotities);
47:
48: map.addUser(user);
49:
50: assertPropertyValues(map, definition, "userMap");
51:
52: }
53:
54: private void assertPropertyValues(UserMap assertionValue,
55: RootBeanDefinition definition, String property) {
56: PropertyValue propertyValue = definition.getPropertyValues()
57: .getPropertyValue(property);
58: assertNotNull(propertyValue);
59: assertTrue(propertyValue.getValue() instanceof UserMap);
60: UserMap users = (UserMap) propertyValue.getValue();
61: assertTrue(assertionValue.getUserCount() == users
62: .getUserCount());
63: assertEquals(assertionValue.getUser("vishal"), users
64: .getUser("vishal"));
65: assertTrue(users.getUser("vishal").isEnabled());
66: assertTrue(users.getUser("vishal").isAccountNonExpired());
67: assertTrue(users.getUser("vishal").isAccountNonLocked());
68: assertTrue(users.getUser("vishal").isCredentialsNonExpired());
69: assertEquals(2, users.getUser("vishal").getAuthorities().length);
70: assertEquals(new GrantedAuthorityImpl("ROLE_YO"), users
71: .getUser("vishal").getAuthorities()[0]);
72: assertEquals(new GrantedAuthorityImpl("ROLE_YOYO"), users
73: .getUser("vishal").getAuthorities()[1]);
74: }
75:
76: }
|