• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng;
2 
3 import org.testng.internal.junit.ArrayAsserts;
4 
5 
6 /**
7  * A set of assert methods.  Messages are only displayed when an assert fails.
8  * Renamed from <CODE>junit.framework.Assert</CODE>.
9  */
10 public class AssertJUnit extends ArrayAsserts {
11 
12   /**
13    * Protect constructor since it is a static only class
14    */
AssertJUnit()15   protected AssertJUnit() {
16   }
17 
18   /**
19    * Asserts that a condition is true. If it isn't it throws
20    * an AssertionFailedError with the given message.
21    */
assertTrue(String message, boolean condition)22   static public void assertTrue(String message, boolean condition) {
23     if(!condition) {
24       fail(message);
25     }
26   }
27 
28   /**
29    * Asserts that a condition is true. If it isn't it throws
30    * an AssertionFailedError.
31    */
assertTrue(boolean condition)32   static public void assertTrue(boolean condition) {
33     assertTrue(null, condition);
34   }
35 
36   /**
37    * Asserts that a condition is false. If it isn't it throws
38    * an AssertionFailedError with the given message.
39    */
assertFalse(String message, boolean condition)40   static public void assertFalse(String message, boolean condition) {
41     assertTrue(message, !condition);
42   }
43 
44   /**
45    * Asserts that a condition is false. If it isn't it throws
46    * an AssertionFailedError.
47    */
assertFalse(boolean condition)48   static public void assertFalse(boolean condition) {
49     assertFalse(null, condition);
50   }
51 
52   /**
53    * Fails a test with the given message.
54    */
fail(String message)55   static public void fail(String message) {
56     if (null == message) {
57       message = "";
58     }
59     throw new AssertionError(message);
60   }
61 
62   /**
63    * Fails a test with no message.
64    */
fail()65   static public void fail() {
66     fail(null);
67   }
68 
69   /**
70    * Asserts that two objects are equal. If they are not
71    * an AssertionFailedError is thrown with the given message.
72    */
assertEquals(String message, Object expected, Object actual)73   static public void assertEquals(String message, Object expected, Object actual) {
74     if((expected == null) && (actual == null)) {
75       return;
76     }
77     if((expected != null) && expected.equals(actual)) {
78       return;
79     }
80     failNotEquals(message, expected, actual);
81   }
82 
83   /**
84    * Asserts that two objects are equal. If they are not
85    * an AssertionFailedError is thrown.
86    */
assertEquals(Object expected, Object actual)87   static public void assertEquals(Object expected, Object actual) {
88     assertEquals(null, expected, actual);
89   }
90 
91   /**
92    * Asserts that two Strings are equal.
93    */
assertEquals(String message, String expected, String actual)94   static public void assertEquals(String message, String expected, String actual) {
95     if((expected == null) && (actual == null)) {
96       return;
97     }
98     if((expected != null) && expected.equals(actual)) {
99       return;
100     }
101     throw new AssertionError(format(message, expected, actual));
102   }
103 
104   /**
105    * Asserts that two Strings are equal.
106    */
assertEquals(String expected, String actual)107   static public void assertEquals(String expected, String actual) {
108     assertEquals(null, expected, actual);
109   }
110 
111   /**
112    * Asserts that two doubles are equal concerning a delta.  If they are not
113    * an AssertionFailedError is thrown with the given message.  If the expected
114    * value is infinity then the delta value is ignored.
115    */
assertEquals(String message, double expected, double actual, double delta)116   static public void assertEquals(String message, double expected, double actual, double delta) {
117 
118     // handle infinity specially since subtracting to infinite values gives NaN and the
119     // the following test fails
120     if(Double.isInfinite(expected)) {
121       if(!(expected == actual)) {
122         failNotEquals(message, expected, actual);
123       }
124     }
125     else if(!(Math.abs(expected - actual) <= delta)) { // Because comparison with NaN always returns false
126       failNotEquals(message, expected, actual);
127     }
128   }
129 
130   /**
131    * Asserts that two doubles are equal concerning a delta. If the expected
132    * value is infinity then the delta value is ignored.
133    */
assertEquals(double expected, double actual, double delta)134   static public void assertEquals(double expected, double actual, double delta) {
135     assertEquals(null, expected, actual, delta);
136   }
137 
138   /**
139    * Asserts that two floats are equal concerning a delta. If they are not
140    * an AssertionFailedError is thrown with the given message.  If the expected
141    * value is infinity then the delta value is ignored.
142    */
assertEquals(String message, float expected, float actual, float delta)143   static public void assertEquals(String message, float expected, float actual, float delta) {
144 
145     // handle infinity specially since subtracting to infinite values gives NaN and the
146     // the following test fails
147     if(Float.isInfinite(expected)) {
148       if(!(expected == actual)) {
149         failNotEquals(message, expected, actual);
150       }
151     }
152     else if(!(Math.abs(expected - actual) <= delta)) {
153       failNotEquals(message, expected, actual);
154     }
155   }
156 
157   /**
158    * Asserts that two floats are equal concerning a delta. If the expected
159    * value is infinity then the delta value is ignored.
160    */
assertEquals(float expected, float actual, float delta)161   static public void assertEquals(float expected, float actual, float delta) {
162     assertEquals(null, expected, actual, delta);
163   }
164 
165   /**
166    * Asserts that two longs are equal. If they are not
167    * an AssertionFailedError is thrown with the given message.
168    */
assertEquals(String message, long expected, long actual)169   static public void assertEquals(String message, long expected, long actual) {
170     assertEquals(message, Long.valueOf(expected), Long.valueOf(actual));
171   }
172 
173   /**
174    * Asserts that two longs are equal.
175    */
assertEquals(long expected, long actual)176   static public void assertEquals(long expected, long actual) {
177     assertEquals(null, expected, actual);
178   }
179 
180   /**
181    * Asserts that two booleans are equal. If they are not
182    * an AssertionFailedError is thrown with the given message.
183    */
assertEquals(String message, boolean expected, boolean actual)184   static public void assertEquals(String message, boolean expected, boolean actual) {
185     assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
186   }
187 
188   /**
189    * Asserts that two booleans are equal.
190    */
assertEquals(boolean expected, boolean actual)191   static public void assertEquals(boolean expected, boolean actual) {
192     assertEquals(null, expected, actual);
193   }
194 
195   /**
196    * Asserts that two bytes are equal. If they are not
197    * an AssertionFailedError is thrown with the given message.
198    */
assertEquals(String message, byte expected, byte actual)199   static public void assertEquals(String message, byte expected, byte actual) {
200     assertEquals(message, Byte.valueOf(expected), Byte.valueOf(actual));
201   }
202 
203   /**
204      * Asserts that two bytes are equal.
205    */
assertEquals(byte expected, byte actual)206   static public void assertEquals(byte expected, byte actual) {
207     assertEquals(null, expected, actual);
208   }
209 
210   /**
211    * Asserts that two chars are equal. If they are not
212    * an AssertionFailedError is thrown with the given message.
213    */
assertEquals(String message, char expected, char actual)214   static public void assertEquals(String message, char expected, char actual) {
215     assertEquals(message, Character.valueOf(expected), Character.valueOf(actual));
216   }
217 
218   /**
219    * Asserts that two chars are equal.
220    */
assertEquals(char expected, char actual)221   static public void assertEquals(char expected, char actual) {
222     assertEquals(null, expected, actual);
223   }
224 
225   /**
226    * Asserts that two shorts are equal. If they are not
227    * an AssertionFailedError is thrown with the given message.
228    */
assertEquals(String message, short expected, short actual)229   static public void assertEquals(String message, short expected, short actual) {
230     assertEquals(message, Short.valueOf(expected), Short.valueOf(actual));
231   }
232 
233   /**
234   * Asserts that two shorts are equal.
235   */
assertEquals(short expected, short actual)236   static public void assertEquals(short expected, short actual) {
237     assertEquals(null, expected, actual);
238   }
239 
240   /**
241    * Asserts that two ints are equal. If they are not
242    * an AssertionFailedError is thrown with the given message.
243    */
assertEquals(String message, int expected, int actual)244   static public void assertEquals(String message, int expected, int actual) {
245     assertEquals(message, Integer.valueOf(expected), Integer.valueOf(actual));
246   }
247 
248   /**
249    * Asserts that two ints are equal.
250   */
assertEquals(int expected, int actual)251   static public void assertEquals(int expected, int actual) {
252     assertEquals(null, expected, actual);
253   }
254 
255   /**
256    * Asserts that an object isn't null.
257    */
assertNotNull(Object object)258   static public void assertNotNull(Object object) {
259     assertNotNull(null, object);
260   }
261 
262   /**
263    * Asserts that an object isn't null. If it is
264    * an AssertionFailedError is thrown with the given message.
265    */
assertNotNull(String message, Object object)266   static public void assertNotNull(String message, Object object) {
267     assertTrue(message, object != null);
268   }
269 
270   /**
271    * Asserts that an object is null.
272    */
assertNull(Object object)273   static public void assertNull(Object object) {
274     assertNull(null, object);
275   }
276 
277   /**
278    * Asserts that an object is null.  If it is not
279    * an AssertionFailedError is thrown with the given message.
280    */
assertNull(String message, Object object)281   static public void assertNull(String message, Object object) {
282     assertTrue(message, object == null);
283   }
284 
285   /**
286    * Asserts that two objects refer to the same object. If they are not
287    * an AssertionFailedError is thrown with the given message.
288    */
assertSame(String message, Object expected, Object actual)289   static public void assertSame(String message, Object expected, Object actual) {
290     if(expected == actual) {
291       return;
292     }
293     failNotSame(message, expected, actual);
294   }
295 
296   /**
297    * Asserts that two objects refer to the same object. If they are not
298    * the same an AssertionFailedError is thrown.
299    */
assertSame(Object expected, Object actual)300   static public void assertSame(Object expected, Object actual) {
301     assertSame(null, expected, actual);
302   }
303 
304   /**
305    * Asserts that two objects refer to the same object. If they are not
306    * an AssertionFailedError is thrown with the given message.
307    */
assertNotSame(String message, Object expected, Object actual)308   static public void assertNotSame(String message, Object expected, Object actual) {
309     if(expected == actual) {
310       failSame(message);
311     }
312   }
313 
314   /**
315    * Asserts that two objects refer to the same object. If they are not
316    * the same an AssertionFailedError is thrown.
317    */
assertNotSame(Object expected, Object actual)318   static public void assertNotSame(Object expected, Object actual) {
319     assertNotSame(null, expected, actual);
320   }
321 
assertEquals(final byte[] expected, final byte[] actual)322   static public void assertEquals(final byte[] expected, final byte[] actual) {
323     assertEquals("", expected, actual);
324   }
325 
assertEquals(final String message, final byte[] expected, final byte[] actual)326   static public void assertEquals(final String message, final byte[] expected, final byte[] actual) {
327     if(expected == actual) {
328         return;
329     }
330     if(null == expected) {
331       fail("expected a null array, but not null found. " + message);
332     }
333     if(null == actual) {
334         fail("expected not null array, but null found. " + message);
335     }
336 
337     assertEquals("arrays don't have the same size. " + message, expected.length, actual.length);
338 
339     for(int i= 0; i < expected.length; i++) {
340         if(expected[i] != actual[i]) {
341             fail("arrays differ firstly at element [" + i +"]; "
342                 + format(message, expected[i], actual[i]));
343         }
344     }
345   }
346 
failSame(String message)347   static private void failSame(String message) {
348     String formatted = "";
349     if(message != null) {
350       formatted = message + " ";
351     }
352     fail(formatted + "expected not same");
353   }
354 
failNotSame(String message, Object expected, Object actual)355   static private void failNotSame(String message, Object expected, Object actual) {
356     String formatted = "";
357     if(message != null) {
358       formatted = message + " ";
359     }
360     fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">");
361   }
362 
failNotEquals(String message, Object expected, Object actual)363   static private void failNotEquals(String message, Object expected, Object actual) {
364     fail(format(message, expected, actual));
365   }
366 
format(String message, Object expected, Object actual)367   static String format(String message, Object expected, Object actual) {
368     String formatted = "";
369     if(message != null) {
370       formatted = message + " ";
371     }
372 
373     return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
374   }
375 }
376