Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 152   Methods: 8
NCLOC: 63   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NamedEntityObjectIdentity.java 100% 100% 100% 100%
coverage
 1    /* Copyright 2004, 2005 Acegi Technology Pty Limited
 2    *
 3    * Licensed under the Apache License, Version 2.0 (the "License");
 4    * you may not use this file except in compliance with the License.
 5    * You may obtain a copy of the License at
 6    *
 7    * http://www.apache.org/licenses/LICENSE-2.0
 8    *
 9    * Unless required by applicable law or agreed to in writing, software
 10    * distributed under the License is distributed on an "AS IS" BASIS,
 11    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    * See the License for the specific language governing permissions and
 13    * limitations under the License.
 14    */
 15   
 16    package org.acegisecurity.acl.basic;
 17   
 18    import org.springframework.util.Assert;
 19   
 20    import java.lang.reflect.InvocationTargetException;
 21    import java.lang.reflect.Method;
 22   
 23   
 24    /**
 25    * Simple implementation of {@link AclObjectIdentity}.
 26    *
 27    * <P>
 28    * Uses <code>String</code>s to store the identity of the domain object
 29    * instance. Also offers a constructor that uses reflection to build the
 30    * identity information.
 31    * </p>
 32    */
 33    public class NamedEntityObjectIdentity implements AclObjectIdentity {
 34    //~ Instance fields ========================================================
 35   
 36    private String classname;
 37    private String id;
 38   
 39    //~ Constructors ===========================================================
 40   
 41  168 public NamedEntityObjectIdentity(String classname, String id) {
 42  168 Assert.hasText(classname, "classname required");
 43  166 Assert.hasText(id, "id required");
 44  164 this.classname = classname;
 45  164 this.id = id;
 46    }
 47   
 48  1 protected NamedEntityObjectIdentity() {
 49  1 throw new IllegalArgumentException("Cannot use default constructor");
 50    }
 51   
 52    /**
 53    * Creates the <code>NamedEntityObjectIdentity</code> based on the passed
 54    * object instance. The passed object must provide a <code>getId()</code>
 55    * method, otherwise an exception will be thrown.
 56    *
 57    * @param object the domain object instance to create an identity for
 58    *
 59    * @throws IllegalAccessException
 60    * @throws InvocationTargetException
 61    * @throws IllegalArgumentException
 62    */
 63  9 public NamedEntityObjectIdentity(Object object)
 64    throws IllegalAccessException, InvocationTargetException {
 65  9 Assert.notNull(object, "object cannot be null");
 66   
 67  8 this.classname = object.getClass().getName();
 68   
 69  8 Class clazz = object.getClass();
 70   
 71  8 try {
 72  8 Method method = clazz.getMethod("getId", new Class[] {});
 73  3 Object result = method.invoke(object, new Object[] {});
 74  3 this.id = result.toString();
 75    } catch (NoSuchMethodException nsme) {
 76  5 throw new IllegalArgumentException("Object of class '" + clazz
 77    + "' does not provide the required getId() method: " + object);
 78    }
 79    }
 80   
 81    //~ Methods ================================================================
 82   
 83    /**
 84    * Important so caching operates properly.
 85    *
 86    * <P>
 87    * Considers an object of the same class equal if it has the same
 88    * <code>classname</code> and <code>id</code> properties.
 89    * </p>
 90    *
 91    * @param arg0 object to compare
 92    *
 93    * @return <code>true</code> if the presented object matches this object
 94    */
 95  13 public boolean equals(Object arg0) {
 96  13 if (arg0 == null) {
 97  1 return false;
 98    }
 99   
 100  12 if (!(arg0 instanceof NamedEntityObjectIdentity)) {
 101  1 return false;
 102    }
 103   
 104  11 NamedEntityObjectIdentity other = (NamedEntityObjectIdentity) arg0;
 105   
 106  11 if (this.getId().equals(other.getId())
 107    && this.getClassname().equals(other.getClassname())) {
 108  10 return true;
 109    }
 110   
 111  1 return false;
 112    }
 113   
 114    /**
 115    * Indicates the classname portion of the object identity.
 116    *
 117    * @return the classname (never <code>null</code>)
 118    */
 119  82 public String getClassname() {
 120  82 return classname;
 121    }
 122   
 123    /**
 124    * Indicates the instance identity portion of the object identity.
 125    *
 126    * @return the instance identity (never <code>null</code>)
 127    */
 128  84 public String getId() {
 129  84 return id;
 130    }
 131   
 132    /**
 133    * Important so caching operates properly.
 134    *
 135    * @return the hash of the classname and id
 136    */
 137  15 public int hashCode() {
 138  15 StringBuffer sb = new StringBuffer();
 139  15 sb.append(this.classname).append(this.id);
 140   
 141  15 return sb.toString().hashCode();
 142    }
 143   
 144  63 public String toString() {
 145  63 StringBuffer sb = new StringBuffer();
 146  63 sb.append(this.getClass().getName()).append("[");
 147  63 sb.append("Classname: ").append(this.classname);
 148  63 sb.append("; Identity: ").append(this.id).append("]");
 149   
 150  63 return sb.toString();
 151    }
 152    }