KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > Assert


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 (primarly testing the negative assertion of the
59  * correspondent methods found in the <tt>junit.framework.Assert</tt> class).
60  *
61  * <h4>Usage</h4>
62  *
63  * <pre>
64  * import junitx.framework.Assert;
65  *
66  * Assert.assertNotEquals(expected, actual);
67  * </pre>
68  *
69  * @version $Revision: 1.13 $ $Date: 2003/03/21 06:13:49 $
70  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
71  */

72 public class Assert
73         extends junit.framework.Assert {
74
75     /**
76      * Don't let anyone have access to this constructor.
77      */

78     private Assert() {
79     }
80
81     /**
82      * Asserts that a condition is false. Throws a
83      * <tt>junitx.framework.ComparisonFailure</tt> if not.
84      *
85      * @see junitx.framework.ComparisonFailure
86      */

87     public static void assertFalse(String JavaDoc message,
88                                    boolean condition) {
89         if (condition) {
90             failFalse(message);
91         }
92     }
93
94     /**
95      * Asserts that a condition is false. Throws a
96      * <tt>junitx.framework.ComparisonFailure</tt> if not.
97      *
98      * @see junitx.framework.ComparisonFailure
99      */

100     public static void assertFalse(boolean condition) {
101         assertFalse(null, condition);
102     }
103
104     /**
105      * Asserts that two strings are equal. Throws an
106      * <tt>AssertionFailedError</tt> if not.
107      */

108     public static void assertEquals(String JavaDoc expected, String JavaDoc actual) {
109         assertEquals(null, expected, actual);
110     }
111
112     /**
113      * Asserts that two strings are equal. Throws an
114      * <tt>AssertionFailedError</tt> if not.
115      */

116     public static void assertEquals(String JavaDoc message, String JavaDoc expected, String JavaDoc actual) {
117         if ((expected == null && actual == null) || (expected != null && expected.equals(actual))) {
118             return;
119         }
120         throw new ComparisonFailure(message, expected, actual);
121     }
122
123     /**
124      * Asserts that two objects are not equal. Throws an
125      * <tt>AssertionFailedError</tt> if they are equal.
126      */

127     public static void assertNotEquals(String JavaDoc message,
128                                        Object JavaDoc expected,
129                                        Object JavaDoc actual) {
130         if ((expected == null && actual == null) ||
131                 (expected != null && expected.equals(actual))) {
132             failNotEquals(message, expected);
133         }
134     }
135
136     /**
137      * Asserts that two objects are not equal. Throws an
138      * <tt>AssertionFailedError</tt> if they are equal.
139      */

140     public static void assertNotEquals(Object JavaDoc expected,
141                                        Object JavaDoc actual) {
142         assertNotEquals(null, expected, actual);
143     }
144
145     /**
146      * Asserts that two bytes are not equal. Throws an
147      * <tt>AssertionFailedError</tt> if they are equal.
148      */

149     public static void assertNotEquals(String JavaDoc message,
150                                        byte expected,
151                                        byte actual) {
152         assertNotEquals(message, new Byte JavaDoc(expected), new Byte JavaDoc(actual));
153     }
154
155     /**
156      * Asserts that two bytes are not equal. Throws an
157      * <tt>AssertionFailedError</tt> if they are equal.
158      */

159     public static void assertNotEquals(byte expected,
160                                        byte actual) {
161         assertNotEquals(null, expected, actual);
162     }
163
164     /**
165      * Asserts that two chars are not equal. Throws an
166      * <tt>AssertionFailedError</tt> if they are equal.
167      */

168     public static void assertNotEquals(String JavaDoc message,
169                                        char expected,
170                                        char actual) {
171         assertNotEquals(message, new Character JavaDoc(expected), new Character JavaDoc(actual));
172     }
173
174     /**
175      * Asserts that two chars are not equal. Throws an
176      * <tt>AssertionFailedError</tt> if they are equal.
177      */

178     public static void assertNotEquals(char expected,
179                                        char actual) {
180         assertNotEquals(null, expected, actual);
181     }
182
183     /**
184      * Asserts that two doubles are not equal concerning a delta. If the
185      * expected value is infinity then the delta value is ignored. Throws an
186      * <tt>AssertionFailedError</tt> if they are equal.
187      */

188     public static void assertNotEquals(String JavaDoc message,
189                                        double expected,
190                                        double actual,
191                                        double delta) {
192         if (Double.isInfinite(expected)) {
193             if (expected == actual) {
194                 failNotEquals(message, new Double JavaDoc(expected));
195             }
196         } else if (Math.abs(expected - actual) <= delta) {
197             failNotEquals(message, new Double JavaDoc(expected));
198         }
199     }
200
201     /**
202      * Asserts that two doubles are not equal concerning a delta. If the
203      * expected value is infinity then the delta value is ignored. Throws an
204      * <tt>AssertionFailedError</tt> if they are equal.
205      */

206     public static void assertNotEquals(double expected,
207                                        double actual,
208                                        double delta) {
209         assertNotEquals(null, expected, actual, delta);
210     }
211
212     /**
213      * Asserts that two floats are not equal concerning a delta. If the
214      * expected value is infinity then the delta value is ignored. Throws an
215      * <tt>AssertionFailedError</tt> if they are equal.
216      */

217     public static void assertNotEquals(String JavaDoc message,
218                                        float expected,
219                                        float actual,
220                                        float delta) {
221         if (Float.isInfinite(expected)) {
222             if (expected == actual) {
223                 failNotEquals(message, new Float JavaDoc(expected));
224             }
225         } else if (Math.abs(expected - actual) <= delta) {
226             failNotEquals(message, new Float JavaDoc(expected));
227         }
228     }
229
230     /**
231      * Asserts that two floats are not equal concerning a delta. If the
232      * expected value is infinity then the delta value is ignored. Throws an
233      * <tt>AssertionFailedError</tt> if they are equal.
234      */

235     public static void assertNotEquals(float expected,
236                                        float actual,
237                                        float delta) {
238         assertNotEquals(null, expected, actual, delta);
239     }
240
241     /**
242      * Asserts that two ints are not equal. Throws an
243      * <tt>AssertionFailedError</tt> if they are equal.
244      */

245     public static void assertNotEquals(String JavaDoc message,
246                                        int expected,
247                                        int actual) {
248         assertNotEquals(message, new Integer JavaDoc(expected), new Integer JavaDoc(actual));
249     }
250
251     /**
252      * Asserts that two ints are not equal. Throws an
253      * <tt>AssertionFailedError</tt> if they are equal.
254      */

255     public static void assertNotEquals(int expected,
256                                        int actual) {
257         assertNotEquals(null, expected, actual);
258     }
259
260     /**
261      * Asserts that longs objects are not equal. Throws an
262      * <tt>AssertionFailedError</tt> if they are equal.
263      */

264     public static void assertNotEquals(String JavaDoc message,
265                                        long expected,
266                                        long actual) {
267         assertNotEquals(message, new Long JavaDoc(expected), new Long JavaDoc(actual));
268     }
269
270     /**
271      * Asserts that two longs are not equal. Throws an
272      * <tt>AssertionFailedError</tt> if they are equal.
273      */

274     public static void assertNotEquals(long expected,
275                                        long actual) {
276         assertNotEquals(null, expected, actual);
277     }
278
279     /**
280      * Asserts that two shorts are not equal. Throws an
281      * <tt>AssertionFailedError</tt> if they are equal.
282      */

283     public static void assertNotEquals(String JavaDoc message,
284                                        short expected,
285                                        short actual) {
286         assertNotEquals(message, new Short JavaDoc(expected), new Short JavaDoc(actual));
287     }
288
289     /**
290      * Asserts that two shorts are not equal. Throws an
291      * <tt>AssertionFailedError</tt> if they are equal.
292      */

293     public static void assertNotEquals(short expected,
294                                        short actual) {
295         assertNotEquals(null, expected, actual);
296     }
297
298     /**
299      * Asserts that two boolean are not equal. Throws an
300      * <tt>AssertionFailedError</tt> if they are equal.
301      */

302     public static void assertNotEquals(String JavaDoc message,
303                                        boolean expected,
304                                        boolean actual) {
305         assertNotEquals(message, new Boolean JavaDoc(expected), new Boolean JavaDoc(actual));
306     }
307
308     /**
309      * Asserts that two booleans are not equal. Throws an
310      * <tt>AssertionFailedError</tt> if they are equal.
311      */

312     public static void assertNotEquals(boolean expected,
313                                        boolean actual) {
314         assertNotEquals(null, expected, actual);
315     }
316
317     /**
318      * Asserts that two objects do not refer to the same object. Throws an
319      * <tt>AssertionFailedError</tt> if they are equal.
320      */

321     public static void assertNotSame(String JavaDoc message,
322                                      Object JavaDoc expected,
323                                      Object JavaDoc actual) {
324         if (expected == actual) {
325             failSame(message, expected);
326         }
327     }
328
329     /**
330      * Asserts that two objects do not refer to the same object. Throws an
331      * <tt>AssertionFailedError</tt> if they are equal.
332      */

333     public static void assertNotSame(Object JavaDoc expected,
334                                      Object JavaDoc actual) {
335         assertNotSame(null, expected, actual);
336     }
337
338     static private void failFalse(String JavaDoc message) {
339         String JavaDoc formatted = "";
340         if (message != null) {
341             formatted = message + " ";
342         }
343
344         fail(formatted + "expected <false>");
345     }
346
347     static private void failNotEquals(String JavaDoc message,
348                                       Object JavaDoc expected) {
349         String JavaDoc formatted = "";
350         if (message != null) {
351             formatted = message + " ";
352         }
353
354         fail(formatted + "expected not equals to: <" + expected + ">");
355     }
356
357     static private void failSame(String JavaDoc message,
358                                  Object JavaDoc expected) {
359         String JavaDoc formatted = "";
360         if (message != null) {
361             formatted = message + " ";
362         }
363
364         fail(formatted + "expected not same as: <" + expected + ">");
365     }
366
367     /**
368      * Fails a test with the given <tt>Throwable</tt> object causing the
369      * failure.
370      */

371     static public void fail(Throwable JavaDoc cause) {
372         fail(null, cause);
373     }
374
375     /**
376      * Fails a test with the given message and the <tt>Throwable</tt> object
377      * causing the failure.
378      */

379     static public void fail(String JavaDoc message, Throwable JavaDoc cause) {
380         throw new AssertionFailedError(message, cause);
381     }
382
383 }
384
Popular Tags