1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.providers.dao.salt;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.AuthenticationServiceException;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.GrantedAuthorityImpl;
23  import org.acegisecurity.userdetails.User;
24  import org.acegisecurity.userdetails.UserDetails;
25  
26  
27  /***
28   * Tests {@link ReflectionSaltSource}.
29   *
30   * @author Ben Alex
31   * @version $Id: ReflectionSaltSourceTests.java,v 1.8 2005/11/29 13:10:11 benalex Exp $
32   */
33  public class ReflectionSaltSourceTests extends TestCase {
34      //~ Constructors ===========================================================
35  
36      public ReflectionSaltSourceTests() {
37          super();
38      }
39  
40      public ReflectionSaltSourceTests(String arg0) {
41          super(arg0);
42      }
43  
44      //~ Methods ================================================================
45  
46      public final void setUp() throws Exception {
47          super.setUp();
48      }
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(ReflectionSaltSourceTests.class);
52      }
53  
54      public void testDetectsMissingUserPropertyToUse() throws Exception {
55          ReflectionSaltSource saltSource = new ReflectionSaltSource();
56  
57          try {
58              saltSource.afterPropertiesSet();
59              fail("Should have thrown IllegalArgumentException");
60          } catch (IllegalArgumentException expected) {
61              assertEquals("A userPropertyToUse must be set",
62                  expected.getMessage());
63          }
64      }
65  
66      public void testExceptionWhenInvalidPropertyRequested() {
67          ReflectionSaltSource saltSource = new ReflectionSaltSource();
68          saltSource.setUserPropertyToUse("getDoesNotExist");
69  
70          UserDetails user = new User("scott", "wombat", true, true, true, true,
71                  new GrantedAuthority[] {new GrantedAuthorityImpl("HOLDER")});
72  
73          try {
74              saltSource.getSalt(user);
75              fail("Should have thrown AuthenticationServiceException");
76          } catch (AuthenticationServiceException expected) {
77              assertTrue(true);
78          }
79      }
80  
81      public void testGettersSetters() {
82          ReflectionSaltSource saltSource = new ReflectionSaltSource();
83          saltSource.setUserPropertyToUse("getUsername");
84          assertEquals("getUsername", saltSource.getUserPropertyToUse());
85      }
86  
87      public void testNormalOperation() throws Exception {
88          ReflectionSaltSource saltSource = new ReflectionSaltSource();
89          saltSource.setUserPropertyToUse("getUsername");
90          saltSource.afterPropertiesSet();
91  
92          UserDetails user = new User("scott", "wombat", true, true, true, true,
93                  new GrantedAuthority[] {new GrantedAuthorityImpl("HOLDER")});
94          assertEquals("scott", saltSource.getSalt(user));
95      }
96  }