1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.web;
17  
18  
19  import java.security.Principal;
20  
21  
22  /***
23   * <p>Mock <strong>Principal</strong> object for low-level unit tests.</p>
24   */
25  
26  public class MockPrincipal implements Principal {
27  
28  
29      public MockPrincipal() {
30          super();
31          this.name = "";
32          this.roles = new String[0];
33      }
34  
35  
36      public MockPrincipal(String name) {
37          super();
38          this.name = name;
39          this.roles = new String[0];
40      }
41  
42  
43      public MockPrincipal(String name, String roles[]) {
44          super();
45          this.name = name;
46          this.roles = roles;
47      }
48  
49  
50      protected String name = null;
51  
52  
53      protected String roles[] = null;
54  
55  
56      public String getName() {
57          return (this.name);
58      }
59  
60  
61      public boolean isUserInRole(String role) {
62          for (int i = 0; i < roles.length; i++) {
63              if (role.equals(roles[i])) {
64                  return (true);
65              }
66          }
67          return (false);
68      }
69  
70  
71      public boolean equals(Object o) {
72          if (o == null) {
73              return (false);
74          }
75          if (!(o instanceof Principal)) {
76              return (false);
77          }
78          Principal p = (Principal) o;
79          if (name == null) {
80              return (p.getName() == null);
81          } else {
82              return (name.equals(p.getName()));
83          }
84      }
85  
86  
87      public int hashCode() {
88          if (name == null) {
89              return ("".hashCode());
90          } else {
91              return (name.hashCode());
92          }
93      }
94  
95  
96  }