001 /*
002 * Created on Dec 26, 2006
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 @2006-2011 the original author or authors.
014 */
015 package org.fest.assertions;
016
017 import static org.fest.assertions.Formatting.format;
018
019 /**
020 * Assertions for {@code String}s.
021 * <p>
022 * To create a new instance of this class invoke <code>{@link Assertions#assertThat(String)}</code>.
023 * </p>
024 *
025 * @author Yvonne Wang
026 * @author David DIDIER
027 */
028 public class StringAssert extends GroupAssert<StringAssert, String> {
029
030 /**
031 * Creates a new </code>{@link StringAssert}</code>.
032 * @param actual the target to verify.
033 */
034 protected StringAssert(String actual) {
035 super(StringAssert.class, actual);
036 }
037
038 /**
039 * Verifies that the actual {@code String} is equal to the given one ignoring case.
040 * @param expected the given {@code String} to compare the actual {@code String} to.
041 * @return this assertion object.
042 * @throws AssertionError if the actual {@code String} is {@code null}.
043 * @throws AssertionError if the actual {@code String} is not equal to the given one ignoring case.
044 */
045 public StringAssert isEqualToIgnoringCase(String expected) {
046 if (actual == null && expected == null) return this;
047 isNotNull();
048 if (actual.equalsIgnoreCase(expected)) return this;
049 failIfCustomMessageIsSet();
050 throw failure(format("<%s> should be equal to :<%s> ignoring case", actual, expected));
051 }
052
053 /**
054 * Returns the number of elements in the actual {@code String}.
055 * @return the number of elements in the actual {@code String}.
056 */
057 @Override protected int actualGroupSize() {
058 isNotNull();
059 return actual.length();
060 }
061
062 /**
063 * Verifies that the actual {@code String} contains the given one.
064 * @param expected the given {@code String} expected to be contained in the actual one.
065 * @return this assertion object.
066 * @throws AssertionError if the actual {@code String} is {@code null}.
067 * @throws AssertionError if the actual {@code String} does not contain the given one.
068 */
069 public StringAssert contains(String expected) {
070 isNotNull();
071 if (actual.indexOf(expected) != -1) return this;
072 failIfCustomMessageIsSet();
073 throw failure(format("<%s> should contain the String:<%s>", actual, expected));
074 }
075
076 /**
077 * Verifies that the actual {@code String} ends with the given one.
078 * @param expected the given {@code String} expected to be at the end of the actual one.
079 * @return this assertion object.
080 * @throws AssertionError if the actual {@code String} is {@code null}.
081 * @throws AssertionError if the actual {@code String} does not end with the given one.
082 */
083 public StringAssert endsWith(String expected) {
084 isNotNull();
085 if (actual.endsWith(expected)) return this;
086 failIfCustomMessageIsSet();
087 throw failure(format("<%s> should end with:<%s>", actual, expected));
088 }
089
090 /**
091 * Verifies that the actual {@code String} starts with the given one.
092 * @param expected the given {@code String} expected to be at the beginning of the actual one.
093 * @return this assertion object.
094 * @throws AssertionError if the actual {@code String} is {@code null}.
095 * @throws AssertionError if the actual {@code String} does not start with the given one.
096 */
097 public StringAssert startsWith(String expected) {
098 isNotNull();
099 if (actual.startsWith(expected)) return this;
100 failIfCustomMessageIsSet();
101 throw failure(format("<%s> should start with:<%s>", actual, expected));
102 }
103
104 /**
105 * Verifies that the actual {@code String} does not contains the given one.
106 * @param s the given {@code String} expected not to be contained in the actual one.
107 * @return this assertion object.
108 * @throws AssertionError if the actual {@code String} is {@code null}.
109 * @throws AssertionError if the actual {@code String} does contain the given one.
110 */
111 public StringAssert excludes(String s) {
112 isNotNull();
113 if (actual.indexOf(s) == -1) return this;
114 failIfCustomMessageIsSet();
115 throw failure(format("<%s> should not contain the String:<%s>", actual, s));
116 }
117
118 /**
119 * Verifies that the actual {@code String} matches the given one.
120 * @param regex the given regular expression expected to be matched by the actual one.
121 * @return this assertion object.
122 * @throws AssertionError if the actual {@code String} is {@code null}.
123 * @throws AssertionError if the actual {@code String} does not match the given regular expression.
124 */
125 public StringAssert matches(String regex) {
126 isNotNull();
127 if (actual.matches(regex)) return this;
128 failIfCustomMessageIsSet();
129 throw failure(format("<%s> should match the regular expression:<%s>", actual, regex));
130 }
131
132 /**
133 * Verifies that the actual {@code String} does not match the given one.
134 * @param regex the given regular expression expected not to be matched by the actual one.
135 * @return this assertion object.
136 * @throws AssertionError if the actual {@code String} is {@code null}.
137 * @throws AssertionError if the actual {@code String} matches the given regular expression.
138 */
139 public StringAssert doesNotMatch(String regex) {
140 isNotNull();
141 if (!actual.matches(regex)) return this;
142 failIfCustomMessageIsSet();
143 throw failure(format("<%s> should not match the regular expression:<%s>", actual, regex));
144 }
145
146 /**
147 * Verifies that the actual {@code String} contains the given text regardless of the case.
148 * @param text the given text.
149 * @return this assertion object.
150 * @throws AssertionError if the actual {@code String} is {@code null}.
151 * @throws AssertionError if the actual {@code String} does not contain the given text.
152 * @throws NullPointerException if the given {@code String} is {@code null}.
153 * @since 1.3
154 */
155 public StringAssert containsIgnoringCase(String text) {
156 validateNotNull(text);
157 isNotNull();
158 if (actual.toLowerCase().contains(text.toLowerCase())) return this;
159 failIfCustomMessageIsSet();
160 throw failure(format("<%s> does not contain <%s>", actual, text));
161 }
162
163 /**
164 * Verifies that the actual {@code String} does not contain the given text.
165 * @param text the given text.
166 * @return this assertion object.
167 * @throws AssertionError if the actual {@code String} is {@code null}.
168 * @throws AssertionError if the actual {@code String} contains the given text.
169 * @throws NullPointerException if the given {@code String} is {@code null}.
170 * @since 1.3
171 */
172 public StringAssert doesNotContain(String text) {
173 validateNotNull(text);
174 isNotNull();
175 if (!actual.contains(text)) return this;
176 failIfCustomMessageIsSet();
177 throw failure(format("<%s> should not contain <%s>", actual, text));
178 }
179
180 private static void validateNotNull(String text) {
181 if (text == null) throw new NullPointerException("The given String should not be null");
182 }
183 }