### Question:
WebContainerLoginModule extends AbstractLoginModule { public boolean login() throws LoginException { HttpRequestCallback rcb = new HttpRequestCallback(); Callback[] callbacks = new Callback[] { rcb }; String userId = null; try { m_handler.handle( callbacks ); HttpServletRequest request = rcb.getRequest(); if ( request == null ) { throw new LoginException( "No Http request supplied." ); } HttpSession session = request.getSession(false); String sid = (session == null) ? NULL : session.getId(); Principal principal = request.getUserPrincipal(); if ( principal == null ) { if ( log.isDebugEnabled() ) { log.debug( "No userPrincipal found for session ID=" + sid); } userId = request.getRemoteUser(); if ( userId == null ) { if ( log.isDebugEnabled() ) { log.debug( "No remoteUser found for session ID=" + sid); } throw new FailedLoginException( "No remote user found" ); } principal = new WikiPrincipal( userId, WikiPrincipal.LOGIN_NAME ); } if ( log.isDebugEnabled() ) { log.debug("Logged in container principal " + principal.getName() + "." ); } m_principals.add( principal ); return true; } catch( IOException e ) { log.error( "IOException: " + e.getMessage() ); return false; } catch( UnsupportedCallbackException e ) { log.error( "UnsupportedCallbackException: " + e.getMessage() ); return false; } }  boolean login();  }### Answer:
@Test public final void testLogin() { Principal principal = new WikiPrincipal( "Andrew Jaquith" ); MockHttpServletRequest request = m_engine.newHttpRequest(); request.setUserPrincipal( principal ); try { CallbackHandler handler = new WebContainerCallbackHandler( m_engine, request ); LoginModule module = new WebContainerLoginModule(); module.initialize( m_subject, handler, new HashMap<String, Object>(), new HashMap<String, Object>()); module.login(); module.commit(); Set< Principal > principals = m_subject.getPrincipals(); Assertions.assertEquals( 1, principals.size() ); Assertions.assertTrue( principals.contains( principal ) ); Assertions.assertFalse( principals.contains( Role.ANONYMOUS ) ); Assertions.assertFalse( principals.contains( Role.ASSERTED ) ); Assertions.assertFalse( principals.contains( Role.AUTHENTICATED ) ); Assertions.assertFalse( principals.contains( Role.ALL ) ); } catch( LoginException e ) { System.err.println( e.getMessage() ); Assertions.assertTrue( false ); } }
@Test public final void testLogout() { Principal principal = new WikiPrincipal( "Andrew Jaquith" ); MockHttpServletRequest request = m_engine.newHttpRequest(); request.setUserPrincipal( principal ); try { CallbackHandler handler = new WebContainerCallbackHandler( m_engine, request ); LoginModule module = new WebContainerLoginModule(); module.initialize( m_subject, handler, new HashMap<String, Object>(), new HashMap<String, Object>()); module.login(); module.commit(); Set< Principal > principals = m_subject.getPrincipals(); Assertions.assertEquals( 1, principals.size() ); Assertions.assertTrue( principals.contains( principal ) ); Assertions.assertFalse( principals.contains( Role.AUTHENTICATED ) ); Assertions.assertFalse( principals.contains( Role.ALL ) ); module.logout(); Assertions.assertEquals( 0, principals.size() ); } catch( LoginException e ) { System.err.println( e.getMessage() ); Assertions.assertTrue( false ); } }