001: package org.apache.turbine.services.security;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.turbine.om.security.Group;
026: import org.apache.turbine.test.BaseTurbineHsqlTest;
027: import org.apache.turbine.util.security.DataBackendException;
028: import org.apache.turbine.util.security.EntityExistsException;
029: import org.apache.turbine.util.security.GroupSet;
030: import org.apache.turbine.util.security.UnknownEntityException;
031:
032: public class TestSecurityGroup extends BaseTurbineHsqlTest {
033: public TestSecurityGroup(String name) throws Exception {
034: super (name, "conf/test/TurbineResources.properties");
035: }
036:
037: public static Test suite() {
038: return new TestSuite(TestSecurityGroup.class);
039: }
040:
041: public void testInit() {
042: SecurityService ss = TurbineSecurity.getService();
043: assertTrue("Service failed to initialize", ss.getInit());
044: }
045:
046: public void testGroupByName() throws Exception {
047: SecurityService ss = TurbineSecurity.getService();
048:
049: Group role = ss.getGroupByName("Turbine");
050: assertNotNull(role);
051: assertEquals("Turbine", role.getName());
052: }
053:
054: public void testGroupById() throws Exception {
055: SecurityService ss = TurbineSecurity.getService();
056:
057: Group role = ss.getGroupById(2);
058: assertNotNull(role);
059: assertEquals("Turbine", role.getName());
060: }
061:
062: public void testAllGroups() throws Exception {
063: SecurityService ss = TurbineSecurity.getService();
064:
065: GroupSet gs = ss.getAllGroups();
066:
067: assertEquals(2, gs.size());
068: }
069:
070: public void testAddGroup() throws Exception {
071: SecurityService ss = TurbineSecurity.getService();
072:
073: Group newbie = ss.getGroupInstance();
074: newbie.setName("newbie");
075:
076: ss.addGroup(newbie);
077:
078: assertEquals("Group was not added", 3, ss.getAllGroups().size());
079:
080: try {
081: Group turbine = ss.getGroupByName("Turbine");
082:
083: ss.addGroup(turbine);
084: fail("Existing Group could be added!");
085: } catch (Exception e) {
086: assertEquals("Wrong Exception thrown: "
087: + e.getClass().getName(),
088: EntityExistsException.class, e.getClass());
089: }
090:
091: try {
092: Group empty = ss.getGroupInstance();
093:
094: ss.addGroup(empty);
095: fail("Group with empty Groupname could be added!");
096: } catch (Exception e) {
097: assertEquals("Wrong Exception thrown: "
098: + e.getClass().getName(),
099: DataBackendException.class, e.getClass());
100: }
101:
102: assertEquals("Group was not added", 3, ss.getAllGroups().size());
103: }
104:
105: public void testRemoveGroup() throws Exception {
106: SecurityService ss = TurbineSecurity.getService();
107:
108: assertEquals("Group was not added", 3, ss.getAllGroups().size());
109:
110: Group newbie = ss.getGroupByName("newbie");
111: assertNotNull(newbie);
112:
113: ss.removeGroup(newbie);
114:
115: try {
116: Group foo = ss.getGroupInstance();
117: foo.setName("foo");
118:
119: ss.removeGroup(foo);
120: fail("Non Existing Group could be deleted!");
121: } catch (Exception e) {
122: assertEquals("Wrong Exception thrown: "
123: + e.getClass().getName(), e.getClass(),
124: UnknownEntityException.class);
125: }
126:
127: assertEquals("Group was not removed", 2, ss.getAllGroups()
128: .size());
129: }
130:
131: public void testSaveGroup() throws Exception {
132: SecurityService ss = TurbineSecurity.getService();
133:
134: Group turbine = ss.getGroupByName("Turbine");
135:
136: ss.saveGroup(turbine);
137:
138: try {
139: Group fake = ss.getGroupInstance("fake");
140:
141: ss.saveGroup(fake);
142: fail("Non Existing Group could be saved!");
143: } catch (Exception e) {
144: assertEquals("Wrong Exception thrown: "
145: + e.getClass().getName(), e.getClass(),
146: UnknownEntityException.class);
147: }
148: }
149:
150: public void testRenameGroup() throws Exception {
151: SecurityService ss = TurbineSecurity.getService();
152:
153: Group newbie = ss.getGroupInstance("newbie");
154: ss.addGroup(newbie);
155:
156: Group test = ss.getGroupByName("newbie");
157: assertNotNull(test);
158:
159: ss.renameGroup(test, "fake");
160:
161: Group fake = ss.getGroupByName("fake");
162: assertNotNull(fake);
163:
164: //
165: // Now this is a Turbine Bug...
166: //
167: // try
168: // {
169: // GroupSet gs = ss.getGroups(new org.apache.torque.util.Criteria());
170: // assertEquals(3, gs.size());
171:
172: // ss.renameGroup(fake, "Turbine");
173:
174: // GroupSet gs2 = ss.getGroups(new org.apache.torque.util.Criteria());
175: // assertEquals("Two groups with the same name exist!", 2, gs2.size());
176:
177: // fail("Group could be renamed to existing Group and got lost from the database!");
178: // }
179: // catch (Exception e)
180: // {
181: // assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), EntityExistsException.class);
182: // }
183: }
184: }
|