1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.mock;
19
20 import java.security.Principal;
21
22 /***
23 * <p>Mock <strong>Principal</strong> object for low-level unit tests of
24 * Struts controller components. Coarser grained tests should be implemented
25 * in terms of the Cactus framework, instead of the mock object classes.</p>
26 *
27 * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
28 * create unit tests is provided, plus additional methods to configure this
29 * object as necessary. Methods for unsupported operations will throw
30 * <code>UnsupportedOperationException</code>.</p>
31 *
32 * <p><strong>WARNING</strong> - Because unit tests operate in a single
33 * threaded environment, no synchronization is performed.</p>
34 *
35 * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
36 * $
37 */
38 public class MockPrincipal implements Principal {
39 protected String name = null;
40 protected String[] roles = null;
41
42 public MockPrincipal() {
43 super();
44 this.name = "";
45 this.roles = new String[0];
46 }
47
48 public MockPrincipal(String name) {
49 super();
50 this.name = name;
51 this.roles = new String[0];
52 }
53
54 public MockPrincipal(String name, String[] roles) {
55 super();
56 this.name = name;
57 this.roles = roles;
58 }
59
60 public String getName() {
61 return (this.name);
62 }
63
64 public boolean isUserInRole(String role) {
65 for (int i = 0; i < roles.length; i++) {
66 if (role.equals(roles[i])) {
67 return (true);
68 }
69 }
70
71 return (false);
72 }
73
74 public boolean equals(Object o) {
75 if (o == null) {
76 return (false);
77 }
78
79 if (!(o instanceof Principal)) {
80 return (false);
81 }
82
83 Principal p = (Principal) o;
84
85 if (name == null) {
86 return (p.getName() == null);
87 } else {
88 return (name.equals(p.getName()));
89 }
90 }
91
92 public int hashCode() {
93 if (name == null) {
94 return ("".hashCode());
95 } else {
96 return (name.hashCode());
97 }
98 }
99 }