1 7 8 package org.jboss.test; 9 10 import java.security.Principal ; 11 import java.security.acl.Group ; 12 import java.util.HashSet ; 13 import java.util.Enumeration ; 14 import junit.framework.*; 15 16 import org.jboss.security.AnybodyPrincipal; 17 import org.jboss.security.NobodyPrincipal; 18 import org.jboss.security.NestableGroup; 19 import org.jboss.security.SimpleGroup; 20 import org.jboss.security.SimplePrincipal; 21 22 29 public class NestableGroupTestCase extends TestCase 30 { 31 static Group [] groups = { 32 new SimpleGroup("roles1"), 33 new SimpleGroup("roles2"), 34 new SimpleGroup("roles3"), 35 new SimpleGroup("roles4") 36 }; 37 static 38 { 39 for(int g = 0; g < groups.length; g ++) 40 { 41 for(int m = 0; m < 4; m ++) 42 groups[g].addMember(new SimplePrincipal("user."+g+'.'+m)); 43 } 44 } 45 static NestableGroup group = new NestableGroup("Roles"); 46 47 public NestableGroupTestCase(String testName) 48 { 49 super(testName); 50 } 51 52 public static Test suite() 53 { 54 TestSuite suite = new TestSuite(); 55 suite.addTest(new NestableGroupTestCase("testGetName")); 56 suite.addTest(new NestableGroupTestCase("testEquals")); 57 suite.addTest(new NestableGroupTestCase("testAddMember")); 58 suite.addTest(new NestableGroupTestCase("testRemoveMember")); 59 suite.addTest(new NestableGroupTestCase("testAnybody")); 60 suite.addTest(new NestableGroupTestCase("testNobody")); 61 62 return suite; 63 } 64 65 public void testGetName() 66 { 67 System.out.println("testGetName"); 68 assertTrue(group.getName().equals("Roles")); 69 } 70 71 public void testEquals() 72 { 73 System.out.println("testEquals"); 74 SimpleGroup CallerPrincipal = new SimpleGroup("Roles"); 75 assertTrue(group.equals(CallerPrincipal)); 76 } 77 78 79 public void testRemoveMember() 80 { 81 System.out.println("testRemoveMember"); 82 for(int g = groups.length -1; g >= 0; g --) 83 { 84 testMembers(g); 85 assertTrue("Remove "+groups[g], group.removeMember(groups[g])); 86 } 87 } 88 89 90 public void testAddMember() 91 { 92 System.out.println("testAddMember"); 93 for(int g = 0; g < groups.length; g ++) 94 { 95 Group grp = groups[g]; 96 group.addMember(grp); 97 testMembers(g); 98 } 99 100 try 101 { 102 group.addMember(new SimplePrincipal("BadGroup")); 103 fail("Was able to add a Principal to NestableGroup"); 104 } 105 catch(IllegalArgumentException e) 106 { 107 } 108 } 109 110 public void testAnybody() 111 { 112 System.out.println("testAnybody"); 113 group.addMember(groups[0]); 114 boolean isMember = group.isMember(AnybodyPrincipal.ANYBODY_PRINCIPAL); 115 assertTrue("AnybodyPrincipal.isMember", isMember); 116 } 117 118 public void testNobody() 119 { 120 System.out.println("testNobody"); 121 SimpleGroup nobodyGroup = new SimpleGroup("<NOBODY>"); 122 SimplePrincipal nobody = new SimplePrincipal("<NOBODY>"); 123 nobodyGroup.addMember(nobody); 124 group.addMember(nobodyGroup); 125 boolean isMember = group.isMember(NobodyPrincipal.NOBODY_PRINCIPAL); 126 assertTrue("NobodyPrincipal.isMember == false", isMember == false); 127 } 128 129 130 private void testMembers(int grpNo) 131 { 132 String user = "user."+grpNo+'.'; 133 HashSet memberSet = new HashSet (); 134 for(int m = 0; m < 4; m ++) 135 { 136 Principal p = new SimplePrincipal(user+m); 137 assertTrue("Is member1, "+p, group.isMember(p)); 138 memberSet.add(p); 139 } 140 141 Enumeration members = group.members(); 142 while( members.hasMoreElements() ) 143 { 144 Principal member = (Principal ) members.nextElement(); 145 assertTrue("Is member2: "+member, memberSet.contains(member)); 146 } 147 } 148 149 public static void main(java.lang.String [] args) 150 { 151 junit.textui.TestRunner.run(suite()); 152 } 153 154 } 155 | Popular Tags |