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