001 /*
002 * Created on Feb 8, 2011
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import java.math.BigDecimal;
018 import java.util.Comparator;
019
020 import org.fest.assertions.core.NumberAssert;
021 import org.fest.assertions.internal.BigDecimals;
022 import org.fest.util.ComparatorBasedComparisonStrategy;
023 import org.fest.util.VisibleForTesting;
024
025 /**
026 * Assertion methods for <code>{@link BigDecimal}</code>s.
027 * <p>
028 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(BigDecimal)}</code>.
029 * </p>
030 *
031 * @author David DIDIER
032 * @author Ted M. Young
033 * @author Yvonne Wang
034 * @author Alex Ruiz
035 * @author Joel Costigliola
036 * @author Mikhail Mazursky
037 */
038 public class BigDecimalAssert extends AbstractUnevenComparableAssert<BigDecimalAssert, BigDecimal> implements
039 NumberAssert<BigDecimal> {
040
041 @VisibleForTesting
042 BigDecimals bigDecimals = BigDecimals.instance();
043
044 protected BigDecimalAssert(BigDecimal actual) {
045 super(actual, BigDecimalAssert.class);
046 }
047
048 /** {@inheritDoc} */
049 public BigDecimalAssert isZero() {
050 bigDecimals.assertIsZero(info, actual);
051 return myself;
052 }
053
054 /** {@inheritDoc} */
055 public BigDecimalAssert isNotZero() {
056 bigDecimals.assertIsNotZero(info, actual);
057 return myself;
058 }
059
060 /** {@inheritDoc} */
061 public BigDecimalAssert isPositive() {
062 bigDecimals.assertIsPositive(info, actual);
063 return myself;
064 }
065
066 /** {@inheritDoc} */
067 public BigDecimalAssert isNegative() {
068 bigDecimals.assertIsNegative(info, actual);
069 return myself;
070 }
071
072 /** {@inheritDoc} */
073 public BigDecimalAssert isNotPositive(){
074 bigDecimals.assertIsNotPositive(info, actual);
075 return this;
076 }
077
078 /** {@inheritDoc} */
079 public BigDecimalAssert isNotNegative(){
080 bigDecimals.assertIsNotNegative(info, actual);
081 return this;
082 }
083
084 /**
085 * Same as {@link AbstractAssert#isEqualTo(Object) isEqualTo(BigDecimal)} but takes care of converting given String to
086 * {@link BigDecimal} for you.
087 */
088 public BigDecimalAssert isEqualTo(String expected) {
089 return super.isEqualTo(new BigDecimal(expected));
090 }
091
092 /**
093 * Same as {@link AbstractUnevenComparableAssert#isEqualByComparingTo(Comparable) isEqualByComparingTo(BigDecimal)}
094 * but takes care of converting given String to {@link BigDecimal} for you.
095 */
096 public BigDecimalAssert isEqualByComparingTo(String expected) {
097 return super.isEqualByComparingTo(new BigDecimal(expected));
098 }
099
100 @Override
101 public BigDecimalAssert usingComparator(Comparator<? super BigDecimal> customComparator) {
102 super.usingComparator(customComparator);
103 this.bigDecimals = new BigDecimals(new ComparatorBasedComparisonStrategy(customComparator));
104 return myself;
105 }
106
107 @Override
108 public BigDecimalAssert usingDefaultComparator() {
109 super.usingDefaultComparator();
110 this.bigDecimals = BigDecimals.instance();
111 return myself;
112 }
113 }