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