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