001 /*
002 * Created on Oct 4, 2009
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 @2009-2010 the original author or authors.
014 */
015 package org.fest.assertions;
016
017 import static org.fest.assertions.ErrorMessages.*;
018 import static org.fest.assertions.Fail.comparisonFailed;
019
020 /**
021 * Understands a template for assertion methods, applicable to <code>{@link Comparable}</code>s.
022 * @param <T> the type of <code>Comparable</code> this template can verify.
023 *
024 * @author Alex Ruiz
025 * @author Ted M. Young
026 */
027 public abstract class ComparableAssert<T extends Comparable<T>> extends GenericAssert<T> {
028
029 /**
030 * Creates a new </code>{@link ComparableAssert}</code>.
031 * @param actual the target to verify.
032 */
033 protected ComparableAssert(T actual) {
034 super(actual);
035 }
036
037 /**
038 * Verifies that the actual <code>{@link Comparable}</code> is equal to the given one.
039 * @param expected the given <code>Comparable</code> to compare the actual <code>Comparable</code> to.
040 * @return this assertion object.
041 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
042 * @throws AssertionError if the actual <code>Comparable</code> is not equal to the given one.
043 */
044 protected abstract ComparableAssert<T> isEqualByComparingTo(T expected);
045
046 /**
047 * Verifies that the actual <code>{@link Comparable}</code> is <b>not</b> equal to the given one.
048 * @param expected the given <code>Comparable</code> to use to compare to the actual <code>Comparable</code>.
049 * @return this assertion object.
050 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
051 * @throws AssertionError if the actual <code>Comparable</code> is equal to the given one.
052 */
053 protected abstract ComparableAssert<T> isNotEqualByComparingTo(T expected);
054
055 /**
056 * Verifies that the actual <code>{@link Comparable}</code> is less than the given one.
057 * @param other the given value.
058 * @return this assertion object.
059 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
060 * @throws AssertionError if the actual <code>Comparable</code> is not less than the given one.
061 */
062 protected abstract ComparableAssert<T> isLessThan(T other);
063
064 /**
065 * Verifies that the actual <code>{@link Comparable}</code> is greater than the given one.
066 * @param other the given value.
067 * @return this assertion object.
068 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
069 * @throws AssertionError if the actual <code>Comparable</code> is not greater than the given one.
070 */
071 protected abstract ComparableAssert<T> isGreaterThan(T other);
072
073 /**
074 * Verifies that the actual <code>{@link Comparable}</code> is less than or equal to the given one.
075 * @param other the given value.
076 * @return this assertion object.
077 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
078 * @throws AssertionError if the actual <code>Comparable</code> is not less than or equal to the given one.
079 */
080 protected abstract ComparableAssert<T> isLessThanOrEqualTo(T other);
081
082 /**
083 * Verifies that the actual <code>{@link Comparable}</code> is greater than or equal to the given one.
084 * @param other the given value.
085 * @return this assertion object.
086 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
087 * @throws AssertionError if the actual <code>Comparable</code> is not greater than or equal to the given one.
088 */
089 protected abstract ComparableAssert<T> isGreaterThanOrEqualTo(T other);
090
091 /**
092 * Verifies that the actual <code>{@link Comparable}</code> is equal to the given one.
093 * @param expected the given <code>Comparable</code> to compare the actual <code>Comparable</code> to.
094 * @throws AssertionError if the actual <code>Comparable</code> value is <code>null</code>.
095 * @throws AssertionError if the actual <code>Comparable</code> value is not equal to the given one.
096 */
097 protected final void assertIsEqualByComparingTo(T expected) {
098 isNotNull();
099 if (actual.compareTo(expected) == 0) return;
100 failIfCustomMessageIsSet();
101 throw comparisonFailed(rawDescription(), actual, expected);
102 }
103
104 /**
105 * Verifies that the actual <code>{@link Comparable}</code> is <b>not</b> equal to the given one.
106 * @param expected the given <code>Comparable</code> to use to compare to the actual <code>Comparable</code>.
107 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
108 * @throws AssertionError if the actual <code>Comparable</code> is equal to the given one.
109 */
110 protected final void assertIsNotEqualByComparingTo(T expected) {
111 isNotNull();
112 if (actual.compareTo(expected) != 0) return;
113 failIfCustomMessageIsSet();
114 fail(unexpectedEqual(actual, expected));
115 }
116
117 /**
118 * Verifies that the actual <code>{@link Comparable}</code> is less than the given one.
119 * @param other the given value.
120 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
121 * @throws AssertionError if the actual <code>Comparable</code> is not less than the given one.
122 */
123 protected final void assertIsLessThan(T other) {
124 isNotNull();
125 if (actual.compareTo(other) < 0) return;
126 failIfCustomMessageIsSet();
127 fail(unexpectedGreaterThanOrEqualTo(actual, other));
128 }
129
130 /**
131 * Verifies that the actual <code>{@link Comparable}</code> is greater than the given one.
132 * @param other the given value.
133 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
134 * @throws AssertionError if the actual <code>Comparable</code> is not greater than the given one.
135 */
136 protected final void assertIsGreaterThan(T other) {
137 isNotNull();
138 if (actual.compareTo(other) > 0) return;
139 failIfCustomMessageIsSet();
140 fail(unexpectedLessThanOrEqualTo(actual, other));
141 }
142
143 /**
144 * Verifies that the actual <code>{@link Comparable}</code> is less than or equal to the given one.
145 * @param other the given value.
146 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
147 * @throws AssertionError if the actual <code>Comparable</code> is not less than or equal to the given one.
148 */
149 protected final void assertIsLessThanOrEqualTo(T other) {
150 isNotNull();
151 if (actual.compareTo(other) <= 0) return;
152 failIfCustomMessageIsSet();
153 fail(unexpectedGreaterThan(actual, other));
154 }
155
156 /**
157 * Verifies that the actual <code>{@link Comparable}</code> is greater than or equal to the given one.
158 * @param other the given value.
159 * @throws AssertionError if the actual <code>Comparable</code> is <code>null</code>.
160 * @throws AssertionError if the actual <code>Comparable</code> is not greater than or equal to the given one.
161 */
162 protected final void assertIsGreaterThanOrEqualTo(T other) {
163 isNotNull();
164 if (actual.compareTo(other) >= 0) return;
165 failIfCustomMessageIsSet();
166 fail(unexpectedLessThan(actual, other));
167 }
168 }