@Test public void notifySuccessAfterDoAuthenticate() { AuthenticationListener mockListener = createMock(AuthenticationListener.class); abstractAuthenticator.getAuthenticationListeners().add(mockListener); AuthenticationToken token = newToken(); mockListener.onSuccess(token, info); replay(mockListener); abstractAuthenticator.authenticate(token); verify(mockListener); }
@Test(expected = AuthenticationException.class) public void notifyFailureAfterDoAuthenticateThrowsNonAuthenticationException() { abstractAuthenticator = new AbstractAuthenticator() { protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws AuthenticationException { throw new IllegalArgumentException("not an AuthenticationException subclass"); } }; AuthenticationToken token = newToken(); abstractAuthenticator.authenticate(token); }
/** * Ensures a non-null <tt>Subject</tt> instance is returned from the authenticate() method after a valid * authentication attempt (i.e. the subclass's doAuthenticate implementation returns a valid, non-null * AuthenticationInfo object). */ @Test public void nonNullAuthenticationInfoAfterAuthenticate() { AuthenticationInfo authcInfo = abstractAuthenticator.authenticate(newToken()); assertNotNull(authcInfo); }
/** * Ensures that the authenticate() method throws an AuthenticationException if the subclass returns <tt>null</tt> * as the return value to the doAuthenticate() method. */ @Test(expected = AuthenticationException.class) public void throwAuthenticationExceptionIfDoAuthenticateReturnsNull() { abstractAuthenticator = createAuthcReturnNull(); abstractAuthenticator.authenticate(newToken()); }
@Test public void logExceptionAfterDoAuthenticateThrowsNonAuthenticationException() { Logger logger = Logger.getLogger(AbstractAuthenticator.class); // NOTE: log4j is a test dependency ByteArrayOutputStream out = new ByteArrayOutputStream(); Layout layout = new SimpleLayout(); Appender appender = new WriterAppender(layout, out); logger.addAppender(appender); final String expectedExceptionMessage = "exception thrown for test logExceptionAfterDoAuthenticateThrowsNonAuthenticationException"; abstractAuthenticator = new AbstractAuthenticator() { protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws AuthenticationException { throw new IllegalArgumentException(expectedExceptionMessage); } }; AuthenticationToken token = newToken(); try{ abstractAuthenticator.authenticate(token); fail("the expected AuthenticationException was not thrown"); }catch(AuthenticationException expectedException){ } String logMsg = out.toString(); assertTrue(logMsg.contains("WARN")); assertTrue(logMsg.contains("java.lang.IllegalArgumentException: "+ expectedExceptionMessage)); logger.removeAppender(appender); }
@Test public void notifyFailureAfterDoAuthenticateThrowsAuthenticationException() { AuthenticationListener mockListener = createMock(AuthenticationListener.class); AuthenticationToken token = newToken(); final AuthenticationException ae = new AuthenticationException("dummy exception to test notification"); abstractAuthenticator = new AbstractAuthenticator() { protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws AuthenticationException { throw ae; } }; abstractAuthenticator.getAuthenticationListeners().add(mockListener); mockListener.onFailure(token, ae); replay(mockListener); boolean exceptionThrown = false; try { abstractAuthenticator.authenticate(token); } catch (AuthenticationException e) { exceptionThrown = true; assertEquals(e, ae); } verify(mockListener); if (!exceptionThrown) { fail("An AuthenticationException should have been thrown during the notifyFailure test case."); } }