1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.userdetails.memory;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.userdetails.UsernameNotFoundException;
21 import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
22 import org.acegisecurity.userdetails.memory.UserMap;
23 import org.acegisecurity.userdetails.memory.UserMapEditor;
24
25 import java.util.Properties;
26
27
28
29
30
31
32
33
34 public class InMemoryDaoTests extends TestCase {
35
36
37 public InMemoryDaoTests() {
38 super();
39 }
40
41 public InMemoryDaoTests(String arg0) {
42 super(arg0);
43 }
44
45
46
47 public static void main(String[] args) {
48 junit.textui.TestRunner.run(InMemoryDaoTests.class);
49 }
50
51 private UserMap makeUserMap() {
52 UserMapEditor editor = new UserMapEditor();
53 editor.setAsText("marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
54
55 return (UserMap) editor.getValue();
56 }
57
58 public final void setUp() throws Exception {
59 super.setUp();
60 }
61
62 public void testLookupFails() throws Exception {
63 InMemoryDaoImpl dao = new InMemoryDaoImpl();
64 dao.setUserMap(makeUserMap());
65 dao.afterPropertiesSet();
66
67 try {
68 dao.loadUserByUsername("UNKNOWN_USER");
69 fail("Should have thrown UsernameNotFoundException");
70 } catch (UsernameNotFoundException expected) {
71 assertTrue(true);
72 }
73 }
74
75 public void testLookupSuccess() throws Exception {
76 InMemoryDaoImpl dao = new InMemoryDaoImpl();
77 dao.setUserMap(makeUserMap());
78 dao.afterPropertiesSet();
79 assertEquals("koala", dao.loadUserByUsername("marissa").getPassword());
80 assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
81 }
82
83 public void testLookupSuccessWithMixedCase() throws Exception {
84 InMemoryDaoImpl dao = new InMemoryDaoImpl();
85 dao.setUserMap(makeUserMap());
86 dao.afterPropertiesSet();
87 assertEquals("koala", dao.loadUserByUsername("MaRiSSA").getPassword());
88 assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
89 }
90
91 public void testStartupFailsIfUserMapNotSet() throws Exception {
92 InMemoryDaoImpl dao = new InMemoryDaoImpl();
93
94 try {
95 dao.afterPropertiesSet();
96 fail("Shoudl have thrown IllegalArgumentException");
97 } catch (IllegalArgumentException expected) {
98 assertTrue(true);
99 }
100 }
101
102 public void testStartupFailsIfUserMapSetToNull() throws Exception {
103 InMemoryDaoImpl dao = new InMemoryDaoImpl();
104 dao.setUserMap(null);
105
106 try {
107 dao.afterPropertiesSet();
108 fail("Shoudl have thrown IllegalArgumentException");
109 } catch (IllegalArgumentException expected) {
110 assertTrue(true);
111 }
112 }
113
114 public void testStartupSuccessIfUserMapSet() throws Exception {
115 InMemoryDaoImpl dao = new InMemoryDaoImpl();
116 dao.setUserMap(makeUserMap());
117 dao.afterPropertiesSet();
118 assertEquals(2, dao.getUserMap().getUserCount());
119 }
120
121 public void testUseOfExternalPropertiesObject() throws Exception {
122 InMemoryDaoImpl dao = new InMemoryDaoImpl();
123 Properties props = new Properties();
124 props.put("marissa", "koala,ROLE_ONE,ROLE_TWO,enabled");
125 props.put("scott", "wombat,ROLE_ONE,ROLE_TWO,enabled");
126 dao.setUserProperties(props);
127 assertEquals("koala", dao.loadUserByUsername("marissa").getPassword());
128 assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
129 }
130 }