From: <iro...@us...> - 2010-04-04 04:55:26
|
Revision: 199 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=199&view=rev Author: iroberts Date: 2010-04-04 04:55:19 +0000 (Sun, 04 Apr 2010) Log Message: ----------- *) Pojomatic.doDiff will throw an NPE if either argument is null. Hence Differences has no need to handle the case of one side or the other (or both) being null. *) Pojomatic.doDiff will also throw an exception if asked to compare instances of two classes which are not comparable for equals. *) The work of handling these cases now falls on pojomatic-test-utils. *) assertPojoEquals generated messages are more like those generated by junit. *) assertPojoEquals will show the full expected and actual in addition to the differences. Modified Paths: -------------- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomator.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/Difference.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/ValueDifference.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/ValueDifferenceTest.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java trunk/PojomaticAll/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java Added Paths: ----------- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/NoPojomaticPropertiesException.java trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/DifferentPojo.java Removed Paths: ------------- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnLeft.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnRight.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceToNullTest.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnLeftTest.java trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnRightTest.java Added: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/NoPojomaticPropertiesException.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/NoPojomaticPropertiesException.java (rev 0) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/NoPojomaticPropertiesException.java 2010-04-04 04:55:19 UTC (rev 199) @@ -0,0 +1,11 @@ +package org.pojomatic; + +/** + * An exception thrown when asked to create a {@link Pojomator} for a class which has no properties + * annotated for use with Pojomatic. + */ +public class NoPojomaticPropertiesException extends IllegalArgumentException { + public NoPojomaticPropertiesException(Class<?> pojoClass) { + super("Class " + pojoClass.getName() + " has no Pojomatic properties"); + } +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2010-04-04 04:55:19 UTC (rev 199) @@ -87,11 +87,11 @@ * @param <T> the type of the POJO * @param pojo the POJO - must not be null * @return the {@code toString} representation of {@code pojo}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic + * @throws NoPojomaticPropertiesException if {@code pojo}'s class has no properties annotated for + * use with Pojomatic * @see Pojomator#doToString(Object) */ - public static <T> String toString(T pojo) throws IllegalArgumentException { + public static <T> String toString(T pojo) throws NoPojomaticPropertiesException { return pojomator(getClass(pojo)).doToString(pojo); } @@ -100,11 +100,11 @@ * @param <T> the type of the POJO * @param pojo the POJO - must not be null * @return the {@code hashCode} for {@code pojo}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic + * @throws NoPojomaticPropertiesException if {@code pojo}'s class has no properties annotated for + * use with Pojomatic * @see Pojomator#doHashCode(Object) */ - public static <T> int hashCode(T pojo) throws IllegalArgumentException { + public static <T> int hashCode(T pojo) throws NoPojomaticPropertiesException { return pojomator(getClass(pojo)).doHashCode(pojo); } @@ -116,11 +116,11 @@ * @param other the object to compare to for equality * @return whether {@code pojo} and {@code other} are equal to each other in the sense of * {@code Object}'s {@code equals} method. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic + * @throws NoPojomaticPropertiesException if {@code pojo}'s class has no properties annotated for + * use with Pojomatic * @see Pojomator#doEquals(Object, Object) */ - public static <T> boolean equals(T pojo, Object other) throws IllegalArgumentException { + public static <T> boolean equals(T pojo, Object other) throws NoPojomaticPropertiesException { return pojomator(getClass(pojo)).doEquals(pojo, other); } @@ -146,20 +146,20 @@ * @param other the instance to diff * @return the list of differences (possibly empty) between {@code instance} and {@code other} * among the properties examined by {@link #equals(Object, Object)} for type {@code T}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic + * @throws NullPointerException if {@code pojo} or {@other} are null + * (this behavior may change in future releases). + * @throws NoPojomaticPropertiesException if {@code pojo}'s class has no properties + * annotated for use with Pojomatic, or if the types of {@code pojo} and {@code other} are not + * compatible for equality with each other (this behavior may change in future releases). */ public static <T, S extends T> Differences diff(T pojo, S other) - throws NullPointerException, IllegalArgumentException { + throws NullPointerException, NoPojomaticPropertiesException { if (pojo == null) { - if (other != null) { - return pojomator(getClass(other)).doDiff(null, other); - } - else { //both null - return NoDifferences.getInstance(); - } + throw new NullPointerException("pojo is null"); } - + if (other == null) { + throw new NullPointerException("other is null"); + } return pojomator(getClass(pojo)).doDiff(pojo, other); } @@ -169,11 +169,12 @@ * @param <T> the type represented by {@code pojoClass} * @param pojoClass the class to create a {@code Pojomator} for. * @return a {@code Pojomator<T>} - * @throws IllegalArgumentException if {@code pojoClass} has no properties annotated for use + * @throws NoPojomaticPropertiesException if {@code pojoClass} has no properties annotated for use * with Pojomatic */ @SuppressWarnings("unchecked") // compiler does not know that the type parameter to Pojomator is T - public static <T> Pojomator<T> pojomator(Class<T> pojoClass) throws IllegalArgumentException { + public static <T> Pojomator<T> pojomator(Class<T> pojoClass) + throws NoPojomaticPropertiesException { return (Pojomator<T>) POJOMATORS.get(pojoClass); } Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomator.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomator.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomator.java 2010-04-04 04:55:19 UTC (rev 199) @@ -142,7 +142,11 @@ * @param other the instance to diff * @return the differences between {@code instance} and {@code other} * among the properties examined by {@link #doEquals(Object, Object)}. - * + * @throws NullPointerException if {@code instance} or {@code other} is null + * (this behavoir may change in future releases). + * @throws IllegalArgumentException the type of {@code instance} or of {@code other} is not a + * class which is compatible for equality with {@code T} + * (this behavoir may change in future releases). * @see #doEquals(Object, Object) */ Differences doDiff(T instance, T other); Deleted: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,46 +0,0 @@ -package org.pojomatic.diff; - -import org.pojomatic.Pojomatic; -import org.pojomatic.annotations.AutoProperty; -import org.pojomatic.annotations.DefaultPojomaticPolicy; -import org.pojomatic.annotations.PojomaticPolicy; -import org.pojomatic.annotations.Property; - -/** - * A {@link Differences} whose {@link Differences#toString()} is generated exactly once. - */ -@AutoProperty(policy=DefaultPojomaticPolicy.NONE) -abstract class AbstractNullDifference implements Differences { - @Property(policy=PojomaticPolicy.ALL) - protected final Object instance; - - /** - * @param instance the non-null object being compared to {@code null}. - * @throws NullPointerException if {@code instance} is {@code null}. - */ - public AbstractNullDifference(Object instance) { - if (instance == null) { - throw new NullPointerException("Instance cannot be null"); - } - - this.instance = instance; - } - - public final boolean areEqual() { - return false; - } - - @Override - public abstract String toString(); - - @Override - public int hashCode() { - return Pojomatic.hashCode(this); - } - - @Override - public boolean equals(Object o) { - return Pojomatic.equals(this, o); - } - -} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/Difference.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/Difference.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/Difference.java 2010-04-04 04:55:19 UTC (rev 199) @@ -15,20 +15,6 @@ String propertyName(); /** - * Whether or not the property exists on the left object. - * - * @return {@code true} if the property exists on the left object, {@code false} otherwise - */ - boolean existsOnLeft(); - - /** - * Whether or not the property exists on the right object. - * - * @return {@code true} if the property exists on the right object, {@code false} otherwise - */ - boolean existsOnRight(); - - /** * The value from the left instance (possibly {@code null}). * * @return the value from the left instance (possibly {@code null}) Deleted: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,27 +0,0 @@ -package org.pojomatic.diff; - -/** - * The result of comparing {@code null} to a non-null object. - * @see DifferenceToNull - */ -public final class DifferenceFromNull extends AbstractNullDifference { - private final Iterable<OnlyOnRight> differences; - - public DifferenceFromNull(Object other, Iterable<OnlyOnRight> differences) { - super(other); - if (differences == null) { - throw new NullPointerException("Differences cannot be null"); - } - this.differences = differences; - } - - @Override - public String toString() { - return "null is different than the object {" + instance + "}"; - } - - public Iterable<? extends Difference> differences() { - return differences; - } - -} Deleted: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,26 +0,0 @@ -package org.pojomatic.diff; - -/** - * The result of comparing a non-null object to {@code null}. - * @see DifferenceFromNull - */ -public final class DifferenceToNull extends AbstractNullDifference { - private final Iterable<OnlyOnLeft> differences; - - public DifferenceToNull(Object instance, Iterable<OnlyOnLeft> differences) { - super(instance); - if (differences == null) { - throw new NullPointerException("Differences cannot be null"); - } - this.differences = differences; - } - - @Override - public String toString() { - return "the object {" + instance + "} is different than null"; - } - - public Iterable<? extends Difference> differences() { - return differences; - } -} Deleted: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnLeft.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnLeft.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnLeft.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,62 +0,0 @@ -package org.pojomatic.diff; - -import java.util.NoSuchElementException; - -import org.pojomatic.Pojomatic; -import org.pojomatic.annotations.Property; - -/** - * A {@link Difference} which only has a value on the left. - */ -public final class OnlyOnLeft implements Difference { - - private final String propertyName; - - private final Object leftValue; - - public OnlyOnLeft(String propertyName, Object leftValue) { - if (propertyName == null) { - throw new NullPointerException("Property name cannot be null"); - } - this.propertyName = propertyName; - this.leftValue = leftValue; - } - - @Property - public String propertyName() { - return propertyName; - } - - @Property - public Object leftValue() { - return leftValue; - } - - public boolean existsOnLeft() { - return true; - } - - public boolean existsOnRight() { - return false; - } - - public Object rightValue() throws NoSuchElementException { - throw new NoSuchElementException("Value only exists on the left"); - } - - @Override - public int hashCode() { - return Pojomatic.hashCode(this); - } - - @Override - public String toString() { - return Pojomatic.toString(this); - } - - @Override - public boolean equals(Object o) { - return Pojomatic.equals(this, o); - } - -} Deleted: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnRight.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnRight.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/OnlyOnRight.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,62 +0,0 @@ -package org.pojomatic.diff; - -import java.util.NoSuchElementException; - -import org.pojomatic.Pojomatic; -import org.pojomatic.annotations.Property; - -/** - * A {@link Difference} which only has a value on the right. - */ -public final class OnlyOnRight implements Difference { - - private final String propertyName; - - private final Object rightValue; - - public OnlyOnRight(String propertyName, Object rightValue) { - if (propertyName == null) { - throw new NullPointerException("Property name cannot be null"); - } - this.propertyName = propertyName; - this.rightValue = rightValue; - } - - @Property - public String propertyName() { - return propertyName; - } - - public Object leftValue() { - throw new NoSuchElementException("Value only exists on the right"); - } - - public boolean existsOnLeft() { - return false; - } - - public boolean existsOnRight() { - return true; - } - - @Property - public Object rightValue() throws NoSuchElementException { - return rightValue; - } - - @Override - public int hashCode() { - return Pojomatic.hashCode(this); - } - - @Override - public String toString() { - return Pojomatic.toString(this); - } - - @Override - public boolean equals(Object o) { - return Pojomatic.equals(this, o); - } - -} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/ValueDifference.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/ValueDifference.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/diff/ValueDifference.java 2010-04-04 04:55:19 UTC (rev 199) @@ -29,14 +29,6 @@ return rightValue; } - public boolean existsOnLeft() { - return true; - } - - public boolean existsOnRight() { - return true; - } - @Override public boolean equals(Object obj) { return Pojomatic.equals(this, obj); Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/ClassProperties.java 2010-04-04 04:55:19 UTC (rev 199) @@ -11,6 +11,7 @@ import java.util.regex.Pattern; import org.pojomatic.PropertyElement; +import org.pojomatic.NoPojomaticPropertiesException; import org.pojomatic.annotations.*; /** @@ -50,10 +51,10 @@ * repeatedly is not inefficient. * @param pojoClass the class to inspect for properties * @return The {@code ClassProperties} for {@code pojoClass}. - * @throws IllegalArgumentException if {@code pojoClass} has no properties annotated for use + * @throws NoPojomaticPropertiesException if {@code pojoClass} has no properties annotated for use * with Pojomatic. */ - public static ClassProperties forClass(Class<?> pojoClass) throws IllegalArgumentException { + public static ClassProperties forClass(Class<?> pojoClass) throws NoPojomaticPropertiesException { return INSTANCES.get(pojoClass); } @@ -61,10 +62,10 @@ * Creates an instance for the given {@code pojoClass}. * * @param pojoClass the class to inspect for properties - * @throws IllegalArgumentException if {@code pojoClass} has no properties annotated for use + * @throws NoPojomaticPropertiesException if {@code pojoClass} has no properties annotated for use * with Pojomatic. */ - private ClassProperties(Class<?> pojoClass) throws IllegalArgumentException { + private ClassProperties(Class<?> pojoClass) throws NoPojomaticPropertiesException { if (pojoClass.isInterface()) { extractClassProperties(pojoClass, new OverridableMethods(), new ClassContributionTracker()); equalsParentClass = pojoClass; @@ -237,8 +238,7 @@ return; } } - throw new IllegalArgumentException( - "Class " + pojoClass.getName() + " has no Pojomatic properties"); + throw new NoPojomaticPropertiesException(pojoClass); } private String getPropertyName(Property property) { Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2010-04-04 04:55:19 UTC (rev 199) @@ -8,15 +8,12 @@ import org.pojomatic.Pojomator; import org.pojomatic.PropertyElement; +import org.pojomatic.NoPojomaticPropertiesException; import org.pojomatic.annotations.PojoFormat; import org.pojomatic.annotations.PropertyFormat; import org.pojomatic.diff.Difference; -import org.pojomatic.diff.DifferenceFromNull; -import org.pojomatic.diff.DifferenceToNull; import org.pojomatic.diff.Differences; import org.pojomatic.diff.NoDifferences; -import org.pojomatic.diff.OnlyOnLeft; -import org.pojomatic.diff.OnlyOnRight; import org.pojomatic.diff.PropertyDifferences; import org.pojomatic.diff.ValueDifference; import org.pojomatic.formatter.DefaultPojoFormatter; @@ -35,10 +32,10 @@ * Creates an instance for {@code clazz}. * * @param clazz the class - * @throws IllegalArgumentException if {@code clazz} has no properties annotated for use + * @throws NoPojomaticPropertiesException if {@code clazz} has no properties annotated for use * with Pojomatic */ - public PojomatorImpl(Class<T> clazz) throws IllegalArgumentException { + public PojomatorImpl(Class<T> clazz) throws NoPojomaticPropertiesException { this.clazz = clazz; classProperties = ClassProperties.forClass(clazz); pojoFormatterClass = findPojoFormatterClass(clazz); @@ -231,17 +228,15 @@ public Differences doDiff(T instance, T other) { final Collection<PropertyElement> diffProperties = classProperties.getEqualsProperties(); + if (instance == null) { + throw new NullPointerException("instance is null"); + } + if (other == null) { + throw new NullPointerException("other is null"); + } if (instance == other) { return NoDifferences.getInstance(); } - else if (instance == null) { - checkClass(other, "other"); - return new DifferenceFromNull(other, onlyOnRight(other, diffProperties)); - } - else if (other == null) { - checkClass(instance, "instance"); - return new DifferenceToNull(instance, onlyOnLeft(instance, diffProperties)); - } checkClass(instance, "instance"); checkClass(other, "other"); @@ -260,28 +255,11 @@ return new PropertyDifferences(differences); } - private Iterable<OnlyOnLeft> onlyOnLeft(T instance, Collection<PropertyElement> diffProperties) { - List<OnlyOnLeft> result = new ArrayList<OnlyOnLeft>(); - for (PropertyElement element : diffProperties) { - result.add(new OnlyOnLeft(element.getName(), element.getValue(instance))); - } - return result; - } - - private Iterable<OnlyOnRight> onlyOnRight(Object value, - Collection<PropertyElement> diffProperties) { - List<OnlyOnRight> result = new ArrayList<OnlyOnRight>(); - for (PropertyElement element : diffProperties) { - result.add(new OnlyOnRight(element.getName(), element.getValue(value))); - } - return result; - } - private void checkClass(T instance, String label) { - if (!clazz.isInstance(instance)) { - throw new ClassCastException( + if (!isCompatibleForEquality(instance.getClass())) { + throw new IllegalArgumentException( label + " has type " + instance.getClass().getName() - + " which is not a subtype of " + clazz.getName()); + + " which is not compatible for equality with " + clazz.getName()); } } Modified: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -4,16 +4,9 @@ import org.junit.Test; import org.pojomatic.annotations.Property; -import org.pojomatic.diff.DifferenceFromNull; -import org.pojomatic.diff.DifferenceToNull; -import org.pojomatic.diff.Differences; import org.pojomatic.diff.NoDifferences; -import org.pojomatic.diff.OnlyOnLeft; -import org.pojomatic.diff.OnlyOnRight; import org.pojomatic.internal.PojomatorImpl; -import com.google.common.collect.Sets; - public class PojomaticTest { public static class Bean { @Property public final int x; @@ -40,25 +33,19 @@ assertEquals(NoDifferences.getInstance(), Pojomatic.diff(BEAN, BEAN)); } - @Test + @Test(expected=NullPointerException.class) public void testDiffBothNull() { - assertEquals(NoDifferences.getInstance(), Pojomatic.diff(null, null)); + Pojomatic.diff(null, null); } - @Test + @Test(expected=NullPointerException.class) public void testDiffNullFirst() { - Iterable<OnlyOnRight> beanDifferences = Sets.newHashSet(new OnlyOnRight("x", BEAN.x)); - Differences differences = Pojomatic.diff(null, BEAN); - assertEquals(new DifferenceFromNull(BEAN, beanDifferences), differences); - assertEquals(beanDifferences, Sets.newHashSet(differences.differences())); + Pojomatic.diff(null, BEAN); } - @Test + @Test(expected=NullPointerException.class) public void testDiffNullSecond() { - Iterable<OnlyOnLeft> beanDifferences = Sets.newHashSet(new OnlyOnLeft("x", BEAN.x)); - Differences differences = Pojomatic.diff(BEAN, null); - assertEquals(new DifferenceToNull(BEAN, beanDifferences), differences); - assertEquals(beanDifferences, Sets.newHashSet(differences.differences())); + Pojomatic.diff(BEAN, null); } @Test Deleted: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,33 +0,0 @@ -package org.pojomatic.diff; - -import static org.junit.Assert.*; - -import java.util.Collections; - -import org.junit.Test; - -public class DifferenceFromNullTest { - private static final Iterable<OnlyOnRight> EMPTY_DIFFERENCES = - Collections.<OnlyOnRight>emptyList(); - - @Test(expected = NullPointerException.class) - public void testNullValue() { - new DifferenceFromNull(null, EMPTY_DIFFERENCES); - } - - @Test(expected = NullPointerException.class) - public void testNullDifferences() { - new DifferenceFromNull(new Object(), null); - } - - @Test - public void testToString() { - DifferenceFromNull difference = new DifferenceFromNull(3, EMPTY_DIFFERENCES); - assertEquals("null is different than the object {3}", difference.toString()); - } - - @Test - public void testAreEqual() { - assertFalse(new DifferenceFromNull(3, EMPTY_DIFFERENCES).areEqual()); - } -} Deleted: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceToNullTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceToNullTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceToNullTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,32 +0,0 @@ -package org.pojomatic.diff; - -import static org.junit.Assert.*; - -import java.util.Collections; - -import org.junit.Test; - -public class DifferenceToNullTest { - private static final Iterable<OnlyOnLeft> EMPTY_DIFFERENCES = Collections.<OnlyOnLeft>emptyList(); - - @Test(expected = NullPointerException.class) - public void testNullValue() { - new DifferenceToNull(null, EMPTY_DIFFERENCES); - } - - @Test(expected = NullPointerException.class) - public void testNullDifferences() { - new DifferenceToNull(new Object(), null); - } - - @Test - public void testToString() { - DifferenceToNull differences = new DifferenceToNull(3, EMPTY_DIFFERENCES); - assertEquals("the object {3} is different than null", differences.toString()); - } - - @Test - public void testAreEqual() { - assertFalse(new DifferenceToNull(3, EMPTY_DIFFERENCES).areEqual()); - } -} Deleted: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnLeftTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnLeftTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnLeftTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,42 +0,0 @@ -package org.pojomatic.diff; - -import static org.junit.Assert.*; - -import java.util.NoSuchElementException; - -import org.junit.Test; - -public class OnlyOnLeftTest { - private static final OnlyOnLeft INSTANCE = new OnlyOnLeft("name", "value"); - - @Test(expected = NullPointerException.class) - public void testNullPropertyName() { - new OnlyOnLeft(null, new Object()); - } - - @Test - public void testPropertyName() { - assertEquals("name", INSTANCE.propertyName()); - } - - @Test - public void testLeftValue() { - assertEquals("value", INSTANCE.leftValue()); - } - - @Test - public void testExistsOnLeft() { - assertTrue(INSTANCE.existsOnLeft()); - } - - @Test - public void testExistsOnRight() { - assertFalse(INSTANCE.existsOnRight()); - } - - @Test(expected = NoSuchElementException.class) - public void testRightValue() { - INSTANCE.rightValue(); - } - -} Deleted: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnRightTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnRightTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/OnlyOnRightTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,42 +0,0 @@ -package org.pojomatic.diff; - -import static org.junit.Assert.*; - -import java.util.NoSuchElementException; - -import org.junit.Test; - -public class OnlyOnRightTest { - private static final OnlyOnRight INSTANCE = new OnlyOnRight("name", "value"); - - @Test(expected = NullPointerException.class) - public void testNullPropertyName() { - new OnlyOnRight(null, new Object()); - } - - @Test - public void testPropertyName() { - assertEquals("name", INSTANCE.propertyName()); - } - - @Test(expected = NoSuchElementException.class) - public void testLeftValue() { - INSTANCE.leftValue(); - } - - @Test - public void testExistsOnLeft() { - assertFalse(INSTANCE.existsOnLeft()); - } - - @Test - public void testExistsOnRight() { - assertTrue(INSTANCE.existsOnRight()); - } - - @Test - public void testRightValue() { - assertEquals("value", INSTANCE.rightValue()); - } - -} Modified: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/ValueDifferenceTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/ValueDifferenceTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/diff/ValueDifferenceTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -21,15 +21,4 @@ public void testRightValue() { assertEquals("that", DIFFERENCE.rightValue()); } - - @Test - public void testExistsOnLeft() { - assertTrue(DIFFERENCE.existsOnLeft()); - } - - @Test - public void testExistsOnRight() { - assertTrue(DIFFERENCE.existsOnRight()); - } - } Modified: trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -11,12 +11,9 @@ import org.junit.Test; import org.pojomatic.Pojomatic; import org.pojomatic.Pojomator; +import org.pojomatic.NoPojomaticPropertiesException; import org.pojomatic.annotations.*; -import org.pojomatic.diff.OnlyOnLeft; -import org.pojomatic.diff.OnlyOnRight; import org.pojomatic.diff.ValueDifference; -import org.pojomatic.diff.DifferenceFromNull; -import org.pojomatic.diff.DifferenceToNull; import org.pojomatic.diff.Differences; import org.pojomatic.diff.PropertyDifferences; import org.pojomatic.formatter.DefaultPojoFormatter; @@ -227,26 +224,21 @@ makePojomatorImpl(FormattedObject.class).doToString(new FormattedObject("x"))); } - @Test public void testDiffNullInstance() { + @Test(expected=NullPointerException.class) + public void testDiffNullInstance() { ObjectPairProperty other = new ObjectPairProperty("this", "that"); - Differences actual = OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, other); - assertFalse(actual.areEqual()); - assertTrue(actual instanceof DifferenceFromNull); - assertEquals(Sets.newHashSet(new OnlyOnRight("s", "this"), new OnlyOnRight("t", "that")), - Sets.newHashSet(actual.differences())); + OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, other); } - @Test public void testDiffNullOther() { + @Test(expected=NullPointerException.class) + public void testDiffNullOther() { ObjectPairProperty instance = new ObjectPairProperty("this", "that"); - Differences actual = OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(instance, null); - assertFalse(actual.areEqual()); - assertTrue(actual instanceof DifferenceToNull); - assertEquals(Sets.newHashSet(new OnlyOnLeft("s", "this"), new OnlyOnLeft("t", "that")), - Sets.newHashSet(actual.differences())); + OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(instance, null); } - @Test public void testDiffNulls() { - assertTrue(OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, null).areEqual()); + @Test(expected=NullPointerException.class) + public void testDiffNulls() { + OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, null); } @Test public void testDiffSameObject() { @@ -286,9 +278,9 @@ misCastPojomator.doDiff(new ObjectPairProperty(1,2), "wrong"); fail("exception expcected"); } - catch (ClassCastException e) { + catch (IllegalArgumentException e) { assertEquals( - "other has type java.lang.String which is not a subtype of org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", + "other has type java.lang.String which is not compatible for equality with org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", e.getMessage()); } } @@ -300,14 +292,14 @@ misCastPojomator.doDiff("wrong", new ObjectPairProperty(1,2)); fail("exception expcected"); } - catch (ClassCastException e) { + catch (IllegalArgumentException e) { assertEquals( - "instance has type java.lang.String which is not a subtype of org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", + "instance has type java.lang.String which is not compatible for equality with org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", e.getMessage()); } } - @Test(expected=IllegalArgumentException.class) + @Test(expected= NoPojomaticPropertiesException.class) public void testNonPojomatedClass() { makePojomatorImpl(String.class); } Modified: trunk/PojomaticAll/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java 2010-04-04 04:55:19 UTC (rev 199) @@ -1,6 +1,7 @@ package org.pojomatic.test; import org.pojomatic.Pojomatic; +import org.pojomatic.NoPojomaticPropertiesException; import org.pojomatic.diff.Differences; /** @@ -30,24 +31,43 @@ * Asserts that the objects are equal via {@link #equal(Object, Object)}. * * @param message the message to add if the assertion fails - * @param first will be displayed first if the assertion fails - * @param second will be displayed second if the assertion fails + * @param expected will be displayed first if the assertion fails + * @param actual will be displayed second if the assertion fails * @throws AssertionError if the objects are not equal. {@link AssertionError#getMessage()} will * include information about the differences */ - public static void assertEquals(String message, Object first, Object second) { - if (!equal(first, second)) { - throw new AssertionError(buildMessage(message, Pojomatic.diff(first, second))); - } + public static void assertEquals(String message, Object expected, Object actual) { + if (!equal(expected, actual)) { + if (expected == null) { + throw new AssertionError( + makeBuilder(message).append("expected is null, but actual is ").append(actual)); + } + if (actual == null) { + throw new AssertionError( + makeBuilder(message).append("actual is null, but expected is ").append(expected)); + } + try { + if (Pojomatic.areCompatibleForEquals(expected.getClass(), actual.getClass())) { + throw new AssertionError(appendStandardEqualityMessage( + makeBuilder(message).append("differences between expected and actual:") + .append(Pojomatic.diff(expected, actual)) + .append(" ("), expected, actual).append(")").toString()); + } + } + catch (NoPojomaticPropertiesException e) {} + throw new AssertionError( + appendStandardEqualityMessage(makeBuilder(message), expected, actual).toString()); + } } - private static String buildMessage(String message, Differences differences) { - StringBuilder formatted = new StringBuilder(); - if (message != null) { - formatted.append(message).append(" "); - } + private static StringBuilder appendStandardEqualityMessage( + StringBuilder builder, Object expected, Object actual) { + return builder + .append("expected:<").append(expected).append("> but was:<").append(actual).append(">"); + } - return formatted.append(differences).toString(); + private static StringBuilder makeBuilder(String message) { + return message == null ? new StringBuilder() : new StringBuilder(message).append(" "); } private AssertUtils() {} Modified: trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -5,8 +5,8 @@ public class PojomaticAssertTest extends AssertTest { @Override - protected void performAssertEquals(Object first, Object second, String message) { - PojomaticAssert.assertEqualsWithDiff(first, second, message); + protected void performAssertEquals(Object expected, Object actual, String message) { + PojomaticAssert.assertEqualsWithDiff(expected, actual, message); } } Modified: trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -15,57 +15,94 @@ * Only the unit under test should throw {@link AssertionError}, so no assertions are allowed * within the implementation of this method. * - * @param first the object which should appear first if the assertion fails. Note that this - * could be either "expected" or "actual". - * @param second the object which should appear second if the assertion fails. Note that this + * @param expected the expected object + * @param actual the actual object * @param message the messaage to include with the assertion */ - protected abstract void performAssertEquals(Object first, Object second, String message); + protected abstract void performAssertEquals(Object expected, Object actual, String message); + private void performAssertEquals( + Object expected, Object actual, String message, String expectedMessage) { + try { + performAssertEquals(expected, actual, message); + fail("exception expected"); + } + catch (AssertionError e) { + assertEquals(expectedMessage, e.getMessage()); + } + } + @Test + public final void assertEqualsWhenEqual() { + performAssertEquals(new Container(3), new Container(3), "message"); + } + + @Test public final void assertEqualsBothNull() { performAssertEquals(null, null, null); } - @Test(expected=AssertionError.class) + @Test public final void assertEqualsNullExpected() { - performAssertEquals(null, new Container(null), null); + performAssertEquals( + null, new Container(null), null, + "expected is null, but actual is Container{test: {null}}"); } - @Test(expected=AssertionError.class) + @Test public final void assertEqualsNullActual() { - performAssertEquals(new Container(null), null, null); + performAssertEquals( + new Container(null), null, null, "actual is null, but expected is Container{test: {null}}"); } /** * Tests that {@link PojomaticAssert#assertEqualsWithDiff(Object, Object)} * uses {@link Object#equals(Object)} instead of {@link Pojomatic#equals(Object, Object)}. */ - @Test(expected=AssertionError.class) + @Test public final void assertEqualsViaInheritedEquals() { //create objects which are never equal via Object.equals(Object), but are equal via //Pojomatic.equals(Object, Object) OnlyPojomaticEqual first = new OnlyPojomaticEqual(); OnlyPojomaticEqual second = new OnlyPojomaticEqual(); + performAssertEquals(first, second, null, + "differences between expected and actual:no differences (expected:<toString> but was:<toString>)"); + } - performAssertEquals(first, second, null); + @Test + public final void assertEqualsNoMessage() { + performAssertEquals( + new Container("foo"), new Container("bar"), null, + "differences between expected and actual:[test: {foo} versus {bar}]" + + " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)"); } @Test - public final void assertEqualsMessage() { + public final void assertEqualsMessag2() { String first = "foo"; String second = "bar"; - try { - performAssertEquals(new Container(first), new Container(second), null); - } - catch (AssertionError e) { - assertEquals("[test: {foo} versus {bar}]", e.getMessage()); - } - try { - performAssertEquals(new Container(first), new Container(second), "custom message"); - } - catch (AssertionError e) { - assertEquals("custom message [test: {foo} versus {bar}]", e.getMessage()); - } + performAssertEquals( + new Container(first), new Container(second), null, + "differences between expected and actual:[test: {foo} versus {bar}]" + + " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)"); } + + @Test + public final void assertEqualsCustomMessage() { + performAssertEquals( + new Container("foo"), new Container("bar"), "hello", + "hello differences between expected and actual:[test: {foo} versus {bar}]" + + " (expected:<Container{test: {foo}}> but was:<Container{test: {bar}}>)"); + } + + @Test + public final void assertEqualsNonPojomatic() { + performAssertEquals("string a", "string b", null, "expected:<string a> but was:<string b>"); + } + + @Test + public final void assertEqualsNonComparable() { + performAssertEquals(new Container("foo"), new DifferentPojo("foo"), null, + "expected:<Container{test: {foo}}> but was:<DifferentPojo{test: {foo}}>"); + } } Copied: trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/DifferentPojo.java (from rev 197, trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/Container.java) =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/DifferentPojo.java (rev 0) +++ trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/DifferentPojo.java 2010-04-04 04:55:19 UTC (rev 199) @@ -0,0 +1,37 @@ +package org.pojomatic.test; + +import org.pojomatic.Pojomatic; +import org.pojomatic.annotations.AutoProperty; + +/** + * Simple one-object container for use in tests. + */ +@AutoProperty +public class DifferentPojo { + + private final Object test; + + public DifferentPojo(Object test) { + this.test = test; + } + + public Object getTest() { + return test; + } + + @Override + public int hashCode() { + return Pojomatic.hashCode(this); + } + + @Override + public String toString() { + return Pojomatic.toString(this); + } + + @Override + public boolean equals(Object o) { + return Pojomatic.equals(this, o); + } + +} Modified: trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java 2010-04-04 04:55:19 UTC (rev 199) @@ -20,4 +20,9 @@ } return false; } + + @Override + public String toString() { + return "toString"; + } } Modified: trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java =================================================================== --- trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java 2010-03-31 04:58:11 UTC (rev 198) +++ trunk/PojomaticAll/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java 2010-04-04 04:55:19 UTC (rev 199) @@ -5,9 +5,9 @@ public class PojomaticAssertTest extends AssertTest { @Override - protected void performAssertEquals(Object first, Object second, String message) { + protected void performAssertEquals(Object expected, Object actual, String message) { //in TestNG, the arguments are included in any failure message in reverse order - PojomaticAssert.assertEqualsWithDiff(second, first, message); + PojomaticAssert.assertEqualsWithDiff(actual, expected, message); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |