KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > StringAssert


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.framework;
56
57 /**
58  * A set of assert methods specially targetted at string objects.
59  *
60  * @version $Revision: 1.8 $ $Date: 2003/04/27 02:13:47 $
61  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
62  */

63 public class StringAssert {
64
65     /**
66      * Don't let anyone have access to this constructor.
67      */

68     private StringAssert() {
69     }
70
71     /**
72      * Asserts that a string contains a substring. Throws an
73      * <tt>AssertionFailedError</tt> if it doesn't contain the substring.
74      */

75     public static void assertContains(String JavaDoc substring,
76                                       String JavaDoc actual) {
77         assertContains(null, substring, actual);
78     }
79
80     /**
81      * Asserts that a string contains a substring. Throws an
82      * <tt>AssertionFailedError</tt> if it doesn't contain the substring.
83      */

84     public static void assertContains(String JavaDoc message,
85                                       String JavaDoc substring,
86                                       String JavaDoc actual) {
87         Assert.assertNotNull(substring);
88         if (actual != null && actual.indexOf(substring) >= 0) {
89             return;
90         }
91         failContains(message, substring, actual);
92     }
93
94     /**
95      * Asserts that a string does not contain a substring. Throws an
96      * <tt>AssertionFailedError</tt> if it contains the substring.
97      */

98     public static void assertNotContains(String JavaDoc substring,
99                                          String JavaDoc actual) {
100         assertNotContains(null, substring, actual);
101     }
102
103     /**
104      * Asserts that a string does not contain a substring. Throws an
105      * <tt>AssertionFailedError</tt> if it contains the substring.
106      */

107     public static void assertNotContains(String JavaDoc message,
108                                          String JavaDoc substring,
109                                          String JavaDoc actual) {
110         if ((actual == null && substring != null) ||
111                 (actual != null && substring == null) ||
112                 (actual != null && actual.indexOf(substring) < 0)) {
113             return;
114         }
115         failNotContains(message, substring, actual);
116     }
117
118     /**
119      * Asserts that a string starts with a substring. Throws an
120      * <tt>AssertionFailedError</tt> if it contains the substring.
121      */

122     public static void assertStartsWith(String JavaDoc substring, String JavaDoc actual) {
123         assertStartsWith(null, substring, actual);
124     }
125     
126     /**
127      * Asserts that a string starts with a substring. Throws an
128      * <tt>AssertionFailedError</tt> if it contains the substring.
129      */

130     public static void assertStartsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
131         Assert.assertNotNull(substring);
132         if (actual != null && actual.startsWith(substring)) {
133             return;
134         }
135         failStartsWith(message, substring, actual);
136     }
137     
138     /**
139      * Asserts that a string doesn't start with a substring. Throws an
140      * <tt>AssertionFailedError</tt> if it contains the substring.
141      */

142     public static void assertNotStartsWith(String JavaDoc substring, String JavaDoc actual) {
143         assertNotStartsWith(null, substring, actual);
144     }
145     
146     /**
147      * Asserts that a string doesn't start with a substring. Throws an
148      * <tt>AssertionFailedError</tt> if it contains the substring.
149      */

150     public static void assertNotStartsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
151         if ((actual == null && substring != null) ||
152                 (actual != null && substring == null) ||
153                 (actual != null && !actual.startsWith(substring))) {
154             return;
155         }
156         failNotStartsWith(message, substring);
157     }
158     
159     /**
160      * Asserts that a string ends with a substring. Throws an
161      * <tt>AssertionFailedError</tt> if it contains the substring.
162      */

163     public static void assertEndsWith(String JavaDoc substring, String JavaDoc actual) {
164         assertEndsWith(null, substring, actual);
165     }
166     
167     /**
168      * Asserts that a string ends with a substring. Throws an
169      * <tt>AssertionFailedError</tt> if it contains the substring.
170      */

171     public static void assertEndsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
172         Assert.assertNotNull(substring);
173         if (actual != null && actual.endsWith(substring)) {
174             return;
175         }
176         failEndsWith(message, substring, actual);
177     }
178     
179     /**
180      * Asserts that a string doesn't end with a substring. Throws an
181      * <tt>AssertionFailedError</tt> if it contains the substring.
182      */

183     public static void assertNotEndsWith(String JavaDoc substring, String JavaDoc actual) {
184         assertNotEndsWith(null, substring, actual);
185     }
186     
187     /**
188      * Asserts that a string doesn't end with a substring. Throws an
189      * <tt>AssertionFailedError</tt> if it contains the substring.
190      */

191     public static void assertNotEndsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
192         if ((actual == null && substring != null) ||
193                 (actual != null && substring == null) ||
194                 (actual != null && !actual.endsWith(substring))) {
195             return;
196         }
197         failNotEndsWith(message, substring);
198     }
199     
200     private static void failContains(String JavaDoc message,
201                                      String JavaDoc substring,
202                                      String JavaDoc actual) {
203         String JavaDoc formatted = "";
204         if (message != null) {
205             formatted = message + " ";
206         }
207
208         Assert.fail(formatted + "expected containing: <" + substring + "> but was: <" + actual + ">");
209     }
210
211     private static void failNotContains(String JavaDoc message,
212                                         String JavaDoc substring,
213                                         String JavaDoc actual) {
214         String JavaDoc formatted = "";
215         if (message != null) {
216             formatted = message + " ";
217         }
218
219         Assert.fail(formatted + "expected not containing: <" + substring + "> but was: <" + actual + ">");
220     }
221
222     private static void failStartsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
223         String JavaDoc formatted = "";
224         if (message != null) {
225             formatted = message + " ";
226         }
227
228         Assert.fail(formatted + "expected starting with: <" + substring + "> but was: <" + actual + ">");
229     }
230
231     private static void failNotStartsWith(String JavaDoc message, String JavaDoc substring) {
232         String JavaDoc formatted = "";
233         if (message != null) {
234             formatted = message + " ";
235         }
236
237         Assert.fail(formatted + "expected not starting with: <" + substring + ">");
238     }
239
240     private static void failEndsWith(String JavaDoc message, String JavaDoc substring, String JavaDoc actual) {
241         String JavaDoc formatted = "";
242         if (message != null) {
243             formatted = message + " ";
244         }
245
246         Assert.fail(formatted + "expected ending with: <" + substring + "> but was: <" + actual + ">");
247     }
248     
249     private static void failNotEndsWith(String JavaDoc message, String JavaDoc substring) {
250         String JavaDoc formatted = "";
251         if (message != null) {
252             formatted = message + " ";
253         }
254
255         Assert.fail(formatted + "expected not ending with: <" + substring + ">");
256     }
257     
258 }
259
Popular Tags