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.Enumeration;
027: import junit.framework.*;
028:
029: import org.jboss.security.AnybodyPrincipal;
030: import org.jboss.security.NestablePrincipal;
031: import org.jboss.security.NobodyPrincipal;
032: import org.jboss.security.SimpleGroup;
033: import org.jboss.security.SimplePrincipal;
034:
035: /** Tests of the NestablePrincipal class.
036:
037: @see org.jboss.security.NestablePrincipal
038:
039: @author Scott.Stark@jboss.org
040: @version $Revision: 37390 $
041: */
042: public class NestablePrincipalTestCase extends TestCase {
043: static Principal[] principals = { new SimplePrincipal("user1"),
044: new SimplePrincipal("user2"), new SimplePrincipal("user2"),
045: new SimplePrincipal("user3") };
046: static NestablePrincipal principal = new NestablePrincipal(
047: "CallerPrincipal");
048:
049: public NestablePrincipalTestCase(String testName) {
050: super (testName);
051: }
052:
053: public static Test suite() {
054: TestSuite suite = new TestSuite();
055: suite.addTest(new NestablePrincipalTestCase("testGetName"));
056: suite.addTest(new NestablePrincipalTestCase("testEquals"));
057: suite.addTest(new NestablePrincipalTestCase("testAddMember"));
058: suite
059: .addTest(new NestablePrincipalTestCase(
060: "testRemoveMember"));
061: suite.addTest(new NestablePrincipalTestCase("testAnybody"));
062: suite.addTest(new NestablePrincipalTestCase("testNobody"));
063:
064: return suite;
065: }
066:
067: public void testGetName() {
068: System.out.println("testGetName");
069: assertTrue(principal.getName().equals("CallerPrincipal"));
070: }
071:
072: public void testEquals() {
073: System.out.println("testEquals");
074: SimpleGroup CallerPrincipal = new SimpleGroup("CallerPrincipal");
075: assertTrue(principal.equals(CallerPrincipal));
076: }
077:
078: /** Test of removeMember method, of class org.jboss.security.NestablePrincipal. */
079: public void testRemoveMember() {
080: System.out.println("testRemoveMember");
081: for (int p = principals.length - 1; p >= 0; p--) {
082: assertTrue("Remove " + principals[p], principal
083: .removeMember(principals[p]));
084: testMembers();
085: }
086: }
087:
088: /** Test of addMember method, of class org.jboss.security.NestablePrincipal. */
089: public void testAddMember() {
090: System.out.println("testAddMember");
091:
092: for (int p = 0; p < principals.length; p++) {
093: Principal user = principals[p];
094: principal.addMember(user);
095: assertTrue("AddMember " + user, principal.isMember(user));
096: testMembers();
097: }
098: }
099:
100: public void testAnybody() {
101: System.out.println("testAnybody");
102: principal.addMember(principals[0]);
103: assertTrue("AnybodyPrincipal.isMember", principal
104: .isMember(AnybodyPrincipal.ANYBODY_PRINCIPAL));
105: }
106:
107: public void testNobody() {
108: System.out.println("testNobody");
109: SimplePrincipal nobody = new SimplePrincipal("<NOBODY>");
110: principal.addMember(nobody);
111: assertTrue("AnybodyPrincipal.isMember", principal
112: .isMember(NobodyPrincipal.NOBODY_PRINCIPAL) == false);
113: }
114:
115: /** Test of members method, of class org.jboss.security.NestablePrincipal. */
116: private void testMembers() {
117: Enumeration members = principal.members();
118: while (members.hasMoreElements()) {
119: Principal user = (Principal) members.nextElement();
120: assertTrue("Members " + user, principal.isMember(user));
121: }
122: }
123:
124: public static void main(java.lang.String[] args) {
125: junit.textui.TestRunner.run(suite());
126: }
127:
128: }
|