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.userdetails.memory;
17  
18  import org.acegisecurity.userdetails.memory.UserAttribute;
19  import org.acegisecurity.userdetails.memory.UserAttributeEditor;
20  
21  import junit.framework.TestCase;
22  
23  
24  /***
25   * Tests {@link UserAttributeEditor} and associated {@link UserAttribute}.
26   *
27   * @author Ben Alex
28   * @version $Id: UserAttributeEditorTests.java,v 1.1 2005/11/29 13:10:10 benalex Exp $
29   */
30  public class UserAttributeEditorTests extends TestCase {
31      //~ Constructors ===========================================================
32  
33      public UserAttributeEditorTests() {
34          super();
35      }
36  
37      public UserAttributeEditorTests(String arg0) {
38          super(arg0);
39      }
40  
41      //~ Methods ================================================================
42  
43      public final void setUp() throws Exception {
44          super.setUp();
45      }
46  
47      public static void main(String[] args) {
48          junit.textui.TestRunner.run(UserAttributeEditorTests.class);
49      }
50  
51      public void testCorrectOperationWithoutEnabledDisabledKeyword() {
52          UserAttributeEditor editor = new UserAttributeEditor();
53          editor.setAsText("password,ROLE_ONE,ROLE_TWO");
54  
55          UserAttribute user = (UserAttribute) editor.getValue();
56          assertTrue(user.isValid());
57          assertTrue(user.isEnabled()); // default
58          assertEquals("password", user.getPassword());
59          assertEquals(2, user.getAuthorities().length);
60          assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
61          assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
62      }
63  
64      public void testDisabledKeyword() {
65          UserAttributeEditor editor = new UserAttributeEditor();
66          editor.setAsText("password,disabled,ROLE_ONE,ROLE_TWO");
67  
68          UserAttribute user = (UserAttribute) editor.getValue();
69          assertTrue(user.isValid());
70          assertTrue(!user.isEnabled());
71          assertEquals("password", user.getPassword());
72          assertEquals(2, user.getAuthorities().length);
73          assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
74          assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
75      }
76  
77      public void testEmptyStringReturnsNull() {
78          UserAttributeEditor editor = new UserAttributeEditor();
79          editor.setAsText("");
80  
81          UserAttribute user = (UserAttribute) editor.getValue();
82          assertTrue(user == null);
83      }
84  
85      public void testEnabledKeyword() {
86          UserAttributeEditor editor = new UserAttributeEditor();
87          editor.setAsText("password,ROLE_ONE,enabled,ROLE_TWO");
88  
89          UserAttribute user = (UserAttribute) editor.getValue();
90          assertTrue(user.isValid());
91          assertTrue(user.isEnabled());
92          assertEquals("password", user.getPassword());
93          assertEquals(2, user.getAuthorities().length);
94          assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
95          assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
96      }
97  
98      public void testMalformedStringReturnsNull() {
99          UserAttributeEditor editor = new UserAttributeEditor();
100         editor.setAsText("MALFORMED_STRING");
101 
102         UserAttribute user = (UserAttribute) editor.getValue();
103         assertTrue(user == null);
104     }
105 
106     public void testNoPasswordOrRolesReturnsNull() {
107         UserAttributeEditor editor = new UserAttributeEditor();
108         editor.setAsText("disabled");
109 
110         UserAttribute user = (UserAttribute) editor.getValue();
111         assertTrue(user == null);
112     }
113 
114     public void testNoRolesReturnsNull() {
115         UserAttributeEditor editor = new UserAttributeEditor();
116         editor.setAsText("password,enabled");
117 
118         UserAttribute user = (UserAttribute) editor.getValue();
119         assertTrue(user == null);
120     }
121 
122     public void testNullReturnsNull() {
123         UserAttributeEditor editor = new UserAttributeEditor();
124         editor.setAsText(null);
125 
126         UserAttribute user = (UserAttribute) editor.getValue();
127         assertTrue(user == null);
128     }
129 
130     public void testCorrectOperationWithTrailingSpaces() {
131         UserAttributeEditor editor = new UserAttributeEditor();
132         editor.setAsText("password ,ROLE_ONE,ROLE_TWO ");
133 
134         UserAttribute user = (UserAttribute) editor.getValue();
135         assertEquals("password", user.getPassword());
136         assertEquals(2, user.getAuthorities().length);
137         assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
138         assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
139     }
140 }