Java tutorial
/* * Licensed to Jasig under one or more contributor license * agreements. See the NOTICE file distributed with this work * for additional information regarding copyright ownership. * Jasig licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a * copy of the License at the following location: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.jasig.cas.adaptors.trusted.web.flow; import java.security.Principal; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import org.jasig.cas.CentralAuthenticationServiceImpl; import org.jasig.cas.adaptors.trusted.authentication.handler.support.PrincipalBearingCredentialsAuthenticationHandler; import org.jasig.cas.adaptors.trusted.authentication.principal.PrincipalBearingCredentialsToPrincipalResolver; import org.jasig.cas.authentication.AuthenticationManagerImpl; import org.jasig.cas.authentication.handler.AuthenticationHandler; import org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver; import org.jasig.cas.authentication.principal.SimpleWebApplicationServiceImpl; import org.jasig.cas.ticket.registry.DefaultTicketRegistry; import org.jasig.cas.ticket.support.NeverExpiresExpirationPolicy; import org.jasig.cas.util.DefaultUniqueTicketIdGenerator; import org.jasig.cas.util.UniqueTicketIdGenerator; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; import org.springframework.webflow.context.servlet.ServletExternalContext; import org.springframework.webflow.test.MockRequestContext; import junit.framework.TestCase; /** * * @author Scott Battaglia * @version $Revision$ $Date$ * @since 3.0.5 * */ public class PrincipalFromRequestUserPrincipalNonInteractiveCredentialsActionTests extends TestCase { private PrincipalFromRequestUserPrincipalNonInteractiveCredentialsAction action; protected void setUp() throws Exception { this.action = new PrincipalFromRequestUserPrincipalNonInteractiveCredentialsAction(); final CentralAuthenticationServiceImpl centralAuthenticationService = new CentralAuthenticationServiceImpl(); centralAuthenticationService.setTicketRegistry(new DefaultTicketRegistry()); final Map<String, UniqueTicketIdGenerator> idGenerators = new HashMap<String, UniqueTicketIdGenerator>(); idGenerators.put(SimpleWebApplicationServiceImpl.class.getName(), new DefaultUniqueTicketIdGenerator()); final AuthenticationManagerImpl authenticationManager = new AuthenticationManagerImpl(); authenticationManager.setAuthenticationHandlers(Arrays .asList(new AuthenticationHandler[] { new PrincipalBearingCredentialsAuthenticationHandler() })); authenticationManager.setCredentialsToPrincipalResolvers(Arrays.asList( new CredentialsToPrincipalResolver[] { new PrincipalBearingCredentialsToPrincipalResolver() })); centralAuthenticationService .setTicketGrantingTicketUniqueTicketIdGenerator(new DefaultUniqueTicketIdGenerator()); centralAuthenticationService.setUniqueTicketIdGeneratorsForService(idGenerators); centralAuthenticationService.setServiceTicketExpirationPolicy(new NeverExpiresExpirationPolicy()); centralAuthenticationService.setTicketGrantingTicketExpirationPolicy(new NeverExpiresExpirationPolicy()); centralAuthenticationService.setAuthenticationManager(authenticationManager); this.action.setCentralAuthenticationService(centralAuthenticationService); } public void testRemoteUserExists() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest(); request.setUserPrincipal(new Principal() { public String getName() { return "test"; } }); final MockRequestContext context = new MockRequestContext(); context.setExternalContext( new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); assertEquals("success", this.action.execute(context).getId()); } public void testRemoteUserDoesntExists() throws Exception { final MockRequestContext context = new MockRequestContext(); context.setExternalContext(new ServletExternalContext(new MockServletContext(), new MockHttpServletRequest(), new MockHttpServletResponse())); assertEquals("error", this.action.execute(context).getId()); } }