1 package org.apache.turbine.services.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 import org.apache.turbine.om.security.Group;
23 import org.apache.turbine.test.BaseTurbineHsqlTest;
24 import org.apache.turbine.util.security.DataBackendException;
25 import org.apache.turbine.util.security.EntityExistsException;
26 import org.apache.turbine.util.security.GroupSet;
27 import org.apache.turbine.util.security.UnknownEntityException;
28
29 public class TestSecurityGroup
30 extends BaseTurbineHsqlTest
31 {
32 public TestSecurityGroup(String name)
33 throws Exception
34 {
35 super(name, "conf/test/TurbineResources.properties");
36 }
37
38 public static Test suite()
39 {
40 return new TestSuite(TestSecurityGroup.class);
41 }
42
43 public void testInit()
44 {
45 SecurityService ss = TurbineSecurity.getService();
46 assertTrue("Service failed to initialize", ss.getInit());
47 }
48
49 public void testGroupByName()
50 throws Exception
51 {
52 SecurityService ss = TurbineSecurity.getService();
53
54 Group role = ss.getGroupByName("Turbine");
55 assertNotNull(role);
56 assertEquals("Turbine", role.getName());
57 }
58
59 public void testGroupById()
60 throws Exception
61 {
62 SecurityService ss = TurbineSecurity.getService();
63
64 Group role = ss.getGroupById(2);
65 assertNotNull(role);
66 assertEquals("Turbine", role.getName());
67 }
68
69 public void testAllGroups()
70 throws Exception
71 {
72 SecurityService ss = TurbineSecurity.getService();
73
74 GroupSet gs = ss.getAllGroups();
75
76 assertEquals(2, gs.size());
77 }
78
79 public void testAddGroup()
80 throws Exception
81 {
82 SecurityService ss = TurbineSecurity.getService();
83
84 Group newbie = ss.getGroupInstance();
85 newbie.setName("newbie");
86
87 ss.addGroup(newbie);
88
89 assertEquals("Group was not added", 3, ss.getAllGroups().size());
90
91 try
92 {
93 Group turbine = ss.getGroupByName("Turbine");
94
95 ss.addGroup(turbine);
96 fail("Existing Group could be added!");
97 }
98 catch (Exception e)
99 {
100 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), EntityExistsException.class, e.getClass());
101 }
102
103 try
104 {
105 Group empty = ss.getGroupInstance();
106
107 ss.addGroup(empty);
108 fail("Group with empty Groupname could be added!");
109 }
110 catch (Exception e)
111 {
112 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), DataBackendException.class, e.getClass());
113 }
114
115 assertEquals("Group was not added", 3, ss.getAllGroups().size());
116 }
117
118 public void testRemoveGroup()
119 throws Exception
120 {
121 SecurityService ss = TurbineSecurity.getService();
122
123 assertEquals("Group was not added", 3, ss.getAllGroups().size());
124
125 Group newbie = ss.getGroupByName("newbie");
126 assertNotNull(newbie);
127
128 ss.removeGroup(newbie);
129
130 try
131 {
132 Group foo = ss.getGroupInstance();
133 foo.setName("foo");
134
135 ss.removeGroup(foo);
136 fail("Non Existing Group could be deleted!");
137 }
138 catch (Exception e)
139 {
140 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), UnknownEntityException.class);
141 }
142
143 assertEquals("Group was not removed", 2, ss.getAllGroups().size());
144 }
145
146 public void testSaveGroup()
147 throws Exception
148 {
149 SecurityService ss = TurbineSecurity.getService();
150
151 Group turbine = ss.getGroupByName("Turbine");
152
153 ss.saveGroup(turbine);
154
155 try
156 {
157 Group fake = ss.getGroupInstance("fake");
158
159 ss.saveGroup(fake);
160 fail("Non Existing Group could be saved!");
161 }
162 catch (Exception e)
163 {
164 assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), UnknownEntityException.class);
165 }
166 }
167
168 public void testRenameGroup()
169 throws Exception
170 {
171 SecurityService ss = TurbineSecurity.getService();
172
173 Group newbie = ss.getGroupInstance("newbie");
174 ss.addGroup(newbie);
175
176 Group test = ss.getGroupByName("newbie");
177 assertNotNull(test);
178
179 ss.renameGroup(test, "fake");
180
181 Group fake = ss.getGroupByName("fake");
182 assertNotNull(fake);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 }
204 }