From: David S. <ds...@us...> - 2007-07-02 18:11:41
|
Update of /cvsroot/junit/junit/src/org/junit In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit Modified Files: Assert.java Added Files: Assume.java Log Message: - The hamcrest-core-1.1 library is now included in the JUnit distribution. For more hamcrest matchers, see the hamcrest-library jar from http://code.google.com/p/hamcrest - The Popper Theory runner (http://popper.tigris.org) has been absorbed into the JUnit project, under the package name org.junit.experimental.theories - Several additional libraries used in the theories tests have been added in a new testlib directory - New "assertThat" statement to work with hamcrest matchers: // same as assertEquals(3, x) assertThat(x, is(3)); // same as assertNull(y) assertThat(y, nullValue()); - New feature: assumeThat. A failed assumption will cause the test to pass, without further execution. (The behavior of assumeThat may change in the future to allow richer reporting of tests that are skipped because of failed assumptions) // pass on any non-Windows system @Test public void getRootDrive() { assumeThat(getOsString(), is("Windows")); getFile("C:\"); // ... } - Convenience assumption functions: // none of these are null assumeNotNull(a, b, c); assumeTrue(everythingOk()); try { getDatabaseConnection(); } catch (Exception e) { assumeNoException(e); } - Documentation fixed for many assertEquals array methods - Two bugs in numeric equality fixed: 1718905: assertEquals does not compare float correctly 1715326: assertEquals does not compare java.math.BigDecimal properly - The protocol for overriding JUnit4ClassRunner has changed again. Please see the source for details. - Extenders can now extend TestMethod to describe the behavior of running methods that do not have a @Test annotation. - Adding Annotations to Description caused a binary compatibility problem with clients compiled on previous JUnit versions. This has been fixed. --- NEW FILE: Assume.java --- package org.junit; import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.SelfDescribing; import org.hamcrest.StringDescription; import org.junit.experimental.theories.matchers.api.Each; public class Assume { public static class AssumptionViolatedException extends RuntimeException implements SelfDescribing { private static final long serialVersionUID= 1L; private final Object fValue; private final Matcher<?> fMatcher; public AssumptionViolatedException(Object value, Matcher<?> matcher) { super(value instanceof Throwable ? (Throwable) value : null); fValue= value; fMatcher= matcher; } @Override public String getMessage() { return StringDescription.asString(this); } public void describeTo(Description description) { description.appendText("got: "); description.appendValue(fValue); description.appendText(", expected: "); description.appendDescriptionOf(fMatcher); } } public static <T> void assumeThat(T value, Matcher<T> assumption) { if (!assumption.matches(value)) throw new AssumptionViolatedException(value, assumption); } public static void assumeNotNull(Object... objects) { assumeThat(asList(objects), Each.each(notNullValue())); } public static void assumeNoException(Throwable t) { assumeThat(t, nullValue()); } public static void assumeTrue(boolean b) { assumeThat(b, is(true)); } } Index: Assert.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/Assert.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Assert.java 26 Apr 2007 19:56:30 -0000 1.6 +++ Assert.java 2 Jul 2007 18:11:07 -0000 1.7 @@ -2,6 +2,9 @@ import java.lang.reflect.Array; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.StringDescription; import org.junit.internal.ArrayComparisonFailure; /** @@ -100,8 +103,6 @@ } private static boolean isEquals(Object expected, Object actual) { - if (expected instanceof Number && actual instanceof Number) - return ((Number) expected).longValue() == ((Number) actual).longValue(); return expected.equals(actual); } @@ -141,13 +142,11 @@ } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and - * <code>actuals</code> are <code>null</code>, they are considered equal. + * Asserts that two byte arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. * @param message the identifying message or <code>null</code> for the {@link AssertionError} - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values. - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * @param expecteds byte array with expected values. + * @param actuals byte array with actual values */ public static void assertArrayEquals(String message, byte[] expecteds, byte[] actuals) throws ArrayComparisonFailure { @@ -155,25 +154,21 @@ } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} - * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>, - * they are considered equal. - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * Asserts that two byte arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * @param expecteds byte array with expected values. + * @param actuals byte array with actual values */ public static void assertArrayEquals(byte[] expecteds, byte[] actuals) { assertArrayEquals(null, expecteds, actuals); } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and - * <code>actuals</code> are <code>null</code>, they are considered equal. + * Asserts that two char arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. * @param message the identifying message or <code>null</code> for the {@link AssertionError} - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values. - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * @param expecteds char array with expected values. + * @param actuals char array with actual values */ public static void assertArrayEquals(String message, char[] expecteds, char[] actuals) throws ArrayComparisonFailure { @@ -181,25 +176,21 @@ } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} - * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>, - * they are considered equal. - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * Asserts that two char arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * @param expecteds char array with expected values. + * @param actuals char array with actual values */ public static void assertArrayEquals(char[] expecteds, char[] actuals) { assertArrayEquals(null, expecteds, actuals); } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and - * <code>actuals</code> are <code>null</code>, they are considered equal. + * Asserts that two short arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. * @param message the identifying message or <code>null</code> for the {@link AssertionError} - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values. - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * @param expecteds short array with expected values. + * @param actuals short array with actual values */ public static void assertArrayEquals(String message, short[] expecteds, short[] actuals) throws ArrayComparisonFailure { @@ -207,25 +198,21 @@ } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} - * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>, - * they are considered equal. - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * Asserts that two short arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * @param expecteds short array with expected values. + * @param actuals short array with actual values */ public static void assertArrayEquals(short[] expecteds, short[] actuals) { assertArrayEquals(null, expecteds, actuals); } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and - * <code>actuals</code> are <code>null</code>, they are considered equal. + * Asserts that two int arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. * @param message the identifying message or <code>null</code> for the {@link AssertionError} - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values. - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * @param expecteds int array with expected values. + * @param actuals int array with actual values */ public static void assertArrayEquals(String message, int[] expecteds, int[] actuals) throws ArrayComparisonFailure { @@ -233,42 +220,36 @@ } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} - * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>, - * they are considered equal. - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * Asserts that two int arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * @param expecteds int array with expected values. + * @param actuals int array with actual values */ public static void assertArrayEquals(int[] expecteds, int[] actuals) { assertArrayEquals(null, expecteds, actuals); } /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} - * is thrown. If <code>expected</code> and <code>actual</code> are <code>null</code>, - * they are considered equal. - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values - */ - public static void assertArrayEquals(long[] expecteds, long[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * TODO: fix javadoc - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and - * <code>actuals</code> are <code>null</code>, they are considered equal. + * Asserts that two long arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. * @param message the identifying message or <code>null</code> for the {@link AssertionError} - * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values. - * @param actuals Object array or array of arrays (multi-dimensional array) with actual values + * @param expecteds long array with expected values. + * @param actuals long array with actual values */ public static void assertArrayEquals(String message, long[] expecteds, long[] actuals) throws ArrayComparisonFailure { internalArrayEquals(message, expecteds, actuals); } + + /** + * Asserts that two long arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * @param expecteds long array with expected values. + * @param actuals long array with actual values + */ + public static void assertArrayEquals(long[] expecteds, long[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } /** * Asserts that two object arrays are equal. If they are not, an @@ -333,7 +314,23 @@ if (!(Math.abs(expected - actual) <= delta)) failNotEquals(message, new Double(expected), new Double(actual)); } + + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, (Long)expected, (Long)actual); + } + static public void assertEquals(double expected, double actual) { + assertEquals(null, expected, actual); + } + + static public void assertEquals(String message, double expected, double actual) { + fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); + } + /** * Asserts that two doubles or floats are equal to within a positive delta. If they * are not, an {@link AssertionError} is thrown. If the @@ -490,4 +487,19 @@ assertArrayEquals(expecteds, actuals); } + + public static <T> void assertThat(T actual, Matcher<T> matcher) { + assertThat("", actual, matcher); + } + + public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) { + if (!matcher.matches(actual)) { + Description description = new StringDescription(); + description.appendText(reason); + description.appendText("\nExpected: "); + matcher.describeTo(description); + description.appendText("\n got: ").appendValue(actual).appendText("\n"); + throw new java.lang.AssertionError(description.toString()); + } + } } |