### Question:
JaasUtils { public static <P extends Principal> P getPrincipal(final Class<P> principalClass) { if (principalClass == null) throw new IllegalArgumentException("Principal class cannot be null."); final Set<P> principals = getCurrentSubject().getPrincipals(principalClass); if (principals.size() != 1) throw new IllegalArgumentException("Incorrect number of principals of type " + principalClass.getCanonicalName() + " on subject: " + Integer.toString(principals.size())); return principals.iterator().next(); }  static Subject getCurrentSubject(); static P getPrincipal(final Class<P> principalClass); static T getPrivateCredential(final Class<T> credentialClass); static boolean hasPrincipal(final Class<? extends Principal> principalClass);  }### Answer:
@Test public void testGetPrincipal() { final Principal principal = mock(Principal.class); final Subject subject = new Subject(); subject.getPrincipals().add(principal); assertThat(Subject.doAs(subject, new PrivilegedAction<Principal>() { @Override public Principal run() { return JaasUtils.getPrincipal(principal.getClass()); } })).isEqualTo(principal); }
@Test public void testGetPrincipalNoPrincipals() { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { Subject.doAs(new Subject(), new PrivilegedAction<Void>() { @Override public Void run() { JaasUtils.getPrincipal(Principal.class); return null; } }); }); assertThat(e.getMessage()).isEqualTo("Incorrect number of principals of type " + Principal.class.getCanonicalName() + " on subject: " + Integer.toString(0)); }
@Test public void testGetPrincipalNullPrincipalClass() { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { JaasUtils.getPrincipal(null); }); assertThat(e.getMessage()).isEqualTo("Principal class cannot be null."); }