### Question:
DefaultAuthenticationPolicy implements AuthenticationPolicy { @Override public boolean isAuthenticationRequired(SubjectConnectionReference conn) { Subject subject = conn.getSubject(); if (subject.isAuthenticated()) { return false; } if (isAnonymousAccessAllowed()) { if (isAnonymousAccount(subject)) { return false; } } if (!isVmConnectionAuthenticationRequired()) { if (isSystemAccount(subject)) { return false; } } return true; }  boolean isVmConnectionAuthenticationRequired(); void setVmConnectionAuthenticationRequired(boolean vmConnectionAuthenticationRequired); String getSystemAccountUsername(); void setSystemAccountUsername(String systemAccountUsername); String getSystemAccountRealmName(); void setSystemAccountRealmName(String systemAccountRealmName); boolean isAnonymousAccessAllowed(); void setAnonymousAccessAllowed(boolean anonymousAccessAllowed); String getAnonymousAccountUsername(); void setAnonymousAccountUsername(String anonymousAccountUsername); String getAnonymousAccountRealmName(); void setAnonymousAccountRealmName(String anonymousAccountRealmName); @Override boolean isAuthenticationRequired(SubjectConnectionReference conn); @Override void customizeSubject(Subject.Builder subjectBuilder, ConnectionReference conn);  }### Answer:
@Test public void testIsAuthenticationRequiredWhenAlreadyRequired() { Subject subject = new SubjectAdapter() { @Override public boolean isAuthenticated() { return true; } }; SubjectConnectionReference sc = new SubjectConnectionReference(new ConnectionContext(), new ConnectionInfo(), new DefaultEnvironment(), subject); assertFalse(policy.isAuthenticationRequired(sc)); }
@Test public void testIsAuthenticationRequiredWhenSystemConnectionAndSystemSubject() { Subject subject = new SubjectAdapter() { @Override public PrincipalCollection getPrincipals() { return new SimplePrincipalCollection("system", "iniRealm"); } }; SubjectConnectionReference sc = new SubjectConnectionReference(new ConnectionContext(), new ConnectionInfo(), new DefaultEnvironment(), subject); assertFalse(policy.isAuthenticationRequired(sc)); }
@Test public void testIsAuthenticationRequiredWhenSystemConnectionDoesNotRequireAuthenticationAndNotSystemAccount() { Subject subject = new SubjectAdapter() { @Override public PrincipalCollection getPrincipals() { return new SimplePrincipalCollection("foo", "iniRealm"); } }; SubjectConnectionReference sc = new SubjectConnectionReference(new ConnectionContext(), new ConnectionInfo(), new DefaultEnvironment(), subject); assertTrue(policy.isAuthenticationRequired(sc)); }