001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.web;
018    
019    
020    import java.security.Principal;
021    
022    
023    /**
024     * <p>Mock <strong>Principal</strong> object for low-level unit tests.</p>
025     */
026    
027    public class MockPrincipal implements Principal {
028    
029    
030        public MockPrincipal() {
031            super();
032            this.name = "";
033            this.roles = new String[0];
034        }
035    
036    
037        public MockPrincipal(String name) {
038            super();
039            this.name = name;
040            this.roles = new String[0];
041        }
042    
043    
044        public MockPrincipal(String name, String roles[]) {
045            super();
046            this.name = name;
047            this.roles = roles;
048        }
049    
050    
051        protected String name = null;
052    
053    
054        protected String roles[] = null;
055    
056    
057        public String getName() {
058            return (this.name);
059        }
060    
061    
062        public boolean isUserInRole(String role) {
063            for (int i = 0; i < roles.length; i++) {
064                if (role.equals(roles[i])) {
065                    return (true);
066                }
067            }
068            return (false);
069        }
070    
071    
072        public boolean equals(Object o) {
073            if (o == null) {
074                return (false);
075            }
076            if (!(o instanceof Principal)) {
077                return (false);
078            }
079            Principal p = (Principal) o;
080            if (name == null) {
081                return (p.getName() == null);
082            } else {
083                return (name.equals(p.getName()));
084            }
085        }
086    
087    
088        public int hashCode() {
089            if (name == null) {
090                return ("".hashCode());
091            } else {
092                return (name.hashCode());
093            }
094        }
095    
096    
097    }