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 org.apache.juneau.commons.utils.IoUtils.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.io.*; 023import java.nio.charset.*; 024import java.util.function.*; 025 026import org.apache.juneau.commons.function.*; 027 028 029/** 030 * Used for fluent assertion calls against byte arrays. 031 * 032 * <h5 class='section'>Test Methods:</h5> 033 * <p> 034 * <ul class='javatree'> 035 * <li class='jc'>{@link FluentPrimitiveArrayAssertion} 036 * <ul class='javatreec'> 037 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isHas(Object...) isHas(Object...)} 038 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#is(Predicate) is(Predicate)} 039 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAny(Predicate) isAny(Predicate)} 040 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAll(Predicate) isAll(Predicate)} 041 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isEmpty() isEmpty()} 042 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotEmpty() isNotEmpty()} 043 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isSize(int) isSize(int)} 044 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isContains(Object) isContains(Object)} 045 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotContains(Object) isNotContains(Object)} 046 * </ul> 047 * <li class='jc'>{@link FluentObjectAssertion} 048 * <ul class='javatreec'> 049 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 050 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 051 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 052 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 053 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 054 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 055 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 056 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 057 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 058 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 059 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 060 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 061 * </ul> 062 * </ul> 063 * 064 * <h5 class='section'>Transform Methods:</h5> 065 * <p> 066 * <ul class='javatree'> 067 * <li class='jc'>{@link FluentByteArrayAssertion} 068 * <ul class='javatreec'> 069 * <li class='jm'>{@link FluentByteArrayAssertion#asString() asString()} 070 * <li class='jm'>{@link FluentByteArrayAssertion#asString(Charset) asString(Charset)} 071 * <li class='jm'>{@link FluentByteArrayAssertion#asBase64() asBase64()} 072 * <li class='jm'>{@link FluentByteArrayAssertion#asHex() asHex()} 073 * <li class='jm'>{@link FluentByteArrayAssertion#asSpacedHex() asSpacedHex()} 074 * </ul> 075 * <li class='jc'>{@link FluentPrimitiveArrayAssertion} 076 * <ul class='javatreec'> 077 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#asItem(int) asItem(int)} 078 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#asLength() asLength()} 079 * </ul> 080 * <li class='jc'>{@link FluentObjectAssertion} 081 * <ul class='javatreec'> 082 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 083 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 084 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 085 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 086 * </ul> 087 * </ul> 088 * 089 * <h5 class='section'>Configuration Methods:</h5> 090 * <p> 091 * <ul class='javatree'> 092 * <li class='jc'>{@link Assertion} 093 * <ul class='javatreec'> 094 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 095 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 096 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 097 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 098 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 099 * </ul> 100 * </ul> 101 * 102 * <h5 class='section'>See Also:</h5><ul> 103 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 104 * </ul> 105 * 106 * @param <R> The return type. 107 */ 108public class FluentByteArrayAssertion<R> extends FluentPrimitiveArrayAssertion<Byte,byte[],R> { 109 110 /** 111 * Chained constructor. 112 * 113 * <p> 114 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 115 * 116 * @param creator 117 * The assertion that created this assertion. 118 * <br>Should be <jk>null</jk> if this is the top-level assertion. 119 * @param value 120 * The object being tested. 121 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 122 * @param returns 123 * The object to return after a test method is called. 124 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 125 * used on the same assertion. 126 */ 127 public FluentByteArrayAssertion(Assertion creator, byte[] value, R returns) { 128 super(creator, value, returns); 129 } 130 131 /** 132 * Constructor. 133 * 134 * @param value 135 * The object being tested. 136 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 137 * @param returns 138 * The object to return after a test method is called. 139 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 140 * used on the same assertion. 141 */ 142 public FluentByteArrayAssertion(byte[] value, R returns) { 143 this(null, value, returns); 144 } 145 146 /** 147 * Converts this byte array to a base-64 encoded string and returns it as a new assertion. 148 * 149 * <h5 class='section'>Example:</h5> 150 * <p class='bjava'> 151 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 152 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asBase64().is(<js>"Zm9v"</js>); 153 * </p> 154 * 155 * @return A new fluent string assertion. 156 */ 157 public FluentStringAssertion<R> asBase64() { 158 return new FluentStringAssertion<>(this, valueIsNull() ? null : base64Encode(value()), returns()); 159 } 160 161 /** 162 * Converts this byte array to hexadecimal and returns it as a new assertion. 163 * 164 * <h5 class='section'>Example:</h5> 165 * <p class='bjava'> 166 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 167 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asHex().is(<js>"666F6F"</js>); 168 * </p> 169 * 170 * @return A new string consisting of hexadecimal characters. 171 */ 172 public FluentStringAssertion<R> asHex() { 173 return new FluentStringAssertion<>(this, valueIsNull() ? null : toHex(value()), returns()); 174 } 175 176 /** 177 * Converts this byte array to spaced hexadecimal and returns it as a new assertion. 178 * 179 * <h5 class='section'>Example:</h5> 180 * <p class='bjava'> 181 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 182 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asSpacedHex().is(<js>"66 6F 6F"</js>); 183 * </p> 184 * 185 * @return A new string consisting of hexadecimal characters. 186 */ 187 public FluentStringAssertion<R> asSpacedHex() { 188 return new FluentStringAssertion<>(this, valueIsNull() ? null : toSpacedHex(value()), returns()); 189 } 190 191 /** 192 * Converts this byte array to a UTF-8 encoded string and returns it as a new assertion. 193 * 194 * <h5 class='section'>Example:</h5> 195 * <p class='bjava'> 196 * <jc>// Validates that the specified byte array contains the string "foobar".</jc> 197 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString().is(<js>"foobar"</js>); 198 * </p> 199 * 200 * @return A new fluent string assertion. 201 */ 202 @Override 203 public FluentStringAssertion<R> asString() { 204 return asString(UTF8); 205 } 206 207 /** 208 * Converts this byte array to a string and returns it as a new assertion. 209 * 210 * <h5 class='section'>Example:</h5> 211 * <p class='bjava'> 212 * <jc>// Validates that the specified byte array contains the string "foobar" encoded in ASCII.</jc> 213 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString(<js>"iso8859-1"</js>).is(<js>"foobar"</js>); 214 * </p> 215 * 216 * @param cs The charset to use to decode the string. 217 * @return A new fluent string assertion. 218 */ 219 public FluentStringAssertion<R> asString(Charset cs) { 220 return new FluentStringAssertion<>(this, valueIsNull() ? null : new String(value(), cs), returns()); 221 } 222 223 @Override /* Overridden from Assertion */ 224 public FluentByteArrayAssertion<R> setMsg(String msg, Object...args) { 225 super.setMsg(msg, args); 226 return this; 227 } 228 229 @Override /* Overridden from Assertion */ 230 public FluentByteArrayAssertion<R> setOut(PrintStream value) { 231 super.setOut(value); 232 return this; 233 } 234 235 @Override /* Overridden from Assertion */ 236 public FluentByteArrayAssertion<R> setSilent() { 237 super.setSilent(); 238 return this; 239 } 240 241 @Override /* Overridden from Assertion */ 242 public FluentByteArrayAssertion<R> setStdOut() { 243 super.setStdOut(); 244 return this; 245 } 246 247 @Override /* Overridden from Assertion */ 248 public FluentByteArrayAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 249 super.setThrowable(value); 250 return this; 251 } 252}