Clover coverage report - jMock - 1.0-beta1
Coverage timestamp: Sat Nov 29 2003 19:35:59 GMT
file stats: LOC: 92   Methods: 5
NCLOC: 42   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
Verifier.java 100% 94.4% 100% 96.6%
coverage coverage
 1   
 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
 2   
 package org.jmock.expectation;
 3   
 
 4   
 import junit.framework.Assert;
 5   
 
 6   
 import java.lang.reflect.Field;
 7   
 import java.util.Vector;
 8   
 
 9   
 /**
 10   
  * Helper class to verify all {@link org.jmock.expectation.Expectation Expectation}s
 11   
  * of an object.
 12   
  * The {@link org.jmock.expectation.Verifier Verifier} class provides two static
 13   
  * methods to verify objects:
 14   
  * <ul>
 15   
  * <li>{@link org.jmock.expectation.Verifier#verifyObject(java.lang.Object) verifyObject(Object)}</li>
 16   
  * <li>{@link Verifier#verifyField(Field,Object,Vector) verifyField(Field, Object)}</li>
 17   
  * </ul>
 18   
  * These two methods can be used to verify any expectation to assert that
 19   
  * they still hold.<p>
 20   
  * <b>Example usage:</b><p>
 21   
  * Verifying all expectations on one object at a time:<p>
 22   
  * <pre>
 23   
  * public class MockX implements Verifiable {
 24   
  *    private Expectation... anExpectation = new Expectation...(...);
 25   
  *    private Expectation... aSecondExpectation = new Expectation...(...);
 26   
  * <p/>
 27   
  *    public void verify() {
 28   
  *       Verifier.verifyObject(this);
 29   
  *    }
 30   
  * }
 31   
  * </pre>
 32   
  * This example shows how most mocks implement
 33   
  * {@link org.jmock.expectation.Verifiable Verifiable}, i.e.:  by delegation.
 34   
  * 
 35   
  * @version $Id: Verifier.java,v 1.5 2002/09/29 16:44:28 smgf Exp $
 36   
  * @see org.jmock.expectation.Expectation
 37   
  * @see org.jmock.expectation.Verifiable
 38   
  */
 39   
 public class Verifier {
 40   
 
 41   
     private static Vector myProcessingObjects = new Vector();
 42   
 
 43   
     /**
 44   
      * Verifies all the fields of type Verifiable in the given object, including
 45   
      * those inherited from superclasses.
 46   
      * 
 47   
      * @param anObject The object to be verified.
 48   
      */
 49  76
     static synchronized public void verifyObject(Object anObject) {
 50  76
         verifyFieldsForClass(anObject, anObject.getClass(), myProcessingObjects);
 51   
     }
 52   
 
 53  214
     static private void verifyFieldsForClass(Object anObject, Class aClass, Vector alreadyProcessed) {
 54  214
         if (alreadyProcessed.contains(anObject) || isBaseObjectClass(aClass)) {
 55  76
             return;
 56   
         }
 57   
 
 58  138
         verifyFieldsForClass(anObject, aClass.getSuperclass(), alreadyProcessed);
 59  136
         try {
 60  136
             alreadyProcessed.addElement(anObject);
 61   
 
 62  136
             Field[] fields = aClass.getDeclaredFields();
 63  136
             for (int i = 0; i < fields.length; ++i) {
 64  388
                 verifyField(fields[i], anObject, alreadyProcessed);
 65   
             }
 66   
         } finally {
 67  136
             alreadyProcessed.removeElement(anObject);
 68   
         }
 69   
     }
 70   
 
 71  388
     static private void verifyField(Field aField, Object anObject, Vector alreadyProcessed) {
 72  388
         try {
 73  388
             aField.setAccessible(true);
 74  388
             Object fieldObject = aField.get(anObject);
 75   
 
 76  388
             if (isVerifiable(fieldObject) && !alreadyProcessed.contains(fieldObject)) {
 77  150
                 ((Verifiable) fieldObject).verify();
 78   
             }
 79   
         } catch (IllegalAccessException e) {
 80  0
             Assert.fail("Could not access field " + aField.getName());
 81   
         }
 82   
     }
 83   
 
 84  388
     private static boolean isVerifiable(Object anObject) {
 85  388
         return anObject instanceof Verifiable;
 86   
     }
 87   
 
 88  214
     private static boolean isBaseObjectClass(Class aClass) {
 89  214
         return aClass.equals(Object.class);
 90   
     }
 91   
 }
 92