001 /*
002 * Created on Dec 17, 2010
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * 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
010 * an "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 *
013 * Copyright @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import java.util.Comparator;
018
019 import org.fest.assertions.core.ArraySortedAssert;
020 import org.fest.assertions.core.EnumerableAssert;
021 import org.fest.assertions.data.Index;
022 import org.fest.assertions.internal.ByteArrays;
023 import org.fest.util.ComparatorBasedComparisonStrategy;
024 import org.fest.util.VisibleForTesting;
025
026 /**
027 * Assertion methods for arrays of {@code byte}s.
028 * <p>
029 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(byte[])}</code>.
030 * </p>
031 *
032 * @author Yvonne Wang
033 * @author Alex Ruiz
034 * @author Joel Costigliola
035 */
036 public class ByteArrayAssert extends AbstractAssert<ByteArrayAssert, byte[]> implements
037 EnumerableAssert<ByteArrayAssert>, ArraySortedAssert<ByteArrayAssert, Byte> {
038
039 @VisibleForTesting
040 ByteArrays arrays = ByteArrays.instance();
041
042 protected ByteArrayAssert(byte[] actual) {
043 super(actual, ByteArrayAssert.class);
044 }
045
046 /** {@inheritDoc} */
047 public void isNullOrEmpty() {
048 arrays.assertNullOrEmpty(info, actual);
049 }
050
051 /** {@inheritDoc} */
052 public void isEmpty() {
053 arrays.assertEmpty(info, actual);
054 }
055
056 /** {@inheritDoc} */
057 public ByteArrayAssert isNotEmpty() {
058 arrays.assertNotEmpty(info, actual);
059 return this;
060 }
061
062 /** {@inheritDoc} */
063 public ByteArrayAssert hasSize(int expected) {
064 arrays.assertHasSize(info, actual, expected);
065 return this;
066 }
067
068 /**
069 * Verifies that the actual array contains the given values, in any order.
070 * @param values the given values.
071 * @return {@code this} assertion object.
072 * @throws NullPointerException if the given argument is {@code null}.
073 * @throws IllegalArgumentException if the given argument is an empty array.
074 * @throws AssertionError if the actual array is {@code null}.
075 * @throws AssertionError if the actual array does not contain the given values.
076 */
077 public ByteArrayAssert contains(byte... values) {
078 arrays.assertContains(info, actual, values);
079 return this;
080 }
081
082 /**
083 * Verifies that the actual array contains only the given values and nothing else, in any order.
084 * @param values the given values.
085 * @return {@code this} assertion object.
086 * @throws NullPointerException if the given argument is {@code null}.
087 * @throws IllegalArgumentException if the given argument is an empty array.
088 * @throws AssertionError if the actual array is {@code null}.
089 * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some
090 * or none of the given values, or the actual array contains more values than the given ones.
091 */
092 public ByteArrayAssert containsOnly(byte... values) {
093 arrays.assertContainsOnly(info, actual, values);
094 return this;
095 }
096
097 /**
098 * Verifies that the actual array contains the given sequence, without any other values between them.
099 * @param sequence the sequence of values to look for.
100 * @return this assertion object.
101 * @throws AssertionError if the actual array is {@code null}.
102 * @throws AssertionError if the given array is {@code null}.
103 * @throws AssertionError if the actual array does not contain the given sequence.
104 */
105 public ByteArrayAssert containsSequence(byte... sequence) {
106 arrays.assertContainsSequence(info, actual, sequence);
107 return this;
108 }
109
110 /**
111 * Verifies that the actual array contains the given value at the given index.
112 * @param value the value to look for.
113 * @param index the index where the value should be stored in the actual array.
114 * @return this assertion object.
115 * @throws AssertionError if the actual array is {@code null} or empty.
116 * @throws NullPointerException if the given {@code Index} is {@code null}.
117 * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
118 * the actual array.
119 * @throws AssertionError if the actual array does not contain the given value at the given index.
120 */
121 public ByteArrayAssert contains(byte value, Index index) {
122 arrays.assertContains(info, actual, value, index);
123 return this;
124 }
125
126 /**
127 * Verifies that the actual array does not contain the given values.
128 * @param values the given values.
129 * @return {@code this} assertion object.
130 * @throws NullPointerException if the given argument is {@code null}.
131 * @throws IllegalArgumentException if the given argument is an empty array.
132 * @throws AssertionError if the actual array is {@code null}.
133 * @throws AssertionError if the actual array contains any of the given values.
134 */
135 public ByteArrayAssert doesNotContain(byte... values) {
136 arrays.assertDoesNotContain(info, actual, values);
137 return this;
138 }
139
140 /**
141 * Verifies that the actual array does not contain the given value at the given index.
142 * @param value the value to look for.
143 * @param index the index where the value should be stored in the actual array.
144 * @return this assertion object.
145 * @throws AssertionError if the actual array is {@code null}.
146 * @throws NullPointerException if the given {@code Index} is {@code null}.
147 * @throws AssertionError if the actual array contains the given value at the given index.
148 */
149 public ByteArrayAssert doesNotContain(byte value, Index index) {
150 arrays.assertDoesNotContain(info, actual, value, index);
151 return this;
152 }
153
154 /**
155 * Verifies that the actual array does not contain duplicates.
156 * @return {@code this} assertion object.
157 * @throws AssertionError if the actual array is {@code null}.
158 * @throws AssertionError if the actual array contains duplicates.
159 */
160 public ByteArrayAssert doesNotHaveDuplicates() {
161 arrays.assertDoesNotHaveDuplicates(info, actual);
162 return this;
163 }
164
165 /**
166 * Verifies that the actual array starts with the given sequence of values, without any other values between them.
167 * Similar to <code>{@link #containsSequence(byte...)}</code>, but it also verifies that the first element in the
168 * sequence is also first element of the actual array.
169 * @param sequence the sequence of values to look for.
170 * @return this assertion object.
171 * @throws NullPointerException if the given argument is {@code null}.
172 * @throws IllegalArgumentException if the given argument is an empty array.
173 * @throws AssertionError if the actual array is {@code null}.
174 * @throws AssertionError if the actual array does not start with the given sequence.
175 */
176 public ByteArrayAssert startsWith(byte... sequence) {
177 arrays.assertStartsWith(info, actual, sequence);
178 return this;
179 }
180
181 /**
182 * Verifies that the actual array ends with the given sequence of values, without any other values between them.
183 * Similar to <code>{@link #containsSequence(byte...)}</code>, but it also verifies that the last element in the
184 * sequence is also last element of the actual array.
185 * @param sequence the sequence of values to look for.
186 * @return this assertion object.
187 * @throws NullPointerException if the given argument is {@code null}.
188 * @throws IllegalArgumentException if the given argument is an empty array.
189 * @throws AssertionError if the actual array is {@code null}.
190 * @throws AssertionError if the actual array does not end with the given sequence.
191 */
192 public ByteArrayAssert endsWith(byte... sequence) {
193 arrays.assertEndsWith(info, actual, sequence);
194 return this;
195 }
196
197 /** {@inheritDoc} */
198 public ByteArrayAssert isSorted() {
199 arrays.assertIsSorted(info, actual);
200 return this;
201 }
202
203 /** {@inheritDoc} */
204 public ByteArrayAssert isSortedAccordingTo(Comparator<? extends Byte> comparator) {
205 arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
206 return this;
207 }
208
209 @Override
210 public ByteArrayAssert usingComparator(Comparator<?> customComparator) {
211 super.usingComparator(customComparator);
212 this.arrays = new ByteArrays(new ComparatorBasedComparisonStrategy(customComparator));
213 return myself;
214 }
215
216 @Override
217 public ByteArrayAssert usingDefaultComparator() {
218 super.usingDefaultComparator();
219 this.arrays = ByteArrays.instance();
220 return myself;
221 }
222 }