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-2009 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static org.fest.assertions.ErrorMessages.*;
018    
019    /**
020     * Understands assertion methods for <code>byte</code>s. To create a new instance of this class use the
021     * method <code>{@link Assertions#assertThat(byte)}</code>.
022     *
023     * @author Yvonne Wang
024     * @author David DIDIER
025     *
026     * @since 1.2
027     */
028    public class ByteAssert extends PrimitiveAssert implements NumberAssert {
029    
030      private static final byte ZERO = (byte)0;
031    
032      private final byte actual;
033    
034      /**
035       * Creates a new </code>{@link ByteAssert}</code>.
036       * @param actual the target to verify.
037       */
038      protected ByteAssert(byte actual) {
039        this.actual = actual;
040      }
041    
042      /**
043       * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
044       * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
045       * failure will not show the provided description.
046       * <p>
047       * For example:
048       * <pre>
049       * assertThat(value).<strong>as</strong>(&quot;Some value&quot;).isEqualTo(otherValue);
050       * </pre>
051       * </p>
052       * @param description the description of the actual value.
053       * @return this assertion object.
054       */
055      public ByteAssert as(String description) {
056        description(description);
057        return this;
058      }
059    
060      /**
061       * Alias for <code>{@link #as(String)}</code>, since "as" is a keyword in
062       * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
063       * method, otherwise any assertion failure will not show the provided description.
064       * <p>
065       * For example:
066       * <pre>
067       * assertThat(value).<strong>describedAs</strong>(&quot;Some value&quot;).isEqualTo(otherValue);
068       * </pre>
069       * </p>
070       * @param description the description of the actual value.
071       * @return this assertion object.
072       */
073      public ByteAssert describedAs(String description) {
074        return as(description);
075      }
076    
077      /**
078       * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
079       * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
080       * failure will not show the provided description.
081       * <p>
082       * For example:
083       * <pre>
084       * assertThat(value).<strong>as</strong>(new BasicDescription(&quot;Some value&quot;)).isEqualTo(otherValue);
085       * </pre>
086       * </p>
087       * @param description the description of the actual value.
088       * @return this assertion object.
089       */
090      public ByteAssert as(Description description) {
091        description(description);
092        return this;
093      }
094    
095      /**
096       * Alias for <code>{@link #as(Description)}</code>, since "as" is a keyword in
097       * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
098       * method, otherwise any assertion failure will not show the provided description.
099       * <p>
100       * For example:
101       * <pre>
102       * assertThat(value).<strong>describedAs</strong>(new BasicDescription(&quot;Some value&quot;)).isEqualTo(otherValue);
103       * </pre>
104       * </p>
105       * @param description the description of the actual value.
106       * @return this assertion object.
107       */
108      public ByteAssert describedAs(Description description) {
109        return as(description);
110      }
111    
112      /**
113       * Verifies that the actual <code>byte</code> value is equal to the given one.
114       * @param expected the value to compare the actual one to.
115       * @return this assertion object.
116       * @throws AssertionError if the actual <code>byte</code> value is not equal to the given one.
117       */
118      public ByteAssert isEqualTo(byte expected) {
119        if (actual == expected) return this;
120        failIfCustomMessageIsSet();
121        throw fail(unexpectedNotEqual(actual, expected));
122      }
123    
124      /**
125       * Verifies that the actual <code>byte</code> value is not equal to the given one.
126       * @param other the given value.
127       * @return this assertion object.
128       * @throws AssertionError if the actual <code>byte</code> value is equal to the given one.
129       */
130      public ByteAssert isNotEqualTo(byte other) {
131        if (actual != other) return this;
132        failIfCustomMessageIsSet();
133        throw failure(unexpectedEqual(actual, other));
134      }
135    
136      /**
137       * Verifies that the actual <code>byte</code> value is greater than the given one.
138       * @param other the given value.
139       * @return this assertion object.
140       * @throws AssertionError if the actual <code>byte</code> value is not greater than the given one.
141       */
142      public ByteAssert isGreaterThan(byte other) {
143        if (actual > other) return this;
144        failIfCustomMessageIsSet();
145        throw failure(unexpectedLessThanOrEqualTo(actual, other));
146      }
147    
148      /**
149       * Verifies that the actual <code>byte</code> value is less than the given one.
150       * @param other the given value.
151       * @return this assertion object.
152       * @throws AssertionError if the actual <code>byte</code> value is not less than the given one.
153       */
154      public ByteAssert isLessThan(byte other) {
155        if (actual < other) return this;
156        failIfCustomMessageIsSet();
157        throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
158      }
159    
160      /**
161       * Verifies that the actual <code>byte</code> value is greater or equal to the given one.
162       * @param other the given value.
163       * @return this assertion object.
164       * @throws AssertionError if the actual <code>byte</code> value is not greater than or equal to the given one.
165       */
166      public ByteAssert isGreaterThanOrEqualTo(byte other) {
167        if (actual >= other) return this;
168        failIfCustomMessageIsSet();
169        throw failure(unexpectedLessThan(actual, other));
170      }
171    
172      /**
173       * Verifies that the actual <code>byte</code> value is less or equal to the given one.
174       * @param other the given value.
175       * @return this assertion object.
176       * @throws AssertionError if the actual <code>byte</code> value is not less than or equal to the given one.
177       */
178      public ByteAssert isLessThanOrEqualTo(byte other) {
179        if (actual <= other) return this;
180        failIfCustomMessageIsSet();
181        throw failure(unexpectedGreaterThan(actual, other));
182      }
183    
184      /**
185       * Verifies that the actual <code>byte</code> value is equal to zero.
186       * @return this assertion object.
187       * @throws AssertionError if the actual <code>byte</code> value is not equal to zero.
188       */
189      public ByteAssert isZero() {
190        return isEqualTo(ZERO);
191      }
192    
193      /**
194       * Verifies that the actual <code>byte</code> value is positive.
195       * @return this assertion object.
196       * @throws AssertionError if the actual <code>byte</code> value is not positive.
197       */
198      public ByteAssert isPositive() {
199        return isGreaterThan(ZERO);
200      }
201    
202      /**
203       * Verifies that the actual <code>byte</code> value is negative.
204       * @return this assertion object.
205       * @throws AssertionError if the actual <code>byte</code> value is not negative.
206       */
207      public ByteAssert isNegative() {
208        return isLessThan(ZERO);
209      }
210    
211      /** {@inheritDoc} */
212      public ByteAssert overridingErrorMessage(String message) {
213        replaceDefaultErrorMessagesWith(message);
214        return this;
215      }
216    }