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     */
037    public class StringAssert extends AbstractAssert<StringAssert, String> implements EnumerableAssert<StringAssert> {
038    
039      @VisibleForTesting Strings strings = Strings.instance();
040    
041      protected StringAssert(String actual) {
042        super(actual, StringAssert.class);
043      }
044    
045      /** {@inheritDoc} */
046      public void isNullOrEmpty() {
047        strings.assertNullOrEmpty(info, actual);
048      }
049    
050      /** {@inheritDoc} */
051      public void isEmpty() {
052        strings.assertEmpty(info, actual);
053      }
054    
055      /** {@inheritDoc} */
056      public StringAssert isNotEmpty() {
057        strings.assertNotEmpty(info, actual);
058        return this;
059      }
060    
061      /** {@inheritDoc} */
062      public StringAssert hasSize(int expected) {
063        strings.assertHasSize(info, actual, expected);
064        return this;
065      }
066    
067      /**
068       * Verifies that the actual {@code String} is equal to the given one, ignoring case considerations.
069       * @param expected the given {@code String} to compare the actual {@code String} to.
070       * @return {@code this} assertion object.
071       * @throws AssertionError if the actual {@code String} is not equal to the given one.
072       */
073      public StringAssert isEqualToIgnoringCase(String expected) {
074        strings.assertEqualsIgnoringCase(info, actual, expected);
075        return this;
076      }
077    
078      /**
079       * Verifies that the actual {@code String} contains the given sequence.
080       * @param sequence the sequence to search for.
081       * @return {@code this} assertion object.
082       * @throws NullPointerException if the given sequence is {@code null}.
083       * @throws AssertionError if the actual {@code String} is {@code null}.
084       * @throws AssertionError if the actual {@code String} does not contain the given one.
085       */
086      public StringAssert contains(String sequence) {
087        strings.assertContains(info, actual, sequence);
088        return this;
089      }
090    
091      /**
092       * Verifies that the actual {@code String} contains the given sequence, ignoring case considerations.
093       * @param sequence the sequence to search for.
094       * @return {@code this} assertion object.
095       * @throws NullPointerException if the given sequence is {@code null}.
096       * @throws AssertionError if the actual {@code String} is {@code null}.
097       * @throws AssertionError if the actual {@code String} does not contain the given one.
098       */
099      public StringAssert containsIgnoringCase(String sequence) {
100        strings.assertContainsIgnoringCase(info, actual, sequence);
101        return this;
102      }
103    
104      /**
105       * Verifies that the actual {@code String} does not contain the given sequence.
106       * @param sequence the sequence to search for.
107       * @return {@code this} assertion object.
108       * @throws NullPointerException if the given sequence is {@code null}.
109       * @throws AssertionError if the actual {@code String} is {@code null}.
110       * @throws AssertionError if the actual {@code String} contains the given one.
111       */
112      public StringAssert doesNotContain(String sequence) {
113        strings.assertDoesNotContain(info, actual, sequence);
114        return this;
115      }
116    
117      /**
118       * Verifies that the actual {@code String} starts with the given prefix.
119       * @param prefix the given prefix.
120       * @return {@code this} assertion object.
121       * @throws NullPointerException if the given prefix is {@code null}.
122       * @throws AssertionError if the actual {@code String} is {@code null}.
123       * @throws AssertionError if the actual {@code String} does not start with the given prefix.
124       */
125      public StringAssert startsWith(String prefix) {
126        strings.assertStartsWith(info, actual, prefix);
127        return this;
128      }
129    
130      /**
131       * Verifies that the actual {@code String} ends with the given suffix.
132       * @param suffix the given suffix.
133       * @return {@code this} assertion object.
134       * @throws NullPointerException if the given suffix is {@code null}.
135       * @throws AssertionError if the actual {@code String} is {@code null}.
136       * @throws AssertionError if the actual {@code String} does not end with the given suffix.
137       */
138      public StringAssert endsWith(String suffix) {
139        strings.assertEndsWith(info, actual, suffix);
140        return this;
141      }
142    
143      /**
144       * Verifies that the actual {@code String} matches the given regular expression.
145       * @param regex the regular expression to which the actual {@code String} is to be matched.
146       * @return {@code this} assertion object.
147       * @throws NullPointerException if the given pattern is {@code null}.
148       * @throws PatternSyntaxException if the regular expression's syntax is invalid.
149       * @throws AssertionError if the actual {@code String} is {@code null}.
150       * @throws AssertionError if the actual {@code String} does not match the given regular expression.
151       */
152      public StringAssert matches(String regex) {
153        strings.assertMatches(info, actual, regex);
154        return this;
155      }
156    
157      /**
158       * Verifies that the actual {@code String} does not match the given regular expression.
159       * @param regex the regular expression to which the actual {@code String} is to be matched.
160       * @return {@code this} assertion object.
161       * @throws NullPointerException if the given pattern is {@code null}.
162       * @throws PatternSyntaxException if the regular expression's syntax is invalid.
163       * @throws AssertionError if the actual {@code String} is {@code null}.
164       * @throws AssertionError if the actual {@code String} matches the given regular expression.
165       */
166      public StringAssert doesNotMatch(String regex) {
167        strings.assertDoesNotMatch(info, actual, regex);
168        return this;
169      }
170    
171      /**
172       * Verifies that the actual {@code String} matches the given regular expression.
173       * @param pattern the regular expression to which the actual {@code String} is to be matched.
174       * @return {@code this} assertion object.
175       * @throws NullPointerException if the given pattern is {@code null}.
176       * @throws AssertionError if the actual {@code String} is {@code null}.
177       * @throws AssertionError if the actual {@code String} does not match the given regular expression.
178       */
179      public StringAssert matches(Pattern pattern) {
180        strings.assertMatches(info, actual, pattern);
181        return this;
182      }
183    
184      /**
185       * Verifies that the actual {@code String} does not match the given regular expression.
186       * @param pattern the regular expression to which the actual {@code String} is to be matched.
187       * @return {@code this} assertion object.
188       * @throws NullPointerException if the given pattern is {@code null}.
189       * @throws AssertionError if the actual {@code String} does not match the given regular expression.
190       */
191      public StringAssert doesNotMatch(Pattern pattern) {
192        strings.assertDoesNotMatch(info, actual, pattern);
193        return this;
194      }
195    
196      @Override
197      public StringAssert usingComparator(Comparator<?> customComparator) {
198        super.usingComparator(customComparator);
199        this.strings = new Strings(new ComparatorBasedComparisonStrategy(customComparator));
200        return myself;
201      }
202      
203      @Override
204      public StringAssert usingDefaultComparator() {
205        super.usingDefaultComparator();
206        this.strings = Strings.instance();
207        return myself;
208      }
209    }