1   package org.apache.turbine.services.security;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 // Now this is a Turbine Bug...
186 //
187 //         try
188 //         {
189 //             GroupSet gs = ss.getGroups(new org.apache.torque.util.Criteria());
190 //             assertEquals(3, gs.size());
191 
192 //             ss.renameGroup(fake, "Turbine");
193 
194 //             GroupSet gs2 = ss.getGroups(new org.apache.torque.util.Criteria());
195 //             assertEquals("Two groups with the same name exist!", 2, gs2.size());
196 
197 //             fail("Group could be renamed to existing Group and got lost from the database!");
198 //         }
199 //         catch (Exception e)
200 //         {
201 //             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), EntityExistsException.class);
202 //         }
203     }
204 }