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