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 */
017package org.apache.commons.chain.web;
018
019import java.security.Principal;
020
021/**
022 * Mock <strong>Principal</strong> object for low-level unit tests.
023 */
024public class MockPrincipal implements Principal {
025
026    public MockPrincipal() {
027        this.name = "";
028        this.roles = new String[0];
029    }
030
031    public MockPrincipal(String name) {
032        this.name = name;
033        this.roles = new String[0];
034    }
035
036    public MockPrincipal(String name, String roles[]) {
037        this.name = name;
038        this.roles = roles;
039    }
040
041    protected String name = null;
042
043    protected String roles[] = null;
044
045    @Override
046    public String getName() {
047        return this.name;
048    }
049
050    public boolean isUserInRole(String role) {
051        for (String role2 : roles) {
052            if (role.equals(role2)) {
053                return true;
054            }
055        }
056        return false;
057    }
058
059    @Override
060    public boolean equals(Object o) {
061        if (!(o instanceof Principal)) {
062            return false;
063        }
064        Principal p = (Principal) o;
065        if (name == null) {
066            return p.getName() == null;
067        } else {
068            return name.equals(p.getName());
069        }
070    }
071
072    @Override
073    public int hashCode() {
074        if (name == null) {
075            return "".hashCode();
076        } else {
077            return name.hashCode();
078        }
079    }
080}