001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.rcp;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023:
024: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
025:
026: /**
027: * Tests {@link RemoteAuthenticationProvider}.
028: *
029: * @author Ben Alex
030: * @version $Id: RemoteAuthenticationProviderTests.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public class RemoteAuthenticationProviderTests extends TestCase {
033: //~ Methods ========================================================================================================
034:
035: public static void main(String[] args) {
036: junit.textui.TestRunner
037: .run(RemoteAuthenticationProviderTests.class);
038: }
039:
040: public final void setUp() throws Exception {
041: super .setUp();
042: }
043:
044: public void testExceptionsGetPassedBackToCaller() {
045: RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
046: provider
047: .setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
048: false));
049:
050: try {
051: provider
052: .authenticate(new UsernamePasswordAuthenticationToken(
053: "marissa", "password"));
054: fail("Should have thrown RemoteAuthenticationException");
055: } catch (RemoteAuthenticationException expected) {
056: assertTrue(true);
057: }
058: }
059:
060: public void testGettersSetters() {
061: RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
062: provider
063: .setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
064: true));
065: assertNotNull(provider.getRemoteAuthenticationManager());
066: }
067:
068: public void testStartupChecksAuthenticationManagerSet()
069: throws Exception {
070: RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
071:
072: try {
073: provider.afterPropertiesSet();
074: fail("Should have thrown IllegalArgumentException");
075: } catch (IllegalArgumentException expected) {
076: assertTrue(true);
077: }
078:
079: provider
080: .setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
081: true));
082: provider.afterPropertiesSet();
083: assertTrue(true);
084: }
085:
086: public void testSuccessfulAuthenticationCreatesObject() {
087: RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
088: provider
089: .setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
090: true));
091:
092: Authentication result = provider
093: .authenticate(new UsernamePasswordAuthenticationToken(
094: "marissa", "password"));
095: assertEquals("marissa", result.getPrincipal());
096: assertEquals("password", result.getCredentials());
097: assertEquals("foo", result.getAuthorities()[0].getAuthority());
098: }
099:
100: public void testSupports() {
101: RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
102: assertTrue(provider
103: .supports(UsernamePasswordAuthenticationToken.class));
104: }
105:
106: //~ Inner Classes ==================================================================================================
107:
108: private class MockRemoteAuthenticationManager implements
109: RemoteAuthenticationManager {
110: private boolean grantAccess;
111:
112: public MockRemoteAuthenticationManager(boolean grantAccess) {
113: this .grantAccess = grantAccess;
114: }
115:
116: public GrantedAuthority[] attemptAuthentication(
117: String username, String password)
118: throws RemoteAuthenticationException {
119: if (grantAccess) {
120: return new GrantedAuthority[] { new GrantedAuthorityImpl(
121: "foo") };
122: } else {
123: throw new RemoteAuthenticationException("as requested");
124: }
125: }
126: }
127: }
|