KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > ComparableAssert


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 to comparable objects.
59  *
60  * @version $Revision: 1.6 $ $Date: 2003/03/23 01:25:24 $
61  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
62  */

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

68     private ComparableAssert() {
69     }
70
71     /**
72      * Asserts that the <tt>actual</tt> object is lesser than the
73      * <tt>limit</tt> object.
74      * Throws an <tt>AssertionFailedError</tt> if it is greater or equal.
75      */

76     static public void assertLesser(String JavaDoc message,
77                                     Comparable JavaDoc limit,
78                                     Comparable JavaDoc actual) {
79         Assert.assertNotNull(message, limit);
80         Assert.assertNotNull(message, actual);
81         if (limit.compareTo(actual) <= 0) {
82             failLesser(message, limit, actual);
83         }
84     }
85
86     /**
87      * Asserts that the <tt>actual</tt> object is lesser than the
88      * <tt>limit</tt> object.
89      * Throws an <tt>AssertionFailedError</tt> if it is greater or equal.
90      */

91     static public void assertLesser(Comparable JavaDoc limit,
92                                     Comparable JavaDoc actual) {
93         assertLesser(null, limit, actual);
94     }
95
96     /**
97      * Asserts that the <tt>actual</tt> object is not lesser than the
98      * <tt>limit</tt> object.
99      * Throws an <tt>AssertionFailedError</tt> if it is lesser.
100      */

101     static public void assertNotLesser(String JavaDoc message,
102                                        Comparable JavaDoc limit,
103                                        Comparable JavaDoc actual) {
104         Assert.assertNotNull(message, limit);
105         Assert.assertNotNull(message, actual);
106         if (limit.compareTo(actual) > 0) {
107             failNotLesser(message, limit, actual);
108         }
109     }
110
111     /**
112      * Asserts that the <tt>actual</tt> object is not lesser than the
113      * <tt>limit</tt> object.
114      * Throws an <tt>AssertionFailedError</tt> if it is lesser.
115      */

116     static public void assertNotLesser(Comparable JavaDoc limit,
117                                        Comparable JavaDoc actual) {
118         assertNotLesser(null, limit, actual);
119     }
120
121     /**
122      * Asserts that the <tt>expected</tt> and <tt>actual</tt> are equals
123      * (comparables).
124      * Throws an <tt>AssertionFailedError</tt> if it is lesser or equal.
125      */

126     static public void assertEquals(String JavaDoc message,
127                                     Comparable JavaDoc expected,
128                                     Comparable JavaDoc actual) {
129         Assert.assertNotNull(message, expected);
130         Assert.assertNotNull(message, actual);
131         if (expected.compareTo(actual) != 0) {
132             failNotEquals(message, expected, actual);
133         }
134     }
135
136     /**
137      * Asserts that the <tt>expected</tt> and <tt>actual</tt> are equals
138      * (comparables).
139      * Throws an <tt>AssertionFailedError</tt> if it is lesser or equal.
140      */

141     static public void assertEquals(Comparable JavaDoc expected,
142                                     Comparable JavaDoc actual) {
143         assertEquals(null, expected, actual);
144     }
145
146     /**
147      * Asserts that the <tt>expected</tt> and <tt>actual</tt> are not equals
148      * (comparables). Throws an <tt>AssertionFailedError</tt> if it is lesser or
149      * equal.
150      */

151     static public void assertNotEquals(String JavaDoc message,
152                                        Comparable JavaDoc expected,
153                                        Comparable JavaDoc actual) {
154         Assert.assertNotNull(message, expected);
155         Assert.assertNotNull(message, actual);
156         if (expected.compareTo(actual) == 0) {
157             failEquals(message, expected);
158         }
159     }
160
161     /**
162      * Asserts that the <tt>expected</tt> and <tt>actual</tt> are not equals
163      * (comparables). Throws an <tt>AssertionFailedError</tt> if it is lesser or
164      * equal.
165      */

166     static public void assertNotEquals(Comparable JavaDoc expected,
167                                        Comparable JavaDoc actual) {
168         assertNotEquals(null, expected, actual);
169     }
170
171     /**
172      * Asserts that the <tt>actual</tt> object is greater than the
173      * <tt>limit</tt> object.
174      * Throws an <tt>AssertionFailedError</tt> if it is lesser or equal.
175      */

176     static public void assertGreater(String JavaDoc message,
177                                      Comparable JavaDoc limit,
178                                      Comparable JavaDoc actual) {
179         Assert.assertNotNull(message, limit);
180         Assert.assertNotNull(message, actual);
181         if (limit.compareTo(actual) >= 0) {
182             failGreater(message, limit, actual);
183         }
184     }
185
186     /**
187      * Asserts that the <tt>actual</tt> object is greater than the
188      * <tt>limit</tt> object.
189      * Throws an <tt>AssertionFailedError</tt> if it is lesser or equal.
190      */

191     static public void assertGreater(Comparable JavaDoc limit,
192                                      Comparable JavaDoc actual) {
193         assertGreater(null, limit, actual);
194     }
195
196     /**
197      * Asserts that the <tt>actual</tt> object is not greater than the
198      * <tt>limit</tt> object.
199      * Throws an <tt>AssertionFailedError</tt> if it is greater.
200      */

201     static public void assertNotGreater(String JavaDoc message,
202                                         Comparable JavaDoc limit,
203                                         Comparable JavaDoc actual) {
204         Assert.assertNotNull(message, limit);
205         Assert.assertNotNull(message, actual);
206         if (limit.compareTo(actual) < 0) {
207             failNotGreater(message, limit, actual);
208         }
209     }
210
211     /**
212      * Asserts that the <tt>actual</tt> object is not greater than the
213      * <tt>limit</tt> object.
214      * Throws an <tt>AssertionFailedError</tt> if it is greater.
215      */

216     static public void assertNotGreater(Comparable JavaDoc limit,
217                                         Comparable JavaDoc actual) {
218         assertNotGreater(null, limit, actual);
219     }
220
221     static private void failGreater(String JavaDoc message,
222                                       Object JavaDoc limit,
223                                       Object JavaDoc actual) {
224         String JavaDoc formatted = "";
225         if (message != null) {
226             formatted = message + " ";
227         }
228
229         Assert.fail(formatted + "expected greater than:<" +
230                 limit + "> but was:<" + actual + ">");
231     }
232
233     static private void failNotGreater(String JavaDoc message,
234                                          Object JavaDoc limit,
235                                          Object JavaDoc actual) {
236         String JavaDoc formatted = "";
237         if (message != null) {
238             formatted = message + " ";
239         }
240
241         Assert.fail(formatted + "expected not greater than:<" +
242                 limit + "> but was:<" + actual + ">");
243     }
244
245     static private void failLesser(String JavaDoc message,
246                                      Object JavaDoc limit,
247                                      Object JavaDoc actual) {
248         String JavaDoc formatted = "";
249         if (message != null) {
250             formatted = message + " ";
251         }
252
253         Assert.fail(formatted + "expected lesser than:<" +
254                 limit + "> but was:<" + actual + ">");
255     }
256
257     static private void failNotLesser(String JavaDoc message,
258                                         Object JavaDoc limit,
259                                         Object JavaDoc actual) {
260         String JavaDoc formatted = "";
261         if (message != null) {
262             formatted = message + " ";
263         }
264
265         Assert.fail(formatted + "expected not lesser than:<" +
266                 limit + "> but was:<" + actual + ">");
267     }
268
269     static private void failNotEquals(String JavaDoc message,
270                                         Object JavaDoc expected,
271                                         Object JavaDoc actual) {
272         String JavaDoc formatted = "";
273         if (message != null) {
274             formatted = message + " ";
275         }
276
277         Assert.fail(formatted + "expected equals to:<" +
278                 expected + "> but was:<" + actual + ">");
279     }
280
281     static private void failEquals(String JavaDoc message,
282                                      Object JavaDoc expected) {
283         String JavaDoc formatted = "";
284         if (message != null) {
285             formatted = message + " ";
286         }
287
288         Assert.fail(formatted + "expected not equals to:<" +
289                 expected + ">");
290     }
291
292 }
293
Popular Tags