001 /* 002 * Created on Feb 14, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance 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 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2008-2009 the original author or authors. 015 */ 016 package org.fest.assertions; 017 018 import static org.fest.assertions.ArrayInspection.copy; 019 import static org.fest.assertions.ErrorMessages.unexpectedEqual; 020 import static org.fest.assertions.ErrorMessages.unexpectedNotEqual; 021 022 import java.util.Arrays; 023 024 /** 025 * Understands assertion methods for <code>boolean</code> arrays. To create a new instance of this class use the 026 * method <code>{@link Assertions#assertThat(boolean[])}</code>. 027 * 028 * @author Yvonne Wang 029 * @author Alex Ruiz 030 */ 031 public class BooleanArrayAssert extends ArrayAssert<boolean[]> { 032 033 /** 034 * Creates a new </code>{@link BooleanArrayAssert}</code>. 035 * @param actual the target to verify. 036 */ 037 protected BooleanArrayAssert(boolean... actual) { 038 super(actual); 039 } 040 041 /** {@inheritDoc} */ 042 public BooleanArrayAssert as(String description) { 043 description(description); 044 return this; 045 } 046 047 /** {@inheritDoc} */ 048 public BooleanArrayAssert describedAs(String description) { 049 return as(description); 050 } 051 052 /** {@inheritDoc} */ 053 public BooleanArrayAssert as(Description description) { 054 description(description); 055 return this; 056 } 057 058 /** {@inheritDoc} */ 059 public BooleanArrayAssert describedAs(Description description) { 060 return as(description); 061 } 062 063 /** 064 * Verifies that the actual <code>boolean</code> array contains the given values. 065 * @param values the values to look for. 066 * @return this assertion object. 067 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 068 * @throws NullPointerException if the given <code>boolean</code> array is <code>null</code>. 069 * @throws AssertionError if the actual <code>boolean</code> array does not contain the given values. 070 */ 071 public BooleanArrayAssert contains(boolean...values) { 072 isNotNull(); 073 validateIsNotNull(values); 074 assertContains(copy(values)); 075 return this; 076 } 077 078 /** 079 * Verifies that the actual <code>boolean</code> array contains the given values <strong>only</strong>. 080 * @param values the values to look for. 081 * @return this assertion object. 082 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 083 * @throws NullPointerException if the given <code>boolean</code> array is <code>null</code>. 084 * @throws AssertionError if the actual <code>boolean</code> array does not contain the given objects, or if the 085 * actual <code>boolean</code> array contains elements other than the ones specified. 086 */ 087 public BooleanArrayAssert containsOnly(boolean...values) { 088 isNotNull(); 089 validateIsNotNull(values); 090 assertContainsOnly(copy(values)); 091 return this; 092 } 093 094 /** 095 * Verifies that the actual <code>boolean</code> array does not contain the given values. 096 * @param values the values the array should exclude. 097 * @return this assertion object. 098 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 099 * @throws NullPointerException if the given <code>boolean</code> array is <code>null</code>. 100 * @throws AssertionError if the actual <code>boolean</code> array contains any of the given values. 101 */ 102 public BooleanArrayAssert excludes(boolean...values) { 103 isNotNull(); 104 validateIsNotNull(values); 105 assertExcludes(copy(values)); 106 return this; 107 } 108 109 private void validateIsNotNull(boolean[] values) { 110 if (values == null) 111 throw new NullPointerException(formattedErrorMessage("the given array of booleans should not be null")); 112 } 113 114 /** 115 * Verifies that the actual <code>boolean</code> array satisfies the given condition. 116 * @param condition the given condition. 117 * @return this assertion object. 118 * @throws NullPointerException if the given condition is <code>null</code>. 119 * @throws AssertionError if the actual <code>boolean</code> array does not satisfy the given condition. 120 * @see #is(Condition) 121 */ 122 public BooleanArrayAssert satisfies(Condition<boolean[]> condition) { 123 assertSatisfies(condition); 124 return this; 125 } 126 127 /** 128 * Verifies that the actual <code>boolean</code> array does not satisfy the given condition. 129 * @param condition the given condition. 130 * @return this assertion object. 131 * @throws NullPointerException if the given condition is <code>null</code>. 132 * @throws AssertionError if the actual <code>boolean</code> array satisfies the given condition. 133 * @see #isNot(Condition) 134 */ 135 public BooleanArrayAssert doesNotSatisfy(Condition<boolean[]> condition) { 136 assertDoesNotSatisfy(condition); 137 return this; 138 } 139 140 /** 141 * Alias for <code>{@link #satisfies(Condition)}</code>. 142 * @param condition the given condition. 143 * @return this assertion object. 144 * @throws NullPointerException if the given condition is <code>null</code>. 145 * @throws AssertionError if the actual <code>boolean</code> array does not satisfy the given condition. 146 * @since 1.2 147 */ 148 public BooleanArrayAssert is(Condition<boolean[]> condition) { 149 assertIs(condition); 150 return this; 151 } 152 153 /** 154 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>. 155 * @param condition the given condition. 156 * @return this assertion object. 157 * @throws NullPointerException if the given condition is <code>null</code>. 158 * @throws AssertionError if the actual <code>boolean</code> array satisfies the given condition. 159 * @since 1.2 160 */ 161 public BooleanArrayAssert isNot(Condition<boolean[]> condition) { 162 assertIsNot(condition); 163 return this; 164 } 165 166 /** 167 * Verifies that the actual <code>boolean</code> array is not <code>null</code>. 168 * @return this assertion object. 169 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 170 */ 171 public BooleanArrayAssert isNotNull() { 172 assertThatActualIsNotNull(); 173 return this; 174 } 175 176 /** 177 * Verifies that the actual <code>boolean</code> array contains at least on element. 178 * @return this assertion object. 179 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 180 * @throws AssertionError if the actual <code>boolean</code> array is empty. 181 */ 182 public BooleanArrayAssert isNotEmpty() { 183 assertThatActualIsNotEmpty(); 184 return this; 185 } 186 187 /** 188 * Verifies that the actual <code>boolean</code> array is equal to the given array. Array equality is checked by 189 * <code>{@link Arrays#equals(boolean[], boolean[])}</code>. 190 * @param expected the given array to compare the actual array to. 191 * @return this assertion object. 192 * @throws AssertionError if the actual <code>boolean</code> array is not equal to the given one. 193 */ 194 public BooleanArrayAssert isEqualTo(boolean[] expected) { 195 if (Arrays.equals(actual, expected)) return this; 196 failIfCustomMessageIsSet(); 197 throw failure(unexpectedNotEqual(actual, expected)); 198 } 199 200 /** 201 * Verifies that the actual <code>boolean</code> array is not equal to the given array. Array equality is checked by 202 * <code>{@link Arrays#equals(boolean[], boolean[])}</code>. 203 * @param array the given array to compare the actual array to. 204 * @return this assertion object. 205 * @throws AssertionError if the actual <code>boolean</code> array is equal to the given one. 206 */ 207 public BooleanArrayAssert isNotEqualTo(boolean[] array) { 208 if (!Arrays.equals(actual, array)) return this; 209 failIfCustomMessageIsSet(); 210 throw failure(unexpectedEqual(actual, array)); 211 } 212 213 /** 214 * Verifies that the number of elements in the actual <code>boolean</code> array is equal to the given one. 215 * @param expected the expected number of elements in the actual <code>boolean</code> array. 216 * @return this assertion object. 217 * @throws AssertionError if the actual <code>boolean</code> array is <code>null</code>. 218 * @throws AssertionError if the number of elements in the actual <code>boolean</code> array is not equal to the given 219 * one. 220 */ 221 public BooleanArrayAssert hasSize(int expected) { 222 assertThatActualHasSize(expected); 223 return this; 224 } 225 226 /** 227 * Verifies that the actual <code>boolean</code> array is the same as the given array. 228 * @param expected the given array to compare the actual array to. 229 * @return this assertion object. 230 * @throws AssertionError if the actual <code>boolean</code> array is not the same as the given one. 231 */ 232 public BooleanArrayAssert isSameAs(boolean[] expected) { 233 assertSameAs(expected); 234 return this; 235 } 236 237 /** 238 * Verifies that the actual <code>boolean</code> array is not the same as the given array. 239 * @param expected the given array to compare the actual array to. 240 * @return this assertion object. 241 * @throws AssertionError if the actual <code>boolean</code> array is the same as the given one. 242 */ 243 public BooleanArrayAssert isNotSameAs(boolean[] expected) { 244 assertNotSameAs(expected); 245 return this; 246 } 247 248 /** {@inheritDoc} */ 249 public BooleanArrayAssert overridingErrorMessage(String message) { 250 replaceDefaultErrorMessagesWith(message); 251 return this; 252 } 253 }