001 /*
002 * Created on Oct 17, 2010
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 @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import java.util.Comparator;
018
019 import org.fest.assertions.core.NumberAssert;
020 import org.fest.assertions.internal.Integers;
021 import org.fest.util.ComparatorBasedComparisonStrategy;
022 import org.fest.util.VisibleForTesting;
023
024 /**
025 * Assertion methods for integers.
026 * <p>
027 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Integer)}</code> or
028 * <code>{@link Assertions#assertThat(int)}</code>.
029 * </p>
030 *
031 * @author Yvonne Wang
032 * @author David DIDIER
033 * @author Ansgar Konermann
034 * @author Alex Ruiz
035 * @author Joel Costigliola
036 */
037 public class IntegerAssert extends AbstractComparableAssert<IntegerAssert, Integer> implements NumberAssert<Integer> {
038
039 @VisibleForTesting Integers integers = Integers.instance();
040
041 protected IntegerAssert(Integer actual) {
042 super(actual, IntegerAssert.class);
043 }
044
045 /**
046 * Verifies that the actual value is equal to the given one.
047 * @param expected the given value to compare the actual value to.
048 * @return {@code this} assertion object.
049 * @throws AssertionError if the actual value is {@code null}.
050 * @throws AssertionError if the actual value is not equal to the given one.
051 */
052 public IntegerAssert isEqualTo(int expected) {
053 integers.assertEqual(info, actual, expected);
054 return this;
055 }
056
057 /**
058 * Verifies that the actual value is not equal to the given one.
059 * @param other the given value to compare the actual value to.
060 * @return {@code this} assertion object.
061 * @throws AssertionError if the actual value is {@code null}.
062 * @throws AssertionError if the actual value is equal to the given one.
063 */
064 public IntegerAssert isNotEqualTo(int other) {
065 integers.assertNotEqual(info, actual, other);
066 return this;
067 }
068
069 /** {@inheritDoc} */
070 public IntegerAssert isZero() {
071 integers.assertIsZero(info, actual);
072 return this;
073 }
074
075 /** {@inheritDoc} */
076 public IntegerAssert isNotZero() {
077 integers.assertIsNotZero(info, actual);
078 return this;
079 }
080
081 /** {@inheritDoc} */
082 public IntegerAssert isPositive() {
083 integers.assertIsPositive(info, actual);
084 return this;
085 }
086
087 /** {@inheritDoc} */
088 public IntegerAssert isNegative() {
089 integers.assertIsNegative(info, actual);
090 return this;
091 }
092
093 /**
094 * Verifies that the actual value is less than the given one.
095 * @param other the given value to compare the actual value to.
096 * @return {@code this} assertion object.
097 * @throws AssertionError if the actual value is {@code null}.
098 * @throws AssertionError if the actual value is equal to or greater than the given one.
099 */
100 public IntegerAssert isLessThan(int other) {
101 integers.assertLessThan(info, actual, other);
102 return this;
103 }
104
105 /**
106 * Verifies that the actual value is less than or equal to the given one.
107 * @param other the given value to compare the actual value to.
108 * @return {@code this} assertion object.
109 * @throws AssertionError if the actual value is {@code null}.
110 * @throws AssertionError if the actual value is greater than the given one.
111 */
112 public IntegerAssert isLessThanOrEqualTo(int other) {
113 integers.assertLessThanOrEqualTo(info, actual, other);
114 return this;
115 }
116
117 /**
118 * Verifies that the actual value is greater than the given one.
119 * @param other the given value to compare the actual value to.
120 * @return {@code this} assertion object.
121 * @throws AssertionError if the actual value is {@code null}.
122 * @throws AssertionError if the actual value is equal to or less than the given one.
123 */
124 public IntegerAssert isGreaterThan(int other) {
125 integers.assertGreaterThan(info, actual, other);
126 return this;
127 }
128
129 /**
130 * Verifies that the actual value is greater than or equal to the given one.
131 * @param other the given value to compare the actual value to.
132 * @return {@code this} assertion object.
133 * @throws AssertionError if the actual value is {@code null}.
134 * @throws AssertionError if the actual value is less than the given one.
135 */
136 public IntegerAssert isGreaterThanOrEqualTo(int other) {
137 integers.assertGreaterThanOrEqualTo(info, actual, other);
138 return this;
139 }
140
141 @Override
142 public IntegerAssert usingComparator(Comparator<?> customComparator) {
143 super.usingComparator(customComparator);
144 this.integers = new Integers(new ComparatorBasedComparisonStrategy(customComparator));
145 return myself;
146 }
147
148 @Override
149 public IntegerAssert usingDefaultComparator() {
150 super.usingDefaultComparator();
151 this.integers = Integers.instance();
152 return myself;
153 }
154 }