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;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.AuthenticationException;
022: import org.acegisecurity.AuthenticationServiceException;
023: import org.acegisecurity.GrantedAuthority;
024: import org.acegisecurity.GrantedAuthorityImpl;
025:
026: import org.acegisecurity.concurrent.ConcurrentSessionControllerImpl;
027: import org.acegisecurity.concurrent.NullConcurrentSessionController;
028:
029: import org.springframework.context.ApplicationEvent;
030: import org.springframework.context.ApplicationEventPublisher;
031:
032: import java.util.List;
033: import java.util.Vector;
034:
035: /**
036: * Tests {@link ProviderManager}.
037: *
038: * @author Ben Alex
039: * @version $Id: ProviderManagerTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class ProviderManagerTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public ProviderManagerTests() {
045: super ();
046: }
047:
048: public ProviderManagerTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: public static void main(String[] args) {
055: junit.textui.TestRunner.run(ProviderManagerTests.class);
056: }
057:
058: private ProviderManager makeProviderManager() throws Exception {
059: MockProvider provider1 = new MockProvider();
060: List providers = new Vector();
061: providers.add(provider1);
062:
063: ProviderManager mgr = new ProviderManager();
064: mgr.setProviders(providers);
065:
066: mgr.afterPropertiesSet();
067:
068: return mgr;
069: }
070:
071: private ProviderManager makeProviderManagerWithMockProviderWhichReturnsNullInList() {
072: MockProviderWhichReturnsNull provider1 = new MockProviderWhichReturnsNull();
073: MockProvider provider2 = new MockProvider();
074: List providers = new Vector();
075: providers.add(provider1);
076: providers.add(provider2);
077:
078: ProviderManager mgr = new ProviderManager();
079: mgr.setProviders(providers);
080:
081: return mgr;
082: }
083:
084: public final void setUp() throws Exception {
085: super .setUp();
086: }
087:
088: public void testAuthenticationFails() throws Exception {
089: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
090: "Test", "Password", new GrantedAuthority[] {
091: new GrantedAuthorityImpl("ROLE_ONE"),
092: new GrantedAuthorityImpl("ROLE_TWO") });
093:
094: ProviderManager mgr = makeProviderManager();
095: mgr
096: .setApplicationEventPublisher(new MockApplicationEventPublisher(
097: true));
098:
099: try {
100: mgr.authenticate(token);
101: fail("Should have thrown ProviderNotFoundException");
102: } catch (ProviderNotFoundException expected) {
103: assertTrue(true);
104: }
105: }
106:
107: public void testAuthenticationSuccess() throws Exception {
108: TestingAuthenticationToken token = new TestingAuthenticationToken(
109: "Test", "Password", new GrantedAuthority[] {
110: new GrantedAuthorityImpl("ROLE_ONE"),
111: new GrantedAuthorityImpl("ROLE_TWO") });
112:
113: ProviderManager mgr = makeProviderManager();
114: mgr
115: .setApplicationEventPublisher(new MockApplicationEventPublisher(
116: true));
117:
118: Authentication result = mgr.authenticate(token);
119:
120: if (!(result instanceof TestingAuthenticationToken)) {
121: fail("Should have returned instance of TestingAuthenticationToken");
122: }
123:
124: TestingAuthenticationToken castResult = (TestingAuthenticationToken) result;
125: assertEquals("Test", castResult.getPrincipal());
126: assertEquals("Password", castResult.getCredentials());
127: assertEquals("ROLE_ONE", castResult.getAuthorities()[0]
128: .getAuthority());
129: assertEquals("ROLE_TWO", castResult.getAuthorities()[1]
130: .getAuthority());
131: }
132:
133: public void testAuthenticationSuccessWhenFirstProviderReturnsNullButSecondAuthenticates() {
134: TestingAuthenticationToken token = new TestingAuthenticationToken(
135: "Test", "Password", new GrantedAuthority[] {
136: new GrantedAuthorityImpl("ROLE_ONE"),
137: new GrantedAuthorityImpl("ROLE_TWO") });
138:
139: ProviderManager mgr = makeProviderManagerWithMockProviderWhichReturnsNullInList();
140: mgr
141: .setApplicationEventPublisher(new MockApplicationEventPublisher(
142: true));
143:
144: Authentication result = mgr.authenticate(token);
145:
146: if (!(result instanceof TestingAuthenticationToken)) {
147: fail("Should have returned instance of TestingAuthenticationToken");
148: }
149:
150: TestingAuthenticationToken castResult = (TestingAuthenticationToken) result;
151: assertEquals("Test", castResult.getPrincipal());
152: assertEquals("Password", castResult.getCredentials());
153: assertEquals("ROLE_ONE", castResult.getAuthorities()[0]
154: .getAuthority());
155: assertEquals("ROLE_TWO", castResult.getAuthorities()[1]
156: .getAuthority());
157: }
158:
159: public void testConcurrentSessionControllerConfiguration()
160: throws Exception {
161: ProviderManager target = new ProviderManager();
162:
163: //The NullConcurrentSessionController should be the default
164: assertNotNull(target.getSessionController());
165: assertTrue(target.getSessionController() instanceof NullConcurrentSessionController);
166:
167: ConcurrentSessionControllerImpl impl = new ConcurrentSessionControllerImpl();
168: target.setSessionController(impl);
169: assertEquals(impl, target.getSessionController());
170: }
171:
172: public void testStartupFailsIfProviderListDoesNotContainingProviders()
173: throws Exception {
174: List providers = new Vector();
175: providers.add("THIS_IS_NOT_A_PROVIDER");
176:
177: ProviderManager mgr = new ProviderManager();
178:
179: try {
180: mgr.setProviders(providers);
181: fail("Should have thrown IllegalArgumentException");
182: } catch (IllegalArgumentException expected) {
183: assertTrue(true);
184: }
185: }
186:
187: public void testStartupFailsIfProviderListNotSet() throws Exception {
188: ProviderManager mgr = new ProviderManager();
189:
190: try {
191: mgr.afterPropertiesSet();
192: fail("Should have thrown IllegalArgumentException");
193: } catch (IllegalArgumentException expected) {
194: assertTrue(true);
195: }
196: }
197:
198: public void testStartupFailsIfProviderListNull() throws Exception {
199: ProviderManager mgr = new ProviderManager();
200:
201: try {
202: mgr.setProviders(null);
203: fail("Should have thrown IllegalArgumentException");
204: } catch (IllegalArgumentException expected) {
205: assertTrue(true);
206: }
207: }
208:
209: public void testSuccessfulStartup() throws Exception {
210: ProviderManager mgr = makeProviderManager();
211: mgr.afterPropertiesSet();
212: assertTrue(true);
213: assertEquals(1, mgr.getProviders().size());
214: }
215:
216: //~ Inner Classes ==================================================================================================
217:
218: private class MockApplicationEventPublisher implements
219: ApplicationEventPublisher {
220: private boolean expectedEvent;
221:
222: public MockApplicationEventPublisher(boolean expectedEvent) {
223: this .expectedEvent = expectedEvent;
224: }
225:
226: public void publishEvent(ApplicationEvent event) {
227: if (expectedEvent == false) {
228: throw new IllegalStateException(
229: "The ApplicationEventPublisher did not expect to receive this event");
230: }
231: }
232: }
233:
234: private class MockProvider implements AuthenticationProvider {
235: public Authentication authenticate(Authentication authentication)
236: throws AuthenticationException {
237: if (supports(authentication.getClass())) {
238: return authentication;
239: } else {
240: throw new AuthenticationServiceException(
241: "Don't support this class");
242: }
243: }
244:
245: public boolean supports(Class authentication) {
246: if (TestingAuthenticationToken.class
247: .isAssignableFrom(authentication)) {
248: return true;
249: } else {
250: return false;
251: }
252: }
253: }
254:
255: private class MockProviderWhichReturnsNull implements
256: AuthenticationProvider {
257: public Authentication authenticate(Authentication authentication)
258: throws AuthenticationException {
259: if (supports(authentication.getClass())) {
260: return null;
261: } else {
262: throw new AuthenticationServiceException(
263: "Don't support this class");
264: }
265: }
266:
267: public boolean supports(Class authentication) {
268: if (TestingAuthenticationToken.class
269: .isAssignableFrom(authentication)) {
270: return true;
271: } else {
272: return false;
273: }
274: }
275: }
276: }
|