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-2011 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.*;
020
021 import java.util.Arrays;
022
023 /**
024 * Assertions for {@code char} arrays.
025 * <p>
026 * To create a new instance of this class invoke <code>{@link Assertions#assertThat(char[])}</code>.
027 * </p>
028 *
029 * @author Yvonne Wang
030 * @author Alex Ruiz
031 */
032 public class CharArrayAssert extends ArrayAssert<CharArrayAssert, char[]> {
033
034 /**
035 * Creates a new </code>{@link CharArrayAssert}</code>.
036 * @param actual the target to verify.
037 */
038 protected CharArrayAssert(char... actual) {
039 super(CharArrayAssert.class, actual);
040 }
041
042 /**
043 * Verifies that the actual {@code char} array contains the given values.
044 * @param values the values to look for.
045 * @return this assertion object.
046 * @throws AssertionError if the actual {@code char} array is {@code null}.
047 * @throws NullPointerException if the given {@code char} array is {@code null}.
048 * @throws AssertionError if the actual {@code char} array does not contain the given values.
049 */
050 public CharArrayAssert contains(char...values) {
051 assertContains(copy(values));
052 return this;
053 }
054
055 /**
056 * Verifies that the actual {@code char} array contains the given values <strong>only</strong>.
057 * @param values the values to look for.
058 * @return this assertion object.
059 * @throws AssertionError if the actual {@code char} array is {@code null}.
060 * @throws NullPointerException if the given {@code char} array is {@code null}.
061 * @throws AssertionError if the actual {@code char} array does not contain the given objects, or if the actual
062 * {@code char} array contains elements other than the ones specified.
063 */
064 public CharArrayAssert containsOnly(char...values) {
065 assertContainsOnly(copy(values));
066 return this;
067 }
068
069 /**
070 * Verifies that the actual {@code char} array does not contain the given values.
071 * @param values the values the array should exclude.
072 * @return this assertion object.
073 * @throws AssertionError if the actual {@code char} array is {@code null}.
074 * @throws NullPointerException if the given {@code char} array is {@code null}.
075 * @throws AssertionError if the actual {@code char} array contains any of the given values.
076 */
077 public CharArrayAssert excludes(char...values) {
078 assertExcludes(copy(values));
079 return this;
080 }
081
082 /**
083 * Verifies that the actual {@code char} array is equal to the given array. Array equality is checked by
084 * <code>{@link Arrays#equals(char[], char[])}</code>.
085 * @param expected the given array to compare the actual array to.
086 * @return this assertion object.
087 * @throws AssertionError if the actual {@code char} array is not equal to the given one.
088 */
089 @Override public CharArrayAssert isEqualTo(char[] expected) {
090 if (Arrays.equals(actual, expected)) return this;
091 failIfCustomMessageIsSet();
092 throw failure(unexpectedNotEqual(actual, expected));
093 }
094
095 /**
096 * Verifies that the actual {@code char} array is not equal to the given array. Array equality is checked by
097 * <code>{@link Arrays#equals(char[], char[])}</code>.
098 * @param array the given array to compare the actual array to.
099 * @return this assertion object.
100 * @throws AssertionError if the actual {@code char} array is equal to the given one.
101 */
102 @Override public CharArrayAssert isNotEqualTo(char[] array) {
103 if (!Arrays.equals(actual, array)) return this;
104 failIfCustomMessageIsSet();
105 throw failure(unexpectedEqual(actual, array));
106 }
107 }