001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.assertions; 014 015import static org.apache.juneau.common.internal.StringUtils.*; 016 017import java.io.*; 018import java.nio.charset.*; 019import java.util.function.*; 020 021import org.apache.juneau.common.internal.*; 022import org.apache.juneau.internal.*; 023import org.apache.juneau.serializer.*; 024 025/** 026 * Used for fluent assertion calls against byte arrays. 027 * 028 * <h5 class='section'>Test Methods:</h5> 029 * <p> 030 * <ul class='javatree'> 031 * <li class='jc'>{@link FluentPrimitiveArrayAssertion} 032 * <ul class='javatreec'> 033 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isHas(Object...) isHas(Object...)} 034 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#is(Predicate) is(Predicate)} 035 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAny(Predicate) isAny(Predicate)} 036 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAll(Predicate) isAll(Predicate)} 037 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isEmpty() isEmpty()} 038 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotEmpty() isNotEmpty()} 039 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isSize(int) isSize(int)} 040 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isContains(Object) isContains(Object)} 041 * <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotContains(Object) isNotContains(Object)} 042 * </ul> 043 * <li class='jc'>{@link FluentObjectAssertion} 044 * <ul class='javatreec'> 045 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 046 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 047 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 048 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 049 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 050 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 051 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 052 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 053 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 054 * <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)} 055 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 056 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 057 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 058 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 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(WriterSerializer) asString(WriterSerializer)} 084 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 085 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 086 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 087 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 088 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 089 * </ul> 090 * </ul> 091 * 092 * <h5 class='section'>Configuration Methods:</h5> 093 * <p> 094 * <ul class='javatree'> 095 * <li class='jc'>{@link Assertion} 096 * <ul class='javatreec'> 097 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 098 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 099 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 100 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 101 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 102 * </ul> 103 * </ul> 104 * 105 * <h5 class='section'>See Also:</h5><ul> 106 * <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview > juneau-assertions > Overview</a> 107 * </ul> 108 * 109 * @param <R> The return type. 110 */ 111@FluentSetters(returns="FluentByteArrayAssertion<R>") 112public class FluentByteArrayAssertion<R> extends FluentPrimitiveArrayAssertion<Byte,byte[],R> { 113 114 //----------------------------------------------------------------------------------------------------------------- 115 // Instance 116 //----------------------------------------------------------------------------------------------------------------- 117 118 /** 119 * Constructor. 120 * 121 * @param value 122 * The object being tested. 123 * <br>Can be <jk>null</jk>. 124 * @param returns 125 * The object to return after a test method is called. 126 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 127 * used on the same assertion. 128 */ 129 public FluentByteArrayAssertion(byte[] value, R returns) { 130 this(null, value, returns); 131 } 132 133 /** 134 * Chained constructor. 135 * 136 * <p> 137 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 138 * 139 * @param creator 140 * The assertion that created this assertion. 141 * <br>Should be <jk>null</jk> if this is the top-level assertion. 142 * @param value 143 * The object being tested. 144 * <br>Can be <jk>null</jk>. 145 * @param returns 146 * The object to return after a test method is called. 147 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 148 * used on the same assertion. 149 */ 150 public FluentByteArrayAssertion(Assertion creator, byte[] value, R returns) { 151 super(creator, value, returns); 152 } 153 154 //----------------------------------------------------------------------------------------------------------------- 155 // Transform methods 156 //----------------------------------------------------------------------------------------------------------------- 157 158 /** 159 * Converts this byte array to a UTF-8 encoded string and returns it as a new assertion. 160 * 161 * <h5 class='section'>Example:</h5> 162 * <p class='bjava'> 163 * <jc>// Validates that the specified byte array contains the string "foobar".</jc> 164 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString().is(<js>"foobar"</js>); 165 * </p> 166 * 167 * @return A new fluent string assertion. 168 */ 169 @Override 170 public FluentStringAssertion<R> asString() { 171 return asString(IOUtils.UTF8); 172 } 173 174 /** 175 * Converts this byte array to a string and returns it as a new assertion. 176 * 177 * <h5 class='section'>Example:</h5> 178 * <p class='bjava'> 179 * <jc>// Validates that the specified byte array contains the string "foobar" encoded in ASCII.</jc> 180 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString(<js>"iso8859-1"</js>).is(<js>"foobar"</js>); 181 * </p> 182 * 183 * @param cs The charset to use to decode the string. 184 * @return A new fluent string assertion. 185 */ 186 public FluentStringAssertion<R> asString(Charset cs) { 187 return new FluentStringAssertion<>(this, valueIsNull() ? null : new String(value(), cs), returns()); 188 } 189 190 /** 191 * Converts this byte array to a base-64 encoded string and returns it as a new assertion. 192 * 193 * <h5 class='section'>Example:</h5> 194 * <p class='bjava'> 195 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 196 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asBase64().is(<js>"Zm9v"</js>); 197 * </p> 198 * 199 * @return A new fluent string assertion. 200 */ 201 public FluentStringAssertion<R> asBase64() { 202 return new FluentStringAssertion<>(this, valueIsNull() ? null : base64Encode(value()), returns()); 203 } 204 205 /** 206 * Converts this byte array to hexadecimal and returns it as a new assertion. 207 * 208 * <h5 class='section'>Example:</h5> 209 * <p class='bjava'> 210 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 211 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asHex().is(<js>"666F6F"</js>); 212 * </p> 213 * 214 * @return A new string consisting of hexadecimal characters. 215 */ 216 public FluentStringAssertion<R> asHex() { 217 return new FluentStringAssertion<>(this, valueIsNull() ? null : toHex(value()), returns()); 218 } 219 220 /** 221 * Converts this byte array to spaced hexadecimal and returns it as a new assertion. 222 * 223 * <h5 class='section'>Example:</h5> 224 * <p class='bjava'> 225 * <jc>// Validates that the specified byte array contains the string "foo".</jc> 226 * <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asSpacedHex().is(<js>"66 6F 6F"</js>); 227 * </p> 228 * 229 * @return A new string consisting of hexadecimal characters. 230 */ 231 public FluentStringAssertion<R> asSpacedHex() { 232 return new FluentStringAssertion<>(this, valueIsNull() ? null : toSpacedHex(value()), returns()); 233 } 234 235 //----------------------------------------------------------------------------------------------------------------- 236 // Fluent setters 237 //----------------------------------------------------------------------------------------------------------------- 238 239 // <FluentSetters> 240 241 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 242 public FluentByteArrayAssertion<R> setMsg(String msg, Object...args) { 243 super.setMsg(msg, args); 244 return this; 245 } 246 247 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 248 public FluentByteArrayAssertion<R> setOut(PrintStream value) { 249 super.setOut(value); 250 return this; 251 } 252 253 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 254 public FluentByteArrayAssertion<R> setSilent() { 255 super.setSilent(); 256 return this; 257 } 258 259 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 260 public FluentByteArrayAssertion<R> setStdOut() { 261 super.setStdOut(); 262 return this; 263 } 264 265 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 266 public FluentByteArrayAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) { 267 super.setThrowable(value); 268 return this; 269 } 270 271 // </FluentSetters> 272}