001 /*
002 * Created on Dec 22, 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.api;
016
017 import java.util.Comparator;
018 import java.util.regex.Pattern;
019 import java.util.regex.PatternSyntaxException;
020
021 import org.fest.assertions.core.EnumerableAssert;
022 import org.fest.assertions.internal.Strings;
023 import org.fest.util.ComparatorBasedComparisonStrategy;
024 import org.fest.util.VisibleForTesting;
025
026 /**
027 * Assertion methods for {@code String}s.
028 * <p>
029 * To create a new instance of this class, invoke <code>{@link Assertions#assertThat(String)}</code>.
030 * </p>
031 *
032 * @author Yvonne Wang
033 * @author David DIDIER
034 * @author Alex Ruiz
035 * @author Joel Costigliola
036 * @author Mikhail Mazursky
037 */
038 public class StringAssert extends AbstractAssert<StringAssert, String> implements EnumerableAssert<StringAssert, String> {
039
040 @VisibleForTesting Strings strings = Strings.instance();
041
042 protected StringAssert(String actual) {
043 super(actual, StringAssert.class);
044 }
045
046 /** {@inheritDoc} */
047 public void isNullOrEmpty() {
048 strings.assertNullOrEmpty(info, actual);
049 }
050
051 /** {@inheritDoc} */
052 public void isEmpty() {
053 strings.assertEmpty(info, actual);
054 }
055
056 /** {@inheritDoc} */
057 public StringAssert isNotEmpty() {
058 strings.assertNotEmpty(info, actual);
059 return this;
060 }
061
062 /** {@inheritDoc} */
063 public StringAssert hasSize(int expected) {
064 strings.assertHasSize(info, actual, expected);
065 return this;
066 }
067
068 /**
069 * Verifies that the actual {@code String} is equal to the given one, ignoring case considerations.
070 * @param expected the given {@code String} to compare the actual {@code String} to.
071 * @return {@code this} assertion object.
072 * @throws AssertionError if the actual {@code String} is not equal to the given one.
073 */
074 public StringAssert isEqualToIgnoringCase(String expected) {
075 strings.assertEqualsIgnoringCase(info, actual, expected);
076 return this;
077 }
078
079 /**
080 * Verifies that the actual {@code String} contains the given sequence.
081 * @param sequence the sequence to search for.
082 * @return {@code this} assertion object.
083 * @throws NullPointerException if the given sequence is {@code null}.
084 * @throws AssertionError if the actual {@code String} is {@code null}.
085 * @throws AssertionError if the actual {@code String} does not contain the given one.
086 */
087 public StringAssert contains(String sequence) {
088 strings.assertContains(info, actual, sequence);
089 return this;
090 }
091
092 /**
093 * Verifies that the actual {@code String} contains the given sequence, ignoring case considerations.
094 * @param sequence the sequence to search for.
095 * @return {@code this} assertion object.
096 * @throws NullPointerException if the given sequence is {@code null}.
097 * @throws AssertionError if the actual {@code String} is {@code null}.
098 * @throws AssertionError if the actual {@code String} does not contain the given one.
099 */
100 public StringAssert containsIgnoringCase(String sequence) {
101 strings.assertContainsIgnoringCase(info, actual, sequence);
102 return this;
103 }
104
105 /**
106 * Verifies that the actual {@code String} does not contain the given sequence.
107 * @param sequence the sequence to search for.
108 * @return {@code this} assertion object.
109 * @throws NullPointerException if the given sequence is {@code null}.
110 * @throws AssertionError if the actual {@code String} is {@code null}.
111 * @throws AssertionError if the actual {@code String} contains the given one.
112 */
113 public StringAssert doesNotContain(String sequence) {
114 strings.assertDoesNotContain(info, actual, sequence);
115 return this;
116 }
117
118 /**
119 * Verifies that the actual {@code String} starts with the given prefix.
120 * @param prefix the given prefix.
121 * @return {@code this} assertion object.
122 * @throws NullPointerException if the given prefix is {@code null}.
123 * @throws AssertionError if the actual {@code String} is {@code null}.
124 * @throws AssertionError if the actual {@code String} does not start with the given prefix.
125 */
126 public StringAssert startsWith(String prefix) {
127 strings.assertStartsWith(info, actual, prefix);
128 return this;
129 }
130
131 /**
132 * Verifies that the actual {@code String} ends with the given suffix.
133 * @param suffix the given suffix.
134 * @return {@code this} assertion object.
135 * @throws NullPointerException if the given suffix is {@code null}.
136 * @throws AssertionError if the actual {@code String} is {@code null}.
137 * @throws AssertionError if the actual {@code String} does not end with the given suffix.
138 */
139 public StringAssert endsWith(String suffix) {
140 strings.assertEndsWith(info, actual, suffix);
141 return this;
142 }
143
144 /**
145 * Verifies that the actual {@code String} matches the given regular expression.
146 * @param regex the regular expression to which the actual {@code String} is to be matched.
147 * @return {@code this} assertion object.
148 * @throws NullPointerException if the given pattern is {@code null}.
149 * @throws PatternSyntaxException if the regular expression's syntax is invalid.
150 * @throws AssertionError if the actual {@code String} is {@code null}.
151 * @throws AssertionError if the actual {@code String} does not match the given regular expression.
152 */
153 public StringAssert matches(String regex) {
154 strings.assertMatches(info, actual, regex);
155 return this;
156 }
157
158 /**
159 * Verifies that the actual {@code String} does not match the given regular expression.
160 * @param regex the regular expression to which the actual {@code String} is to be matched.
161 * @return {@code this} assertion object.
162 * @throws NullPointerException if the given pattern is {@code null}.
163 * @throws PatternSyntaxException if the regular expression's syntax is invalid.
164 * @throws AssertionError if the actual {@code String} is {@code null}.
165 * @throws AssertionError if the actual {@code String} matches the given regular expression.
166 */
167 public StringAssert doesNotMatch(String regex) {
168 strings.assertDoesNotMatch(info, actual, regex);
169 return this;
170 }
171
172 /**
173 * Verifies that the actual {@code String} matches the given regular expression.
174 * @param pattern the regular expression to which the actual {@code String} is to be matched.
175 * @return {@code this} assertion object.
176 * @throws NullPointerException if the given pattern is {@code null}.
177 * @throws AssertionError if the actual {@code String} is {@code null}.
178 * @throws AssertionError if the actual {@code String} does not match the given regular expression.
179 */
180 public StringAssert matches(Pattern pattern) {
181 strings.assertMatches(info, actual, pattern);
182 return this;
183 }
184
185 /**
186 * Verifies that the actual {@code String} does not match the given regular expression.
187 * @param pattern the regular expression to which the actual {@code String} is to be matched.
188 * @return {@code this} assertion object.
189 * @throws NullPointerException if the given pattern is {@code null}.
190 * @throws AssertionError if the actual {@code String} does not match the given regular expression.
191 */
192 public StringAssert doesNotMatch(Pattern pattern) {
193 strings.assertDoesNotMatch(info, actual, pattern);
194 return this;
195 }
196
197 /** {@inheritDoc} */
198 public StringAssert usingElementComparator(Comparator<? super String> customComparator) {
199 // TODO maybe use Comparator<? super Character>
200 throw new UnsupportedOperationException("custom element Comparator is not supported for String comparison");
201 }
202
203 /** {@inheritDoc} */
204 public StringAssert usingDefaultElementComparator() {
205 throw new UnsupportedOperationException("custom element Comparator is not supported for String comparison");
206 }
207
208 @Override
209 public StringAssert usingComparator(Comparator<? super String> customComparator) {
210 super.usingComparator(customComparator);
211 this.strings = new Strings(new ComparatorBasedComparisonStrategy(customComparator));
212 return myself;
213 }
214
215 @Override
216 public StringAssert usingDefaultComparator() {
217 super.usingDefaultComparator();
218 this.strings = Strings.instance();
219 return myself;
220 }
221 }