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