Moved AbstractAuthenticationManagerTests into ProviderManager as tested methods have already been moved there (maven wasn't running Abstract* tests but they were actually failing).

This commit is contained in:
Luke Taylor 2007-12-10 23:36:27 +00:00
parent 32038d8b92
commit 894c90dadd
2 changed files with 49 additions and 111 deletions

View File

@ -1,87 +0,0 @@
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed 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
*
* 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.springframework.security;
import junit.framework.TestCase;
import org.springframework.security.providers.TestingAuthenticationToken;
/**
* Tests {@link AbstractAuthenticationManager}.
*
* @author Luke Taylor
* @version $Id$
*/
public class AbstractAuthenticationManagerTests extends TestCase {
//~ Constructors ===================================================================================================
public AbstractAuthenticationManagerTests() {
super();
}
public AbstractAuthenticationManagerTests(String arg0) {
super(arg0);
}
//~ Methods ========================================================================================================
/**
* Creates an AuthenticationManager which will return a token with the given details object set on it.
*
* @param resultDetails DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private AuthenticationManager createAuthenticationManager(final Object resultDetails) {
return new AbstractAuthenticationManager() {
protected Authentication doAuthentication(Authentication authentication)
throws AuthenticationException {
TestingAuthenticationToken token = createAuthenticationToken();
token.setDetails(resultDetails);
return token;
}
};
}
private TestingAuthenticationToken createAuthenticationToken() {
return new TestingAuthenticationToken("name", "password", new GrantedAuthorityImpl[0]);
}
public void testDetailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() {
Object requestDetails = new String("(Request Details)");
Object resultDetails = new String("(Result Details)");
AuthenticationManager authMgr = createAuthenticationManager(resultDetails);
TestingAuthenticationToken request = createAuthenticationToken();
request.setDetails(requestDetails);
Authentication result = authMgr.authenticate(request);
assertEquals(resultDetails, result.getDetails());
}
public void testDetailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
AuthenticationManager authMgr = createAuthenticationManager(null);
Object details = new Object();
TestingAuthenticationToken request = createAuthenticationToken();
request.setDetails(details);
Authentication result = authMgr.authenticate(request);
assertEquals(details, result.getDetails());
}
}

View File

@ -15,20 +15,19 @@
package org.springframework.security.providers;
import junit.framework.TestCase;
import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationException;
import org.springframework.security.AuthenticationServiceException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.concurrent.ConcurrentSessionControllerImpl;
import org.springframework.security.concurrent.NullConcurrentSessionController;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import junit.framework.TestCase;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
@ -43,7 +42,6 @@ public class ProviderManagerTests extends TestCase {
//~ Constructors ===================================================================================================
public ProviderManagerTests() {
super();
}
public ProviderManagerTests(String arg0) {
@ -52,10 +50,6 @@ public class ProviderManagerTests extends TestCase {
//~ Methods ========================================================================================================
public static void main(String[] args) {
junit.textui.TestRunner.run(ProviderManagerTests.class);
}
private ProviderManager makeProviderManager() throws Exception {
MockProvider provider1 = new MockProvider();
List providers = new Vector();
@ -82,10 +76,6 @@ public class ProviderManagerTests extends TestCase {
return mgr;
}
public final void setUp() throws Exception {
super.setUp();
}
public void testAuthenticationFails() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
@ -141,8 +131,7 @@ public class ProviderManagerTests extends TestCase {
assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority());
}
public void testConcurrentSessionControllerConfiguration()
throws Exception {
public void testConcurrentSessionControllerConfiguration() throws Exception {
ProviderManager target = new ProviderManager();
//The NullConcurrentSessionController should be the default
@ -154,8 +143,7 @@ public class ProviderManagerTests extends TestCase {
assertEquals(impl, target.getSessionController());
}
public void testStartupFailsIfProviderListDoesNotContainingProviders()
throws Exception {
public void testStartupFailsIfProviderListDoesNotContainingProviders() throws Exception {
List providers = new Vector();
providers.add("THIS_IS_NOT_A_PROVIDER");
@ -169,8 +157,7 @@ public class ProviderManagerTests extends TestCase {
}
}
public void testStartupFailsIfProviderListNotSet()
throws Exception {
public void testStartupFailsIfProviderListNotSet() throws Exception {
ProviderManager mgr = new ProviderManager();
try {
@ -199,6 +186,46 @@ public class ProviderManagerTests extends TestCase {
assertEquals(1, mgr.getProviders().size());
}
public void testDetailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
Object requestDetails = new String("(Request Details)");
final Object resultDetails = new String("(Result Details)");
ProviderManager authMgr = makeProviderManager();
AuthenticationProvider provider = new AuthenticationProvider() {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
((TestingAuthenticationToken)authentication).setDetails(resultDetails);
return authentication;
}
public boolean supports(Class authentication) {
return true;
}
};
authMgr.setProviders(Arrays.asList(new AuthenticationProvider[] {provider}));
TestingAuthenticationToken request = createAuthenticationToken();
request.setDetails(requestDetails);
Authentication result = authMgr.authenticate(request);
assertEquals(resultDetails, result.getDetails());
}
public void testDetailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() throws Exception {
Object details = new Object();
ProviderManager authMgr = makeProviderManager();
TestingAuthenticationToken request = createAuthenticationToken();
request.setDetails(details);
Authentication result = authMgr.authenticate(request);
assertEquals(details, result.getDetails());
}
private TestingAuthenticationToken createAuthenticationToken() {
return new TestingAuthenticationToken("name", "password", new GrantedAuthorityImpl[0]);
}
//~ Inner Classes ==================================================================================================
private class MockApplicationEventPublisher implements ApplicationEventPublisher {
@ -209,15 +236,14 @@ public class ProviderManagerTests extends TestCase {
}
public void publishEvent(ApplicationEvent event) {
if (expectedEvent == false) {
if (!expectedEvent) {
throw new IllegalStateException("The ApplicationEventPublisher did not expect to receive this event");
}
}
}
private class MockProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (supports(authentication.getClass())) {
return authentication;
} else {
@ -235,8 +261,7 @@ public class ProviderManagerTests extends TestCase {
}
private class MockProviderWhichReturnsNull implements AuthenticationProvider {
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (supports(authentication.getClass())) {
return null;
} else {