001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.bct.assertions; 018 019import java.io.*; 020import java.util.function.*; 021 022import org.apache.juneau.commons.function.*; 023import org.apache.juneau.commons.lang.*; 024 025/** 026 * Used for fluent assertion calls against {@link Version} objects. 027 * 028 * <h5 class='section'>Example:</h5> 029 * <p class='bjava'> 030 * <jc>// Validates the response expiration is after the current date.</jc> 031 * <jv>client</jv> 032 * .get(<jsf>URL</jsf>) 033 * .run() 034 * .getHeader(ClientVersion.<jk>class</jk>).assertVersion().asMajor().isGreaterThanOrEqual(2); 035 * </p> 036 * 037 * 038 * <h5 class='section'>Test Methods:</h5> 039 * <p> 040 * <ul class='javatree'> 041 * <li class='jc'>{@link FluentComparableAssertion} 042 * <ul class='javatreec'> 043 * <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)} 044 * <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)} 045 * <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)} 046 * <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)} 047 * <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)} 048 * </ul> 049 * <li class='jc'>{@link FluentObjectAssertion} 050 * <ul class='javatreec'> 051 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 052 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 053 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 054 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 055 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 056 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 057 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 058 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 059 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 060 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 061 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 062 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 063 * </ul> 064 * </ul> 065 * 066 * <h5 class='section'>Transform Methods:</h5> 067 * <p> 068 * <ul class='javatree'> 069 * <li class='jc'>{@link FluentVersionAssertion} 070 * <ul class='javatreec'> 071 * <li class='jm'>{@link FluentVersionAssertion#asPart(int) asPart(int)} 072 * <li class='jm'>{@link FluentVersionAssertion#asMajor() asMajor()} 073 * <li class='jm'>{@link FluentVersionAssertion#asMinor() asMinor()} 074 * <li class='jm'>{@link FluentVersionAssertion#asMaintenance() asMaintenance()} 075 * </ul> 076 * <li class='jc'>{@link FluentObjectAssertion} 077 * <ul class='javatreec'> 078 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 079 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 080 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 081 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 082 * </ul> 083 * </ul> 084 * 085 * <h5 class='section'>Configuration Methods:</h5> 086 * <p> 087 * <ul class='javatree'> 088 * <li class='jc'>{@link Assertion} 089 * <ul class='javatreec'> 090 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 091 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 092 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 093 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 094 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 095 * </ul> 096 * </ul> 097 * 098 * <h5 class='section'>See Also:</h5><ul> 099 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 100 * </ul> 101 * 102 * @param <R> The return type. 103 */ 104public class FluentVersionAssertion<R> extends FluentComparableAssertion<Version,R> { 105 106 /** 107 * Chained constructor. 108 * 109 * <p> 110 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 111 * 112 * @param creator 113 * The assertion that created this assertion. 114 * <br>Should be <jk>null</jk> if this is the top-level assertion. 115 * @param value 116 * The object being tested. 117 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 118 * @param returns 119 * The object to return after a test method is called. 120 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 121 * used on the same assertion. 122 */ 123 public FluentVersionAssertion(Assertion creator, Version value, R returns) { 124 super(creator, value, returns); 125 } 126 127 /** 128 * Constructor. 129 * 130 * @param value 131 * The object being tested. 132 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 133 * @param returns 134 * The object to return after a test method is called. 135 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 136 * used on the same assertion. 137 */ 138 public FluentVersionAssertion(Version value, R returns) { 139 this(null, value, returns); 140 } 141 142 /** 143 * Extracts the maintenance part of the version string (index position 2). 144 * 145 * @return This object. 146 */ 147 public FluentIntegerAssertion<R> asMaintenance() { 148 return asPart(2); 149 } 150 151 /** 152 * Extracts the major part of the version string (index position 0). 153 * 154 * @return This object. 155 */ 156 public FluentIntegerAssertion<R> asMajor() { 157 return asPart(0); 158 } 159 160 /** 161 * Extracts the minor part of the version string (index position 1). 162 * 163 * @return This object. 164 */ 165 public FluentIntegerAssertion<R> asMinor() { 166 return asPart(1); 167 } 168 169 /** 170 * Extracts the specified version part (zero-indexed position). 171 * 172 * @param index The index of the version part to extract. 173 * @return This object. 174 */ 175 public FluentIntegerAssertion<R> asPart(int index) { 176 return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().getPart(index).orElse(null), returns()); 177 } 178 179 @Override /* Overridden from Assertion */ 180 public FluentVersionAssertion<R> setMsg(String msg, Object...args) { 181 super.setMsg(msg, args); 182 return this; 183 } 184 185 @Override /* Overridden from Assertion */ 186 public FluentVersionAssertion<R> setOut(PrintStream value) { 187 super.setOut(value); 188 return this; 189 } 190 191 @Override /* Overridden from Assertion */ 192 public FluentVersionAssertion<R> setSilent() { 193 super.setSilent(); 194 return this; 195 } 196 197 @Override /* Overridden from Assertion */ 198 public FluentVersionAssertion<R> setStdOut() { 199 super.setStdOut(); 200 return this; 201 } 202 203 @Override /* Overridden from Assertion */ 204 public FluentVersionAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 205 super.setThrowable(value); 206 return this; 207 } 208}