View Javadoc

1   /*
2    * $Id: MockPrincipal.java 471754 2006-11-06 14:55:09Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.mock;
22  
23  import java.security.Principal;
24  
25  /**
26   * <p>Mock <strong>Principal</strong> object for low-level unit tests of
27   * Struts controller components.  Coarser grained tests should be implemented
28   * in terms of the Cactus framework, instead of the mock object classes.</p>
29   *
30   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
31   * create unit tests is provided, plus additional methods to configure this
32   * object as necessary.  Methods for unsupported operations will throw
33   * <code>UnsupportedOperationException</code>.</p>
34   *
35   * <p><strong>WARNING</strong> - Because unit tests operate in a single
36   * threaded environment, no synchronization is performed.</p>
37   *
38   * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
39   *          $
40   */
41  public class MockPrincipal implements Principal {
42      protected String name = null;
43      protected String[] roles = null;
44  
45      public MockPrincipal() {
46          super();
47          this.name = "";
48          this.roles = new String[0];
49      }
50  
51      public MockPrincipal(String name) {
52          super();
53          this.name = name;
54          this.roles = new String[0];
55      }
56  
57      public MockPrincipal(String name, String[] roles) {
58          super();
59          this.name = name;
60          this.roles = roles;
61      }
62  
63      public String getName() {
64          return (this.name);
65      }
66  
67      public boolean isUserInRole(String role) {
68          for (int i = 0; i < roles.length; i++) {
69              if (role.equals(roles[i])) {
70                  return (true);
71              }
72          }
73  
74          return (false);
75      }
76  
77      public boolean equals(Object o) {
78          if (o == null) {
79              return (false);
80          }
81  
82          if (!(o instanceof Principal)) {
83              return (false);
84          }
85  
86          Principal p = (Principal) o;
87  
88          if (name == null) {
89              return (p.getName() == null);
90          } else {
91              return (name.equals(p.getName()));
92          }
93      }
94  
95      public int hashCode() {
96          if (name == null) {
97              return ("".hashCode());
98          } else {
99              return (name.hashCode());
100         }
101     }
102 }