1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi;
18  
19  import java.security.Principal;
20  import java.util.Iterator;
21  
22  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  /***
28   * <p>
29   * Unit testing for {@link UserSecurityHandler}.
30   * </p>
31   * 
32   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
33   */
34  public class TestUserSecurityHandler extends AbstractSecurityTestcase
35  {
36  
37      
38      /***
39       * @see junit.framework.TestCase#setUp()
40       */
41      protected void setUp() throws Exception
42      {
43          super.setUp();
44          destroyUsers();
45          initUsers();
46      }
47  
48      /***
49       * @see junit.framework.TestCase#tearDown()
50       */
51      public void tearDown() throws Exception
52      {
53          destroyUsers();
54          super.tearDown();
55      }
56  
57      /***
58       * <p>
59       * Constructs the suite.
60       * </p>
61       * 
62       * @return The {@Test}.
63       */
64      public static Test suite()
65      {
66          return new TestSuite(TestUserSecurityHandler.class);
67      }
68  
69      /***
70       * <p>
71       * Test <code>getUserPrincipal</code>.
72       * </p>
73       */
74      public void testGetUserPrincipal() throws Exception
75      {
76          Principal principal = ush.getUserPrincipal("testuser1");
77          assertNotNull(principal);
78          assertEquals("testuser1", principal.getName());
79      }
80      
81      /***
82       * <p>
83       * Test <code>getUserPrincipals</code>.
84       * </p>
85       */
86      public void testGetUserPrincipals() throws Exception
87      {
88          Iterator principals = ush.getUserPrincipals("").iterator();
89          boolean foundUser1 =false;
90          boolean foundUser2 =false;
91          
92          while (principals.hasNext())
93          {
94              Principal principal = (Principal) principals.next();
95              assertNotNull(principal);
96              
97              if (principal.getName().equals("testuser1"))
98              {                
99                  foundUser1 = true;
100             }
101             else if (principal.getName().equals("testuser2"))
102             {
103                foundUser2 = true;
104             }            
105         }
106         assertTrue(foundUser1 && foundUser2);
107     }
108 
109     /***
110      * <p>
111      * Initialize user test object.
112      * </p>
113      */
114     protected void initUsers() throws Exception
115     {
116         ums.addUser("testuser1", "password");
117         ums.addUser("testuser2", "password");
118     }
119 
120     /***
121      * <p>
122      * Destroy user test object.
123      * </p>
124      */
125     protected void destroyUsers() throws Exception
126     {
127         ums.removeUser("testuser1");
128         ums.removeUser("testuser2");
129     }
130 
131 }