001 /*
002 * Created on Oct 20, 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.Longs;
021 import org.fest.util.ComparatorBasedComparisonStrategy;
022 import org.fest.util.VisibleForTesting;
023
024 /**
025 * Assertion methods for longs.
026 * <p>
027 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Long)}</code> or
028 * <code>{@link Assertions#assertThat(long)}</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 LongAssert extends AbstractComparableAssert<LongAssert, Long> implements NumberAssert<Long> {
038
039 @VisibleForTesting Longs longs = Longs.instance();
040
041 protected LongAssert(Long actual) {
042 super(actual, LongAssert.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 LongAssert isEqualTo(long expected) {
053 longs.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 LongAssert isNotEqualTo(long other) {
065 longs.assertNotEqual(info, actual, other);
066 return this;
067 }
068
069 /** {@inheritDoc} */
070 public LongAssert isZero() {
071 longs.assertIsZero(info, actual);
072 return this;
073 }
074
075 /** {@inheritDoc} */
076 public LongAssert isNotZero() {
077 longs.assertIsNotZero(info, actual);
078 return this;
079 }
080
081 /** {@inheritDoc} */
082 public LongAssert isPositive() {
083 longs.assertIsPositive(info, actual);
084 return this;
085 }
086
087 /** {@inheritDoc} */
088 public LongAssert isNegative() {
089 longs.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 LongAssert isLessThan(long other) {
101 longs.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 LongAssert isLessThanOrEqualTo(long other) {
113 longs.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 LongAssert isGreaterThan(long other) {
125 longs.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 LongAssert isGreaterThanOrEqualTo(long other) {
137 longs.assertGreaterThanOrEqualTo(info, actual, other);
138 return this;
139 }
140
141 @Override
142 public LongAssert usingComparator(Comparator<?> customComparator) {
143 super.usingComparator(customComparator);
144 this.longs = new Longs(new ComparatorBasedComparisonStrategy(customComparator));
145 return myself;
146 }
147
148 @Override
149 public LongAssert usingDefaultComparator() {
150 super.usingDefaultComparator();
151 this.longs = Longs.instance();
152 return myself;
153 }
154 }