001    /*
002     * Created on Jun 18, 2007
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 @2007-2011 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static java.lang.Character.valueOf;
018    import static org.fest.assertions.ErrorMessages.*;
019    
020    /**
021     * Assertions for {@code Character}s and {@code char}s.
022     * <p>
023     * To create a new instance of this class invoke either <code>{@link Assertions#assertThat(Character)}</code> or
024     * <code>{@link Assertions#assertThat(char)}</code>.
025     * </p>
026     *
027     * @author Yvonne Wang
028     * @author David DIDIER
029     * @author Ansgar Konermann
030     * @author Alex Ruiz
031     */
032    public class CharAssert extends GenericAssert<CharAssert, Character> {
033    
034      /**
035       * Creates a new <code>{@link CharAssert}</code>.
036       * @param actual the actual value to verify.
037       */
038      protected CharAssert(char actual) {
039        super(CharAssert.class, actual);
040      }
041    
042      /**
043       * Creates a new <code>{@link CharAssert}</code>.
044       * @param actual the actual value to verify.
045       */
046      protected CharAssert(Character actual) {
047        super(CharAssert.class, actual);
048      }
049    
050      /**
051       * Verifies that the {@code Character} value is equal to the given one.
052       * @param expected the value to compare the actual one to.
053       * @return this assertion object.
054       * @throws AssertionError if the {@code Character} value is not equal to the given one.
055       */
056      public CharAssert isEqualTo(char expected) {
057        return isEqualTo(valueOf(expected));
058      }
059    
060      /**
061       * Verifies that the {@code Character} value is not equal to the given one.
062       * @param other the given value.
063       * @return this assertion object.
064       * @throws AssertionError if the {@code Character} value is equal to the given one.
065       */
066      public CharAssert isNotEqualTo(char other) {
067        return isNotEqualTo(valueOf(other));
068      }
069    
070      /**
071       * Verifies that the {@code Character} value is greater than the given one.
072       * @param other the given value.
073       * @return this assertion object.
074       * @throws AssertionError if the {@code Character} value is not greater than the given one.
075       */
076      public CharAssert isGreaterThan(char other) {
077        if (actual > other) return this;
078        failIfCustomMessageIsSet();
079        throw failure(unexpectedLessThanOrEqualTo(actual, other));
080      }
081    
082      /**
083       * Verifies that the {@code Character} value is less than the given one.
084       * @param other the given value.
085       * @return this assertion object.
086       * @throws AssertionError if the {@code Character} value is not less than the given one.
087       */
088      public CharAssert isLessThan(char other) {
089        if (actual < other) return this;
090        failIfCustomMessageIsSet();
091        throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
092      }
093    
094      /**
095       * Verifies that the {@code Character} value is greater or equal to the given one.
096       * @param other the given value.
097       * @return this assertion object.
098       * @throws AssertionError if the {@code Character} value is not greater than or equal to the given one.
099       */
100      public CharAssert isGreaterThanOrEqualTo(char other) {
101        if (actual >= other) return this;
102        failIfCustomMessageIsSet();
103        throw failure(unexpectedLessThan(actual, other));
104      }
105    
106      /**
107       * Verifies that the {@code Character} value is less or equal to the given one.
108       * @param other the given value.
109       * @return this assertion object.
110       * @throws AssertionError if the {@code Character} value is not less than or equal to the given one.
111       */
112      public CharAssert isLessThanOrEqualTo(char other) {
113        if (actual <= other) return this;
114        failIfCustomMessageIsSet();
115        throw failure(unexpectedGreaterThan(actual, other));
116      }
117    
118      /**
119       * Verifies that the {@code Character} value is an upper-case value.
120       * @return this assertion object.
121       * @throws AssertionError if the {@code Character} value is not an upper-case value.
122       */
123      public CharAssert isUpperCase() {
124        if (Character.isUpperCase(actual)) return this;
125        failIfCustomMessageIsSet();
126        throw failure(String.format("<%s> should be an upper-case character", actual));
127      }
128    
129      /**
130       * Verifies that the {@code Character} value is an lower-case value.
131       * @return this assertion object.
132       * @throws AssertionError if the {@code Character} value is not an lower-case value.
133       */
134      public CharAssert isLowerCase() {
135        if (Character.isLowerCase(actual)) return this;
136        failIfCustomMessageIsSet();
137        throw failure(String.format("<%s> should be a lower-case character", actual));
138      }
139    }