001 /* 002 * Created on Dec 15, 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.internal; 016 017 import java.util.Comparator; 018 019 import org.fest.assertions.core.*; 020 import org.fest.assertions.data.Index; 021 import org.fest.util.VisibleForTesting; 022 023 /** 024 * Reusable assertions for arrays of {@code boolean}s. 025 * 026 * @author Alex Ruiz 027 * @author Joel Costigliola 028 * @author Mikhail Mazursky 029 */ 030 public class BooleanArrays { 031 032 private static final BooleanArrays INSTANCE = new BooleanArrays(); 033 034 /** 035 * Returns the singleton instance of this class. 036 * @return the singleton instance of this class. 037 */ 038 public static BooleanArrays instance() { 039 return INSTANCE; 040 } 041 042 private final Arrays arrays = Arrays.instance(); 043 044 @VisibleForTesting 045 Failures failures = Failures.instance(); 046 047 @VisibleForTesting 048 BooleanArrays() {} 049 050 /** 051 * Asserts that the given array is {@code null} or empty. 052 * @param info contains information about the assertion. 053 * @param actual the given array. 054 * @throws AssertionError if the given array is not {@code null} *and* contains one or more elements. 055 */ 056 public void assertNullOrEmpty(AssertionInfo info, boolean[] actual) { 057 arrays.assertNullOrEmpty(info, failures, actual); 058 } 059 060 /** 061 * Asserts that the given array is empty. 062 * @param info contains information about the assertion. 063 * @param actual the given array. 064 * @throws AssertionError if the given array is {@code null}. 065 * @throws AssertionError if the given array is not empty. 066 */ 067 public void assertEmpty(AssertionInfo info, boolean[] actual) { 068 arrays.assertEmpty(info, failures, actual); 069 } 070 071 /** 072 * Asserts that the given array is not empty. 073 * @param info contains information about the assertion. 074 * @param actual the given array. 075 * @throws AssertionError if the given array is {@code null}. 076 * @throws AssertionError if the given array is empty. 077 */ 078 public void assertNotEmpty(AssertionInfo info, boolean[] actual) { 079 arrays.assertNotEmpty(info, failures, actual); 080 } 081 082 /** 083 * Asserts that the number of elements in the given array is equal to the expected one. 084 * @param info contains information about the assertion. 085 * @param actual the given array. 086 * @param expectedSize the expected size of {@code actual}. 087 * @throws AssertionError if the given array is {@code null}. 088 * @throws AssertionError if the number of elements in the given array is different than the expected one. 089 */ 090 public void assertHasSize(AssertionInfo info, boolean[] actual, int expectedSize) { 091 arrays.assertHasSize(info, failures, actual, expectedSize); 092 } 093 094 /** 095 * Asserts that the given array contains the given values, in any order. 096 * @param info contains information about the assertion. 097 * @param actual the given array. 098 * @param values the values that are expected to be in the given array. 099 * @throws NullPointerException if the array of values is {@code null}. 100 * @throws IllegalArgumentException if the array of values is empty. 101 * @throws AssertionError if the given array is {@code null}. 102 * @throws AssertionError if the given array does not contain the given values. 103 */ 104 public void assertContains(AssertionInfo info, boolean[] actual, boolean[] values) { 105 arrays.assertContains(info, failures, actual, values); 106 } 107 108 /** 109 * Verifies that the given array contains the given value at the given index. 110 * @param info contains information about the assertion. 111 * @param actual the given array. 112 * @param value the value to look for. 113 * @param index the index where the value should be stored in the given array. 114 * @throws AssertionError if the given 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 given array. 118 * @throws AssertionError if the given array does not contain the given value at the given index. 119 */ 120 public void assertContains(AssertionInfo info, boolean[] actual, boolean value, Index index) { 121 arrays.assertContains(info, failures, actual, value, index); 122 } 123 124 /** 125 * Verifies that the given array does not contain the given value at the given index. 126 * @param info contains information about the assertion. 127 * @param actual the given array. 128 * @param value the value to look for. 129 * @param index the index where the value should be stored in the given array. 130 * @throws AssertionError if the given array is {@code null}. 131 * @throws NullPointerException if the given {@code Index} is {@code null}. 132 * @throws AssertionError if the given array contains the given value at the given index. 133 */ 134 public void assertDoesNotContain(AssertionInfo info, boolean[] actual, boolean value, Index index) { 135 arrays.assertDoesNotContain(info, failures, actual, value, index); 136 } 137 138 /** 139 * Asserts that the given array contains only the given values and nothing else, in any order. 140 * @param info contains information about the assertion. 141 * @param actual the given array. 142 * @param values the values that are expected to be in the given array. 143 * @throws NullPointerException if the array of values is {@code null}. 144 * @throws IllegalArgumentException if the array of values is empty. 145 * @throws AssertionError if the given array is {@code null}. 146 * @throws AssertionError if the given array does not contain the given values or if the given array contains values 147 * that are not in the given array. 148 */ 149 public void assertContainsOnly(AssertionInfo info, boolean[] actual, boolean[] values) { 150 arrays.assertContainsOnly(info, failures, actual, values); 151 } 152 153 /** 154 * Verifies that the given array contains the given sequence of values, without any other values between them. 155 * @param info contains information about the assertion. 156 * @param actual the given array. 157 * @param sequence the sequence of values to look for. 158 * @throws AssertionError if the given array is {@code null}. 159 * @throws NullPointerException if the given sequence is {@code null}. 160 * @throws IllegalArgumentException if the given sequence is empty. 161 * @throws AssertionError if the given array does not contain the given sequence of values. 162 */ 163 public void assertContainsSequence(AssertionInfo info, boolean[] actual, boolean[] sequence) { 164 arrays.assertContainsSequence(info, failures, actual, sequence); 165 } 166 167 /** 168 * Asserts that the given array does not contain the given values. 169 * @param info contains information about the assertion. 170 * @param actual the given array. 171 * @param values the values that are expected not to be in the given array. 172 * @throws NullPointerException if the array of values is {@code null}. 173 * @throws IllegalArgumentException if the array of values is empty. 174 * @throws AssertionError if the given array is {@code null}. 175 * @throws AssertionError if the given array contains any of given values. 176 */ 177 public void assertDoesNotContain(AssertionInfo info, boolean[] actual, boolean[] values) { 178 arrays.assertDoesNotContain(info, failures, actual, values); 179 } 180 181 /** 182 * Asserts that the given array does not have duplicate values. 183 * @param info contains information about the assertion. 184 * @param actual the given array. 185 * @throws NullPointerException if the array of values is {@code null}. 186 * @throws IllegalArgumentException if the array of values is empty. 187 * @throws AssertionError if the given array is {@code null}. 188 * @throws AssertionError if the given array contains duplicate values. 189 */ 190 public void assertDoesNotHaveDuplicates(AssertionInfo info, boolean[] actual) { 191 arrays.assertDoesNotHaveDuplicates(info, failures, actual); 192 } 193 194 /** 195 * Verifies that the given array starts with the given sequence of values, without any other values between them. 196 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, boolean[], boolean[])}</code>, but it also verifies 197 * that the first element in the sequence is also the first element of the given array. 198 * @param info contains information about the assertion. 199 * @param actual the given array. 200 * @param sequence the sequence of values to look for. 201 * @throws NullPointerException if the given argument is {@code null}. 202 * @throws IllegalArgumentException if the given argument is an empty array. 203 * @throws AssertionError if the given array is {@code null}. 204 * @throws AssertionError if the given array does not start with the given sequence of values. 205 */ 206 public void assertStartsWith(AssertionInfo info, boolean[] actual, boolean[] sequence) { 207 arrays.assertStartsWith(info, failures, actual, sequence); 208 } 209 210 /** 211 * Verifies that the given array ends with the given sequence of values, without any other values between them. 212 * Similar to <code>{@link #assertContainsSequence(AssertionInfo, boolean[], boolean[])}</code>, but it also verifies 213 * that the last element in the sequence is also the last element of the given array. 214 * @param info contains information about the assertion. 215 * @param actual the given array. 216 * @param sequence the sequence of values to look for. 217 * @throws NullPointerException if the given argument is {@code null}. 218 * @throws IllegalArgumentException if the given argument is an empty array. 219 * @throws AssertionError if the given array is {@code null}. 220 * @throws AssertionError if the given array does not end with the given sequence of values. 221 */ 222 public void assertEndsWith(AssertionInfo info, boolean[] actual, boolean[] sequence) { 223 arrays.assertEndsWith(info, failures, actual, sequence); 224 } 225 226 /** 227 * Concrete implementation of {@link ArraySortedAssert#isSorted()}. 228 * 229 * @param info contains information about the assertion. 230 * @param actual the given array. 231 */ 232 public void assertIsSorted(AssertionInfo info, boolean[] actual) { 233 arrays.assertIsSorted(info, failures, actual); 234 } 235 236 /** 237 * Concrete implementation of {@link ArraySortedAssert#isSortedAccordingTo(Comparator)}. 238 * 239 * @param info contains information about the assertion. 240 * @param actual the given array. 241 * @param comparator the {@link Comparator} used to compare array elements 242 */ 243 public void assertIsSortedAccordingToComparator(AssertionInfo info, boolean[] actual, 244 Comparator<? super Boolean> comparator) { 245 Arrays.assertIsSortedAccordingToComparator(info, failures, actual, comparator); 246 } 247 248 }