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 */
035 public class BooleanArrayAssert extends AbstractAssert<BooleanArrayAssert, boolean[]> implements
036 EnumerableAssert<BooleanArrayAssert>, ArraySortedAssert<BooleanArrayAssert, Boolean> {
037
038 @VisibleForTesting
039 BooleanArrays arrays = BooleanArrays.instance();
040
041 protected BooleanArrayAssert(boolean[] actual) {
042 super(actual, BooleanArrayAssert.class);
043 }
044
045 /** {@inheritDoc} */
046 public void isNullOrEmpty() {
047 arrays.assertNullOrEmpty(info, actual);
048 }
049
050 /** {@inheritDoc} */
051 public void isEmpty() {
052 arrays.assertEmpty(info, actual);
053 }
054
055 /** {@inheritDoc} */
056 public BooleanArrayAssert isNotEmpty() {
057 arrays.assertNotEmpty(info, actual);
058 return this;
059 }
060
061 /** {@inheritDoc} */
062 public BooleanArrayAssert hasSize(int expected) {
063 arrays.assertHasSize(info, actual, expected);
064 return this;
065 }
066
067 /**
068 * Verifies that the actual array contains the given values, in any order.
069 * @param values the given values.
070 * @return {@code this} assertion object.
071 * @throws NullPointerException if the given argument is {@code null}.
072 * @throws IllegalArgumentException if the given argument is an empty array.
073 * @throws AssertionError if the actual array is {@code null}.
074 * @throws AssertionError if the actual array does not contain the given values.
075 */
076 public BooleanArrayAssert contains(boolean... values) {
077 arrays.assertContains(info, actual, values);
078 return this;
079 }
080
081 /**
082 * Verifies that the actual array contains only the given values and nothing else, in any order.
083 * @param values the given values.
084 * @return {@code this} assertion object.
085 * @throws NullPointerException if the given argument is {@code null}.
086 * @throws IllegalArgumentException if the given argument is an empty array.
087 * @throws AssertionError if the actual array is {@code null}.
088 * @throws AssertionError if the actual array does not contain the given values, i.e. the actual array contains some
089 * or none of the given values, or the actual array contains more values than the given ones.
090 */
091 public BooleanArrayAssert containsOnly(boolean... values) {
092 arrays.assertContainsOnly(info, actual, values);
093 return this;
094 }
095
096 /**
097 * Verifies that the actual array contains the given sequence, without any other values between them.
098 * @param sequence the sequence of values to look for.
099 * @return this assertion object.
100 * @throws AssertionError if the actual array is {@code null}.
101 * @throws AssertionError if the given array is {@code null}.
102 * @throws AssertionError if the actual array does not contain the given sequence.
103 */
104 public BooleanArrayAssert containsSequence(boolean... sequence) {
105 arrays.assertContainsSequence(info, actual, sequence);
106 return this;
107 }
108
109 /**
110 * Verifies that the actual array contains the given value at the given index.
111 * @param value the value to look for.
112 * @param index the index where the value should be stored in the actual array.
113 * @return this assertion object.
114 * @throws AssertionError if the actual array is {@code null} or empty.
115 * @throws NullPointerException if the given {@code Index} is {@code null}.
116 * @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of
117 * the actual array.
118 * @throws AssertionError if the actual array does not contain the given value at the given index.
119 */
120 public BooleanArrayAssert contains(boolean value, Index index) {
121 arrays.assertContains(info, actual, value, index);
122 return this;
123 }
124
125 /**
126 * Verifies that the actual array does not contain the given values.
127 * @param values the given values.
128 * @return {@code this} assertion object.
129 * @throws NullPointerException if the given argument is {@code null}.
130 * @throws IllegalArgumentException if the given argument is an empty array.
131 * @throws AssertionError if the actual array is {@code null}.
132 * @throws AssertionError if the actual array contains any of the given values.
133 */
134 public BooleanArrayAssert doesNotContain(boolean... values) {
135 arrays.assertDoesNotContain(info, actual, values);
136 return this;
137 }
138
139 /**
140 * Verifies that the actual array does not contain the given value at the given index.
141 * @param value the value to look for.
142 * @param index the index where the value should be stored in the actual array.
143 * @return this assertion object.
144 * @throws AssertionError if the actual array is {@code null}.
145 * @throws NullPointerException if the given {@code Index} is {@code null}.
146 * @throws AssertionError if the actual array contains the given value at the given index.
147 */
148 public BooleanArrayAssert doesNotContain(boolean value, Index index) {
149 arrays.assertDoesNotContain(info, actual, value, index);
150 return this;
151 }
152
153 /**
154 * Verifies that the actual array does not contain duplicates.
155 * @return {@code this} assertion object.
156 * @throws AssertionError if the actual array is {@code null}.
157 * @throws AssertionError if the actual array contains duplicates.
158 */
159 public BooleanArrayAssert doesNotHaveDuplicates() {
160 arrays.assertDoesNotHaveDuplicates(info, actual);
161 return this;
162 }
163
164 /**
165 * Verifies that the actual array starts with the given sequence of values, without any other values between them.
166 * Similar to <code>{@link #containsSequence(boolean...)}</code>, but it also verifies that the first element in the
167 * sequence is also first element of the actual array.
168 * @param sequence the sequence of values to look for.
169 * @return this assertion object.
170 * @throws NullPointerException if the given argument is {@code null}.
171 * @throws IllegalArgumentException if the given argument is an empty array.
172 * @throws AssertionError if the actual array is {@code null}.
173 * @throws AssertionError if the actual array does not start with the given sequence.
174 */
175 public BooleanArrayAssert startsWith(boolean... sequence) {
176 arrays.assertStartsWith(info, actual, sequence);
177 return this;
178 }
179
180 /**
181 * Verifies that the actual array ends with the given sequence of values, without any other values between them.
182 * Similar to <code>{@link #containsSequence(boolean...)}</code>, but it also verifies that the last element in the
183 * sequence is also last element of the actual array.
184 * @param sequence the sequence of values to look for.
185 * @return this assertion object.
186 * @throws NullPointerException if the given argument is {@code null}.
187 * @throws IllegalArgumentException if the given argument is an empty array.
188 * @throws AssertionError if the actual array is {@code null}.
189 * @throws AssertionError if the actual array does not end with the given sequence.
190 */
191 public BooleanArrayAssert endsWith(boolean... sequence) {
192 arrays.assertEndsWith(info, actual, sequence);
193 return this;
194 }
195
196 /** {@inheritDoc} */
197 public BooleanArrayAssert isSorted() {
198 arrays.assertIsSorted(info, actual);
199 return this;
200 }
201
202 /** {@inheritDoc} */
203 public BooleanArrayAssert isSortedAccordingTo(Comparator<? extends Boolean> comparator) {
204 arrays.assertIsSortedAccordingToComparator(info, actual, comparator);
205 return this;
206 }
207
208 @Override
209 public BooleanArrayAssert usingComparator(Comparator<?> customComparator) {
210 throw new UnsupportedOperationException("custom Comparator is not supported for Boolean array comparison");
211 }
212 }