001 /*
002 * Created on Dec 14, 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.BooleanArrays;
023 import org.fest.util.VisibleForTesting;
024
025 /**
026 * Assertion methods for arrays of {@code boolean}s.
027 * <p>
028 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(boolean[])}</code>.
029 * </p>
030 *
031 * @author Yvonne Wang
032 * @author Alex Ruiz
033 * @author Joel Costigliola
034 * @author Mikhail Mazursky
035 */
036 public class BooleanArrayAssert extends AbstractAssert<BooleanArrayAssert, boolean[]> implements
037 EnumerableAssert<BooleanArrayAssert, Boolean>, ArraySortedAssert<BooleanArrayAssert, Boolean> {
038
039 @VisibleForTesting
040 BooleanArrays arrays = BooleanArrays.instance();
041
042 protected BooleanArrayAssert(boolean[] actual) {
043 super(actual, BooleanArrayAssert.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 BooleanArrayAssert isNotEmpty() {
058 arrays.assertNotEmpty(info, actual);
059 return this;
060 }
061
062 /** {@inheritDoc} */
063 public BooleanArrayAssert 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 BooleanArrayAssert contains(boolean... 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 BooleanArrayAssert containsOnly(boolean... 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 BooleanArrayAssert containsSequence(boolean... 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 BooleanArrayAssert contains(boolean 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 BooleanArrayAssert doesNotContain(boolean... 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 BooleanArrayAssert doesNotContain(boolean 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 BooleanArrayAssert 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(boolean...)}</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 BooleanArrayAssert startsWith(boolean... 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(boolean...)}</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 BooleanArrayAssert endsWith(boolean... sequence) {
193 arrays.assertEndsWith(info, actual, sequence);
194 return this;
195 }
196
197 /** {@inheritDoc} */
198 public BooleanArrayAssert isSorted() {
199 arrays.assertIsSorted(info, actual);
200 return this;
201 }
202
203 /** {@inheritDoc} */
204 public BooleanArrayAssert isSortedAccordingTo(Comparator<? super Boolean> comparator) {
205 arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
206 return this;
207 }
208
209 /** {@inheritDoc} */
210 public BooleanArrayAssert usingElementComparator(Comparator<? super Boolean> customComparator) {
211 throw new UnsupportedOperationException("custom element Comparator is not supported for Boolean array comparison");
212 }
213
214 /** {@inheritDoc} */
215 public BooleanArrayAssert usingDefaultElementComparator() {
216 throw new UnsupportedOperationException("custom element Comparator is not supported for Boolean array comparison");
217 }
218 }