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.internal.StringUtils.*;
016
017import java.nio.charset.*;
018
019import org.apache.juneau.internal.*;
020
021/**
022 * Used for fluent assertion calls against byte arrays.
023 *
024 * @param <R> The return type.
025 */
026@FluentSetters(returns="FluentByteArrayAssertion<R>")
027public class FluentByteArrayAssertion<R> extends FluentArrayAssertion<R> {
028
029   private byte[] value;
030
031   /**
032    * Constructor.
033    *
034    * @param contents The byte array being tested.
035    * @param returns The object to return after the test.
036    */
037   public FluentByteArrayAssertion(byte[] contents, R returns) {
038      this(null, contents, returns);
039   }
040
041   /**
042    * Constructor.
043    *
044    * @param creator The assertion that created this assertion.
045    * @param contents The byte array being tested.
046    * @param returns The object to return after the test.
047    */
048   public FluentByteArrayAssertion(Assertion creator, byte[] contents, R returns) {
049      super(creator, contents, returns);
050      this.value = contents;
051   }
052
053   /**
054    * Converts this byte array to a UTF-8 encoded string and returns it as a new assertion.
055    *
056    * <h5 class='section'>Example:</h5>
057    * <p class='bcode w800'>
058    *    <jc>// Validates that the specified byte array contains the string "foobar".</jc>
059    *    <jsm>assertBytes<jsm>(myByteArray).string().is(<js>"foobar"</js>);
060    * </p>
061    *
062    * @return A new fluent string assertion.
063    */
064   @Override
065   public FluentStringAssertion<R> string() {
066      return string(IOUtils.UTF8);
067   }
068
069   /**
070    * Converts this byte array to a string and returns it as a new assertion.
071    *
072    * <h5 class='section'>Example:</h5>
073    * <p class='bcode w800'>
074    *    <jc>// Validates that the specified byte array contains the string "foobar" encoded in ASCII.</jc>
075    *    <jsm>assertBytes<jsm>(myByteArray).string(<js>"iso8859-1"</js>).is(<js>"foobar"</js>);
076    * </p>
077    *
078    * @param cs The charset to use to decode the string.
079    * @return A new fluent string assertion.
080    */
081   public FluentStringAssertion<R> string(Charset cs) {
082      return new FluentStringAssertion<>(this, value == null ? null : new String(value, cs), returns());
083   }
084
085   /**
086    * Converts this byte array to a base-64 encoded string and returns it as a new assertion.
087    *
088    * <h5 class='section'>Example:</h5>
089    * <p class='bcode w800'>
090    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
091    *    <jsm>assertBytes<jsm>(myByteArray).base64().is(<js>"Zm9v"</js>);
092    * </p>
093    *
094    * @return A new fluent string assertion.
095    */
096   public FluentStringAssertion<R> base64() {
097      return new FluentStringAssertion<>(this, value == null ? null : base64Encode(value), returns());
098   }
099
100   /**
101    * Converts this byte array to hexadecimal and returns it as a new assertion.
102    *
103    * <h5 class='section'>Example:</h5>
104    * <p class='bcode w800'>
105    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
106    *    <jsm>assertBytes<jsm>(myByteArray).hex().is(<js>"666F6F"</js>);
107    * </p>
108    *
109    * @return A new string consisting of hexadecimal characters.
110    */
111   public FluentStringAssertion<R> hex() {
112      return new FluentStringAssertion<>(this, value == null ? null : toHex(value), returns());
113   }
114
115   /**
116    * Converts this byte array to spaced hexadecimal and returns it as a new assertion.
117    *
118    * <h5 class='section'>Example:</h5>
119    * <p class='bcode w800'>
120    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
121    *    <jsm>assertBytes<jsm>(myByteArray).spacedHex().is(<js>"66 6F 6F"</js>);
122    * </p>
123    *
124    * @return A new string consisting of hexadecimal characters.
125    */
126   public FluentStringAssertion<R> spacedHex() {
127      return new FluentStringAssertion<>(this, value == null ? null : toSpacedHex(value), returns());
128   }
129
130   // <FluentSetters>
131
132   @Override /* GENERATED - Assertion */
133   public FluentByteArrayAssertion<R> msg(String msg, Object...args) {
134      super.msg(msg, args);
135      return this;
136   }
137
138   @Override /* GENERATED - Assertion */
139   public FluentByteArrayAssertion<R> stderr() {
140      super.stderr();
141      return this;
142   }
143
144   @Override /* GENERATED - Assertion */
145   public FluentByteArrayAssertion<R> stdout() {
146      super.stdout();
147      return this;
148   }
149
150   // </FluentSetters>
151}