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