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.dao.salt;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AuthenticationServiceException;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023:
024: import org.acegisecurity.userdetails.User;
025: import org.acegisecurity.userdetails.UserDetails;
026:
027: /**
028: * Tests {@link ReflectionSaltSource}.
029: *
030: * @author Ben Alex
031: * @version $Id: ReflectionSaltSourceTests.java 1496 2006-05-23 13:38:33Z benalex $
032: */
033: public class ReflectionSaltSourceTests extends TestCase {
034: //~ Constructors ===================================================================================================
035:
036: public ReflectionSaltSourceTests() {
037: super ();
038: }
039:
040: public ReflectionSaltSourceTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(ReflectionSaltSourceTests.class);
048: }
049:
050: public final void setUp() throws Exception {
051: super .setUp();
052: }
053:
054: public void testDetectsMissingUserPropertyToUse() throws Exception {
055: ReflectionSaltSource saltSource = new ReflectionSaltSource();
056:
057: try {
058: saltSource.afterPropertiesSet();
059: fail("Should have thrown IllegalArgumentException");
060: } catch (IllegalArgumentException expected) {
061: assertEquals("A userPropertyToUse must be set", expected
062: .getMessage());
063: }
064: }
065:
066: public void testExceptionWhenInvalidPropertyRequested() {
067: ReflectionSaltSource saltSource = new ReflectionSaltSource();
068: saltSource.setUserPropertyToUse("getDoesNotExist");
069:
070: UserDetails user = new User("scott", "wombat", true, true,
071: true, true,
072: new GrantedAuthority[] { new GrantedAuthorityImpl(
073: "HOLDER") });
074:
075: try {
076: saltSource.getSalt(user);
077: fail("Should have thrown AuthenticationServiceException");
078: } catch (AuthenticationServiceException expected) {
079: assertTrue(true);
080: }
081: }
082:
083: public void testGettersSetters() {
084: ReflectionSaltSource saltSource = new ReflectionSaltSource();
085: saltSource.setUserPropertyToUse("getUsername");
086: assertEquals("getUsername", saltSource.getUserPropertyToUse());
087: }
088:
089: public void testNormalOperation() throws Exception {
090: ReflectionSaltSource saltSource = new ReflectionSaltSource();
091: saltSource.setUserPropertyToUse("getUsername");
092: saltSource.afterPropertiesSet();
093:
094: UserDetails user = new User("scott", "wombat", true, true,
095: true, true,
096: new GrantedAuthority[] { new GrantedAuthorityImpl(
097: "HOLDER") });
098: assertEquals("scott", saltSource.getSalt(user));
099: }
100: }
|