View Javadoc
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 static synchronized public void verifyObject(Object anObject) { 50 verifyFieldsForClass(anObject, anObject.getClass(), myProcessingObjects); 51 } 52 53 static private void verifyFieldsForClass(Object anObject, Class aClass, Vector alreadyProcessed) { 54 if (alreadyProcessed.contains(anObject) || isBaseObjectClass(aClass)) { 55 return; 56 } 57 58 verifyFieldsForClass(anObject, aClass.getSuperclass(), alreadyProcessed); 59 try { 60 alreadyProcessed.addElement(anObject); 61 62 Field[] fields = aClass.getDeclaredFields(); 63 for (int i = 0; i < fields.length; ++i) { 64 verifyField(fields[i], anObject, alreadyProcessed); 65 } 66 } finally { 67 alreadyProcessed.removeElement(anObject); 68 } 69 } 70 71 static private void verifyField(Field aField, Object anObject, Vector alreadyProcessed) { 72 try { 73 aField.setAccessible(true); 74 Object fieldObject = aField.get(anObject); 75 76 if (isVerifiable(fieldObject) && !alreadyProcessed.contains(fieldObject)) { 77 ((Verifiable) fieldObject).verify(); 78 } 79 } catch (IllegalAccessException e) { 80 Assert.fail("Could not access field " + aField.getName()); 81 } 82 } 83 84 private static boolean isVerifiable(Object anObject) { 85 return anObject instanceof Verifiable; 86 } 87 88 private static boolean isBaseObjectClass(Class aClass) { 89 return aClass.equals(Object.class); 90 } 91 }

This page was automatically generated by Maven