KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
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 /** Tests of the NestablePrincipal class.
22
23 @see org.jboss.security.NestablePrincipal
24
25 @author Scott.Stark@jboss.org
26 @version $Revision: 1.3.6.1 $
27 */

28 public class NestablePrincipalTestCase extends TestCase
29 {
30     static Principal JavaDoc[] 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 JavaDoc 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     /** Test of removeMember method, of class org.jboss.security.NestablePrincipal. */
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     /** Test of addMember method, of class org.jboss.security.NestablePrincipal. */
81     public void testAddMember()
82     {
83         System.out.println("testAddMember");
84         
85         for(int p = 0; p < principals.length; p ++)
86         {
87             Principal JavaDoc 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     /** Test of members method, of class org.jboss.security.NestablePrincipal. */
110     private void testMembers()
111     {
112         Enumeration JavaDoc members = principal.members();
113         while( members.hasMoreElements() )
114         {
115             Principal JavaDoc user = (Principal JavaDoc) members.nextElement();
116             assertTrue("Members "+user, principal.isMember(user));
117         }
118     }
119
120     public static void main(java.lang.String JavaDoc[] args)
121     {
122         junit.textui.TestRunner.run(suite());
123     }
124
125 }
126
Popular Tags