### Question:
DefaultSecurityContext implements LoginSecurityContext { public boolean isUserInRole(String role) { if (subject != null) { for (Principal principal : subject.getPrincipals()) { if (isGroupPrincipal(principal) && checkGroup((Principal)principal, role)) { return true; } else if (p != principal && role.equals(principal.getName())) { return true; } } } return false; } DefaultSecurityContext(Subject subject);  DefaultSecurityContext(String principalName, Subject subject);  DefaultSecurityContext(Principal p, Subject subject); Principal getUserPrincipal(); boolean isUserInRole(String role); Subject getSubject(); Set<Principal> getUserRoles(); static boolean isGroupPrincipal(Principal principal);  }### Answer:
@Test public void testUserNotInRole() { Subject s = new Subject(); Principal p = new SimplePrincipal("Barry"); s.getPrincipals().add(p); assertFalse(new DefaultSecurityContext(p, s).isUserInRole("friend")); }
@Test public void testUserInRole() { Subject s = new Subject(); Principal p = new SimplePrincipal("Barry"); s.getPrincipals().add(p); s.getPrincipals().add(new SimpleGroup("friend", p)); assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend")); }
@Test public void testUserInRole2() { Subject s = new Subject(); Principal p = new SimplePrincipal("Barry"); s.getPrincipals().add(p); GroupPrincipal group = new SimpleGroup("Roles", p); group.addMember(new SimpleGroup("friend")); s.getPrincipals().add(group); assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend")); }
@Test public void testUserInRole3() { Subject s = new Subject(); Principal p = new SimplePrincipal("Barry"); s.getPrincipals().add(p); GroupPrincipal group = new SimpleGroup("Roles", p); GroupPrincipal subgroup = new SimpleGroup("subgroup"); subgroup.addMember(new SimpleGroup("friend")); group.addMember(subgroup); s.getPrincipals().add(group); assertTrue(new DefaultSecurityContext(p, s).isUserInRole("friend")); }
@Test public void testUserInImplicitRoles() { Subject s = new Subject(); Principal p = new SimplePrincipal("Barry"); s.getPrincipals().add(p); Principal role = new SimplePrincipal("friend"); s.getPrincipals().add(role); LoginSecurityContext context = new DefaultSecurityContext(p, s); assertTrue(context.isUserInRole("friend")); assertFalse(context.isUserInRole("family")); assertFalse(context.isUserInRole("Barry")); }