001 /*
002 * Created on Feb 14, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with 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
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2008-2009 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.assertions.ArrayInspection.copy;
019 import static org.fest.assertions.ErrorMessages.unexpectedEqual;
020 import static org.fest.assertions.ErrorMessages.unexpectedNotEqual;
021
022 import java.util.Arrays;
023
024 /**
025 * Understands assertion methods for <code>char</code> arrays. To create a new instance of this class use the
026 * method <code>{@link Assertions#assertThat(char[])}</code>.
027 *
028 * @author Yvonne Wang
029 * @author Alex Ruiz
030 */
031 public class CharArrayAssert extends ArrayAssert<char[]> {
032
033 /**
034 * Creates a new </code>{@link CharArrayAssert}</code>.
035 * @param actual the target to verify.
036 */
037 protected CharArrayAssert(char... actual) {
038 super(actual);
039 }
040
041 /**
042 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
043 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
044 * failure will not show the provided description.
045 * <p>
046 * For example:
047 * <pre>
048 * assertThat(values).<strong>as</strong>("Some values").isNotEmpty();
049 * </pre>
050 * </p>
051 * @param description the description of the actual value.
052 * @return this assertion object.
053 */
054 public CharArrayAssert as(String description) {
055 description(description);
056 return this;
057 }
058
059 /**
060 * Alias for <code>{@link #as(String)}</code>, since "as" is a keyword in
061 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
062 * method, otherwise any assertion failure will not show the provided description.
063 * <p>
064 * For example:
065 * <pre>
066 * assertThat(values).<strong>describedAs</strong>("Some values").isNotEmpty();
067 * </pre>
068 * </p>
069 * @param description the description of the actual value.
070 * @return this assertion object.
071 */
072 public CharArrayAssert describedAs(String description) {
073 return as(description);
074 }
075
076 /**
077 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
078 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
079 * failure will not show the provided description.
080 * <p>
081 * For example:
082 * <pre>
083 * assertThat(values).<strong>as</strong>(new BasicDescription("Some values")).isNotEmpty();
084 * </pre>
085 * </p>
086 * @param description the description of the actual value.
087 * @return this assertion object.
088 */
089 public CharArrayAssert as(Description description) {
090 description(description);
091 return this;
092 }
093
094 /**
095 * Alias for <code>{@link #as(Description)}</code>, since "as" is a keyword in
096 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
097 * method, otherwise any assertion failure will not show the provided description.
098 * <p>
099 * For example:
100 * <pre>
101 * assertThat(values).<strong>describedAs</strong>(new BasicDescription("Some values")).isNotEmpty();
102 * </pre>
103 * </p>
104 * @param description the description of the actual value.
105 * @return this assertion object.
106 */
107 public CharArrayAssert describedAs(Description description) {
108 return as(description);
109 }
110
111 /**
112 * Verifies that the actual <code>char</code> array contains the given values.
113 * @param values the values to look for.
114 * @return this assertion object.
115 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
116 * @throws NullPointerException if the given <code>char</code> array is <code>null</code>.
117 * @throws AssertionError if the actual <code>char</code> array does not contain the given values.
118 */
119 public CharArrayAssert contains(char...values) {
120 isNotNull();
121 validateIsNotNull(values);
122 assertContains(copy(values));
123 return this;
124 }
125
126 /**
127 * Verifies that the actual <code>char</code> array contains the given values <strong>only</strong>.
128 * @param values the values to look for.
129 * @return this assertion object.
130 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
131 * @throws NullPointerException if the given <code>char</code> array is <code>null</code>.
132 * @throws AssertionError if the actual <code>char</code> array does not contain the given objects, or if the actual
133 * <code>char</code> array contains elements other than the ones specified.
134 */
135 public CharArrayAssert containsOnly(char...values) {
136 isNotNull();
137 validateIsNotNull(values);
138 assertContainsOnly(copy(values));
139 return this;
140 }
141
142 /**
143 * Verifies that the actual <code>char</code> array does not contain the given values.
144 * @param values the values the array should exclude.
145 * @return this assertion object.
146 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
147 * @throws NullPointerException if the given <code>char</code> array is <code>null</code>.
148 * @throws AssertionError if the actual <code>char</code> array contains any of the given values.
149 */
150 public CharArrayAssert excludes(char...values) {
151 isNotNull();
152 validateIsNotNull(values);
153 assertExcludes(copy(values));
154 return this;
155 }
156
157 private void validateIsNotNull(char[] values) {
158 if (values == null)
159 throw new NullPointerException(formattedErrorMessage("the given array of chars should not be null"));
160 }
161
162 /**
163 * Verifies that the actual <code>char</code> array satisfies the given condition.
164 * @param condition the given condition.
165 * @return this assertion object.
166 * @throws NullPointerException if the given condition is <code>null</code>.
167 * @throws AssertionError if the actual <code>char</code> array does not satisfy the given condition.
168 * @see #is(Condition)
169 */
170 public CharArrayAssert satisfies(Condition<char[]> condition) {
171 assertSatisfies(condition);
172 return this;
173 }
174
175 /**
176 * Verifies that the actual <code>char</code> array does not satisfy the given condition.
177 * @param condition the given condition.
178 * @return this assertion object.
179 * @throws NullPointerException if the given condition is <code>null</code>.
180 * @throws AssertionError if the actual <code>char</code> array satisfies the given condition.
181 * @see #isNot(Condition)
182 */
183 public CharArrayAssert doesNotSatisfy(Condition<char[]> condition) {
184 assertDoesNotSatisfy(condition);
185 return this;
186 }
187
188 /**
189 * Alias for <code>{@link #satisfies(Condition)}</code>.
190 * @param condition the given condition.
191 * @return this assertion object.
192 * @throws NullPointerException if the given condition is <code>null</code>.
193 * @throws AssertionError if the actual <code>char</code> array does not satisfy the given condition.
194 * @since 1.2
195 */
196 public CharArrayAssert is(Condition<char[]> condition) {
197 assertIs(condition);
198 return this;
199 }
200
201 /**
202 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>.
203 * @param condition the given condition.
204 * @return this assertion object.
205 * @throws NullPointerException if the given condition is <code>null</code>.
206 * @throws AssertionError if the actual <code>char</code> array satisfies the given condition.
207 * @since 1.2
208 */
209 public CharArrayAssert isNot(Condition<char[]> condition) {
210 assertIsNot(condition);
211 return this;
212 }
213
214 /**
215 * Verifies that the actual <code>char</code> array is not <code>null</code>.
216 * @return this assertion object.
217 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
218 */
219 public CharArrayAssert isNotNull() {
220 assertThatActualIsNotNull();
221 return this;
222 }
223
224 /**
225 * Verifies that the actual <code>char</code> array contains at least on element.
226 * @return this assertion object.
227 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
228 * @throws AssertionError if the actual <code>char</code> array is empty.
229 */
230 public CharArrayAssert isNotEmpty() {
231 assertThatActualIsNotEmpty();
232 return this;
233 }
234
235 /**
236 * Verifies that the actual <code>char</code> array is equal to the given array. Array equality is checked by
237 * <code>{@link Arrays#equals(char[], char[])}</code>.
238 * @param expected the given array to compare the actual array to.
239 * @return this assertion object.
240 * @throws AssertionError if the actual <code>char</code> array is not equal to the given one.
241 */
242 public CharArrayAssert isEqualTo(char[] expected) {
243 if (Arrays.equals(actual, expected)) return this;
244 failIfCustomMessageIsSet();
245 throw failure(unexpectedNotEqual(actual, expected));
246 }
247
248 /**
249 * Verifies that the actual <code>char</code> array is not equal to the given array. Array equality is checked by
250 * <code>{@link Arrays#equals(char[], char[])}</code>.
251 * @param array the given array to compare the actual array to.
252 * @return this assertion object.
253 * @throws AssertionError if the actual <code>char</code> array is equal to the given one.
254 */
255 public CharArrayAssert isNotEqualTo(char[] array) {
256 if (!Arrays.equals(actual, array)) return this;
257 failIfCustomMessageIsSet();
258 throw failure(unexpectedEqual(actual, array));
259 }
260
261 /**
262 * Verifies that the number of elements in the actual <code>char</code> array is equal to the given one.
263 * @param expected the expected number of elements in the actual <code>char</code> array.
264 * @return this assertion object.
265 * @throws AssertionError if the actual <code>char</code> array is <code>null</code>.
266 * @throws AssertionError if the number of elements in the actual <code>char</code> array is not equal to the given
267 * one.
268 */
269 public CharArrayAssert hasSize(int expected) {
270 assertThatActualHasSize(expected);
271 return this;
272 }
273
274 /**
275 * Verifies that the actual <code>char</code> array is the same as the given array.
276 * @param expected the given array to compare the actual array to.
277 * @return this assertion object.
278 * @throws AssertionError if the actual <code>char</code> array is not the same as the given one.
279 */
280 public CharArrayAssert isSameAs(char[] expected) {
281 assertSameAs(expected);
282 return this;
283 }
284
285 /**
286 * Verifies that the actual <code>char</code> array is not the same as the given array.
287 * @param expected the given array to compare the actual array to.
288 * @return this assertion object.
289 * @throws AssertionError if the actual <code>char</code> array is the same as the given one.
290 */
291 public CharArrayAssert isNotSameAs(char[] expected) {
292 assertNotSameAs(expected);
293 return this;
294 }
295
296 /** {@inheritDoc} */
297 public CharArrayAssert overridingErrorMessage(String message) {
298 replaceDefaultErrorMessagesWith(message);
299 return this;
300 }
301 }