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.userdetails.memory;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.userdetails.memory.UserAttribute;
021: import org.acegisecurity.userdetails.memory.UserAttributeEditor;
022:
023: /**
024: * Tests {@link UserAttributeEditor} and associated {@link UserAttribute}.
025: *
026: * @author Ben Alex
027: * @version $Id: UserAttributeEditorTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class UserAttributeEditorTests extends TestCase {
030: //~ Constructors ===================================================================================================
031:
032: public UserAttributeEditorTests() {
033: super ();
034: }
035:
036: public UserAttributeEditorTests(String arg0) {
037: super (arg0);
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(UserAttributeEditorTests.class);
044: }
045:
046: public final void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: public void testCorrectOperationWithTrailingSpaces() {
051: UserAttributeEditor editor = new UserAttributeEditor();
052: editor.setAsText("password ,ROLE_ONE,ROLE_TWO ");
053:
054: UserAttribute user = (UserAttribute) editor.getValue();
055: assertEquals("password", user.getPassword());
056: assertEquals(2, user.getAuthorities().length);
057: assertEquals("ROLE_ONE", user.getAuthorities()[0]
058: .getAuthority());
059: assertEquals("ROLE_TWO", user.getAuthorities()[1]
060: .getAuthority());
061: }
062:
063: public void testCorrectOperationWithoutEnabledDisabledKeyword() {
064: UserAttributeEditor editor = new UserAttributeEditor();
065: editor.setAsText("password,ROLE_ONE,ROLE_TWO");
066:
067: UserAttribute user = (UserAttribute) editor.getValue();
068: assertTrue(user.isValid());
069: assertTrue(user.isEnabled()); // default
070: assertEquals("password", user.getPassword());
071: assertEquals(2, user.getAuthorities().length);
072: assertEquals("ROLE_ONE", user.getAuthorities()[0]
073: .getAuthority());
074: assertEquals("ROLE_TWO", user.getAuthorities()[1]
075: .getAuthority());
076: }
077:
078: public void testDisabledKeyword() {
079: UserAttributeEditor editor = new UserAttributeEditor();
080: editor.setAsText("password,disabled,ROLE_ONE,ROLE_TWO");
081:
082: UserAttribute user = (UserAttribute) editor.getValue();
083: assertTrue(user.isValid());
084: assertTrue(!user.isEnabled());
085: assertEquals("password", user.getPassword());
086: assertEquals(2, user.getAuthorities().length);
087: assertEquals("ROLE_ONE", user.getAuthorities()[0]
088: .getAuthority());
089: assertEquals("ROLE_TWO", user.getAuthorities()[1]
090: .getAuthority());
091: }
092:
093: public void testEmptyStringReturnsNull() {
094: UserAttributeEditor editor = new UserAttributeEditor();
095: editor.setAsText("");
096:
097: UserAttribute user = (UserAttribute) editor.getValue();
098: assertTrue(user == null);
099: }
100:
101: public void testEnabledKeyword() {
102: UserAttributeEditor editor = new UserAttributeEditor();
103: editor.setAsText("password,ROLE_ONE,enabled,ROLE_TWO");
104:
105: UserAttribute user = (UserAttribute) editor.getValue();
106: assertTrue(user.isValid());
107: assertTrue(user.isEnabled());
108: assertEquals("password", user.getPassword());
109: assertEquals(2, user.getAuthorities().length);
110: assertEquals("ROLE_ONE", user.getAuthorities()[0]
111: .getAuthority());
112: assertEquals("ROLE_TWO", user.getAuthorities()[1]
113: .getAuthority());
114: }
115:
116: public void testMalformedStringReturnsNull() {
117: UserAttributeEditor editor = new UserAttributeEditor();
118: editor.setAsText("MALFORMED_STRING");
119:
120: UserAttribute user = (UserAttribute) editor.getValue();
121: assertTrue(user == null);
122: }
123:
124: public void testNoPasswordOrRolesReturnsNull() {
125: UserAttributeEditor editor = new UserAttributeEditor();
126: editor.setAsText("disabled");
127:
128: UserAttribute user = (UserAttribute) editor.getValue();
129: assertTrue(user == null);
130: }
131:
132: public void testNoRolesReturnsNull() {
133: UserAttributeEditor editor = new UserAttributeEditor();
134: editor.setAsText("password,enabled");
135:
136: UserAttribute user = (UserAttribute) editor.getValue();
137: assertTrue(user == null);
138: }
139:
140: public void testNullReturnsNull() {
141: UserAttributeEditor editor = new UserAttributeEditor();
142: editor.setAsText(null);
143:
144: UserAttribute user = (UserAttribute) editor.getValue();
145: assertTrue(user == null);
146: }
147: }
|