/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * <p/> * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { return org.hamcrest.number.BigDecimalCloseTo.closeTo(operand, error); }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<BigDecimal> closeTo(BigDecimal operand, BigDecimal error) { return new BigDecimalCloseTo(operand, error); }
@Override public boolean matchesSafely(BigDecimal item) { return actualDelta(item).compareTo(BigDecimal.ZERO) <= 0; }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { return org.hamcrest.number.BigDecimalCloseTo.closeTo(operand, error); }
@Override public boolean matchesSafely(BigDecimal item) { return actualDelta(item).compareTo(BigDecimal.ZERO) <= 0; }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * <p/> * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ @Factory public static Matcher<BigDecimal> closeTo(BigDecimal operand, BigDecimal error) { return new BigDecimalCloseTo(operand, error); }
@Override protected Matcher<?> createMatcher() { BigDecimal irrelevant = new BigDecimal("0.01"); return closeTo(irrelevant, irrelevant); }
@Override public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)); }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<BigDecimal> closeTo(BigDecimal operand, BigDecimal error) { return new BigDecimalCloseTo(operand, error); }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { return org.hamcrest.number.BigDecimalCloseTo.closeTo(operand, error); }
@Override public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)) .appendText(" more than delta ") .appendValue(delta); }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static Matcher<BigDecimal> closeTo(BigDecimal operand, BigDecimal error) { return new BigDecimalCloseTo(operand, error); }
/** * Creates a matcher of {@link java.math.BigDecimal}s that matches when an examined BigDecimal is equal * to the specified <code>operand</code>, within a range of +/- <code>error</code>. The comparison for equality * is done by BigDecimals {@link java.math.BigDecimal#compareTo(java.math.BigDecimal)} method. * For example: * <pre>assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))</pre> * * @param operand * the expected value of matching BigDecimals * @param error * the delta (+/-) within which matches will be allowed */ public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) { return org.hamcrest.number.BigDecimalCloseTo.closeTo(operand, error); }
@Override public boolean matchesSafely(BigDecimal item) { return actualDelta(item).compareTo(BigDecimal.ZERO) <= 0; }
@Test public void testDeposit() throws Exception { BigDecimal start = service.getBalance(1L); BigDecimal amount = new BigDecimal("50.0"); service.deposit(1L, amount); BigDecimal finish = start.add(amount); assertThat(finish, is(closeTo(service.getBalance(1L), new BigDecimal("0.01")))); }
@Override public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)) .appendText(" more than delta ") .appendValue(delta); }
@Test public void testWithdraw() throws Exception { BigDecimal start = service.getBalance(1L); BigDecimal amount = new BigDecimal("50.0"); service.withdraw(1L, amount); BigDecimal finish = start.subtract(amount); assertThat(finish, is(closeTo(service.getBalance(1L), new BigDecimal("0.01")))); }
@Override public boolean matchesSafely(BigDecimal item) { return actualDelta(item).compareTo(BigDecimal.ZERO) <= 0; }
@Test public void testTransfer() throws Exception { BigDecimal acct1start = service.getBalance(1L); BigDecimal acct2start = service.getBalance(2L); BigDecimal amount = new BigDecimal("50.0"); service.transfer(1L, 2L, amount); BigDecimal acct1finish = acct1start.subtract(amount); BigDecimal acct2finish = acct2start.add(amount); assertThat(acct1finish, is(closeTo(service.getBalance(1L), new BigDecimal("0.01")))); assertThat(acct2finish, is(closeTo(service.getBalance(2L), new BigDecimal("0.01")))); } }
@Override public void describeMismatchSafely(BigDecimal item, Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" differed by ") .appendValue(actualDelta(item)) .appendText(" more than delta ") .appendValue(delta); }