KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > util > Verifier


1 package com.mockobjects.util;
2
3 import com.mockobjects.Verifiable;
4
5 import java.lang.reflect.Field JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import junit.framework.Assert;
9
10 /**
11  * Helper class to verify all {@link com.mockobjects.Expectation Expectation}s
12  * of an object.
13  * The {@link com.mockobjects.util.Verifier Verifier} class provides two static
14  * methods to verify objects:
15  * <ul>
16  * <li>{@link com.mockobjects.util.Verifier#verifyObject(java.lang.Object) verifyObject(Object)}</li>
17  * <li>{@link Verifier#verifyField(Field,Object,Vector) verifyField(Field, Object)}</li>
18  * </ul>
19  * These two methods can be used to verify any expectation to assert that
20  * they still hold.<p>
21  * <b>Example usage:</b><p>
22  * Verifying all expectations on one object at a time:<p>
23  * <pre>
24  * public class MockX implements Verifiable {
25  * private Expectation... anExpectation = new Expectation...(...);
26  * private Expectation... aSecondExpectation = new Expectation...(...);
27  *
28  * public void verify() {
29  * Verifier.verifyObject(this);
30  * }
31  * }
32  * </pre>
33  * This example shows how most mocks implement
34  * {@link com.mockobjects.Verifiable Verifiable}, i.e.: by delegation.
35  * @see com.mockobjects.Expectation
36  * @see com.mockobjects.Verifiable
37  * @version $Id: Verifier.java,v 1.5 2002/09/29 16:44:28 smgf Exp $
38  */

39 public class Verifier {
40
41     private static Vector JavaDoc myProcessingObjects = new Vector JavaDoc();
42
43     /**
44      * Verifies all the fields of type Verifiable in the given object, including
45      * those inherited from superclasses.
46      * @param anObject The object to be verified.
47      */

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