### Question:
SubjectUtil { public static List<String> getUsernamesFromSubject(Subject subject) { List<String> userList = new ArrayList<String>(); Set<Principal> princSet = null; if (subject != null) { princSet = subject.getPrincipals(); for (Principal p : princSet) { if ((p != null) && ( p.getClass().isAssignableFrom(PrincipalImpl.class) || "weblogic.security.principal.WLSUserImpl".equals(p.getClass().getCanonicalName()) )) { String uName = p.getName(); userList.add(uName); } } } return userList; }  static List<String> getUsernamesFromSubject(Subject subject); static List<String> getGroupnamesFromSubject(Subject subject);  }### Answer:
@Test public void testUserNameUtil() { Subject sub = createSub(USERNAME, GROUPS); List<String> usernames = SubjectUtil.getUsernamesFromSubject(sub); if (debug) System.out.println("user list =" + usernames); Assert.assertEquals(1, usernames.size()); }
@Test public void testUserNameUtil_empty() { Subject sub = createSub(null, GROUPS); List<String> usernames = SubjectUtil.getUsernamesFromSubject(sub); Assert.assertEquals(0, usernames.size()); }
@Test public void testUserNameUtil_multi() { Subject sub = createSub(USERNAME, GROUPS); sub.getPrincipals().add(new PrincipalImpl(USERNAME2)); List<String> usernames = SubjectUtil.getUsernamesFromSubject(sub); if (debug) System.out.println("user list =" + usernames); Assert.assertEquals(2, usernames.size()); }