|
From: <fra...@ya...> - 2002-03-27 05:50:10
|
Hi all !
Here is some documentation for the above mentionned class. I also refactored a
bit. The tests still run, so no problem there :) I created two query methods:
isRootClass(Class) and isVerifiable(Object). The methods are
self-explanatory, but they are documented anyway, as requested in the Code
section of the ToDo page.
You will notice that most lines have been touched. This is because I use
IntelliJ's IdeaJ, and I have reformatted the source code according to the
coding conventions.
Would someone create a blank "package.html" in com.mockobjects.util and
com.mockobjects ? This would allow me to diff against them to send my
submissions.
I do have one question though: Should the license be copied verbatim from the
license page, or should a reference to it be made ? I copied the license, as I
thought that this would be the best way to show the information. Please advise
on how to proceed for this.
Thanks !
Francois Beausoleil
Index: Verifier.java
===================================================================
RCS file:
/cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/Verifier.java,v
retrieving revision 1.1
diff -u -r1.1 Verifier.java
--- Verifier.java 29 Jul 2001 19:50:24 -0000 1.1
+++ Verifier.java 27 Mar 2002 05:47:32 -0000
@@ -1,41 +1,176 @@
package com.mockobjects.util;
-import junit.framework.AssertionFailedError;
+import com.mockobjects.Verifiable;
+
import java.lang.reflect.Field;
-import com.mockobjects.*;
+/**
+ * Helper class to verify all {@link com.mockobjects.Expectation Expectation}s
+ * of an object.
+ * The {@link com.mockobjects.util.Verifier Verifier} class provides two
static
+ * 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>
+ * </ul>
+ * These two methods can be used to verify any expectation to assert that
+ * they still hold.<p>
+ * <b>Example usage:</b><p>
+ * Verifying all expectations on one object at a time:<p>
+ * <pre>
+ * public class MockX implements Verifiable {
+ * private Expectation... anExpectation = new Expectation...(...);
+ * private Expectation... aSecondExpectation = new Expectation...(...);
+ *
+ * public void verify() {
+ * Verifier.verifyObject(this);
+ * }
+ * }
+ * </pre>
+ * This example shows how most mocks implement
+ * {@link com.mockobjects.Verifiable Verifiable}, i.e.: by delegation.
+ * <pre>
+ * ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" must not be
+ * used to endorse or promote products derived from this software
+ * without prior written permission. For written permission, please
+ * contact ap...@ap....
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ * </pre>
+ * @see com.mockobjects.Expectation
+ * @see com.mockobjects.Verifiable
+ * @author <a href="mailto:fb...@us...">Francois Beausoleil
(fb...@us...)</a>
+ * @version $Id$
+ */
public class Verifier {
-
+ /**
+ * Public, no-op constructor.
+ * You should not need to instantiate this object, since all methods
+ * are <code>static</code>.
+ */
public Verifier() {
super();
}
- static public void verifyField(Field field, Object anObject) {
+ /**
+ * 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 {
- field.setAccessible(true);
- Object fieldObject = field.get(anObject);
- if (fieldObject instanceof Verifiable) {
- ((Verifiable) fieldObject).verify();
+ aField.setAccessible(true);
+ Object fieldObject = aField.get(anObject);
+
+ if (isVerifiable(fieldObject)) {
+ ((Verifiable)fieldObject).verify();
}
- } catch (IllegalAccessException ex) {
- throw new AssertionFailedError(
- "Could not access field: " + field.getName());
+ } catch (IllegalAccessException e) {
+ junit.framework.Assert.fail("Could not access field "
+ + aField.getName());
}
}
+ /**
+ * Recursively verifies all fields of the passed object.
+ * @param anObject The object to be verified.
+ */
static public void verifyObject(Object anObject) {
verifyObjectFromClass(anObject, anObject.getClass());
}
+ /**
+ * 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 (aClass.equals(Object.class)) {
+ if (isRootClass(aClass)) {
return;
}
+
verifyObjectFromClass(anObject, aClass.getSuperclass());
Field[] fields = aClass.getDeclaredFields();
for (int i = 0; i < fields.length; ++i) {
verifyField(fields[i], anObject);
}
+ }
+
+ /**
+ * 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) {
+ return aClass.equals(Object.class);
}
}
__________________________________________________________
Lèche-vitrine ou lèche-écran ?
magasinage.yahoo.ca
|