com.fitbur.assertj.api.AbstractBigDecimalAssert Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.api;
import java.math.BigDecimal;
import java.util.Comparator;
import com.fitbur.assertj.data.Offset;
import com.fitbur.assertj.data.Percentage;
import com.fitbur.assertj.internal.BigDecimals;
import com.fitbur.assertj.internal.ComparatorBasedComparisonStrategy;
import com.fitbur.assertj.util.VisibleForTesting;
/**
* Base class for all implementations of assertions for {@link BigDecimal}s.
*
* @param the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
* for more details.
*
* @author David DIDIER
* @author Ted M. Young
* @author Yvonne Wang
* @author Alex Ruiz
* @author Joel Costigliola
* @author Mikhail Mazursky
*/
public abstract class AbstractBigDecimalAssert> extends
AbstractComparableAssert implements NumberAssert {
@VisibleForTesting
BigDecimals bigDecimals = BigDecimals.instance();
protected AbstractBigDecimalAssert(BigDecimal actual, Class> selfType) {
super(actual, selfType);
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(BigDecimal.ZERO).isZero();
*
* // assertion will fail
* assertThat(new BigDecimal("8.00")).isZero();
*
*
*/
@Override
public S isZero() {
bigDecimals.assertIsZero(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("8.00")).isNotZero();
*
* // assertion will fail
* assertThat(BigDecimal.ZERO).isNotZero();
*
*
*/
@Override
public S isNotZero() {
bigDecimals.assertIsNotZero(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("8.0")).isPositive();
*
* // assertion will fail
* assertThat(new BigDecimal("-8.0")).isPositive();
*
*
*/
@Override
public S isPositive() {
bigDecimals.assertIsPositive(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("-8.0")).isNegative();
*
* // assertion will fail
* assertThat(new BigDecimal("8.0")).isNegative();
*
*
*/
@Override
public S isNegative() {
bigDecimals.assertIsNegative(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("-8.0")).isNotPositive();
*
* // assertion will fail
* assertThat(new BigDecimal("8.0")).isNotPositive();
*
*
*/
@Override
public S isNotPositive() {
bigDecimals.assertIsNotPositive(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("8.0")).isNotNegative();
*
* // assertion will fail
* assertThat(new BigDecimal("-8.0")).isNotNegative();
*
*
*/
@Override
public S isNotNegative() {
bigDecimals.assertIsNotNegative(info, actual);
return myself;
}
/**
* Verifies that the actual value is in [start, end] range (start and end included).
*
* Example:
*
// assertions will pass
* assertThat(new BigDecimal("8.0")).isBetween(new BigDecimal("7.0"), new BigDecimal("9.0"));
* assertThat(new BigDecimal("8.00")).isBetween(new BigDecimal("7.0"), new BigDecimal("9.0"));
* assertThat(new BigDecimal("8.0")).isBetween(new BigDecimal("8.0"), new BigDecimal("9.0"));
* assertThat(new BigDecimal("8.0")).isBetween(new BigDecimal("7.0"), new BigDecimal("8.0"));
*
* // assertion will fail
* assertThat(new BigDecimal("8.0")).isBetween(new BigDecimal("6.0"), new BigDecimal("7.0"));
*
* Note that comparison of {@link BigDecimal} is done by value without scale consideration, i.e 2.0 and 2.00 are
* considered equal in value (not like {@link BigDecimal#equals(Object)}.
*
*/
@Override
public S isBetween(BigDecimal start, BigDecimal end) {
bigDecimals.assertIsBetween(info, actual, start, end);
return myself;
}
/**
* Verifies that the actual value is in ]start, end[ range (start excluded, end excluded).
*
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("8.0")).isStrictlyBetween(new BigDecimal("7.0"), new BigDecimal("9.0"));
*
* // assertions will fail
* assertThat(new BigDecimal("8.0")).isStrictlyBetween(new BigDecimal("8.0"), new BigDecimal("9.0"));
* assertThat(new BigDecimal("8.0")).isStrictlyBetween(new BigDecimal("7.0"), new BigDecimal("8.0"));
*
*
*/
@Override
public S isStrictlyBetween(BigDecimal start, BigDecimal end) {
bigDecimals.assertIsStrictlyBetween(info, actual, start, end);
return myself;
}
/**
* Same as {@link AbstractAssert#isEqualTo(Object) isEqualTo(BigDecimal)} but takes care of converting given String to
* {@link BigDecimal} for you.
*
* Example:
*
// assertion will pass
* assertThat(new BigDecimal("8.0")).isEqualTo("8.0");
*
* // assertion will fail because 8.00 is not equals to 8.0
* assertThat(new BigDecimal("8.00")).isEqualTo("8.0");
*
*
*/
public S isEqualTo(String expected) {
return isEqualTo(new BigDecimal(expected));
}
/**
* Same as {@link AbstractComparableAssert#isEqualByComparingTo(Comparable) isEqualByComparingTo(BigDecimal)} but
* takes care of converting given String to {@link BigDecimal}.
*
* Example:
*
// assertions will pass
* assertThat(new BigDecimal("8.0")).isEqualByComparingTo("8.0");
* // assertion will pass because 8.0 is equals to 8.00 using {@link BigDecimal#compareTo(Object)}
* assertThat(new BigDecimal("8.0")).isEqualByComparingTo("8.00");
*
* // assertion will fail
* assertThat(new BigDecimal("8.0")).isEqualByComparingTo("2.0");
*/
public S isEqualByComparingTo(String expected) {
return isEqualByComparingTo(new BigDecimal(expected));
}
/**
* Same as {@link AbstractComparableAssert#isNotEqualByComparingTo(Comparable) isNotEqualByComparingTo(BigDecimal)} but
* takes care of converting given String to {@link BigDecimal}.
*
* Example:
*
// assertions will pass
* assertThat(new BigDecimal("8.0")).isNotEqualByComparingTo("7.99");
*
* // assertion will fail
* assertThat(new BigDecimal("8.0")).isNotEqualByComparingTo("8.00");
*/
public S isNotEqualByComparingTo(String expected) {
return isNotEqualByComparingTo(new BigDecimal(expected));
}
@Override
public S usingComparator(Comparator super BigDecimal> customComparator) {
super.usingComparator(customComparator);
this.bigDecimals = new BigDecimals(new ComparatorBasedComparisonStrategy(customComparator));
return myself;
}
@Override
public S usingDefaultComparator() {
super.usingDefaultComparator();
this.bigDecimals = BigDecimals.instance();
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given offset.
* If difference is equal to offset value, assertion is considered valid.
*
* Example:
*
final BigDecimal actual = new BigDecimal("8.1");
* final BigDecimal other = new BigDecimal("8.0");
*
* // valid assertion
* assertThat(actual).isCloseTo(other, within(new BigDecimal("0.2")));
*
* // if difference is exactly equals to given offset value, it's ok
* assertThat(actual).isCloseTo(other, within(new BigDecimal("0.1")));
*
* // BidDecimal format has no impact on the assertion, this assertion is valid:
* assertThat(actual).isCloseTo(new BigDecimal("8.00"), within(new BigDecimal("0.100")));
*
* // but if difference is greater than given offset value assertion will fail :
* assertThat(actual).isCloseTo(other, within(new BigDecimal("0.01")));
*/
@Override
public S isCloseTo(final BigDecimal other, final Offset offset) {
bigDecimals.assertIsCloseTo(info, actual, other, offset);
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given percentage.
* If difference is equal to the percentage value, assertion is considered valid.
*
* Example with BigDecimal:
*
// assertions will pass:
* assertThat(BigDecimal.valueOf(11.0)).isCloseTo(BigDecimal.valueOf(10.0), withinPercentage(BigDecimal.valueOf(20d)));
*
* // if difference is exactly equals to the computed offset (1.0), it's ok
* assertThat(BigDecimal.valueOf(11.0)).isCloseTo(BigDecimal.valueOf(10.0), withinPercentage(BigDecimal.valueOf(10d)));
*
* // assertion will fail
* assertThat(BigDecimal.valueOf(11.0)).isCloseTo(BigDecimal.valueOf(10.0), withinPercentage(BigDecimal.valueOf(5d)));
*
* @param expected the given number to compare the actual value to.
* @param percentage the given positive percentage.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws NullPointerException if the expected number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
@Override
public S isCloseTo(BigDecimal expected, Percentage percentage) {
bigDecimals.assertIsCloseToPercentage(info, actual, expected, percentage);
return myself;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy