001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with 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 an * 010// * "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// *************************************************************************************************************************** 013package org.apache.juneau.assertions; 014 015import static org.apache.juneau.common.internal.ArgUtils.*; 016 017import java.io.*; 018import java.util.function.*; 019 020import org.apache.juneau.cp.*; 021import org.apache.juneau.internal.*; 022import org.apache.juneau.serializer.*; 023 024/** 025 * Used for fluent assertion calls against comparable objects. 026 * 027 * <h5 class='section'>Test Methods:</h5> 028 * <p> 029 * <ul class='javatree'> 030 * <li class='jc'>{@link FluentComparableAssertion} 031 * <ul class='javatreec'> 032 * <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)} 033 * <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)} 034 * <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)} 035 * <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)} 036 * <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)} 037 * </ul> 038 * <li class='jc'>{@link FluentObjectAssertion} 039 * <ul class='javatreec'> 040 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 041 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 042 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 043 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 044 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 045 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 046 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 047 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 048 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 049 * <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)} 050 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 051 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 052 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 053 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 054 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 055 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 056 * </ul> 057 * </ul> 058 * 059 * <h5 class='section'>Transform Methods:</h5> 060 * <p> 061 * <ul class='javatree'> 062 * <li class='jc'>{@link FluentObjectAssertion} 063 * <ul class='javatreec'> 064 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 065 * <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} 066 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 067 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 068 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 069 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 070 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 071 * </ul> 072 * </ul> 073 * 074 * <h5 class='section'>Configuration Methods:</h5> 075 * <p> 076 * <ul class='javatree'> 077 * <li class='jc'>{@link Assertion} 078 * <ul class='javatreec'> 079 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 080 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 081 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 082 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 083 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 084 * </ul> 085 * </ul> 086 * 087 * <h5 class='section'>See Also:</h5><ul> 088 * <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview > juneau-assertions > Overview</a> 089 * </ul> 090 * 091 * @param <T> The value type 092 * @param <R> The return type. 093 */ 094@FluentSetters(returns="FluentComparableAssertion<T,R>") 095@SuppressWarnings("rawtypes") 096public class FluentComparableAssertion<T extends Comparable,R> extends FluentObjectAssertion<T,R> { 097 098 //----------------------------------------------------------------------------------------------------------------- 099 // Static 100 //----------------------------------------------------------------------------------------------------------------- 101 102 private static final Messages MESSAGES = Messages.of(FluentComparableAssertion.class, "Messages"); 103 private static final String 104 MSG_valueWasNotGreaterThanExpected = MESSAGES.getString("valueWasNotGreaterThanExpected"), 105 MSG_valueWasNotGreaterOrEqualsToExpected = MESSAGES.getString("valueWasNotGreaterOrEqualsToExpected"), 106 MSG_valueWasNotLessThanExpected = MESSAGES.getString("valueWasNotLessThanExpected"), 107 MSG_valueWasNotLessOrEqualsToExpected = MESSAGES.getString("valueWasNotLessOrEqualsToExpected"); 108 109 //----------------------------------------------------------------------------------------------------------------- 110 // Instance 111 //----------------------------------------------------------------------------------------------------------------- 112 113 /** 114 * Constructor. 115 * 116 * @param value 117 * The object being tested. 118 * <br>Can be <jk>null</jk>. 119 * @param returns 120 * The object to return after a test method is called. 121 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 122 * used on the same assertion. 123 */ 124 public FluentComparableAssertion(T value, R returns) { 125 this(null, value, returns); 126 } 127 128 /** 129 * Chained constructor. 130 * 131 * <p> 132 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 133 * 134 * @param creator 135 * The assertion that created this assertion. 136 * <br>Should be <jk>null</jk> if this is the top-level assertion. 137 * @param value 138 * The object being tested. 139 * <br>Can be <jk>null</jk>. 140 * @param returns 141 * The object to return after a test method is called. 142 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 143 * used on the same assertion. 144 */ 145 public FluentComparableAssertion(Assertion creator, T value, R returns) { 146 super(creator, value, returns); 147 } 148 149 //----------------------------------------------------------------------------------------------------------------- 150 // Transform methods 151 //----------------------------------------------------------------------------------------------------------------- 152 153 @Override /* FluentObjectAssertion */ 154 public FluentComparableAssertion<T,R> asTransformed(Function<T,T> function) { 155 return new FluentComparableAssertion<>(this, function.apply(orElse(null)), returns()); 156 } 157 158 //----------------------------------------------------------------------------------------------------------------- 159 // Test methods 160 //----------------------------------------------------------------------------------------------------------------- 161 162 /** 163 * Asserts that the value is greater than the specified value. 164 * 165 * @param value The values to check against. 166 * @return The fluent return object. 167 * @throws AssertionError If assertion failed. 168 */ 169 public R isGt(Comparable value) throws AssertionError { 170 assertArgNotNull("value", value); 171 if (value().compareTo(value) <= 0) 172 throw error(MSG_valueWasNotGreaterThanExpected, value, value()); 173 return returns(); 174 } 175 176 /** 177 * Asserts that the value is greater than or equal to the specified value. 178 * 179 * @param value The values to check against. 180 * @return The fluent return object. 181 * @throws AssertionError If assertion failed. 182 */ 183 public R isGte(Comparable value) throws AssertionError { 184 assertArgNotNull("value", value); 185 if (value().compareTo(value) < 0) 186 throw error(MSG_valueWasNotGreaterOrEqualsToExpected, value, value()); 187 return returns(); 188 } 189 190 /** 191 * Asserts that the value is less than the specified value. 192 * 193 * @param value The values to check against. 194 * @return The fluent return object. 195 * @throws AssertionError If assertion failed. 196 */ 197 public R isLt(Comparable value) throws AssertionError { 198 assertArgNotNull("value", value); 199 if (value().compareTo(value) >= 0) 200 throw error(MSG_valueWasNotLessThanExpected, value, value()); 201 return returns(); 202 } 203 204 /** 205 * Asserts that the value is less than or equals to the specified value. 206 * 207 * @param value The values to check against. 208 * @return The fluent return object. 209 * @throws AssertionError If assertion failed. 210 */ 211 public R isLte(Comparable value) throws AssertionError { 212 assertArgNotNull("value", value); 213 if (value().compareTo(value) > 0) 214 throw error(MSG_valueWasNotLessOrEqualsToExpected, value, value()); 215 return returns(); 216 } 217 218 /** 219 * Asserts that the value is between (inclusive) the specified upper and lower values. 220 * 221 * @param lower The lower value to check against. 222 * @param upper The upper value to check against. 223 * @return The fluent return object. 224 * @throws AssertionError If assertion failed. 225 */ 226 public R isBetween(Comparable lower, Comparable upper) throws AssertionError { 227 isExists(); 228 assertArgNotNull("lower", lower); 229 assertArgNotNull("upper", upper); 230 isLte(upper); 231 isGte(lower); 232 return returns(); 233 } 234 235 //----------------------------------------------------------------------------------------------------------------- 236 // Fluent setters 237 //----------------------------------------------------------------------------------------------------------------- 238 239 // <FluentSetters> 240 241 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 242 public FluentComparableAssertion<T,R> setMsg(String msg, Object...args) { 243 super.setMsg(msg, args); 244 return this; 245 } 246 247 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 248 public FluentComparableAssertion<T,R> setOut(PrintStream value) { 249 super.setOut(value); 250 return this; 251 } 252 253 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 254 public FluentComparableAssertion<T,R> setSilent() { 255 super.setSilent(); 256 return this; 257 } 258 259 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 260 public FluentComparableAssertion<T,R> setStdOut() { 261 super.setStdOut(); 262 return this; 263 } 264 265 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 266 public FluentComparableAssertion<T,R> setThrowable(Class<? extends java.lang.RuntimeException> value) { 267 super.setThrowable(value); 268 return this; 269 } 270 271 // </FluentSetters> 272}