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.UserMap;
021: import org.acegisecurity.userdetails.memory.UserMapEditor;
022:
023: /**
024: * Tests {@link UserMapEditor}.
025: *
026: * @author Ben Alex
027: * @version $Id: UserMapEditorTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class UserMapEditorTests extends TestCase {
030: //~ Constructors ===================================================================================================
031:
032: public UserMapEditorTests() {
033: super ();
034: }
035:
036: public UserMapEditorTests(String arg0) {
037: super (arg0);
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(UserMapEditorTests.class);
044: }
045:
046: public final void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: public void testConvertedIntoUserSuccessfullyWhenDisabled() {
051: UserMapEditor editor = new UserMapEditor();
052: editor.setAsText("marissa=koala,ROLE_ONE,ROLE_TWO,disabled");
053:
054: UserMap map = (UserMap) editor.getValue();
055: assertTrue(!map.getUser("marissa").isEnabled());
056: }
057:
058: public void testConvertedIntoUserSuccessfullyWhenEnabled() {
059: UserMapEditor editor = new UserMapEditor();
060: editor.setAsText("marissa=koala,ROLE_ONE,ROLE_TWO");
061:
062: UserMap map = (UserMap) editor.getValue();
063: assertEquals("marissa", map.getUser("marissa").getUsername());
064: assertEquals("koala", map.getUser("marissa").getPassword());
065: assertEquals("ROLE_ONE", map.getUser("marissa")
066: .getAuthorities()[0].getAuthority());
067: assertEquals("ROLE_TWO", map.getUser("marissa")
068: .getAuthorities()[1].getAuthority());
069: assertTrue(map.getUser("marissa").isEnabled());
070: }
071:
072: public void testEmptyStringReturnsEmptyMap() {
073: UserMapEditor editor = new UserMapEditor();
074: editor.setAsText("");
075:
076: UserMap map = (UserMap) editor.getValue();
077: assertEquals(0, map.getUserCount());
078: }
079:
080: public void testMalformedStringReturnsEmptyMap() {
081: UserMapEditor editor = new UserMapEditor();
082: editor.setAsText("MALFORMED_STRING");
083:
084: UserMap map = (UserMap) editor.getValue();
085: assertEquals(0, map.getUserCount());
086: }
087:
088: public void testMultiUserParsing() {
089: UserMapEditor editor = new UserMapEditor();
090: editor
091: .setAsText("marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
092:
093: UserMap map = (UserMap) editor.getValue();
094: assertEquals("marissa", map.getUser("marissa").getUsername());
095: assertEquals("scott", map.getUser("scott").getUsername());
096: }
097:
098: public void testNullReturnsEmptyMap() {
099: UserMapEditor editor = new UserMapEditor();
100: editor.setAsText(null);
101:
102: UserMap map = (UserMap) editor.getValue();
103: assertEquals(0, map.getUserCount());
104: }
105: }
|