AuthenticationManagerImpl implements AuthenticationManager { public Authentication authenticate(Credential credential) throws InvalidCredentialException{ boolean foundSupported = false; boolean authenticated = false; if(credential==null){ throw new EmptyCredentialException(); } AuthenticationException unAuthSupportedHandlerException = InvalidCredentialException.INSTANCE; if(authenticationHandlers!=null && authenticationHandlers.size()>0){ for(AuthenticationHandler handler:authenticationHandlers){ if(handler.supports(credential)){ foundSupported = true; try { authenticated = handler.authenticate(credential); if(authenticated){ break; } }catch (AuthenticationException e) { unAuthSupportedHandlerException = e; } } } } if(!foundSupported){ throw new UnsupportedCredentialsException(); } if(!authenticated){ throw unAuthSupportedHandlerException; } Principal principal = null; foundSupported = false; if(credentialToPrincipalResolvers!=null && credentialToPrincipalResolvers.size()>0){ for(CredentialToPrincipalResolver resolver:credentialToPrincipalResolvers){ if(resolver.supports(credential)){ foundSupported = true; principal = resolver.resolvePrincipal(credential); if(principal!=null){ break; } } } } if(!foundSupported){ throw new UnsupportedCredentialsException(); } if(authenticationPostHandler==null){ throw new NoAuthenticationPostHandlerException(); } return authenticationPostHandler.postAuthentication(credential, principal); } AuthenticationManagerImpl(); void setAuthenticationPostHandler(
			AuthenticationPostHandler authenticationPostHandler); void setCredentialToPrincipalResolvers(
			List<CredentialToPrincipalResolver> credentialToPrincipalResolvers); void setAuthenticationHandlers(
			List<AuthenticationHandler> authenticationHandlers); Authentication authenticate(Credential credential);  }
@Test public void testAuthenticate() { try{ authenticationManager.authenticate(null); fail("当参数为Null应该抛出异常信息"); }catch (EmptyCredentialException e) { } Credential credential = Mockito.mock(Credential.class); try{ authenticationManager.authenticate(credential); fail("当没有认证处理器应该抛出异常信息"); }catch (UnsupportedCredentialsException e) { } List<AuthenticationHandler> authenticationHandlers = null; try{ authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationManager.setAuthenticationHandlers(authenticationHandlers); authenticationManager.authenticate(credential); fail("当没有认证处理器应该抛出异常信息"); }catch (UnsupportedCredentialsException e) { } AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class); Mockito.when(handler.supports(credential)).thenReturn(false); try{ authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationHandlers.add(handler); authenticationManager.setAuthenticationHandlers(authenticationHandlers); authenticationManager.authenticate(credential); fail("当没有认证处理器应该抛出异常信息"); }catch (UnsupportedCredentialsException e) { } handler = Mockito.mock(AuthenticationHandler.class); Mockito.when(handler.supports(credential)).thenReturn(true); Mockito.when(handler.authenticate(credential)).thenReturn(true); try{ authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationHandlers.add(handler); authenticationManager.setAuthenticationHandlers(authenticationHandlers); authenticationManager.authenticate(credential); fail("当没有凭据转换器应该抛出异常信息"); }catch (UnsupportedCredentialsException e) { } List<CredentialToPrincipalResolver> resolvers = new ArrayList<CredentialToPrincipalResolver>(); handler = Mockito.mock(AuthenticationHandler.class); Mockito.when(handler.supports(credential)).thenReturn(true); Mockito.when(handler.authenticate(credential)).thenReturn(true); try{ authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationHandlers.add(handler); authenticationManager.setAuthenticationHandlers(authenticationHandlers); authenticationManager.setCredentialToPrincipalResolvers(resolvers); authenticationManager.authenticate(credential); fail("当没有凭据转换器应该抛出异常信息"); }catch (UnsupportedCredentialsException e) { } handler = Mockito.mock(AuthenticationHandler.class); Mockito.when(handler.supports(credential)).thenReturn(true); Mockito.when(handler.authenticate(credential)).thenReturn(true); CredentialToPrincipalResolver resolver = Mockito.mock(CredentialToPrincipalResolver.class); Principal principal = Mockito.mock(Principal.class); Mockito.when(resolver.supports(credential)).thenReturn(true); Mockito.when(resolver.resolvePrincipal(credential)).thenReturn(principal); try{ authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationHandlers.add(handler); authenticationManager.setAuthenticationHandlers(authenticationHandlers); resolvers = new ArrayList<CredentialToPrincipalResolver>(); resolvers.add(resolver); authenticationManager.setCredentialToPrincipalResolvers(resolvers); authenticationManager.authenticate(credential); fail("当没有认证后处理器应该抛出异常信息"); }catch (NoAuthenticationPostHandlerException e) { } handler = Mockito.mock(AuthenticationHandler.class); Mockito.when(handler.supports(credential)).thenReturn(true); Mockito.when(handler.authenticate(credential)).thenReturn(true); resolver = Mockito.mock(CredentialToPrincipalResolver.class); principal = Mockito.mock(Principal.class); Mockito.when(resolver.supports(credential)).thenReturn(true); Mockito.when(resolver.resolvePrincipal(credential)).thenReturn(principal); AuthenticationPostHandler authenticationPostHandler = Mockito.mock(AuthenticationPostHandler.class); Authentication authentication = Mockito.mock(Authentication.class); Mockito.when(authenticationPostHandler.postAuthentication(credential, principal)).thenReturn(authentication); authenticationHandlers = new ArrayList<AuthenticationHandler>(); authenticationHandlers.add(handler); authenticationManager.setAuthenticationHandlers(authenticationHandlers); resolvers = new ArrayList<CredentialToPrincipalResolver>(); resolvers.add(resolver); authenticationManager.setCredentialToPrincipalResolvers(resolvers); authenticationManager.setAuthenticationPostHandler(authenticationPostHandler); authenticationManager.authenticate(credential); }