### Question:
UserDatabaseLoginModule extends AbstractLoginModule { public boolean login() throws LoginException { UserDatabaseCallback ucb = new UserDatabaseCallback(); NameCallback ncb = new NameCallback( "User name" ); PasswordCallback pcb = new PasswordCallback( "Password", false ); Callback[] callbacks = new Callback[] { ucb, ncb, pcb }; try { m_handler.handle( callbacks ); UserDatabase db = ucb.getUserDatabase(); String username = ncb.getName(); String password = new String( pcb.getPassword() ); if ( db == null ) { throw new FailedLoginException( "No user database: check the callback handler code!" ); } UserProfile profile = db.findByLoginName( username ); String storedPassword = profile.getPassword(); if ( storedPassword != null && db.validatePassword( username, password ) ) { if ( log.isDebugEnabled() ) { log.debug( "Logged in user database user " + username ); } m_principals.add( new WikiPrincipal( username, WikiPrincipal.LOGIN_NAME ) ); return true; } throw new FailedLoginException( "The username or password is incorrect." ); } catch( IOException e ) { String message = "IO exception; disallowing login."; log.error( message, e ); throw new LoginException( message ); } catch( UnsupportedCallbackException e ) { String message = "Unable to handle callback; disallowing login."; log.error( message, e ); throw new LoginException( message ); } catch( NoSuchPrincipalException e ) { throw new FailedLoginException( "The username or password is incorrect." ); } }  boolean login();  }### Answer:
@Test public final void testLogin() { try { CallbackHandler handler = new WikiCallbackHandler( m_engine, null, "user", "password" ); LoginModule module = new UserDatabaseLoginModule(); 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( new WikiPrincipal( "user", WikiPrincipal.LOGIN_NAME ) ) ); Assertions.assertFalse( principals.contains( Role.AUTHENTICATED ) ); Assertions.assertFalse( principals.contains( Role.ALL ) ); m_subject = new Subject(); handler = new WikiCallbackHandler( m_engine, null, "janne", "myP@5sw0rd" ); module = new UserDatabaseLoginModule(); module.initialize( m_subject, handler, new HashMap<String, Object>(), new HashMap<String, Object>() ); module.login(); module.commit(); principals = m_subject.getPrincipals(); Assertions.assertEquals( 1, principals.size() ); Assertions.assertTrue( principals.contains( new WikiPrincipal( "janne", WikiPrincipal.LOGIN_NAME ) ) ); 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() { try { CallbackHandler handler = new WikiCallbackHandler( m_engine, null, "user", "password" ); LoginModule module = new UserDatabaseLoginModule(); 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( new WikiPrincipal( "user", WikiPrincipal.LOGIN_NAME ) ) ); 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 ); } }