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 static java.util.Arrays.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.io.*; 023import java.util.function.*; 024 025import org.apache.juneau.commons.bean.*; 026import org.apache.juneau.commons.function.*; 027 028/** 029 * Used for fluent assertion calls against Java beans. 030 * 031 * <h5 class='section'>Test Methods:</h5> 032 * <p> 033 * <ul class='javatree'> 034 * <li class='jc'>{@link FluentObjectAssertion} 035 * <ul class='javatreec'> 036 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 037 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 038 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 039 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 040 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 041 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 042 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 043 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 044 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 045 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 046 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 047 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 048 * </ul> 049 * </ul> 050 * 051 * <h5 class='section'>Transform Methods:</h5> 052 * <p> 053 * <ul class='javatree'> 054 * <li class='jc'>{@link FluentBeanAssertion} 055 * <ul class='javatreec'> 056 * <li class='jm'>{@link FluentBeanAssertion#asPropertyMap(String...) asPropertyMap(String...)} 057 * <li class='jm'>{@link FluentBeanAssertion#asProperty(String) asProperty(String)} 058 * <li class='jm'>{@link FluentBeanAssertion#asProperties(String...) asProperties(String...)} 059 * </ul> 060 * <li class='jc'>{@link FluentObjectAssertion} 061 * <ul class='javatreec'> 062 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 063 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 064 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 065 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 066 * </ul> 067 * </ul> 068 * 069 * <h5 class='section'>Configuration Methods:</h5> 070 * <p> 071 * <ul class='javatree'> 072 * <li class='jc'>{@link Assertion} 073 * <ul class='javatreec'> 074 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 075 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 076 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 077 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 078 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 079* </ul> 080 * </ul> 081 * 082 * <h5 class='section'>See Also:</h5><ul> 083 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 084 * </ul> 085 * 086 * @param <T> The bean type. 087 * @param <R> The return type. 088 */ 089public class FluentBeanAssertion<T,R> extends FluentObjectAssertion<T,R> { 090 091 /** 092 * Chained constructor. 093 * 094 * <p> 095 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 096 * 097 * @param creator 098 * The assertion that created this assertion. 099 * <br>Should be <jk>null</jk> if this is the top-level assertion. 100 * @param value 101 * The object being tested. 102 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 103 * @param returns 104 * The object to return after a test method is called. 105 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 106 * used on the same assertion. 107 */ 108 public FluentBeanAssertion(Assertion creator, T value, R returns) { 109 super(creator, value, returns); 110 } 111 112 /** 113 * Constructor. 114 * 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 FluentBeanAssertion(T value, R returns) { 124 this(null, value, returns); 125 } 126 127 /** 128 * Extracts the specified property as an {@link FluentListAssertion}. 129 * 130 * @param names The names of the properties to extract. Can also pass in comma-delimited lists. 131 * @return An assertion of the property values. 132 */ 133 public FluentListAssertion<Object,R> asProperties(String...names) { 134 BeanMap<T> bm = toBeanMap(); 135 return new FluentListAssertion<>(this, stream(names).map(bm::get).toList(), returns()); 136 } 137 138 /** 139 * Extracts the specified property as an {@link FluentAnyAssertion}. 140 * 141 * @param name The property to extract. Can also pass in comma-delimited lists. 142 * @return An assertion of the property value. 143 */ 144 public FluentAnyAssertion<Object,R> asProperty(String name) { 145 return new FluentAnyAssertion<>(this, toBeanMap().get(name), returns()); 146 } 147 148 /** 149 * Extracts the specified fields of this bean into a simple map of key/value pairs and returns it as 150 * a new {@link MapAssertion}. 151 * 152 * @param names The fields to extract. Can also pass in comma-delimited lists. 153 * @return This object. 154 */ 155 public FluentMapAssertion<String,Object,R> asPropertyMap(String...names) { 156 return new FluentMapAssertion<>(this, toBeanMap().getProperties(splita(names, ',')), returns()); 157 } 158 159 @Override /* Overridden from FluentObjectAssertion */ 160 public FluentBeanAssertion<T,R> asTransformed(UnaryOperator<T> function) { 161 return new FluentBeanAssertion<>(this, function.apply(orElse(null)), returns()); 162 } 163 164 @Override /* Overridden from Assertion */ 165 public FluentBeanAssertion<T,R> setMsg(String msg, Object...args) { 166 super.setMsg(msg, args); 167 return this; 168 } 169 170 @Override /* Overridden from Assertion */ 171 public FluentBeanAssertion<T,R> setOut(PrintStream value) { 172 super.setOut(value); 173 return this; 174 } 175 176 @Override /* Overridden from Assertion */ 177 public FluentBeanAssertion<T,R> setSilent() { 178 super.setSilent(); 179 return this; 180 } 181 182 @Override /* Overridden from Assertion */ 183 public FluentBeanAssertion<T,R> setStdOut() { 184 super.setStdOut(); 185 return this; 186 } 187 188 @Override /* Overridden from Assertion */ 189 public FluentBeanAssertion<T,R> setThrowable(Class<? extends RuntimeException> value) { 190 super.setThrowable(value); 191 return this; 192 } 193 194 private BeanMap<T> toBeanMap() { 195 return BeanMap.of(value()); 196 } 197}