1 17 package org.apache.geronimo.security; 18 19 import java.util.Set ; 20 import java.security.Principal ; 21 import javax.security.auth.Subject ; 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 30 public class ContextManagerTest extends TestCase { 31 32 public void testGetCallerPrincipal() throws Exception { 33 Subject subject = new Subject (); 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 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 principal = ContextManager.getCurrentPrincipal(subject); 45 assertSame("Expected GeronimoCallerPrincipal", userPrincipal, principal); 46 } 47 48 private final Subject s1 = new Subject (); 49 private final Subject s2 = new Subject (); 50 private final Subject s3 = new Subject (); 51 52 public void testPushNextCallerWithSubjectPresent() throws Exception { 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 |