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 static org.apache.juneau.common.utils.StringUtils.*; 020 021import java.io.*; 022import java.nio.charset.*; 023import java.util.function.*; 024 025import org.apache.juneau.common.utils.*; 026import org.apache.juneau.internal.*; 027import org.apache.juneau.serializer.*; 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#isJson(String) isJson(String)} 059 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 060 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 061 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 062 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 063 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 064 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 065 * </ul> 066 * </ul> 067 * 068 * <h5 class='section'>Transform Methods:</h5> 069 * <p> 070 * <ul class='javatree'> 071 * <li class='jc'>{@link FluentByteArrayAssertion} 072 * <ul class='javatreec'> 073 * <li class='jm'>{@link FluentByteArrayAssertion#asString() asString()} 074 * <li class='jm'>{@link FluentByteArrayAssertion#asString(Charset) asString(Charset)} 075 * <li class='jm'>{@link FluentByteArrayAssertion#asBase64() asBase64()} 076 * <li class='jm'>{@link FluentByteArrayAssertion#asHex() asHex()} 077 * <li class='jm'>{@link FluentByteArrayAssertion#asSpacedHex() asSpacedHex()} 078 * </ul> 079 * <li class='jc'>{@link FluentPrimitiveArrayAssertion} 080 * <ul class='javatreec'> 081 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#asItem(int) asItem(int)} 082 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#asLength() asLength()} 083 * </ul> 084 * <li class='jc'>{@link FluentObjectAssertion} 085 * <ul class='javatreec'> 086 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 087 * <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} 088 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 089 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 090 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 091 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 092 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 093 * </ul> 094 * </ul> 095 * 096 * <h5 class='section'>Configuration Methods:</h5> 097 * <p> 098 * <ul class='javatree'> 099 * <li class='jc'>{@link Assertion} 100 * <ul class='javatreec'> 101 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 102 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 103 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 104 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 105 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 106 * </ul> 107 * </ul> 108 * 109 * <h5 class='section'>See Also:</h5><ul> 110 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a> 111 * </ul> 112 * 113 * @param <R> The return type. 114 */ 115public class FluentByteArrayAssertion<R> extends FluentPrimitiveArrayAssertion<Byte,byte[],R> { 116 117 //----------------------------------------------------------------------------------------------------------------- 118 // Instance 119 //----------------------------------------------------------------------------------------------------------------- 120 121 /** 122 * Constructor. 123 * 124 * @param value 125 * The object being tested. 126 * <br>Can be <jk>null</jk>. 127 * @param returns 128 * The object to return after a test method is called. 129 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 130 * used on the same assertion. 131 */ 132 public FluentByteArrayAssertion(byte[] value, R returns) { 133 this(null, value, returns); 134 } 135 136 /** 137 * Chained constructor. 138 * 139 * <p> 140 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 141 * 142 * @param creator 143 * The assertion that created this assertion. 144 * <br>Should be <jk>null</jk> if this is the top-level assertion. 145 * @param value 146 * The object being tested. 147 * <br>Can be <jk>null</jk>. 148 * @param returns 149 * The object to return after a test method is called. 150 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 151 * used on the same assertion. 152 */ 153 public FluentByteArrayAssertion(Assertion creator, byte[] value, R returns) { 154 super(creator, value, returns); 155 } 156 157 //----------------------------------------------------------------------------------------------------------------- 158 // Transform methods 159 //----------------------------------------------------------------------------------------------------------------- 160 161 /** 162 * Converts this byte array to a UTF-8 encoded string 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 "foobar".</jc> 167 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString().is(<js>"foobar"</js>); 168 * </p> 169 * 170 * @return A new fluent string assertion. 171 */ 172 @Override 173 public FluentStringAssertion<R> asString() { 174 return asString(IOUtils.UTF8); 175 } 176 177 /** 178 * Converts this byte array to a string and returns it as a new assertion. 179 * 180 * <h5 class='section'>Example:</h5> 181 * <p class='bjava'> 182 * <jc>// Validates that the specified byte array contains the string "foobar" encoded in ASCII.</jc> 183 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString(<js>"iso8859-1"</js>).is(<js>"foobar"</js>); 184 * </p> 185 * 186 * @param cs The charset to use to decode the string. 187 * @return A new fluent string assertion. 188 */ 189 public FluentStringAssertion<R> asString(Charset cs) { 190 return new FluentStringAssertion<>(this, valueIsNull() ? null : new String(value(), cs), returns()); 191 } 192 193 /** 194 * Converts this byte array to a base-64 encoded string and returns it as a new assertion. 195 * 196 * <h5 class='section'>Example:</h5> 197 * <p class='bjava'> 198 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 199 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asBase64().is(<js>"Zm9v"</js>); 200 * </p> 201 * 202 * @return A new fluent string assertion. 203 */ 204 public FluentStringAssertion<R> asBase64() { 205 return new FluentStringAssertion<>(this, valueIsNull() ? null : base64Encode(value()), returns()); 206 } 207 208 /** 209 * Converts this byte array to hexadecimal and returns it as a new assertion. 210 * 211 * <h5 class='section'>Example:</h5> 212 * <p class='bjava'> 213 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 214 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asHex().is(<js>"666F6F"</js>); 215 * </p> 216 * 217 * @return A new string consisting of hexadecimal characters. 218 */ 219 public FluentStringAssertion<R> asHex() { 220 return new FluentStringAssertion<>(this, valueIsNull() ? null : toHex(value()), returns()); 221 } 222 223 /** 224 * Converts this byte array to spaced hexadecimal and returns it as a new assertion. 225 * 226 * <h5 class='section'>Example:</h5> 227 * <p class='bjava'> 228 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 229 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asSpacedHex().is(<js>"66 6F 6F"</js>); 230 * </p> 231 * 232 * @return A new string consisting of hexadecimal characters. 233 */ 234 public FluentStringAssertion<R> asSpacedHex() { 235 return new FluentStringAssertion<>(this, valueIsNull() ? null : toSpacedHex(value()), returns()); 236 } 237 238 //----------------------------------------------------------------------------------------------------------------- 239 // Fluent setters 240 //----------------------------------------------------------------------------------------------------------------- 241 @Override /* Overridden from Assertion */ 242 public FluentByteArrayAssertion<R> setMsg(String msg, Object...args) { 243 super.setMsg(msg, args); 244 return this; 245 } 246 247 @Override /* Overridden from Assertion */ 248 public FluentByteArrayAssertion<R> setOut(PrintStream value) { 249 super.setOut(value); 250 return this; 251 } 252 253 @Override /* Overridden from Assertion */ 254 public FluentByteArrayAssertion<R> setSilent() { 255 super.setSilent(); 256 return this; 257 } 258 259 @Override /* Overridden from Assertion */ 260 public FluentByteArrayAssertion<R> setStdOut() { 261 super.setStdOut(); 262 return this; 263 } 264 265 @Override /* Overridden from Assertion */ 266 public FluentByteArrayAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) { 267 super.setThrowable(value); 268 return this; 269 } 270}