KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > util > Verifier


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.util;
4
5 import java.lang.reflect.Field JavaDoc;
6 import java.util.Vector JavaDoc;
7 import junit.framework.Assert;
8 import org.jmock.core.Verifiable;
9
10
11 /**
12  * Helper class to verify all {@link org.jmock.core.Verifiable} fields of an object.
13  * <p><b>Example usage:</b><p>
14  * Verifying all expectations on one object at a time:<p>
15  * <pre>
16  * public class MockX implements Verifiable {
17  * private Expectation... anExpectation = new Expectation...(...);
18  * private Expectation... aSecondExpectation = new Expectation...(...);
19  * <p/>
20  * public void verify() {
21  * Verifier.verifyObject(this);
22  * }
23  * }
24  * </pre>
25  * This example shows how most mock objects implement
26  * {@link org.jmock.core.Verifiable Verifiable} by delegation.
27  *
28  * @version $Id: Verifier.java,v 1.6 2004/05/05 08:52:48 npryce Exp $
29  * @see org.jmock.core.Verifiable
30  */

31 public class Verifier
32 {
33
34     private static Vector JavaDoc myProcessingObjects = new Vector JavaDoc();
35
36     /**
37      * Verifies all the fields of type Verifiable in the given object, including
38      * those inherited from superclasses.
39      *
40      * @param anObject The object to be verified.
41      */

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