001    /*
002     * Created on Jun 18, 2007
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 @2007-2011 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static java.lang.Long.valueOf;
018    import static org.fest.assertions.ErrorMessages.*;
019    
020    /**
021     * Assertions for {@code Long}s and {@code long}s.
022     * <p>
023     * To create a new instance of this class invoke either <code>{@link Assertions#assertThat(Long)}</code> or
024     * <code>{@link Assertions#assertThat(long)}</code>.
025     * </p>
026     *
027     * @author Yvonne Wang
028     * @author David DIDIER
029     * @author Ansgar Konermann
030     * @author Alex Ruiz
031     */
032    public class LongAssert extends GenericAssert<LongAssert, Long> implements NumberAssert {
033    
034      private static final long ZERO = 0L;
035    
036      /**
037       * Creates a new <code>{@link LongAssert}</code>.
038       * @param actual the actual value to verify.
039       */
040      protected LongAssert(long actual) {
041        super(LongAssert.class, actual);
042      }
043    
044      /**
045       * Creates a new <code>{@link LongAssert}</code>.
046       * @param actual the actual value to verify.
047       */
048      protected LongAssert(Long actual) {
049        super(LongAssert.class, actual);
050      }
051    
052      /**
053       * Verifies that the actual {@code Long} is equal to the given one.
054       * @param expected the value to compare the actual one to.
055       * @return this assertion object.
056       * @throws AssertionError if the actual {@code Long} is not equal to the given one.
057       */
058      public LongAssert isEqualTo(long expected) {
059        return isEqualTo(valueOf(expected));
060      }
061    
062      /**
063       * Verifies that the actual {@code Long} is not equal to the given one.
064       * @param other the given value.
065       * @return this assertion object.
066       * @throws AssertionError if the actual {@code Long} is equal to the given one.
067       */
068      public LongAssert isNotEqualTo(long other) {
069        return isNotEqualTo(valueOf(other));
070      }
071    
072      /**
073       * Verifies that the actual {@code Long} is greater than the given one.
074       * @param other the given value.
075       * @return this assertion object.
076       * @throws AssertionError if the actual {@code Long} is not greater than the given one.
077       */
078      public LongAssert isGreaterThan(long other) {
079        if (actual > other) return this;
080        failIfCustomMessageIsSet();
081        throw failure(unexpectedLessThanOrEqualTo(actual, other));
082      }
083    
084      /**
085       * Verifies that the actual {@code Long} is less than the given one.
086       * @param other the given value.
087       * @return this assertion object.
088       * @throws AssertionError if the actual {@code Long} is not less than the given one.
089       */
090      public LongAssert isLessThan(long other) {
091        if (actual < other) return this;
092        failIfCustomMessageIsSet();
093        throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
094      }
095    
096      /**
097       * Verifies that the actual {@code Long} is greater or equal to the given one.
098       * @param other the given value.
099       * @return this assertion object.
100       * @throws AssertionError if the actual {@code Long} is not greater than or equal to the given one.
101       */
102      public LongAssert isGreaterThanOrEqualTo(long other) {
103        if (actual >= other) return this;
104        failIfCustomMessageIsSet();
105        throw failure(unexpectedLessThan(actual, other));
106      }
107    
108      /**
109       * Verifies that the actual {@code Long} is less or equal to the given one.
110       * @param other the given value.
111       * @return this assertion object.
112       * @throws AssertionError if the actual {@code Long} is not less than or equal to the given one.
113       */
114      public LongAssert isLessThanOrEqualTo(long other) {
115        if (actual <= other) return this;
116        failIfCustomMessageIsSet();
117        throw failure(unexpectedGreaterThan(actual, other));
118      }
119    
120      /**
121       * Verifies that the actual {@code Long} is equal to zero.
122       * @return this assertion object.
123       * @throws AssertionError if the actual {@code Long} is not equal to zero.
124       */
125      public LongAssert isZero() {
126        return isEqualTo(ZERO);
127      }
128    
129      /**
130       * Verifies that the actual {@code Long} is positive.
131       * @return this assertion object.
132       * @throws AssertionError if the actual {@code Long} is not positive.
133       */
134      public LongAssert isPositive() {
135        return isGreaterThan(ZERO);
136      }
137    
138      /**
139       * Verifies that the actual {@code Long} is negative.
140       * @return this assertion object.
141       * @throws AssertionError if the actual {@code Long} is not negative.
142       */
143      public LongAssert isNegative() {
144        return isLessThan(ZERO);
145      }
146    }