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