KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > MockPrincipal


1 /*
2  * $Id: MockPrincipal.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.mock;
21
22
23 import java.security.Principal JavaDoc;
24
25
26 /**
27  * <p>Mock <strong>Principal</strong> object for low-level unit tests
28  * of Struts controller components. Coarser grained tests should be
29  * implemented in terms of the Cactus framework, instead of the mock
30  * object classes.</p>
31  *
32  * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
33  * create unit tests is provided, plus additional methods to configure this
34  * object as necessary. Methods for unsupported operations will throw
35  * <code>UnsupportedOperationException</code>.</p>
36  *
37  * <p><strong>WARNING</strong> - Because unit tests operate in a single
38  * threaded environment, no synchronization is performed.</p>
39  *
40  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
41  */

42
43 public class MockPrincipal implements Principal JavaDoc {
44
45
46     public MockPrincipal() {
47         super();
48         this.name = "";
49         this.roles = new String JavaDoc[0];
50     }
51
52
53     public MockPrincipal(String JavaDoc name) {
54         super();
55         this.name = name;
56         this.roles = new String JavaDoc[0];
57     }
58
59
60     public MockPrincipal(String JavaDoc name, String JavaDoc roles[]) {
61         super();
62         this.name = name;
63         this.roles = roles;
64     }
65
66
67     protected String JavaDoc name = null;
68
69
70     protected String JavaDoc roles[] = null;
71
72
73     public String JavaDoc getName() {
74         return (this.name);
75     }
76
77
78     public boolean isUserInRole(String JavaDoc role) {
79         for (int i = 0; i < roles.length; i++) {
80             if (role.equals(roles[i])) {
81                 return (true);
82             }
83         }
84         return (false);
85     }
86
87
88     public boolean equals(Object JavaDoc o) {
89         if (o == null) {
90             return (false);
91         }
92         if (!(o instanceof Principal JavaDoc)) {
93             return (false);
94         }
95         Principal JavaDoc p = (Principal JavaDoc) o;
96         if (name == null) {
97             return (p.getName() == null);
98         } else {
99             return (name.equals(p.getName()));
100         }
101     }
102
103
104     public int hashCode() {
105         if (name == null) {
106             return ("".hashCode());
107         } else {
108             return (name.hashCode());
109         }
110     }
111
112
113 }
114
Popular Tags