001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.security.Principal;
025: import java.security.acl.Group;
026: import java.util.HashSet;
027: import java.util.Enumeration;
028: import junit.framework.*;
029:
030: import org.jboss.security.AnybodyPrincipal;
031: import org.jboss.security.NobodyPrincipal;
032: import org.jboss.security.NestableGroup;
033: import org.jboss.security.SimpleGroup;
034: import org.jboss.security.SimplePrincipal;
035:
036: /** Tests of the NestableGroup class.
037:
038: @see org.jboss.security.NestableGroup
039:
040: @author Scott.Stark@jboss.org
041: @version $Revision: 37390 $
042: */
043: public class NestableGroupTestCase extends TestCase {
044: static Group[] groups = { new SimpleGroup("roles1"),
045: new SimpleGroup("roles2"), new SimpleGroup("roles3"),
046: new SimpleGroup("roles4") };
047: static {
048: for (int g = 0; g < groups.length; g++) {
049: for (int m = 0; m < 4; m++)
050: groups[g].addMember(new SimplePrincipal("user." + g
051: + '.' + m));
052: }
053: }
054: static NestableGroup group = new NestableGroup("Roles");
055:
056: public NestableGroupTestCase(String testName) {
057: super (testName);
058: }
059:
060: public static Test suite() {
061: TestSuite suite = new TestSuite();
062: suite.addTest(new NestableGroupTestCase("testGetName"));
063: suite.addTest(new NestableGroupTestCase("testEquals"));
064: suite.addTest(new NestableGroupTestCase("testAddMember"));
065: suite.addTest(new NestableGroupTestCase("testRemoveMember"));
066: suite.addTest(new NestableGroupTestCase("testAnybody"));
067: suite.addTest(new NestableGroupTestCase("testNobody"));
068:
069: return suite;
070: }
071:
072: public void testGetName() {
073: System.out.println("testGetName");
074: assertTrue(group.getName().equals("Roles"));
075: }
076:
077: public void testEquals() {
078: System.out.println("testEquals");
079: SimpleGroup CallerPrincipal = new SimpleGroup("Roles");
080: assertTrue(group.equals(CallerPrincipal));
081: }
082:
083: /** Test of removeMember method, of class org.jboss.security.NestableGroup. */
084: public void testRemoveMember() {
085: System.out.println("testRemoveMember");
086: for (int g = groups.length - 1; g >= 0; g--) {
087: testMembers(g);
088: assertTrue("Remove " + groups[g], group
089: .removeMember(groups[g]));
090: }
091: }
092:
093: /** Test of addMember method, of class org.jboss.security.NestableGroup. */
094: public void testAddMember() {
095: System.out.println("testAddMember");
096: for (int g = 0; g < groups.length; g++) {
097: Group grp = groups[g];
098: group.addMember(grp);
099: testMembers(g);
100: }
101:
102: try {
103: group.addMember(new SimplePrincipal("BadGroup"));
104: fail("Was able to add a Principal to NestableGroup");
105: } catch (IllegalArgumentException e) {
106: }
107: }
108:
109: public void testAnybody() {
110: System.out.println("testAnybody");
111: group.addMember(groups[0]);
112: boolean isMember = group
113: .isMember(AnybodyPrincipal.ANYBODY_PRINCIPAL);
114: assertTrue("AnybodyPrincipal.isMember", isMember);
115: }
116:
117: public void testNobody() {
118: System.out.println("testNobody");
119: SimpleGroup nobodyGroup = new SimpleGroup("<NOBODY>");
120: SimplePrincipal nobody = new SimplePrincipal("<NOBODY>");
121: nobodyGroup.addMember(nobody);
122: group.addMember(nobodyGroup);
123: boolean isMember = group
124: .isMember(NobodyPrincipal.NOBODY_PRINCIPAL);
125: assertTrue("NobodyPrincipal.isMember == false",
126: isMember == false);
127: }
128:
129: /** Test of members method, of class org.jboss.security.NestableGroup. */
130: private void testMembers(int grpNo) {
131: String user = "user." + grpNo + '.';
132: HashSet memberSet = new HashSet();
133: for (int m = 0; m < 4; m++) {
134: Principal p = new SimplePrincipal(user + m);
135: assertTrue("Is member1, " + p, group.isMember(p));
136: memberSet.add(p);
137: }
138:
139: Enumeration members = group.members();
140: while (members.hasMoreElements()) {
141: Principal member = (Principal) members.nextElement();
142: assertTrue("Is member2: " + member, memberSet
143: .contains(member));
144: }
145: }
146:
147: public static void main(java.lang.String[] args) {
148: junit.textui.TestRunner.run(suite());
149: }
150:
151: }
|