1. package junit.framework;
  2. /**
  3. * A set of assert methods. Messages are only displayed when an assert fails.
  4. */
  5. public class Assert {
  6. /**
  7. * Protect constructor since it is a static only class
  8. */
  9. protected Assert() {
  10. }
  11. /**
  12. * Asserts that a condition is true. If it isn't it throws
  13. * an AssertionFailedError with the given message.
  14. */
  15. static public void assertTrue(String message, boolean condition) {
  16. if (!condition)
  17. fail(message);
  18. }
  19. /**
  20. * Asserts that a condition is true. If it isn't it throws
  21. * an AssertionFailedError.
  22. */
  23. static public void assertTrue(boolean condition) {
  24. assertTrue(null, condition);
  25. }
  26. /**
  27. * Asserts that a condition is false. If it isn't it throws
  28. * an AssertionFailedError with the given message.
  29. */
  30. static public void assertFalse(String message, boolean condition) {
  31. assertTrue(message, !condition);
  32. }
  33. /**
  34. * Asserts that a condition is false. If it isn't it throws
  35. * an AssertionFailedError.
  36. */
  37. static public void assertFalse(boolean condition) {
  38. assertFalse(null, condition);
  39. }
  40. /**
  41. * Fails a test with the given message.
  42. */
  43. static public void fail(String message) {
  44. throw new AssertionFailedError(message);
  45. }
  46. /**
  47. * Fails a test with no message.
  48. */
  49. static public void fail() {
  50. fail(null);
  51. }
  52. /**
  53. * Asserts that two objects are equal. If they are not
  54. * an AssertionFailedError is thrown with the given message.
  55. */
  56. static public void assertEquals(String message, Object expected, Object actual) {
  57. if (expected == null && actual == null)
  58. return;
  59. if (expected != null && expected.equals(actual))
  60. return;
  61. failNotEquals(message, expected, actual);
  62. }
  63. /**
  64. * Asserts that two objects are equal. If they are not
  65. * an AssertionFailedError is thrown.
  66. */
  67. static public void assertEquals(Object expected, Object actual) {
  68. assertEquals(null, expected, actual);
  69. }
  70. /**
  71. * Asserts that two Strings are equal.
  72. */
  73. static public void assertEquals(String message, String expected, String actual) {
  74. if (expected == null && actual == null)
  75. return;
  76. if (expected != null && expected.equals(actual))
  77. return;
  78. throw new ComparisonFailure(message, expected, actual);
  79. }
  80. /**
  81. * Asserts that two Strings are equal.
  82. */
  83. static public void assertEquals(String expected, String actual) {
  84. assertEquals(null, expected, actual);
  85. }
  86. /**
  87. * Asserts that two doubles are equal concerning a delta. If they are not
  88. * an AssertionFailedError is thrown with the given message. If the expected
  89. * value is infinity then the delta value is ignored.
  90. */
  91. static public void assertEquals(String message, double expected, double actual, double delta) {
  92. // handle infinity specially since subtracting to infinite values gives NaN and the
  93. // the following test fails
  94. if (Double.isInfinite(expected)) {
  95. if (!(expected == actual))
  96. failNotEquals(message, new Double(expected), new Double(actual));
  97. } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false
  98. failNotEquals(message, new Double(expected), new Double(actual));
  99. }
  100. /**
  101. * Asserts that two doubles are equal concerning a delta. If the expected
  102. * value is infinity then the delta value is ignored.
  103. */
  104. static public void assertEquals(double expected, double actual, double delta) {
  105. assertEquals(null, expected, actual, delta);
  106. }
  107. /**
  108. * Asserts that two floats are equal concerning a delta. If they are not
  109. * an AssertionFailedError is thrown with the given message. If the expected
  110. * value is infinity then the delta value is ignored.
  111. */
  112. static public void assertEquals(String message, float expected, float actual, float delta) {
  113. // handle infinity specially since subtracting to infinite values gives NaN and the
  114. // the following test fails
  115. if (Float.isInfinite(expected)) {
  116. if (!(expected == actual))
  117. failNotEquals(message, new Float(expected), new Float(actual));
  118. } else if (!(Math.abs(expected-actual) <= delta))
  119. failNotEquals(message, new Float(expected), new Float(actual));
  120. }
  121. /**
  122. * Asserts that two floats are equal concerning a delta. If the expected
  123. * value is infinity then the delta value is ignored.
  124. */
  125. static public void assertEquals(float expected, float actual, float delta) {
  126. assertEquals(null, expected, actual, delta);
  127. }
  128. /**
  129. * Asserts that two longs are equal. If they are not
  130. * an AssertionFailedError is thrown with the given message.
  131. */
  132. static public void assertEquals(String message, long expected, long actual) {
  133. assertEquals(message, new Long(expected), new Long(actual));
  134. }
  135. /**
  136. * Asserts that two longs are equal.
  137. */
  138. static public void assertEquals(long expected, long actual) {
  139. assertEquals(null, expected, actual);
  140. }
  141. /**
  142. * Asserts that two booleans are equal. If they are not
  143. * an AssertionFailedError is thrown with the given message.
  144. */
  145. static public void assertEquals(String message, boolean expected, boolean actual) {
  146. assertEquals(message, new Boolean(expected), new Boolean(actual));
  147. }
  148. /**
  149. * Asserts that two booleans are equal.
  150. */
  151. static public void assertEquals(boolean expected, boolean actual) {
  152. assertEquals(null, expected, actual);
  153. }
  154. /**
  155. * Asserts that two bytes are equal. If they are not
  156. * an AssertionFailedError is thrown with the given message.
  157. */
  158. static public void assertEquals(String message, byte expected, byte actual) {
  159. assertEquals(message, new Byte(expected), new Byte(actual));
  160. }
  161. /**
  162. * Asserts that two bytes are equal.
  163. */
  164. static public void assertEquals(byte expected, byte actual) {
  165. assertEquals(null, expected, actual);
  166. }
  167. /**
  168. * Asserts that two chars are equal. If they are not
  169. * an AssertionFailedError is thrown with the given message.
  170. */
  171. static public void assertEquals(String message, char expected, char actual) {
  172. assertEquals(message, new Character(expected), new Character(actual));
  173. }
  174. /**
  175. * Asserts that two chars are equal.
  176. */
  177. static public void assertEquals(char expected, char actual) {
  178. assertEquals(null, expected, actual);
  179. }
  180. /**
  181. * Asserts that two shorts are equal. If they are not
  182. * an AssertionFailedError is thrown with the given message.
  183. */
  184. static public void assertEquals(String message, short expected, short actual) {
  185. assertEquals(message, new Short(expected), new Short(actual));
  186. }
  187. /**
  188. * Asserts that two shorts are equal.
  189. */
  190. static public void assertEquals(short expected, short actual) {
  191. assertEquals(null, expected, actual);
  192. }
  193. /**
  194. * Asserts that two ints are equal. If they are not
  195. * an AssertionFailedError is thrown with the given message.
  196. */
  197. static public void assertEquals(String message, int expected, int actual) {
  198. assertEquals(message, new Integer(expected), new Integer(actual));
  199. }
  200. /**
  201. * Asserts that two ints are equal.
  202. */
  203. static public void assertEquals(int expected, int actual) {
  204. assertEquals(null, expected, actual);
  205. }
  206. /**
  207. * Asserts that an object isn't null.
  208. */
  209. static public void assertNotNull(Object object) {
  210. assertNotNull(null, object);
  211. }
  212. /**
  213. * Asserts that an object isn't null. If it is
  214. * an AssertionFailedError is thrown with the given message.
  215. */
  216. static public void assertNotNull(String message, Object object) {
  217. assertTrue(message, object != null);
  218. }
  219. /**
  220. * Asserts that an object is null.
  221. */
  222. static public void assertNull(Object object) {
  223. assertNull(null, object);
  224. }
  225. /**
  226. * Asserts that an object is null. If it is not
  227. * an AssertionFailedError is thrown with the given message.
  228. */
  229. static public void assertNull(String message, Object object) {
  230. assertTrue(message, object == null);
  231. }
  232. /**
  233. * Asserts that two objects refer to the same object. If they are not
  234. * an AssertionFailedError is thrown with the given message.
  235. */
  236. static public void assertSame(String message, Object expected, Object actual) {
  237. if (expected == actual)
  238. return;
  239. failNotSame(message, expected, actual);
  240. }
  241. /**
  242. * Asserts that two objects refer to the same object. If they are not
  243. * the same an AssertionFailedError is thrown.
  244. */
  245. static public void assertSame(Object expected, Object actual) {
  246. assertSame(null, expected, actual);
  247. }
  248. /**
  249. * Asserts that two objects refer to the same object. If they are not
  250. * an AssertionFailedError is thrown with the given message.
  251. */
  252. static public void assertNotSame(String message, Object expected, Object actual) {
  253. if (expected == actual)
  254. failSame(message);
  255. }
  256. /**
  257. * Asserts that two objects refer to the same object. If they are not
  258. * the same an AssertionFailedError is thrown.
  259. */
  260. static public void assertNotSame(Object expected, Object actual) {
  261. assertNotSame(null, expected, actual);
  262. }
  263. static private void failSame(String message) {
  264. String formatted= "";
  265. if (message != null)
  266. formatted= message+" ";
  267. fail(formatted+"expected not same");
  268. }
  269. static private void failNotSame(String message, Object expected, Object actual) {
  270. String formatted= "";
  271. if (message != null)
  272. formatted= message+" ";
  273. fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
  274. }
  275. static private void failNotEquals(String message, Object expected, Object actual) {
  276. fail(format(message, expected, actual));
  277. }
  278. static String format(String message, Object expected, Object actual) {
  279. String formatted= "";
  280. if (message != null)
  281. formatted= message+" ";
  282. return formatted+"expected:<"+expected+"> but was:<"+actual+">";
  283. }
  284. }