001: /*
002: * $Id: MockPrincipal.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.mock;
022:
023: import java.security.Principal;
024:
025: /**
026: * <p>Mock <strong>Principal</strong> object for low-level unit tests of
027: * Struts controller components. Coarser grained tests should be implemented
028: * in terms of the Cactus framework, instead of the mock object classes.</p>
029: *
030: * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
031: * create unit tests is provided, plus additional methods to configure this
032: * object as necessary. Methods for unsupported operations will throw
033: * <code>UnsupportedOperationException</code>.</p>
034: *
035: * <p><strong>WARNING</strong> - Because unit tests operate in a single
036: * threaded environment, no synchronization is performed.</p>
037: *
038: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
039: * $
040: */
041: public class MockPrincipal implements Principal {
042: protected String name = null;
043: protected String[] roles = null;
044:
045: public MockPrincipal() {
046: super ();
047: this .name = "";
048: this .roles = new String[0];
049: }
050:
051: public MockPrincipal(String name) {
052: super ();
053: this .name = name;
054: this .roles = new String[0];
055: }
056:
057: public MockPrincipal(String name, String[] roles) {
058: super ();
059: this .name = name;
060: this .roles = roles;
061: }
062:
063: public String getName() {
064: return (this .name);
065: }
066:
067: public boolean isUserInRole(String role) {
068: for (int i = 0; i < roles.length; i++) {
069: if (role.equals(roles[i])) {
070: return (true);
071: }
072: }
073:
074: return (false);
075: }
076:
077: public boolean equals(Object o) {
078: if (o == null) {
079: return (false);
080: }
081:
082: if (!(o instanceof Principal)) {
083: return (false);
084: }
085:
086: Principal p = (Principal) o;
087:
088: if (name == null) {
089: return (p.getName() == null);
090: } else {
091: return (name.equals(p.getName()));
092: }
093: }
094:
095: public int hashCode() {
096: if (name == null) {
097: return ("".hashCode());
098: } else {
099: return (name.hashCode());
100: }
101: }
102: }
|