001    /*
002     * Created on Dec 27, 2006
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with 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
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2006-2009 the original author or authors.
015     */
016    package org.fest.assertions;
017    
018    import static java.math.BigDecimal.ZERO;
019    
020    import java.math.BigDecimal;
021    
022    /**
023     * Understands assertion methods for <code>{@link BigDecimal}</code>. To create a new instance of this class use the
024     * method <code>{@link Assertions#assertThat(BigDecimal)}</code>.
025     *
026     * @author David DIDIER
027     * @author Ted M. Young
028     * @author Yvonne Wang
029     * @author Alex Ruiz
030     */
031    public class BigDecimalAssert extends ComparableAssert<BigDecimal> implements NumberAssert {
032    
033      /**
034       * Creates a new </code>{@link BigDecimalAssert}</code>.
035       * @param actual the target to verify.
036       */
037      protected BigDecimalAssert(BigDecimal actual) {
038        super(actual);
039      }
040    
041      /** {@inheritDoc} */
042      public BigDecimalAssert as(String description) {
043        description(description);
044        return this;
045      }
046    
047      /** {@inheritDoc} */
048      public BigDecimalAssert describedAs(String description) {
049        return as(description);
050      }
051    
052      /** {@inheritDoc} */
053      public BigDecimalAssert as(Description description) {
054        description(description);
055        return this;
056      }
057    
058      /** {@inheritDoc} */
059      public BigDecimalAssert describedAs(Description description) {
060        return as(description);
061      }
062    
063      /**
064       * Verifies that the actual <code>{@link BigDecimal}</code> satisfies the given condition.
065       * @param condition the given condition.
066       * @return this assertion object.
067       * @throws NullPointerException if the given condition is <code>null</code>.
068       * @throws AssertionError if the actual <code>BigDecimal</code> does not satisfy the given condition.
069       * @see #is(Condition)
070       */
071      public BigDecimalAssert satisfies(Condition<BigDecimal> condition) {
072        assertSatisfies(condition);
073        return this;
074      }
075    
076      /**
077       * Verifies that the actual <code>{@link BigDecimal}</code> does not satisfy the given condition.
078       * @param condition the given condition.
079       * @return this assertion object.
080       * @throws NullPointerException if the given condition is <code>null</code>.
081       * @throws AssertionError if the actual value does satisfies the given condition.
082       * @see #isNot(Condition)
083       */
084      public BigDecimalAssert doesNotSatisfy(Condition<BigDecimal> condition) {
085        assertDoesNotSatisfy(condition);
086        return this;
087      }
088    
089      /**
090       * Alias for <code>{@link #satisfies(Condition)}</code>.
091       * @param condition the given condition.
092       * @return this assertion object.
093       * @throws NullPointerException if the given condition is <code>null</code>.
094       * @throws AssertionError if the actual <code>BigDecimal</code> does not satisfy the given condition.
095       * @since 1.2
096       */
097      public BigDecimalAssert is(Condition<BigDecimal> condition) {
098        assertIs(condition);
099        return this;
100      }
101    
102      /**
103       * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>.
104       * @param condition the given condition.
105       * @return this assertion object.
106       * @throws NullPointerException if the given condition is <code>null</code>.
107       * @throws AssertionError if the actual value does satisfies the given condition.
108       * @since 1.2
109       */
110      public BigDecimalAssert isNot(Condition<BigDecimal> condition) {
111        assertIsNot(condition);
112        return this;
113      }
114    
115      /**
116       * Verifies that the actual <code>{@link BigDecimal}</code> is positive.
117       * @return this assertion object.
118       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
119       * @throws AssertionError if the actual <code>BigDecimal</code> value is not positive.
120       */
121      public BigDecimalAssert isPositive() {
122        return isGreaterThan(ZERO);
123      }
124    
125      /**
126       * Verifies that the actual <code>{@link BigDecimal}</code> is negative.
127       * @return this assertion object.
128       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
129       * @throws AssertionError if the actual <code>BigDecimal</code> value is not negative.
130       */
131      public BigDecimalAssert isNegative() {
132        return isLessThan(ZERO);
133      }
134    
135      /**
136       * Verifies that the actual <code>{@link BigDecimal}</code> is equal to zero, regardless of precision.
137       * Essentially, this is the same as
138       * <code>{@link #isEqualByComparingTo(BigDecimal) isEqualByComparingTo}</code>(<code>{@link BigDecimal#ZERO BigDecimal.ZERO}</code>).
139       * @return this assertion object.
140       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
141       * @throws AssertionError if the actual <code>BigDecimal</code> value is not equal to zero.
142       */
143      public BigDecimalAssert isZero() {
144        return isEqualByComparingTo(ZERO);
145      }
146    
147      /**
148       * Verifies that the actual <code>{@link BigDecimal}</code> is not <code>null</code>.
149       * @return this assertion object.
150       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
151       */
152      public BigDecimalAssert isNotNull() {
153        assertNotNull();
154        return this;
155      }
156    
157      /**
158       * Verifies that the actual <code>{@link BigDecimal}</code> is the same as the given one.
159       * @param expected the given <code>BigDecimal</code> to compare the actual <code>BigDecimal</code> to.
160       * @return this assertion object.
161       * @throws AssertionError if the actual <code>BigDecimal</code> value is not the same as the given one.
162       */
163      public BigDecimalAssert isSameAs(BigDecimal expected) {
164        assertSameAs(expected);
165        return this;
166      }
167    
168      /**
169       * Verifies that the actual <code>{@link BigDecimal}</code> is not the same as the given one.
170       * @param other the given <code>BigDecimal</code> to compare the actual <code>BigDecimal</code> to.
171       * @return this assertion object.
172       * @throws AssertionError if the actual <code>BigDecimal</code> value is the same as the given one.
173       */
174      public BigDecimalAssert isNotSameAs(BigDecimal other) {
175        assertNotSameAs(other);
176        return this;
177      }
178    
179      /**
180       * Verifies that the actual <code>{@link BigDecimal}</code> is equal to the given one. Unlike
181       * <code>{@link #isEqualByComparingTo(BigDecimal)}</code>, this method considers two
182       * <code>{@link BigDecimal}</code> objects equal only if they are equal in value and scale (thus 2.0 is not equal to
183       * 2.00 when compared by this method).
184       * @param expected the given <code>BigDecimal</code> to compare the actual <code>BigDecimal</code> to.
185       * @return this assertion object.
186       * @throws AssertionError if the actual <code>BigDecimal</code> value is not equal to the given one.
187       * @see BigDecimal#equals(Object)
188       * @see #isEqualByComparingTo(BigDecimal)
189       */
190      public BigDecimalAssert isEqualTo(BigDecimal expected) {
191        assertEqualTo(expected);
192        return this;
193      }
194    
195      /**
196       * Verifies that the actual <code>{@link BigDecimal}</code> is not equal to the given one.
197       * @param other the given <code>BigDecimal</code> to compare the actual <code>BigDecimal</code> to.
198       * @return this assertion object.
199       * @throws AssertionError if the actual <code>BigDecimal</code> value is equal to the given one.
200       */
201      public BigDecimalAssert isNotEqualTo(BigDecimal other) {
202        assertNotEqualTo(other);
203        return this;
204      }
205    
206      /**
207       * Verifies that the actual <code>{@link BigDecimal}</code> is equal to the given one. Two
208       * <code>{@link BigDecimal}</code> objects that are equal in value but have a different scale (like 2.0 and 2.00)
209       * are considered equal by this method.
210       * @param expected the given <code>BigDecimal</code> to compare the actual <code>BigDecimal</code> to.
211       * @return this assertion object.
212       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
213       * @throws AssertionError if the actual <code>BigDecimal</code> value is not equal to the given one.
214       * @see BigDecimal#compareTo(BigDecimal)
215       */
216      public BigDecimalAssert isEqualByComparingTo(BigDecimal expected) {
217        assertIsEqualByComparingTo(expected);
218        return this;
219      }
220    
221      /**
222       * Verifies that the actual <code>{@link BigDecimal}</code> is <b>not</b> equal to the given one. Two
223       * <code>{@link BigDecimal}</code> objects that are equal in value but have a different scale (like 2.0 and 2.00)
224       * are considered equal by this method.
225       * @param expected the given <code>BigDecimal</code> to use to compare to the actual <code>BigDecimal</code>.
226       * @return this assertion object.
227       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
228       * @throws AssertionError if the actual <code>BigDecimal</code> value is equal to the given one.
229       * @see BigDecimal#compareTo(BigDecimal)
230       */
231      public BigDecimalAssert isNotEqualByComparingTo(BigDecimal expected) {
232        assertIsNotEqualByComparingTo(expected);
233        return this;
234      }
235    
236      /**
237       * Verifies that the actual <code>{@link BigDecimal}</code> value is less than the given one.
238       * @param other the given value.
239       * @return this assertion object.
240       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
241       * @throws AssertionError if the actual <code>BigDecimal</code> value is not less than the given one.
242       */
243      public BigDecimalAssert isLessThan(BigDecimal other) {
244        assertIsLessThan(other);
245        return this;
246      }
247    
248      /**
249       * Verifies that the actual <code>{@link BigDecimal}</code> value is greater than the given one.
250       * @param other the given value.
251       * @return this assertion object.
252       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
253       * @throws AssertionError if the actual <code>BigDecimal</code> value is not greater than the given one.
254       */
255      public BigDecimalAssert isGreaterThan(BigDecimal other) {
256        assertIsGreaterThan(other);
257        return this;
258      }
259    
260      /**
261       * Verifies that the actual <code>{@link BigDecimal}</code> value is less than or equal to the given one.
262       * @param other the given value.
263       * @return this assertion object.
264       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
265       * @throws AssertionError if the actual <code>BigDecimal</code> value is not less than or equal to the given one.
266       */
267      public BigDecimalAssert isLessThanOrEqualTo(BigDecimal other) {
268        assertIsLessThanOrEqualTo(other);
269        return this;
270      }
271    
272      /**
273       * Verifies that the actual <code>{@link BigDecimal}</code> value is greater than or equal to the given one.
274       * @param other the given value.
275       * @return this assertion object.
276       * @throws AssertionError if the actual <code>BigDecimal</code> value is <code>null</code>.
277       * @throws AssertionError if the actual <code>BigDecimal</code> value is not greater than or equal to the given one.
278       */
279      public BigDecimalAssert isGreaterThanOrEqualTo(BigDecimal other) {
280        assertIsGreaterThanOrEqualTo(other);
281        return this;
282      }
283    
284      /** {@inheritDoc} */
285      public BigDecimalAssert overridingErrorMessage(String message) {
286        replaceDefaultErrorMessagesWith(message);
287        return this;
288      }
289    }