1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }