001 /*
002 * Created on Oct 25, 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.FloatingPointNumberAssert;
020 import org.fest.assertions.data.Offset;
021 import org.fest.assertions.internal.Doubles;
022 import org.fest.util.ComparatorBasedComparisonStrategy;
023 import org.fest.util.VisibleForTesting;
024
025 /**
026 * Assertion methods for doubles.
027 * <p>
028 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Double)}</code> or
029 * <code>{@link Assertions#assertThat(double)}</code>.
030 * </p>
031 *
032 * @author Yvonne Wang
033 * @author David DIDIER
034 * @author Alex Ruiz
035 * @author Ansgar Konermann
036 * @author Joel Costigliola
037 * @author Mikhail Mazursky
038 */
039 public class DoubleAssert extends AbstractComparableAssert<DoubleAssert, Double> implements
040 FloatingPointNumberAssert<Double> {
041
042 @VisibleForTesting
043 Doubles doubles = Doubles.instance();
044
045 protected DoubleAssert(Double actual) {
046 super(actual, DoubleAssert.class);
047 }
048
049 /** {@inheritDoc} */
050 public DoubleAssert isNaN() {
051 doubles.assertIsNaN(info, actual);
052 return this;
053 }
054
055 /** {@inheritDoc} */
056 public DoubleAssert isNotNaN() {
057 doubles.assertIsNotNaN(info, actual);
058 return this;
059 }
060
061 /** {@inheritDoc} */
062 public DoubleAssert isZero() {
063 doubles.assertIsZero(info, actual);
064 return this;
065 }
066
067 /** {@inheritDoc} */
068 public DoubleAssert isNotZero() {
069 doubles.assertIsNotZero(info, actual);
070 return this;
071 }
072
073 /** {@inheritDoc} */
074 public DoubleAssert isPositive() {
075 doubles.assertIsPositive(info, actual);
076 return this;
077 }
078
079 /** {@inheritDoc} */
080 public DoubleAssert isNegative() {
081 doubles.assertIsNegative(info, actual);
082 return this;
083 }
084
085 /**
086 * Verifies that the actual value is equal to the given one.
087 * @param expected the given value to compare the actual value to.
088 * @return {@code this} assertion object.
089 * @throws AssertionError if the actual value is {@code null}.
090 * @throws AssertionError if the actual value is not equal to the given one.
091 */
092 public DoubleAssert isEqualTo(double expected) {
093 doubles.assertEqual(info, actual, expected);
094 return this;
095 }
096
097 /** {@inheritDoc} */
098 public DoubleAssert isEqualTo(Double expected, Offset<Double> offset) {
099 doubles.assertEqual(info, actual, expected, offset);
100 return this;
101 }
102
103 /**
104 * Verifies that the actual value is equal to the given one, within a positive offset.
105 * @param expected the given value to compare the actual value to.
106 * @param offset the given positive offset.
107 * @return {@code this} assertion object.
108 * @throws NullPointerException if the given offset is {@code null}.
109 * @throws AssertionError if the actual value is {@code null}.
110 * @throws AssertionError if the actual value is not equal to the given one.
111 */
112 public DoubleAssert isEqualTo(double expected, Offset<Double> offset) {
113 doubles.assertEqual(info, actual, expected, offset);
114 return this;
115 }
116
117 /**
118 * Verifies that the actual value is not equal to 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 the given one.
123 */
124 public DoubleAssert isNotEqualTo(double other) {
125 doubles.assertNotEqual(info, actual, other);
126 return this;
127 }
128
129 /**
130 * Verifies that the actual value is less than 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 equal to or greater than the given one.
135 */
136 public DoubleAssert isLessThan(double other) {
137 doubles.assertLessThan(info, actual, other);
138 return this;
139 }
140
141 /**
142 * Verifies that the actual value is less than or equal to the given one.
143 * @param other the given value to compare the actual value to.
144 * @return {@code this} assertion object.
145 * @throws AssertionError if the actual value is {@code null}.
146 * @throws AssertionError if the actual value is greater than the given one.
147 */
148 public DoubleAssert isLessThanOrEqualTo(double other) {
149 doubles.assertLessThanOrEqualTo(info, actual, other);
150 return this;
151 }
152
153 /**
154 * Verifies that the actual value is greater than the given one.
155 * @param other the given value to compare the actual value to.
156 * @return {@code this} assertion object.
157 * @throws AssertionError if the actual value is {@code null}.
158 * @throws AssertionError if the actual value is equal to or less than the given one.
159 */
160 public DoubleAssert isGreaterThan(double other) {
161 doubles.assertGreaterThan(info, actual, other);
162 return this;
163 }
164
165 /**
166 * Verifies that the actual value is greater than or equal to the given one.
167 * @param other the given value to compare the actual value to.
168 * @return {@code this} assertion object.
169 * @throws AssertionError if the actual value is {@code null}.
170 * @throws AssertionError if the actual value is less than the given one.
171 */
172 public DoubleAssert isGreaterThanOrEqualTo(double other) {
173 doubles.assertGreaterThanOrEqualTo(info, actual, other);
174 return this;
175 }
176
177 @Override
178 public DoubleAssert usingComparator(Comparator<? super Double> customComparator) {
179 super.usingComparator(customComparator);
180 this.doubles = new Doubles(new ComparatorBasedComparisonStrategy(customComparator));
181 return myself;
182 }
183
184 @Override
185 public DoubleAssert usingDefaultComparator() {
186 super.usingDefaultComparator();
187 this.doubles = Doubles.instance();
188 return myself;
189 }
190 }