From: <iro...@us...> - 2009-04-07 20:02:31
|
Revision: 107 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=107&view=rev Author: iroberts Date: 2009-04-07 20:02:22 +0000 (Tue, 07 Apr 2009) Log Message: ----------- doDiff now checks both arguments for class type. Test this. Modified Paths: -------------- trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java Modified: trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2009-04-07 19:58:12 UTC (rev 106) +++ trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2009-04-07 20:02:22 UTC (rev 107) @@ -266,10 +266,8 @@ if (instance == other) { return new PropertyDifferences(Collections.<Difference>emptyList()); } - if (!clazz.isInstance(other)) { - throw new ClassCastException( - "other has type " + other.getClass() + " which is not a subtype of " + clazz); - } + checkClass(instance, "instance"); + checkClass(other, "other"); List<Difference> differences = new ArrayList<Difference>(); for (PropertyElement prop: classProperties.getEqualsProperties()) { final Object instanceValue = prop.getValue(instance); @@ -281,7 +279,15 @@ return new PropertyDifferences(differences); } + private void checkClass(T instance, String label) { + if (!clazz.isInstance(instance)) { + throw new ClassCastException( + label + " has type " + instance.getClass().getName() + + " which is not a subtype of " + clazz.getName()); + } + } + /** * @param instance * @param other Modified: trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2009-04-07 19:58:12 UTC (rev 106) +++ trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2009-04-07 20:02:22 UTC (rev 107) @@ -260,6 +260,34 @@ assertEquals(DifferenceToNull.class, differences.getClass()); } + @Test public void testDiffAgainstWrongType() { + Pojomator<?> pojomator = OBJECT_PAIR_PROPERTY_POJOMATOR; + @SuppressWarnings("unchecked") Pojomator<Object> misCastPojomator = (Pojomator<Object>) pojomator; + try { + misCastPojomator.doDiff(new ObjectPairProperty(1,2), "wrong"); + fail("exception expcected"); + } + catch (ClassCastException e) { + assertEquals( + "other has type java.lang.String which is not a subtype of org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", + e.getMessage()); + } + } + + @Test public void testDiffWrongType() { + Pojomator<?> pojomator = OBJECT_PAIR_PROPERTY_POJOMATOR; + @SuppressWarnings("unchecked") Pojomator<Object> misCastPojomator = (Pojomator<Object>) pojomator; + try { + misCastPojomator.doDiff("wrong", new ObjectPairProperty(1,2)); + fail("exception expcected"); + } + catch (ClassCastException e) { + assertEquals( + "instance has type java.lang.String which is not a subtype of org.pojomatic.internal.PojomatorImplTest$ObjectPairProperty", + e.getMessage()); + } + } + @Test(expected=IllegalArgumentException.class) public void testNonPojomatedClass() { makePojomatorImpl(String.class); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |