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