}
public void testUsersRoles() throws Exception
{
log.info("testUsersRoles");
UsernamePasswordHandler handler = new UsernamePasswordHandler("scott", "echoman".toCharArray());
LoginContext lc = new LoginContext("testUsersRoles", handler);
lc.login();
Subject subject = lc.getSubject();
Set groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains scott", subject.getPrincipals().contains(new SimplePrincipal("scott")));
assertTrue("Principals contains Roles", groups.contains(new SimplePrincipal("Roles")));
assertTrue("Principals contains CallerPrincipal", groups.contains(new SimplePrincipal("CallerPrincipal")));
Group roles = (Group) groups.iterator().next();
Iterator groupsIter = groups.iterator();
while( groupsIter.hasNext() )
{
roles = (Group) groupsIter.next();
if( roles.getName().equals("Roles") )
{
assertTrue("Echo is a role", roles.isMember(new SimplePrincipal("Echo")));
assertTrue("Java is NOT a role", roles.isMember(new SimplePrincipal("Java")) == false);
assertTrue("Coder is NOT a role", roles.isMember(new SimplePrincipal("Coder")) == false);
}
else if( roles.getName().equals("CallerPrincipal") )
{
log.info("CallerPrincipal is "+roles.members().nextElement());
boolean isMember = roles.isMember(new SimplePrincipal("callerScott"));
assertTrue("CallerPrincipal is callerScott", isMember);
}
}
lc.logout();
handler = new UsernamePasswordHandler("stark", "javaman".toCharArray());
lc = new LoginContext("testUsersRoles", handler);
lc.login();
subject = lc.getSubject();
groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains stark", subject.getPrincipals().contains(new SimplePrincipal("stark")));
assertTrue("Principals contains Roles", groups.contains(new SimplePrincipal("Roles")));
assertTrue("Principals contains CallerPrincipal", groups.contains(new SimplePrincipal("CallerPrincipal")));
groupsIter = groups.iterator();
while( groupsIter.hasNext() )
{
roles = (Group) groupsIter.next();
if( roles.getName().equals("Roles") )
{
assertTrue("Echo is NOT a role", roles.isMember(new SimplePrincipal("Echo")) == false);
assertTrue("Java is a role", roles.isMember(new SimplePrincipal("Java")));
assertTrue("Coder is a role", roles.isMember(new SimplePrincipal("Coder")));
}
else if( roles.getName().equals("CallerPrincipal") )
{
log.info("CallerPrincipal is "+roles.members().nextElement());
boolean isMember = roles.isMember(new SimplePrincipal("callerStark"));
assertTrue("CallerPrincipal is callerStark", isMember);
}
}
lc.logout();
// Test the usernames with common prefix
log.info("Testing similar usernames");
handler = new UsernamePasswordHandler("jdukeman", "anotherduke".toCharArray());
lc = new LoginContext("testUsersRoles", handler);
lc.login();
subject = lc.getSubject();
groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains jdukeman", subject.getPrincipals().contains(new SimplePrincipal("jdukeman")));