KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > ContextManagerTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.security;
18
19 import java.util.Set JavaDoc;
20 import java.security.Principal JavaDoc;
21 import javax.security.auth.Subject JavaDoc;
22
23 import junit.framework.TestCase;
24 import org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal;
25 import org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal;
26
27 /**
28  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  */

30 public class ContextManagerTest extends TestCase {
31
32     public void testGetCallerPrincipal() throws Exception JavaDoc {
33         Subject JavaDoc subject = new Subject JavaDoc();
34         GeronimoUserPrincipal userPrincipal = new GeronimoUserPrincipal("foo");
35         RealmPrincipal realmPrincipal = new RealmPrincipal("realm", "domain", userPrincipal);
36         PrimaryRealmPrincipal primaryRealmPrincipal = new PrimaryRealmPrincipal("realm", "domain", userPrincipal);
37         GeronimoGroupPrincipal groupPrincipal = new GeronimoGroupPrincipal("bar");
38         Set JavaDoc principals = subject.getPrincipals();
39         principals.add(userPrincipal);
40         principals.add(realmPrincipal);
41         principals.add(primaryRealmPrincipal);
42         principals.add(groupPrincipal);
43         ContextManager.registerSubject(subject);
44         Principal JavaDoc principal = ContextManager.getCurrentPrincipal(subject);
45         assertSame("Expected GeronimoCallerPrincipal", userPrincipal, principal);
46     }
47
48     private final Subject JavaDoc s1 = new Subject JavaDoc();
49     private final Subject JavaDoc s2 = new Subject JavaDoc();
50     private final Subject JavaDoc s3 = new Subject JavaDoc();
51
52     public void testPushNextCallerWithSubjectPresent() throws Exception JavaDoc {
53         try {
54             ContextManager.setCallers(s1, s1);
55             Callers c1 = ContextManager.pushNextCaller(s2);
56             assertSame("Callers should have s1 in current position", s1, c1.getCurrentCaller());
57             assertSame("Callers should have s1 in next position", s1, c1.getNextCaller());
58             assertSame("CurrentCaller should be s1", s1, ContextManager.getCurrentCaller());
59             Callers c2 = ContextManager.pushNextCaller(s3);
60             assertSame("Callers should have s1 in current position", s1, c2.getCurrentCaller());
61             assertSame("Callers should have s2 in next position", s2, c2.getNextCaller());
62             assertSame("CurrentCaller should be s2", s2, ContextManager.getCurrentCaller());
63             Callers c3 = ContextManager.pushNextCaller(null);
64             assertSame("Callers should have s2 in current position", s2, c3.getCurrentCaller());
65             assertSame("Callers should have s3 in next position", s3, c3.getNextCaller());
66             assertSame("CurrentCaller should be s3", s3, ContextManager.getCurrentCaller());
67             Callers c4 = ContextManager.pushNextCaller(null);
68             assertSame("Callers should have s3 in current position", s3, c4.getCurrentCaller());
69             assertSame("Callers should have s3 in next position", s3, c4.getNextCaller());
70             assertSame("CurrentCaller should be s3", s3, ContextManager.getCurrentCaller());
71             ContextManager.popCallers(c4);
72             assertSame("CurrentCaller should be s3", s3, ContextManager.getCurrentCaller());
73             ContextManager.popCallers(c3);
74             assertSame("CurrentCaller should be s2", s2, ContextManager.getCurrentCaller());
75             ContextManager.popCallers(c2);
76             assertSame("CurrentCaller should be s1", s1, ContextManager.getCurrentCaller());
77             ContextManager.popCallers(c1);
78             assertSame("CurrentCaller should be s1", s1, ContextManager.getCurrentCaller());
79         } finally {
80             ContextManager.clearCallers();
81         }
82     }
83 }
84
Popular Tags