|
From: Jeff M. <je...@mk...> - 2002-03-27 17:01:58
|
Thanks, I'll try and get this in asap. No need to send diffs for new
files just send the file and I'll stick it in.
Not sure about the license think that's an outstanding issue.
On Wed, 2002-03-27 at 05:50, Francois Beausoleil wrote:
> Hi all !
>=20
> Here is some documentation for the above mentionned class. I also refact=
ored a
> bit. The tests still run, so no problem there :) I created two query me=
thods:
> isRootClass(Class) and isVerifiable(Object). The methods are
> self-explanatory, but they are documented anyway, as requested in the Cod=
e
> section of the ToDo page.
>=20
> 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.
>=20
> 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.
>=20
> I do have one question though: Should the license be copied verbatim fro=
m 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.
>=20
> Thanks !
> Francois Beausoleil
>=20
> Index: Verifier.java
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> RCS file:
> /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/util/Verif=
ier.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;
> =20
> -import junit.framework.AssertionFailedError;
> +import com.mockobjects.Verifiable;
> +
> import java.lang.reflect.Field;
> -import com.mockobjects.*;
> =20
> +/**
> + * Helper class to verify all {@link com.mockobjects.Expectation Expecta=
tion}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.Objec=
t)
> 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 tha=
t
> + * 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 =3D new Expectation...(...);
> + * private Expectation... aSecondExpectation =3D 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>
> + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> + * 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=20
> + * used to endorse or promote products derived from this software=20
> + * without prior written permission. For written permission, please=20
> + * contact ap...@ap....
> + *
> + * 5. Products derived from this software may not be called "Apache",
> + * nor may "Apache" appear in their name, without prior written=20
> + * 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.
> + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> + *
> + * 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=3D"mailto:fb...@us...">Francois Beauso=
leil
> (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();
> }
> =20
> - static public void verifyField(Field field, Object anObject) {
> + /**
> + * Calls {@link com.mockobjects.Verifiable#verify() Verifiable.verif=
y()}
> + * on <code>aField</code>.
> + * This method will result in a no-op if <code>aField</code> does no=
t
> + * 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 =3D field.get(anObject);
> - if (fieldObject instanceof Verifiable) {
> - ((Verifiable) fieldObject).verify();
> + aField.setAccessible(true);
> + Object fieldObject =3D 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());
> }
> }
> =20
> + /**
> + * 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());
> }
> =20
> + /**
> + * 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 aCl=
ass) {
> - if (aClass.equals(Object.class)) {
> + if (isRootClass(aClass)) {
> return;
> }
> +
> verifyObjectFromClass(anObject, aClass.getSuperclass());
> =20
> Field[] fields =3D aClass.getDeclaredFields();
> for (int i =3D 0; i < fields.length; ++i) {
> verifyField(fields[i], anObject);
> }
> + }
> +
> + /**
> + * Returns a <code>boolean</code> indicating whether or not the pass=
ed
> + * 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 pass=
ed
> + * class is the root class.
> + * The root class is where recursive searching for fields should sto=
p.
> + * Usually, this would be {@link java.lang.Object}.
> + * @returns <code>true</code> if <code>aClass</code> is the root cla=
ss,
> + * <code>false</code> otherwise.
> + */
> + private static boolean isRootClass(Class aClass) {
> + return aClass.equals(Object.class);
> }
> }
>=20
>=20
> __________________________________________________________
> L=E8che-vitrine ou l=E8che-=E9cran ?
> magasinage.yahoo.ca
>=20
> _______________________________________________
> Mockobjects-java-dev mailing list
> Moc...@li...
> https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev
--=20
|