|
From: Steve F. <sm...@us...> - 2002-09-29 16:44:32
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util
In directory usw-pr-cvs1:/tmp/cvs-serv20790/src/core/com/mockobjects/util
Modified Files:
Verifier.java
Log Message:
Some tidy-up
Initial refactorings towards avoiding static collection
Hid verifyField()
Index: Verifier.java
===================================================================
RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/Verifier.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Verifier.java 17 Sep 2002 14:51:11 -0000 1.4
+++ Verifier.java 29 Sep 2002 16:44:28 -0000 1.5
@@ -5,6 +5,8 @@
import java.lang.reflect.Field;
import java.util.Vector;
+import junit.framework.Assert;
+
/**
* Helper class to verify all {@link com.mockobjects.Expectation Expectation}s
* of an object.
@@ -12,7 +14,7 @@
* methods to verify objects:
* <ul>
* <li>{@link com.mockobjects.util.Verifier#verifyObject(java.lang.Object) verifyObject(Object)}</li>
- * <li>{@link com.mockobjects.util.Verifier#verifyField(java.lang.reflect.Field, java.lang.Object) verifyField(Field, Object)}</li>
+ * <li>{@link Verifier#verifyField(Field,Object,Vector) verifyField(Field, Object)}</li>
* </ul>
* These two methods can be used to verify any expectation to assert that
* they still hold.<p>
@@ -32,105 +34,58 @@
* {@link com.mockobjects.Verifiable Verifiable}, i.e.: by delegation.
* @see com.mockobjects.Expectation
* @see com.mockobjects.Verifiable
- * @author <a href="mailto:fb...@us...">Francois Beausoleil (fb...@us...)</a>
* @version $Id$
*/
public class Verifier {
private static Vector myProcessingObjects = new Vector();
- /**
- * Public, no-op constructor.
- * You should not need to instantiate this object, since all methods
- * are <code>static</code>.
- */
- public Verifier() {
- super();
- }
/**
- * Calls {@link com.mockobjects.Verifiable#verify() Verifiable.verify()}
- * on <code>aField</code>.
- * This method will result in a no-op if <code>aField</code> does not
- * implement {@link com.mockobjects.Verifiable Verifiable}.
- * @param aField The field which needs to be checked.
- * @param anObject The object in which <code>aField</code> is instantiated.
- */
- static public void verifyField(Field aField, Object anObject) {
- try {
- aField.setAccessible(true);
- Object fieldObject = aField.get(anObject);
-
- if (isVerifiable(fieldObject)) {
- if (!myProcessingObjects.contains(fieldObject)) {
- ((Verifiable)fieldObject).verify();
- }
- }
- } catch (IllegalAccessException e) {
- junit.framework.Assert.fail("Could not access field "
- + aField.getName());
- }
- }
-
- /**
- * Recursively verifies all fields of the passed object.
+ * Verifies all the fields of type Verifiable in the given object, including
+ * those inherited from superclasses.
* @param anObject The object to be verified.
*/
static synchronized public void verifyObject(Object anObject) {
- verifyObjectFromClass(anObject, anObject.getClass());
+ verifyFieldsForClass(anObject, anObject.getClass(), myProcessingObjects);
}
- /**
- * Verifies all fields of the passed object, as if it were of the
- * passed class.
- * This method will be called recursively for each superclass of
- * <code>anObject</code>, until the
- * {@link com.mockobjects.util.Verifier#isRootClass(java.lang.Class) isRootClass(Class)}
- * returns <code>true</code>, at which point the recursion will stop.
- * @param anObject The object on which to verify fields.
- * @param aClass The apparent class of <code>anObject</code>
- */
- static private void verifyObjectFromClass(Object anObject, Class aClass) {
- if (myProcessingObjects.contains(anObject)) {
- return;
- }
- if (isRootClass(aClass)) {
+ static private void verifyFieldsForClass(Object anObject, Class aClass, Vector alreadyProcessed) {
+ if (alreadyProcessed.contains(anObject) || isBaseObjectClass(aClass)) {
return;
}
- verifyObjectFromClass(anObject, aClass.getSuperclass());
+ verifyFieldsForClass(anObject, aClass.getSuperclass(), alreadyProcessed);
try {
- myProcessingObjects.addElement(anObject);
-
+ alreadyProcessed.addElement(anObject);
+
Field[] fields = aClass.getDeclaredFields();
for (int i = 0; i < fields.length; ++i) {
- verifyField(fields[i], anObject);
+ verifyField(fields[i], anObject, alreadyProcessed);
}
}
finally {
- myProcessingObjects.removeElement(anObject);
+ alreadyProcessed.removeElement(anObject);
+ }
+ }
+
+ static private void verifyField(Field aField, Object anObject, Vector alreadyProcessed) {
+ try {
+ aField.setAccessible(true);
+ Object fieldObject = aField.get(anObject);
+
+ if (isVerifiable(fieldObject) && ! alreadyProcessed.contains(fieldObject)) {
+ ((Verifiable)fieldObject).verify();
+ }
+ } catch (IllegalAccessException e) {
+ Assert.fail("Could not access field " + aField.getName());
}
}
- /**
- * Returns a <code>boolean</code> indicating whether or not the passed
- * object is {@link com.mockobjects.Verifiable Verifiable}.
- * @returns <code>true</code> if <code>anObject</code> implements
- * {@link com.mockobjects.Verifiable Verifiable}, <code>false</code>
- * otherwise.
- */
private static boolean isVerifiable(Object anObject) {
return anObject instanceof Verifiable;
}
- /**
- * Returns a <code>boolean</code> indicating whether or not the passed
- * class is the root class.
- * The root class is where recursive searching for fields should stop.
- * Usually, this would be {@link java.lang.Object}.
- * @returns <code>true</code> if <code>aClass</code> is the root class,
- * <code>false</code> otherwise.
- */
- private static boolean isRootClass(Class aClass) {
+ private static boolean isBaseObjectClass(Class aClass) {
return aClass.equals(Object.class);
}
}
|