1 7 8 package org.jboss.test; 9 10 import java.security.Principal ; 11 import java.security.acl.Group ; 12 import java.util.Enumeration ; 13 import junit.framework.*; 14 15 import org.jboss.security.AnybodyPrincipal; 16 import org.jboss.security.NestablePrincipal; 17 import org.jboss.security.NobodyPrincipal; 18 import org.jboss.security.SimpleGroup; 19 import org.jboss.security.SimplePrincipal; 20 21 28 public class NestablePrincipalTestCase extends TestCase 29 { 30 static Principal [] principals = { 31 new SimplePrincipal("user1"), 32 new SimplePrincipal("user2"), 33 new SimplePrincipal("user2"), 34 new SimplePrincipal("user3") 35 }; 36 static NestablePrincipal principal = new NestablePrincipal("CallerPrincipal"); 37 38 public NestablePrincipalTestCase(String testName) 39 { 40 super(testName); 41 } 42 43 public static Test suite() 44 { 45 TestSuite suite = new TestSuite(); 46 suite.addTest(new NestablePrincipalTestCase("testGetName")); 47 suite.addTest(new NestablePrincipalTestCase("testEquals")); 48 suite.addTest(new NestablePrincipalTestCase("testAddMember")); 49 suite.addTest(new NestablePrincipalTestCase("testRemoveMember")); 50 suite.addTest(new NestablePrincipalTestCase("testAnybody")); 51 suite.addTest(new NestablePrincipalTestCase("testNobody")); 52 53 return suite; 54 } 55 56 public void testGetName() 57 { 58 System.out.println("testGetName"); 59 assertTrue(principal.getName().equals("CallerPrincipal")); 60 } 61 62 public void testEquals() 63 { 64 System.out.println("testEquals"); 65 SimpleGroup CallerPrincipal = new SimpleGroup("CallerPrincipal"); 66 assertTrue(principal.equals(CallerPrincipal)); 67 } 68 69 70 public void testRemoveMember() 71 { 72 System.out.println("testRemoveMember"); 73 for(int p = principals.length -1; p >= 0; p --) 74 { 75 assertTrue("Remove "+principals[p], principal.removeMember(principals[p])); 76 testMembers(); 77 } 78 } 79 80 81 public void testAddMember() 82 { 83 System.out.println("testAddMember"); 84 85 for(int p = 0; p < principals.length; p ++) 86 { 87 Principal user = principals[p]; 88 principal.addMember(user); 89 assertTrue("AddMember "+user, principal.isMember(user)); 90 testMembers(); 91 } 92 } 93 94 public void testAnybody() 95 { 96 System.out.println("testAnybody"); 97 principal.addMember(principals[0]); 98 assertTrue("AnybodyPrincipal.isMember", principal.isMember(AnybodyPrincipal.ANYBODY_PRINCIPAL)); 99 } 100 101 public void testNobody() 102 { 103 System.out.println("testNobody"); 104 SimplePrincipal nobody = new SimplePrincipal("<NOBODY>"); 105 principal.addMember(nobody); 106 assertTrue("AnybodyPrincipal.isMember", principal.isMember(NobodyPrincipal.NOBODY_PRINCIPAL) == false); 107 } 108 109 110 private void testMembers() 111 { 112 Enumeration members = principal.members(); 113 while( members.hasMoreElements() ) 114 { 115 Principal user = (Principal ) members.nextElement(); 116 assertTrue("Members "+user, principal.isMember(user)); 117 } 118 } 119 120 public static void main(java.lang.String [] args) 121 { 122 junit.textui.TestRunner.run(suite()); 123 } 124 125 } 126 | Popular Tags |