From: <chr...@us...> - 2009-08-20 05:37:37
|
Revision: 135 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=135&view=rev Author: chriswhansen Date: 2009-08-20 05:37:29 +0000 (Thu, 20 Aug 2009) Log Message: ----------- Initial implementation of assertEqualsWithDiff() methods including JUnit-style and TestNG-style. Added Paths: ----------- branches/TestUtils/PojomaticTestUtils/.classpath branches/TestUtils/PojomaticTestUtils/.project branches/TestUtils/PojomaticTestUtils/.settings/ branches/TestUtils/PojomaticTestUtils/.settings/org.eclipse.jdt.core.prefs branches/TestUtils/PojomaticTestUtils/.settings/org.maven.ide.eclipse.prefs branches/TestUtils/PojomaticTestUtils/pom.xml branches/TestUtils/PojomaticTestUtils/src/ branches/TestUtils/PojomaticTestUtils/src/main/ branches/TestUtils/PojomaticTestUtils/src/main/java/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/junit/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/junit/PojomaticAssert.java branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/test/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/testng/ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/testng/PojomaticAssert.java branches/TestUtils/PojomaticTestUtils/src/test/ branches/TestUtils/PojomaticTestUtils/src/test/java/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/junit/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertUtilsTest.java branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/Container.java branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/testng/ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java Property Changed: ---------------- branches/TestUtils/PojomaticTestUtils/ Property changes on: branches/TestUtils/PojomaticTestUtils ___________________________________________________________________ Added: svn:ignore + target Added: branches/TestUtils/PojomaticTestUtils/.classpath =================================================================== --- branches/TestUtils/PojomaticTestUtils/.classpath (rev 0) +++ branches/TestUtils/PojomaticTestUtils/.classpath 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" output="target/classes" path="src/main/java"/> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> + <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> + <classpathentry kind="output" path="target/classes"/> +</classpath> Added: branches/TestUtils/PojomaticTestUtils/.project =================================================================== --- branches/TestUtils/PojomaticTestUtils/.project (rev 0) +++ branches/TestUtils/PojomaticTestUtils/.project 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>PojomaticTestUtils</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.iam.jdt.core.mavenIncrementalBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.maven.ide.eclipse.maven2Builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.maven.ide.eclipse.maven2Nature</nature> + <nature>org.eclipse.iam.jdt.core.mavenNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Added: branches/TestUtils/PojomaticTestUtils/.settings/org.eclipse.jdt.core.prefs =================================================================== --- branches/TestUtils/PojomaticTestUtils/.settings/org.eclipse.jdt.core.prefs (rev 0) +++ branches/TestUtils/PojomaticTestUtils/.settings/org.eclipse.jdt.core.prefs 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,12 @@ +#Thu Aug 06 23:32:41 MDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 Added: branches/TestUtils/PojomaticTestUtils/.settings/org.maven.ide.eclipse.prefs =================================================================== --- branches/TestUtils/PojomaticTestUtils/.settings/org.maven.ide.eclipse.prefs (rev 0) +++ branches/TestUtils/PojomaticTestUtils/.settings/org.maven.ide.eclipse.prefs 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,9 @@ +#Thu Aug 06 23:37:24 MDT 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +skipCompilerPlugin=true +version=1 Added: branches/TestUtils/PojomaticTestUtils/pom.xml =================================================================== --- branches/TestUtils/PojomaticTestUtils/pom.xml (rev 0) +++ branches/TestUtils/PojomaticTestUtils/pom.xml 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,40 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.pojomatic</groupId> + <artifactId>pojomatic-test-utils</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <name>pojomatic-test-utils</name> + <url>http://maven.apache.org</url> + + <dependencies> + <!-- TODO move this to a common parent pom --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.6</version> + <scope>test</scope> + </dependency> + <dependency> + <!-- TODO move this to a common parent pom --> + <groupId>org.pojomatic</groupId> + <artifactId>pojomatic</artifactId> + <version>TestUtils-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <!-- TODO move this to a common parent pom --> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.0.2</version> + <configuration> + <source>1.5</source> + <target>1.5</target> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/junit/PojomaticAssert.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/junit/PojomaticAssert.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/junit/PojomaticAssert.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,42 @@ +package org.pojomatic.junit; + +import org.pojomatic.Pojomatic; +import org.pojomatic.test.AssertUtils; + +/** + * Pojomatic-related JUnit-style assertion methods useful for writing tests. + * @see org.junit.Assert + */ +public class PojomaticAssert { + + /** + * Asserts that two possibly {@code null} objects are equal according to + * {@link Object#equals(Object)}. If not, the differences, + * via {@link Pojomatic#diff(Object, Object)}, are included in the failure message. + * + * @param expected the expected object + * @param actual the object which should be tested to equal the expected object + * @throws AssertionError if the objects are not equal, with details of the differences + * included in the message + * @see #assertEqualsWithDiff(Object, Object, String) + */ + public static void assertEqualsWithDiff(Object expected, Object actual) { + assertEqualsWithDiff(expected, actual, null); + } + + /** + * Asserts that two possibly {@code null} objects are equal according to + * {@link Object#equals(Object)}. If not, the differences, + * via {@link Pojomatic#diff(Object, Object)}, are included in the failure message. + * + * @param expected the expected object + * @param actual the object which should be tested to equal the expected object + * @throws AssertionError if the objects are not equal, with details of the differences + * included in the message + */ + public static void assertEqualsWithDiff(Object expected, Object actual, String message) { + AssertUtils.assertEquals(message, expected, actual); + } + + private PojomaticAssert() {} +} Added: branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/test/AssertUtils.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,55 @@ +package org.pojomatic.test; + +import org.pojomatic.Pojomatic; +import org.pojomatic.diff.Differences; + +/** + * This class is not meant to be a part of the public API. + */ +public class AssertUtils { + + /** + * Determines if two possibly {@code null} objects are equal. + * + * @return {@code true} if both objects are null, + * or {@code first} is non-null and {@code first.equals(second)}, + * {@code false} otherwise + * @see Object#equals(Object) + */ + public static boolean equal(Object first, Object second) { + if (first == null && second == null) { + return true; + } + if (first != null && first.equals(second)) { + return true; + } + return false; + } + + /** + * 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 + * @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))); + } + } + + private static String buildMessage(String message, Differences differences) { + String formatted = ""; + if (message != null) { + formatted = message + " "; + } + + return formatted + differences.toString(); + } + + private AssertUtils() {} + +} Added: branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/testng/PojomaticAssert.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/testng/PojomaticAssert.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/main/java/org/pojomatic/testng/PojomaticAssert.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,44 @@ +package org.pojomatic.testng; + +import org.pojomatic.Pojomatic; +import org.pojomatic.test.AssertUtils; + +/** + * Pojomatic-related TestNG-style assertion methods useful for writing tests. + * @see org.testng.Assert + */ +public class PojomaticAssert { + + /** + * Asserts that two possibly {@code null} objects are equal according to + * {@link Object#equals(Object)}. If not, the differences, + * via {@link Pojomatic#diff(Object, Object)}, are included in the failure message. + * + * @param expected the expected object + * @param actual the object which should be tested to equal the expected object + * @throws AssertionError if the objects are not equal, with details of the differences + * included in the message + * @see #assertEqualsWithDiff(Object, Object, String) + */ + public static void assertEqualsWithDiff(Object actual, Object expected) { + assertEqualsWithDiff(actual, expected, null); + } + + /** + * Asserts that two possibly {@code null} objects are equal according to + * {@link Object#equals(Object)}. If not, the differences, + * via {@link Pojomatic#diff(Object, Object)}, are included in the failure message. + * + * @param expected the expected object + * @param actual the object which should be tested to equal the expected object + * @param message an optional message provided along with the diff if the objects are not equal + * @throws AssertionError if the objects are not equal, with details of the differences + * included in the message + */ + public static void assertEqualsWithDiff(Object actual, Object expected, String message) { + //the arguments are passed as follows according to display order for a potential error message + AssertUtils.assertEquals(message, expected, actual); + } + + private PojomaticAssert() {} +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/junit/PojomaticAssertTest.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,12 @@ +package org.pojomatic.junit; + +import org.pojomatic.test.AssertTest; + +public class PojomaticAssertTest extends AssertTest { + + @Override + protected void performAssertEquals(Object first, Object second) { + PojomaticAssert.assertEqualsWithDiff(first, second); + } + +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertTest.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,92 @@ +package org.pojomatic.test; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.pojomatic.Pojomatic; +import org.pojomatic.junit.PojomaticAssert; + +/** + * Tests assertion methods such as assertEquals(Object, Object) for correctness of assertions. + */ +public abstract class AssertTest { + + /** + * 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 + * could be either "expected" or "actual". + */ + protected abstract void performAssertEquals(Object first, Object second); + + @Test + public final void assertEqualsBothNull() { + performAssertEquals(null, null); + } + + @Test(expected=AssertionError.class) + public final void assertEqualsNullExpected() { + performAssertEquals(null, new Container(null)); + } + + @Test(expected=AssertionError.class) + public final void assertEqualsNullActual() { + performAssertEquals(new Container(null), null); + } + + /** + * Tests that {@link PojomaticAssert#assertEqualsWithDiff(Object, Object)} + * uses {@link Object#equals(Object)} instead of {@link Pojomatic#equals(Object, Object)}. + */ + @Test(expected=AssertionError.class) + 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); + } + + @Test + public final void assertEqualsMessagingOrder() { + String first = "foo"; + String second = "bar"; + try { + performAssertEquals(new Container(first), new Container(second)); + } + catch (AssertionError e) { + //expected, check the message + assertAppearanceOrder(e.getMessage(), first, second); + } + } + + /** + * Asserts that {@code first} appears before {@code second} in {@code text}. + * + * @param text must contain both {@code first} and {@code second} + * @param first cannot be {@code null} + * @param second cannot be {@code null} + * @throws NullPointerException if {@code first} or {@code second} is {@code null} + * @throws IllegalArgumentException if {@code text} does not contain both + * {@code first} and {@code second} + */ + protected final void assertAppearanceOrder(String text, String first, String second) { + if (first == null || second == null) { + throw new NullPointerException(); + } + int firstIndex = text.indexOf(first); + int secondIndex = text.indexOf(second); + if (firstIndex < 0 || secondIndex < 0) { + throw new IllegalArgumentException("The string \"" + text + "\" does not conatin " + + "both \"" + first + "\" and \"" + second + "\""); + } + String message = "In the string \"" + text + "\", " + + "\"" + first + "\" should come before \"" + second + "\""; + assertTrue(message, firstIndex < secondIndex); + } + +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertUtilsTest.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertUtilsTest.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/AssertUtilsTest.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,40 @@ +package org.pojomatic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class AssertUtilsTest extends AssertTest { + + @Override + protected void performAssertEquals(Object first, Object second) { + AssertUtils.assertEquals(null, first, second); + } + + @Test + public void testEquals() { + assertTrue(AssertUtils.equal(new Container(""), new Container(""))); + } + + @Test + public void testEqualsReflexive() { + Container instance = new Container(""); + assertTrue(AssertUtils.equal(instance, instance)); + } + + @Test + public void testEqualsBothNull() { + assertTrue(AssertUtils.equal(null, null)); + } + + @Test + public void testEqualsNullFirst() { + assertFalse(AssertUtils.equal(null, new Container(null))); + } + + @Test + public void testEqualsNullSecond() { + assertFalse(AssertUtils.equal(new Container(null), null)); + } + +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/Container.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/Container.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/Container.java 2009-08-20 05:37:29 UTC (rev 135) @@ -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 Container { + + private final Object test; + + public Container(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); + } + +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/test/OnlyPojomaticEqual.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,23 @@ +package org.pojomatic.test; + +import org.pojomatic.Pojomatic; +import org.pojomatic.annotations.AutoProperty; + +/** + * Class where every instance is equal via {@link Pojomatic#equals(Object, Object)}, but never + * by {@code this.equals(other)}. + */ +@AutoProperty +public class OnlyPojomaticEqual { + @SuppressWarnings("unused") + private final int number = 3; + + @Override + public boolean equals(Object obj) { + //cannot assert because only the unit under test should throw AssertionError + if (!Pojomatic.equals(this, obj)) { + throw new IllegalStateException("Invariant violated"); + } + return false; + } +} Added: branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java =================================================================== --- branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java (rev 0) +++ branches/TestUtils/PojomaticTestUtils/src/test/java/org/pojomatic/testng/PojomaticAssertTest.java 2009-08-20 05:37:29 UTC (rev 135) @@ -0,0 +1,13 @@ +package org.pojomatic.testng; + +import org.pojomatic.test.AssertTest; + +public class PojomaticAssertTest extends AssertTest { + + @Override + protected void performAssertEquals(Object first, Object second) { + //in TestNG, the arguments are included in any failure message in reverse order + PojomaticAssert.assertEqualsWithDiff(second, first); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |