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.assertions; 018 019import java.io.*; 020import java.util.function.*; 021 022import org.apache.juneau.internal.*; 023import org.apache.juneau.serializer.*; 024 025/** 026 * Used for assertion calls against Java beans. 027 * 028 * <h5 class='section'>Example:</h5> 029 * <p class='bjava'> 030 * <jc>// Validates the specified POJO is the specified type and serializes to the specified value.</jc> 031 * <jsm>assertBean</jsm>(<jv>myBean</jv>) 032 * .isType(MyBean.<jk>class</jk>) 033 * .asProperties(<js>"foo,bar"</js>).asJson().is(<js>"{foo:1,bar:2}"</js>); 034 * </p> 035 * 036 * 037 * <h5 class='section'>Test Methods:</h5> 038 * <p> 039 * <ul class='javatree'> 040 * <li class='jc'>{@link FluentObjectAssertion} 041 * <ul class='javatreec'> 042 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 043 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 044 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 045 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 046 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 047 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 048 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 049 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 050 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 051 * <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)} 052 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 053 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 054 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 055 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 056 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 057 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 058 * </ul> 059 * </ul> 060 * 061 * <h5 class='section'>Transform Methods:</h5> 062 * <p> 063 * <ul class='javatree'> 064 * <li class='jc'>{@link FluentBeanAssertion} 065 * <ul class='javatreec'> 066 * <li class='jm'>{@link FluentBeanAssertion#asPropertyMap(String...) asPropertyMap(String...)} 067 * <li class='jm'>{@link FluentBeanAssertion#asProperty(String) asProperty(String)} 068 * <li class='jm'>{@link FluentBeanAssertion#asProperties(String...) asProperties(String...)} 069 * </ul> 070 * <li class='jc'>{@link FluentObjectAssertion} 071 * <ul class='javatreec'> 072 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 073 * <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} 074 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 075 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 076 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 077 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 078 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 079 * </ul> 080 * </ul> 081 * 082 * <h5 class='section'>Configuration Methods:</h5> 083 * <p> 084 * <ul class='javatree'> 085 * <li class='jc'>{@link Assertion} 086 * <ul class='javatreec'> 087 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 088 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 089 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 090 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 091 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 092 * </ul> 093 * </ul> 094 * 095 * <h5 class='section'>See Also:</h5><ul> 096 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a> 097 * </ul> 098 * 099 * @param <T> The bean type. 100 */ 101public class BeanAssertion<T> extends FluentBeanAssertion<T,BeanAssertion<T>> { 102 103 //----------------------------------------------------------------------------------------------------------------- 104 // Static 105 //----------------------------------------------------------------------------------------------------------------- 106 107 /** 108 * Static creator. 109 * 110 * @param <T> The value type. 111 * @param value 112 * The object being tested. 113 * <br>Can be <jk>null</jk>. 114 * @return A new assertion object. 115 */ 116 public static <T> BeanAssertion<T> create(T value) { 117 return new BeanAssertion<>(value); 118 } 119 120 //----------------------------------------------------------------------------------------------------------------- 121 // Instance 122 //----------------------------------------------------------------------------------------------------------------- 123 124 /** 125 * Constructor. 126 * 127 * @param value 128 * The object being tested. 129 * <br>Can be <jk>null</jk>. 130 */ 131 public BeanAssertion(T value) { 132 super(value, null); 133 } 134 135 //----------------------------------------------------------------------------------------------------------------- 136 // Fluent setters 137 //----------------------------------------------------------------------------------------------------------------- 138 @Override /* Overridden from Assertion */ 139 public BeanAssertion<T> setMsg(String msg, Object...args) { 140 super.setMsg(msg, args); 141 return this; 142 } 143 144 @Override /* Overridden from Assertion */ 145 public BeanAssertion<T> setOut(PrintStream value) { 146 super.setOut(value); 147 return this; 148 } 149 150 @Override /* Overridden from Assertion */ 151 public BeanAssertion<T> setSilent() { 152 super.setSilent(); 153 return this; 154 } 155 156 @Override /* Overridden from Assertion */ 157 public BeanAssertion<T> setStdOut() { 158 super.setStdOut(); 159 return this; 160 } 161 162 @Override /* Overridden from Assertion */ 163 public BeanAssertion<T> setThrowable(Class<? extends java.lang.RuntimeException> value) { 164 super.setThrowable(value); 165 return this; 166 } 167}