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