Coverage report

  %line %branch
org.apache.turbine.util.security.RoleSet
52% 
83% 

 1  
 package org.apache.turbine.util.security;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-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  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 
 24  
 import org.apache.turbine.om.security.Role;
 25  
 
 26  
 /**
 27  
  * This class represents a set of Roles.  It makes it easy to build a
 28  
  * UI that would allow someone to add a group of Roles to a User.
 29  
  * It enforces that only Role objects are
 30  
  * allowed in the set and only relevant methods are available.
 31  
  *
 32  
  * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
 33  
  * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
 34  
  * @author <a href="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
 35  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 36  
  * @version $Id: RoleSet.java,v 1.9.2.2 2004/05/20 03:27:24 seade Exp $
 37  
  */
 38  
 public class RoleSet
 39  
         extends SecuritySet
 40  
 {
 41  
     /**
 42  
      * Constructs an empty RoleSet
 43  
      */
 44  
     public RoleSet()
 45  
     {
 46  18
         super();
 47  18
     }
 48  
 
 49  
     /**
 50  
      * Constructs a new RoleSet with specified contents.
 51  
      *
 52  
      * If the given collection contains multiple objects that are
 53  
      * identical WRT equals() method, some objects will be overwritten.
 54  
      *
 55  
      * @param roles A collection of roles to be contained in the set.
 56  
      */
 57  
     public RoleSet(Collection roles)
 58  
     {
 59  33
         super();
 60  33
         add(roles);
 61  33
     }
 62  
 
 63  
     /**
 64  
      * Adds a Role to this RoleSet.
 65  
      *
 66  
      * @param role A Role.
 67  
      * @return True if Role was added; false if RoleSet already
 68  
      * contained the Role.
 69  
      */
 70  
     public boolean add(Role role)
 71  
     {
 72  87
         boolean res = contains(role);
 73  87
         nameMap.put(role.getName(), role);
 74  87
         idMap.put(role.getIdAsObj(), role);
 75  87
         return res;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Adds the Roles in a Collection to this RoleSet.
 80  
      *
 81  
      * @param roles A Collection of Roles.
 82  
      * @return True if this RoleSet changed as a result; false
 83  
      * if no change to this RoleSet occurred (this RoleSet
 84  
      * already contained all members of the added RoleSet).
 85  
      */
 86  
     public boolean add(Collection roles)
 87  
     {
 88  33
         boolean res = false;
 89  33
         for (Iterator it = roles.iterator(); it.hasNext();)
 90  
         {
 91  75
             Role r = (Role) it.next();
 92  75
             res |= add(r);
 93  
         }
 94  33
         return res;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Adds the Roles in another RoleSet to this RoleSet.
 99  
      *
 100  
      * @param roleSet A RoleSet.
 101  
      * @return True if this RoleSet changed as a result; false
 102  
      * if no change to this RoleSet occurred (this RoleSet
 103  
      * already contained all members of the added RoleSet).
 104  
      */
 105  
     public boolean add(RoleSet roleSet)
 106  
     {
 107  0
         boolean res = false;
 108  0
         for( Iterator it = roleSet.iterator(); it.hasNext();)
 109  
         {
 110  0
             Role r = (Role) it.next();
 111  0
             res |= add(r);
 112  
         }
 113  0
         return res;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Removes a Role from this RoleSet.
 118  
      *
 119  
      * @param role A Role.
 120  
      * @return True if this RoleSet contained the Role
 121  
      * before it was removed.
 122  
      */
 123  
     public boolean remove(Role role)
 124  
     {
 125  1
         boolean res = contains(role);
 126  1
         nameMap.remove(role.getName());
 127  1
         idMap.remove(role.getIdAsObj());
 128  1
         return res;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Checks whether this RoleSet contains a Role.
 133  
      *
 134  
      * @param role A Role.
 135  
      * @return True if this RoleSet contains the Role,
 136  
      * false otherwise.
 137  
      */
 138  
     public boolean contains(Role role)
 139  
     {
 140  102
         return nameMap.containsValue((Object) role);
 141  
     }
 142  
 
 143  
     /**
 144  
      * Returns a Role with the given name, if it is contained in
 145  
      * this RoleSet.
 146  
      *
 147  
      * @param roleName Name of Role.
 148  
      * @return Role if argument matched a Role in this
 149  
      * RoleSet; null if no match.
 150  
      * @deprecated Use <a href="#getRoleByName">getRoleByName</a> instead.
 151  
      */
 152  
     public Role getRole(String roleName)
 153  
     {
 154  0
         return getRoleByName(roleName);
 155  
     }
 156  
 
 157  
     /**
 158  
      * Returns a Role with the given name, if it is contained in
 159  
      * this RoleSet.
 160  
      *
 161  
      * @param roleName Name of Role.
 162  
      * @return Role if argument matched a Role in this
 163  
      * RoleSet; null if no match.
 164  
      */
 165  
     public Role getRoleByName(String roleName)
 166  
     {
 167  23
         return (StringUtils.isNotEmpty(roleName))
 168  
                 ? (Role) nameMap.get(roleName) : null;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Returns a Role with the given id, if it is contained in this
 173  
      * RoleSet.
 174  
      *
 175  
      * @param roleId id of the Role.
 176  
      * @return Role if argument matched a Role in this RoleSet; null
 177  
      * if no match.
 178  
      */
 179  
     public Role getRoleById(int roleId)
 180  
     {
 181  1
         return (roleId != 0) 
 182  
                 ? (Role) idMap.get(new Integer(roleId)) : null;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Returns an Array of Roles in this RoleSet.
 187  
      *
 188  
      * @return An Array of Role objects.
 189  
      */
 190  
     public Role[] getRolesArray()
 191  
     {
 192  0
         return (Role[]) getSet().toArray(new Role[0]);
 193  
     }
 194  
 
 195  
     /**
 196  
      * Print out a RoleSet as a String
 197  
      *
 198  
      * @returns The Role Set as String
 199  
      *
 200  
      */
 201  
     public String toString()
 202  
     {
 203  0
         StringBuffer sb = new StringBuffer();
 204  0
         sb.append("RoleSet: ");
 205  
 
 206  0
         for(Iterator it = iterator(); it.hasNext();)
 207  
         {
 208  0
             Role r = (Role) it.next();
 209  0
             sb.append('[');
 210  0
             sb.append(r.getName());
 211  0
             sb.append(" -> ");
 212  0
             sb.append(r.getIdAsObj());
 213  0
             sb.append(']');
 214  0
             if (it.hasNext())
 215  
             {
 216  0
                 sb.append(", ");
 217  
             }
 218  
         }
 219  
 
 220  0
         return sb.toString();
 221  
     }
 222  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.