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