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