KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > NestableGroupTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7
8 package org.jboss.test;
9
10 import java.security.Principal JavaDoc;
11 import java.security.acl.Group JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Enumeration JavaDoc;
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 /** Tests of the NestableGroup class.
23
24 @see org.jboss.security.NestableGroup
25
26 @author Scott.Stark@jboss.org
27 @version $Revision: 1.3.6.1 $
28 */

29 public class NestableGroupTestCase extends TestCase
30 {
31     static Group JavaDoc[] 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 JavaDoc 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     /** Test of removeMember method, of class org.jboss.security.NestableGroup. */
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     /** Test of addMember method, of class org.jboss.security.NestableGroup. */
90     public void testAddMember()
91     {
92         System.out.println("testAddMember");
93         for(int g = 0; g < groups.length; g ++)
94         {
95             Group JavaDoc 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 JavaDoc 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     /** Test of members method, of class org.jboss.security.NestableGroup. */
130     private void testMembers(int grpNo)
131     {
132         String JavaDoc user = "user."+grpNo+'.';
133         HashSet JavaDoc memberSet = new HashSet JavaDoc();
134         for(int m = 0; m < 4; m ++)
135         {
136             Principal JavaDoc p = new SimplePrincipal(user+m);
137             assertTrue("Is member1, "+p, group.isMember(p));
138             memberSet.add(p);
139         }
140         
141         Enumeration JavaDoc members = group.members();
142         while( members.hasMoreElements() )
143         {
144             Principal JavaDoc member = (Principal JavaDoc) members.nextElement();
145             assertTrue("Is member2: "+member, memberSet.contains(member));
146         }
147     }
148
149     public static void main(java.lang.String JavaDoc[] args)
150     {
151         junit.textui.TestRunner.run(suite());
152     }
153
154 }
155
Popular Tags