Permalink
Browse files

Merge remote-tracking branch 'KentBeck/master' into hamcrest-1.3

Conflicts:
	src/main/java/org/junit/Assert.java
	src/main/java/org/junit/matchers/JUnitMatchers.java
	src/main/java/org/junit/rules/ExpectedException.java
	src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java
  • Loading branch information...
2 parents 8d783ef + 28938e9 commit ee037247a9a03d8ac2e584d52a544b9733f2c016 @marcphilipp marcphilipp committed Jul 15, 2012
Showing with 669 additions and 63 deletions.
  1. +4 −0 acknowledgements.txt
  2. +5 −9 src/main/java/junit/framework/Assert.java
  3. +1 −0 src/main/java/org/junit/After.java
  4. +1 −0 src/main/java/org/junit/AfterClass.java
  5. +187 −8 src/main/java/org/junit/Assert.java
  6. +1 −0 src/main/java/org/junit/Assume.java
  7. +1 −0 src/main/java/org/junit/Before.java
  8. +1 −0 src/main/java/org/junit/BeforeClass.java
  9. +1 −0 src/main/java/org/junit/ClassRule.java
  10. +1 −0 src/main/java/org/junit/ComparisonFailure.java
  11. +1 −0 src/main/java/org/junit/FixMethodOrder.java
  12. +1 −0 src/main/java/org/junit/Ignore.java
  13. +1 −0 src/main/java/org/junit/Rule.java
  14. +1 −0 src/main/java/org/junit/Test.java
  15. +7 −3 src/main/java/org/junit/internal/InexactComparisonCriteria.java
  16. +9 −7 src/main/java/org/junit/internal/MethodSorter.java
  17. +22 −0 src/main/java/org/junit/internal/matchers/CauseMatcher.java
  18. +15 −2 src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java
  19. +10 −0 src/main/java/org/junit/matchers/JUnitMatchers.java
  20. +1 −3 src/main/java/org/junit/rules/ErrorCollector.java
  21. +34 −2 src/main/java/org/junit/rules/ExpectedException.java
  22. +1 −0 src/main/java/org/junit/rules/ExternalResource.java
  23. +1 −0 src/main/java/org/junit/rules/MethodRule.java
  24. +1 −3 src/main/java/org/junit/rules/RuleChain.java
  25. +1 −0 src/main/java/org/junit/rules/RunRules.java
  26. +19 −1 src/main/java/org/junit/rules/TemporaryFolder.java
  27. +1 −0 src/main/java/org/junit/rules/TestName.java
  28. +1 −0 src/main/java/org/junit/rules/TestRule.java
  29. +5 −4 src/main/java/org/junit/rules/TestWatcher.java
  30. +1 −0 src/main/java/org/junit/rules/TestWatchman.java
  31. +1 −3 src/main/java/org/junit/rules/Timeout.java
  32. +2 −1 src/main/java/org/junit/rules/Verifier.java
  33. +1 −0 src/main/java/org/junit/runner/Computer.java
  34. +1 −0 src/main/java/org/junit/runner/Describable.java
  35. +1 −0 src/main/java/org/junit/runner/Description.java
  36. +1 −0 src/main/java/org/junit/runner/JUnitCore.java
  37. +1 −0 src/main/java/org/junit/runner/Request.java
  38. +3 −3 src/main/java/org/junit/runner/Result.java
  39. +1 −0 src/main/java/org/junit/runner/RunWith.java
  40. +1 −0 src/main/java/org/junit/runner/Runner.java
  41. +1 −0 src/main/java/org/junit/runner/manipulation/Filter.java
  42. +1 −0 src/main/java/org/junit/runner/manipulation/Filterable.java
  43. +1 −0 src/main/java/org/junit/runner/manipulation/NoTestsRemainException.java
  44. +1 −0 src/main/java/org/junit/runner/manipulation/Sortable.java
  45. +1 −1 src/main/java/org/junit/runner/manipulation/Sorter.java
  46. +1 −0 src/main/java/org/junit/runner/notification/Failure.java
  47. +1 −0 src/main/java/org/junit/runner/notification/RunListener.java
  48. +1 −0 src/main/java/org/junit/runner/notification/RunNotifier.java
  49. +1 −0 src/main/java/org/junit/runner/notification/StoppedByUserException.java
  50. +1 −0 src/main/java/org/junit/runners/AllTests.java
  51. +1 −0 src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
  52. +1 −0 src/main/java/org/junit/runners/JUnit4.java
  53. +10 −3 src/main/java/org/junit/runners/MethodSorters.java
  54. +1 −0 src/main/java/org/junit/runners/Parameterized.java
  55. +1 −0 src/main/java/org/junit/runners/ParentRunner.java
  56. +1 −0 src/main/java/org/junit/runners/Suite.java
  57. +1 −0 src/main/java/org/junit/runners/model/FrameworkField.java
  58. +5 −0 src/main/java/org/junit/runners/model/FrameworkMember.java
  59. +1 −0 src/main/java/org/junit/runners/model/FrameworkMethod.java
  60. +1 −0 src/main/java/org/junit/runners/model/InitializationError.java
  61. +1 −0 src/main/java/org/junit/runners/model/MultipleFailureException.java
  62. +1 −0 src/main/java/org/junit/runners/model/RunnerBuilder.java
  63. +1 −0 src/main/java/org/junit/runners/model/RunnerScheduler.java
  64. +1 −3 src/main/java/org/junit/runners/model/Statement.java
  65. +1 −0 src/main/java/org/junit/runners/model/TestClass.java
  66. +1 −0 src/test/java/org/junit/internal/MethodSorterTest.java
  67. +2 −0 src/test/java/org/junit/tests/AllTests.java
  68. +82 −0 src/test/java/org/junit/tests/assertion/AssertionTest.java
  69. +18 −0 src/test/java/org/junit/tests/assertion/CauseMatcherTest.java
  70. +16 −0 src/test/java/org/junit/tests/experimental/rules/EventCollector.java
  71. +62 −1 src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java
  72. +20 −3 src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java
  73. +18 −1 src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java
  74. +63 −2 src/test/java/org/junit/tests/internal/runners/statements/FailOnTimeoutTest.java
View
@@ -108,6 +108,10 @@
Daniel Rothmaler (drothmaler@github):
#299: random temp file/folder creation
#300: ErrorCollector.checkThat overload
+
+2011 Jun 25
+ Juan Cortez (jcortez@github): Fixed issue #219 where floats were being
+ printed and represented as doubles in error messages.
2011 Jul 06
Stefan Birkner: Fixed wrong documentation of ClassRule (github#254).
@@ -226,8 +226,8 @@ static public void assertNotNull(String message, Object object) {
* Object to check or <code>null</code>
*/
static public void assertNull(Object object) {
- String message = "Expected: <null> but was: " + String.valueOf(object);
- assertNull(message, object);
+ if (object != null)
+ assertNull("Expected: <null> but was: " + object.toString(), object);
}
/**
* Asserts that an object is null. If it is not
@@ -270,16 +270,12 @@ static public void assertNotSame(Object expected, Object actual) {
}
static public void failSame(String message) {
- String formatted= "";
- if (message != null)
- formatted= message+" ";
- fail(formatted+"expected not same");
+ String formatted= (message != null) ? message+" " : "";
+ fail(formatted+"expected not same");
}
static public void failNotSame(String message, Object expected, Object actual) {
- String formatted= "";
- if (message != null)
- formatted= message+" ";
+ String formatted= (message != null) ? message+" " : "";
fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
}
@@ -31,6 +31,7 @@
*
* @see org.junit.Before
* @see org.junit.Test
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -34,6 +34,7 @@
*
* @see org.junit.BeforeClass
* @see org.junit.Test
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@@ -19,6 +19,7 @@
* </pre>
*
* @see AssertionError
+ * @since 4.0
*/
public class Assert {
/**
@@ -115,9 +116,7 @@ static public void fail() {
*/
static public void assertEquals(String message, Object expected,
Object actual) {
- if (expected == null && actual == null)
- return;
- if (expected != null && isEquals(expected, actual))
+ if (equalsRegardingNull(expected, actual))
return;
else if (expected instanceof String && actual instanceof String) {
String cleanMessage= message == null ? "" : message;
@@ -126,6 +125,13 @@ else if (expected instanceof String && actual instanceof String) {
} else
failNotEquals(message, expected, actual);
}
+
+ private static boolean equalsRegardingNull(Object expected, Object actual) {
+ if (expected == null)
+ return actual == null;
+
+ return isEquals(expected, actual);
+ }
private static boolean isEquals(Object expected, Object actual) {
return expected.equals(actual);
@@ -147,6 +153,120 @@ static public void assertEquals(Object expected, Object actual) {
}
/**
+ * Asserts that two objects are <b>not</b> equals. If they are, an
+ * {@link AssertionError} is thrown with the given message. If
+ * <code>first</code> and <code>second</code> are <code>null</code>,
+ * they are considered equal.
+ * @param message
+ * the identifying message for the {@link AssertionError} (<code>null</code>
+ * okay)
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ */
+ static public void assertNotEquals(String message, Object first,
+ Object second) {
+ if (equalsRegardingNull(first, second))
+ failEquals(message, first);
+ }
+
+ /**
+ * Asserts that two objects are <b>not</b> equals. If they are, an
+ * {@link AssertionError} without a message is thrown. If
+ * <code>first</code> and <code>second</code> are <code>null</code>,
+ * they are considered equal.
+ *
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ */
+ static public void assertNotEquals(Object first, Object second) {
+ assertNotEquals(null, first, second);
+ }
+
+ private static void failEquals(String message, Object actual) {
+ String formatted = "Values should be different. ";
+ if (message != null)
+ formatted = message + ". ";
+
+ formatted += "Actual: " + actual;
+ fail(formatted);
+ }
+
+ /**
+ * Asserts that two longs are <b>not</b> equals. If they are, an
+ * {@link AssertionError} is thrown with the given message.
+ * @param message
+ * the identifying message for the {@link AssertionError} (<code>null</code>
+ * okay)
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ */
+ static public void assertNotEquals(String message, long first, long second) {
+ assertNotEquals(message, (Long) first, (Long) second);
+ }
+
+ /**
+ * Asserts that two longs are <b>not</b> equals. If they are, an
+ * {@link AssertionError} without a message is thrown.
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ */
+ static public void assertNotEquals(long first, long second) {
+ assertNotEquals(null, first, second);
+ }
+
+ /**
+ * Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
+ * If they are, an {@link AssertionError} is thrown with the given
+ * message. If the expected value is infinity then the delta value is
+ * ignored. NaNs are considered equal:
+ * <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
+ *
+ * @param message
+ * the identifying message for the {@link AssertionError} (<code>null</code>
+ * okay)
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+ static public void assertNotEquals(String message, double first,
+ double second, double delta) {
+ if (!doubleIsDifferent(first, second, delta))
+ failEquals(message, new Double(first));
+ }
+
+ /**
+ * Asserts that two doubles or floats are <b>not</b> equal to within a positive delta.
+ * If they are, an {@link AssertionError} is thrown. If the expected
+ * value is infinity then the delta value is ignored.NaNs are considered
+ * equal: <code>assertNotEquals(Double.NaN, Double.NaN, *)</code> fails
+ *
+ * @param first
+ * first value to check
+ * @param second
+ * the value to check against <code>first</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+ static public void assertNotEquals(double first, double second, double delta) {
+ assertNotEquals(null, first, second, delta);
+ }
+
+ /**
* 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>,
@@ -416,7 +536,7 @@ private static void internalArrayEquals(String message, Object expecteds,
}
/**
- * Asserts that two doubles or floats are equal to within a positive delta.
+ * Asserts that two doubles are equal to within a positive delta.
* If they are not, an {@link AssertionError} is thrown with the given
* message. If the expected value is infinity then the delta value is
* ignored. NaNs are considered equal:
@@ -436,10 +556,43 @@ private static void internalArrayEquals(String message, Object expecteds,
*/
static public void assertEquals(String message, double expected,
double actual, double delta) {
- if (Double.compare(expected, actual) == 0)
+ if (doubleIsDifferent(expected, actual, delta))
+ failNotEquals(message, new Double(expected), new Double(actual));
+ }
+
+ /**
+ * Asserts that two floats are equal to within a positive delta.
+ * If they are not, an {@link AssertionError} is thrown with the given
+ * message. If the expected value is infinity then the delta value is
+ * ignored. NaNs are considered equal:
+ * <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
+ *
+ * @param message
+ * the identifying message for the {@link AssertionError} (<code>null</code>
+ * okay)
+ * @param expected
+ * expected value
+ * @param actual
+ * the value to check against <code>expected</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+ static public void assertEquals(String message, float expected,
+ float actual, float delta) {
+ if (Float.compare(expected, actual) == 0)
return;
if (!(Math.abs(expected - actual) <= delta))
- failNotEquals(message, new Double(expected), new Double(actual));
+ failNotEquals(message, new Float(expected), new Float(actual));
+ }
+ static private boolean doubleIsDifferent(double d1, double d2, double delta) {
+ if (Double.compare(d1, d2) == 0)
+ return false;
+ if ((Math.abs(d1 - d2) <= delta))
+ return false;
+
+ return true;
}
/**
@@ -493,7 +646,7 @@ static public void assertEquals(String message, double expected,
}
/**
- * Asserts that two doubles or floats are equal to within a positive delta.
+ * Asserts that two doubles are equal to within a positive delta.
* If they are not, an {@link AssertionError} is thrown. If the expected
* value is infinity then the delta value is ignored.NaNs are considered
* equal: <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
@@ -510,6 +663,26 @@ static public void assertEquals(String message, double expected,
static public void assertEquals(double expected, double actual, double delta) {
assertEquals(null, expected, actual, delta);
}
+
+ /**
+ * Asserts that two floats are equal to within a positive delta.
+ * If they are not, an {@link AssertionError} is thrown. If the expected
+ * value is infinity then the delta value is ignored. NaNs are considered
+ * equal: <code>assertEquals(Float.NaN, Float.NaN, *)</code> passes
+ *
+ * @param expected
+ * expected value
+ * @param actual
+ * the value to check against <code>expected</code>
+ * @param delta
+ * the maximum delta between <code>expected</code> and
+ * <code>actual</code> for which both numbers are still
+ * considered equal.
+ */
+
+ static public void assertEquals(float expected, float actual, float delta) {
+ assertEquals(null, expected, actual, delta);
+ }
/**
* Asserts that an object isn't null. If it is an {@link AssertionError} is
@@ -786,6 +959,12 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
*/
public static <T> void assertThat(String reason, T actual,
Matcher<? super T> matcher) {
- MatcherAssert.assertThat(reason, actual, matcher);
+ try {
+ MatcherAssert.assertThat(reason, actual, matcher);
+ } catch (AssertionError assertionError) {
+ if (actual instanceof Throwable)
+ assertionError.initCause((Throwable) actual);
+ throw assertionError;
+ }
}
}
@@ -29,6 +29,7 @@
* ...
* assumeTrue(...);
* </pre>
+ * @since 4.4
*/
public class Assume {
/**
@@ -31,6 +31,7 @@
*
* @see org.junit.BeforeClass
* @see org.junit.After
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@@ -28,6 +28,7 @@
* }
* </pre>
* @see org.junit.AfterClass
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@@ -79,6 +79,7 @@
* </pre>
*
* For more information and more examples, see {@link org.junit.rules.TestRule}.
+ * @since 4.9
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@@ -6,6 +6,7 @@
* strings.
*
* Inspired by a patch from Alex Chaffee (alex@purpletech.com)
+ * @since 4.0
*/
public class ComparisonFailure extends AssertionError {
/**
@@ -30,6 +30,7 @@
* </pre>
*
* @see org.junit.runners.MethodSorters
+ * @since 4.11
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@@ -28,6 +28,7 @@
* }
* </pre>
*
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@@ -62,6 +62,7 @@
* Note: for backwards compatibility, this annotation may also mark
* fields or methods of type {@link org.junit.rules.MethodRule}, which will be honored. However,
* this is a deprecated interface and feature.
+ * @since 4.7
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@@ -41,6 +41,7 @@
* while(true);
* }
* </pre></p>
+ * @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
Oops, something went wrong.

0 comments on commit ee03724

Please sign in to comment.