1   /* Copyright 2004 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.UserMap;
19  import org.acegisecurity.userdetails.memory.UserMapEditor;
20  
21  import junit.framework.TestCase;
22  
23  
24  /***
25   * Tests {@link UserMapEditor}.
26   *
27   * @author Ben Alex
28   * @version $Id: UserMapEditorTests.java,v 1.1 2005/11/29 13:10:10 benalex Exp $
29   */
30  public class UserMapEditorTests extends TestCase {
31      //~ Constructors ===========================================================
32  
33      public UserMapEditorTests() {
34          super();
35      }
36  
37      public UserMapEditorTests(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(UserMapEditorTests.class);
49      }
50  
51      public void testConvertedIntoUserSuccessfullyWhenDisabled() {
52          UserMapEditor editor = new UserMapEditor();
53          editor.setAsText("marissa=koala,ROLE_ONE,ROLE_TWO,disabled");
54  
55          UserMap map = (UserMap) editor.getValue();
56          assertTrue(!map.getUser("marissa").isEnabled());
57      }
58  
59      public void testConvertedIntoUserSuccessfullyWhenEnabled() {
60          UserMapEditor editor = new UserMapEditor();
61          editor.setAsText("marissa=koala,ROLE_ONE,ROLE_TWO");
62  
63          UserMap map = (UserMap) editor.getValue();
64          assertEquals("marissa", map.getUser("marissa").getUsername());
65          assertEquals("koala", map.getUser("marissa").getPassword());
66          assertEquals("ROLE_ONE",
67              map.getUser("marissa").getAuthorities()[0].getAuthority());
68          assertEquals("ROLE_TWO",
69              map.getUser("marissa").getAuthorities()[1].getAuthority());
70          assertTrue(map.getUser("marissa").isEnabled());
71      }
72  
73      public void testEmptyStringReturnsEmptyMap() {
74          UserMapEditor editor = new UserMapEditor();
75          editor.setAsText("");
76  
77          UserMap map = (UserMap) editor.getValue();
78          assertEquals(0, map.getUserCount());
79      }
80  
81      public void testMalformedStringReturnsEmptyMap() {
82          UserMapEditor editor = new UserMapEditor();
83          editor.setAsText("MALFORMED_STRING");
84  
85          UserMap map = (UserMap) editor.getValue();
86          assertEquals(0, map.getUserCount());
87      }
88  
89      public void testMultiUserParsing() {
90          UserMapEditor editor = new UserMapEditor();
91          editor.setAsText(
92              "marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
93  
94          UserMap map = (UserMap) editor.getValue();
95          assertEquals("marissa", map.getUser("marissa").getUsername());
96          assertEquals("scott", map.getUser("scott").getUsername());
97      }
98  
99      public void testNullReturnsEmptyMap() {
100         UserMapEditor editor = new UserMapEditor();
101         editor.setAsText(null);
102 
103         UserMap map = (UserMap) editor.getValue();
104         assertEquals(0, map.getUserCount());
105     }
106 }