001package junit.framework;
002
003/**
004 * A set of assert methods.  Messages are only displayed when an assert fails.
005 *
006 * @deprecated Please use {@link org.junit.Assert} instead.
007 */
008@Deprecated
009public class Assert {
010    /**
011     * Protect constructor since it is a static only class
012     */
013    protected Assert() {
014    }
015
016    /**
017     * Asserts that a condition is true. If it isn't it throws
018     * an AssertionFailedError with the given message.
019     */
020    public static void assertTrue(String message, boolean condition) {
021        if (!condition) {
022            fail(message);
023        }
024    }
025
026    /**
027     * Asserts that a condition is true. If it isn't it throws
028     * an AssertionFailedError.
029     */
030    public static void assertTrue(boolean condition) {
031        assertTrue(null, condition);
032    }
033
034    /**
035     * Asserts that a condition is false. If it isn't it throws
036     * an AssertionFailedError with the given message.
037     */
038    public static void assertFalse(String message, boolean condition) {
039        assertTrue(message, !condition);
040    }
041
042    /**
043     * Asserts that a condition is false. If it isn't it throws
044     * an AssertionFailedError.
045     */
046    public static void assertFalse(boolean condition) {
047        assertFalse(null, condition);
048    }
049
050    /**
051     * Fails a test with the given message.
052     */
053    public static void fail(String message) {
054        if (message == null) {
055            throw new AssertionFailedError();
056        }
057        throw new AssertionFailedError(message);
058    }
059
060    /**
061     * Fails a test with no message.
062     */
063    public static void fail() {
064        fail(null);
065    }
066
067    /**
068     * Asserts that two objects are equal. If they are not
069     * an AssertionFailedError is thrown with the given message.
070     */
071    public static void assertEquals(String message, Object expected, Object actual) {
072        if (expected == null && actual == null) {
073            return;
074        }
075        if (expected != null && expected.equals(actual)) {
076            return;
077        }
078        failNotEquals(message, expected, actual);
079    }
080
081    /**
082     * Asserts that two objects are equal. If they are not
083     * an AssertionFailedError is thrown.
084     */
085    public static void assertEquals(Object expected, Object actual) {
086        assertEquals(null, expected, actual);
087    }
088
089    /**
090     * Asserts that two Strings are equal.
091     */
092    public static void assertEquals(String message, String expected, String actual) {
093        if (expected == null && actual == null) {
094            return;
095        }
096        if (expected != null && expected.equals(actual)) {
097            return;
098        }
099        String cleanMessage = message == null ? "" : message;
100        throw new ComparisonFailure(cleanMessage, expected, actual);
101    }
102
103    /**
104     * Asserts that two Strings are equal.
105     */
106    public static void assertEquals(String expected, String actual) {
107        assertEquals(null, expected, actual);
108    }
109
110    /**
111     * Asserts that two doubles are equal concerning a delta.  If they are not
112     * an AssertionFailedError is thrown with the given message.  If the expected
113     * value is infinity then the delta value is ignored.
114     */
115    public static void assertEquals(String message, double expected, double actual, double delta) {
116        if (Double.compare(expected, actual) == 0) {
117            return;
118        }
119        if (!(Math.abs(expected - actual) <= delta)) {
120            failNotEquals(message, Double.valueOf(expected), Double.valueOf(actual));
121        }
122    }
123
124    /**
125     * Asserts that two doubles are equal concerning a delta. If the expected
126     * value is infinity then the delta value is ignored.
127     */
128    public static void assertEquals(double expected, double actual, double delta) {
129        assertEquals(null, expected, actual, delta);
130    }
131
132    /**
133     * Asserts that two floats are equal concerning a positive delta. If they
134     * are not an AssertionFailedError is thrown with the given message. If the
135     * expected value is infinity then the delta value is ignored.
136     */
137    public static void assertEquals(String message, float expected, float actual, float delta) {
138        if (Float.compare(expected, actual) == 0) {
139            return;
140        }
141        if (!(Math.abs(expected - actual) <= delta)) {
142            failNotEquals(message, Float.valueOf(expected), Float.valueOf(actual));
143        }
144    }
145
146    /**
147     * Asserts that two floats are equal concerning a delta. If the expected
148     * value is infinity then the delta value is ignored.
149     */
150    public static void assertEquals(float expected, float actual, float delta) {
151        assertEquals(null, expected, actual, delta);
152    }
153
154    /**
155     * Asserts that two longs are equal. If they are not
156     * an AssertionFailedError is thrown with the given message.
157     */
158    public static void assertEquals(String message, long expected, long actual) {
159        assertEquals(message, Long.valueOf(expected), Long.valueOf(actual));
160    }
161
162    /**
163     * Asserts that two longs are equal.
164     */
165    public static void assertEquals(long expected, long actual) {
166        assertEquals(null, expected, actual);
167    }
168
169    /**
170     * Asserts that two booleans are equal. If they are not
171     * an AssertionFailedError is thrown with the given message.
172     */
173    public static void assertEquals(String message, boolean expected, boolean actual) {
174        assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
175    }
176
177    /**
178     * Asserts that two booleans are equal.
179     */
180    public static void assertEquals(boolean expected, boolean actual) {
181        assertEquals(null, expected, actual);
182    }
183
184    /**
185     * Asserts that two bytes are equal. If they are not
186     * an AssertionFailedError is thrown with the given message.
187     */
188    public static void assertEquals(String message, byte expected, byte actual) {
189        assertEquals(message, Byte.valueOf(expected), Byte.valueOf(actual));
190    }
191
192    /**
193     * Asserts that two bytes are equal.
194     */
195    public static void assertEquals(byte expected, byte actual) {
196        assertEquals(null, expected, actual);
197    }
198
199    /**
200     * Asserts that two chars are equal. If they are not
201     * an AssertionFailedError is thrown with the given message.
202     */
203    public static void assertEquals(String message, char expected, char actual) {
204        assertEquals(message, Character.valueOf(expected), Character.valueOf(actual));
205    }
206
207    /**
208     * Asserts that two chars are equal.
209     */
210    public static void assertEquals(char expected, char actual) {
211        assertEquals(null, expected, actual);
212    }
213
214    /**
215     * Asserts that two shorts are equal. If they are not
216     * an AssertionFailedError is thrown with the given message.
217     */
218    public static void assertEquals(String message, short expected, short actual) {
219        assertEquals(message, Short.valueOf(expected), Short.valueOf(actual));
220    }
221
222    /**
223     * Asserts that two shorts are equal.
224     */
225    public static void assertEquals(short expected, short actual) {
226        assertEquals(null, expected, actual);
227    }
228
229    /**
230     * Asserts that two ints are equal. If they are not
231     * an AssertionFailedError is thrown with the given message.
232     */
233    public static void assertEquals(String message, int expected, int actual) {
234        assertEquals(message, Integer.valueOf(expected), Integer.valueOf(actual));
235    }
236
237    /**
238     * Asserts that two ints are equal.
239     */
240    public static void assertEquals(int expected, int actual) {
241        assertEquals(null, expected, actual);
242    }
243
244    /**
245     * Asserts that an object isn't null.
246     */
247    public static void assertNotNull(Object object) {
248        assertNotNull(null, object);
249    }
250
251    /**
252     * Asserts that an object isn't null. If it is
253     * an AssertionFailedError is thrown with the given message.
254     */
255    public static void assertNotNull(String message, Object object) {
256        assertTrue(message, object != null);
257    }
258
259    /**
260     * Asserts that an object is null. If it isn't an {@link AssertionError} is
261     * thrown.
262     * Message contains: Expected: <null> but was: object
263     *
264     * @param object Object to check or <code>null</code>
265     */
266    public static void assertNull(Object object) {
267        if (object != null) {
268            assertNull("Expected: <null> but was: " + object.toString(), object);
269        }
270    }
271
272    /**
273     * Asserts that an object is null.  If it is not
274     * an AssertionFailedError is thrown with the given message.
275     */
276    public static void assertNull(String message, Object object) {
277        assertTrue(message, object == null);
278    }
279
280    /**
281     * Asserts that two objects refer to the same object. If they are not
282     * an AssertionFailedError is thrown with the given message.
283     */
284    public static void assertSame(String message, Object expected, Object actual) {
285        if (expected == actual) {
286            return;
287        }
288        failNotSame(message, expected, actual);
289    }
290
291    /**
292     * Asserts that two objects refer to the same object. If they are not
293     * the same an AssertionFailedError is thrown.
294     */
295    public static void assertSame(Object expected, Object actual) {
296        assertSame(null, expected, actual);
297    }
298
299    /**
300     * Asserts that two objects do not refer to the same object. If they do
301     * refer to the same object an AssertionFailedError is thrown with the
302     * given message.
303     */
304    public static void assertNotSame(String message, Object expected, Object actual) {
305        if (expected == actual) {
306            failSame(message);
307        }
308    }
309
310    /**
311     * Asserts that two objects do not refer to the same object. If they do
312     * refer to the same object an AssertionFailedError is thrown.
313     */
314    public static void assertNotSame(Object expected, Object actual) {
315        assertNotSame(null, expected, actual);
316    }
317
318    public static void failSame(String message) {
319        String formatted = (message != null) ? message + " " : "";
320        fail(formatted + "expected not same");
321    }
322
323    public static void failNotSame(String message, Object expected, Object actual) {
324        String formatted = (message != null) ? message + " " : "";
325        fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">");
326    }
327
328    public static void failNotEquals(String message, Object expected, Object actual) {
329        fail(format(message, expected, actual));
330    }
331
332    public static String format(String message, Object expected, Object actual) {
333        String formatted = "";
334        if (message != null && message.length() > 0) {
335            formatted = message + " ";
336        }
337        return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
338    }
339}