PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/chorus/src/main/java/org/chorusbdd/chorus/util/assertion/ChorusAssert.java

http://github.com/Chorus-bdd/Chorus
Java | 332 lines | 151 code | 6 blank | 175 comment | 37 complexity | 63bc038f3b1594282ebb0fd9e5aebee6 MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * Copyright (C) 2000-2012 The Software Conservancy as Trustee.
  3. * All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to
  7. * deal in the Software without restriction, including without limitation the
  8. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. * sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Nothing in this notice shall be deemed to grant any rights to trademarks,
  24. * copyrights, patents, trade secrets or any other intellectual property of the
  25. * licensor or any contributor except as expressly stated herein. No patent
  26. * license is granted separate from the Software, for code that you delete from
  27. * the Software, or for combinations of the Software with other software or
  28. * hardware.
  29. */
  30. package org.chorusbdd.chorus.util.assertion;
  31. /**
  32. * The classes in this package are derived from JUnit which is released under the Common Public License 1.0
  33. * Hence these classes are also available for reuse under CPL 1.0, the text of which is reproduced in the accompanying
  34. * README.txt.
  35. *
  36. * The purpose of including these classes is to prevent all Chorus dependees from having a mandatory
  37. * dependency on JUnit. However, it is suggested that applications where possible do declare a dependency and make use
  38. * of JUnit Assert rather than ChorusAssert, in order to get the most up to date features and fixes from JUnit
  39. * This can be done without affecting Chorus functionality, since there is no requirement in the Chorus
  40. * interpreter that any specific exception type is thrown from Handler code.
  41. */
  42. public class ChorusAssert {
  43. /**
  44. * Protect constructor since it is a static only class
  45. */
  46. protected ChorusAssert() {
  47. }
  48. /**
  49. * Asserts that a condition is true. If it isn't it throws
  50. * an AssertionFailedError with the given message.
  51. */
  52. static public void assertTrue(String message, boolean condition) {
  53. if (!condition)
  54. fail(message);
  55. }
  56. /**
  57. * Asserts that a condition is true. If it isn't it throws
  58. * an AssertionFailedError.
  59. */
  60. static public void assertTrue(boolean condition) {
  61. assertTrue(null, condition);
  62. }
  63. /**
  64. * Asserts that a condition is false. If it isn't it throws
  65. * an AssertionFailedError with the given message.
  66. */
  67. static public void assertFalse(String message, boolean condition) {
  68. assertTrue(message, !condition);
  69. }
  70. /**
  71. * Asserts that a condition is false. If it isn't it throws
  72. * an AssertionFailedError.
  73. */
  74. static public void assertFalse(boolean condition) {
  75. assertFalse(null, condition);
  76. }
  77. /**
  78. * Fails a test with the given message.
  79. */
  80. static public void fail(String message) {
  81. if (message == null) {
  82. throw new ChorusAssertionError();
  83. }
  84. throw new ChorusAssertionError(message);
  85. }
  86. /**
  87. * Fails a test with no message.
  88. */
  89. static public void fail() {
  90. fail(null);
  91. }
  92. /**
  93. * Asserts that two objects are equal. If they are not
  94. * an AssertionFailedError is thrown with the given message.
  95. */
  96. static public void assertEquals(String message, Object expected, Object actual) {
  97. if (expected == null && actual == null)
  98. return;
  99. if (expected != null && expected.equals(actual))
  100. return;
  101. failNotEquals(message, expected, actual);
  102. }
  103. /**
  104. * Asserts that two objects are equal. If they are not
  105. * an AssertionFailedError is thrown.
  106. */
  107. static public void assertEquals(Object expected, Object actual) {
  108. assertEquals(null, expected, actual);
  109. }
  110. /**
  111. * Asserts that two Strings are equal.
  112. */
  113. static public void assertEquals(String message, String expected, String actual) {
  114. if (expected == null && actual == null)
  115. return;
  116. if (expected != null && expected.equals(actual))
  117. return;
  118. String cleanMessage= message == null ? "" : message;
  119. throw new ChorusComparisonFailure(cleanMessage, expected, actual);
  120. }
  121. /**
  122. * Asserts that two Strings are equal.
  123. */
  124. static public void assertEquals(String expected, String actual) {
  125. assertEquals(null, expected, actual);
  126. }
  127. /**
  128. * Asserts that two doubles are equal concerning a delta. If they are not
  129. * an AssertionFailedError is thrown with the given message. If the expected
  130. * value is infinity then the delta value is ignored.
  131. */
  132. static public void assertEquals(String message, double expected, double actual, double delta) {
  133. if (Double.compare(expected, actual) == 0)
  134. return;
  135. if (!(Math.abs(expected-actual) <= delta))
  136. failNotEquals(message, new Double(expected), new Double(actual));
  137. }
  138. /**
  139. * Asserts that two doubles are equal concerning a delta. If the expected
  140. * value is infinity then the delta value is ignored.
  141. */
  142. static public void assertEquals(double expected, double actual, double delta) {
  143. assertEquals(null, expected, actual, delta);
  144. }
  145. /**
  146. * Asserts that two floats are equal concerning a positive delta. If they
  147. * are not an AssertionFailedError is thrown with the given message. If the
  148. * expected value is infinity then the delta value is ignored.
  149. */
  150. static public void assertEquals(String message, float expected, float actual, float delta) {
  151. if (Float.compare(expected, actual) == 0)
  152. return;
  153. if (!(Math.abs(expected - actual) <= delta))
  154. failNotEquals(message, new Float(expected), new Float(actual));
  155. }
  156. /**
  157. * Asserts that two floats are equal concerning a delta. If the expected
  158. * value is infinity then the delta value is ignored.
  159. */
  160. static public void assertEquals(float expected, float actual, float delta) {
  161. assertEquals(null, expected, actual, delta);
  162. }
  163. /**
  164. * Asserts that two longs are equal. If they are not
  165. * an AssertionFailedError is thrown with the given message.
  166. */
  167. static public void assertEquals(String message, long expected, long actual) {
  168. assertEquals(message, new Long(expected), new Long(actual));
  169. }
  170. /**
  171. * Asserts that two longs are equal.
  172. */
  173. static public void assertEquals(long expected, long actual) {
  174. assertEquals(null, expected, actual);
  175. }
  176. /**
  177. * Asserts that two booleans are equal. If they are not
  178. * an AssertionFailedError is thrown with the given message.
  179. */
  180. static public void assertEquals(String message, boolean expected, boolean actual) {
  181. assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
  182. }
  183. /**
  184. * Asserts that two booleans are equal.
  185. */
  186. static public void assertEquals(boolean expected, boolean actual) {
  187. assertEquals(null, expected, actual);
  188. }
  189. /**
  190. * Asserts that two bytes are equal. If they are not
  191. * an AssertionFailedError is thrown with the given message.
  192. */
  193. static public void assertEquals(String message, byte expected, byte actual) {
  194. assertEquals(message, new Byte(expected), new Byte(actual));
  195. }
  196. /**
  197. * Asserts that two bytes are equal.
  198. */
  199. static public void assertEquals(byte expected, byte actual) {
  200. assertEquals(null, expected, actual);
  201. }
  202. /**
  203. * Asserts that two chars are equal. If they are not
  204. * an AssertionFailedError is thrown with the given message.
  205. */
  206. static public void assertEquals(String message, char expected, char actual) {
  207. assertEquals(message, new Character(expected), new Character(actual));
  208. }
  209. /**
  210. * Asserts that two chars are equal.
  211. */
  212. static public void assertEquals(char expected, char actual) {
  213. assertEquals(null, expected, actual);
  214. }
  215. /**
  216. * Asserts that two shorts are equal. If they are not
  217. * an AssertionFailedError is thrown with the given message.
  218. */
  219. static public void assertEquals(String message, short expected, short actual) {
  220. assertEquals(message, new Short(expected), new Short(actual));
  221. }
  222. /**
  223. * Asserts that two shorts are equal.
  224. */
  225. static public void assertEquals(short expected, short actual) {
  226. assertEquals(null, expected, actual);
  227. }
  228. /**
  229. * Asserts that two ints are equal. If they are not
  230. * an AssertionFailedError is thrown with the given message.
  231. */
  232. static public void assertEquals(String message, int expected, int actual) {
  233. assertEquals(message, new Integer(expected), new Integer(actual));
  234. }
  235. /**
  236. * Asserts that two ints are equal.
  237. */
  238. static public void assertEquals(int expected, int actual) {
  239. assertEquals(null, expected, actual);
  240. }
  241. /**
  242. * Asserts that an object isn't null.
  243. */
  244. static public void assertNotNull(Object object) {
  245. assertNotNull(null, object);
  246. }
  247. /**
  248. * Asserts that an object isn't null. If it is
  249. * an AssertionFailedError is thrown with the given message.
  250. */
  251. static public void assertNotNull(String message, Object object) {
  252. assertTrue(message, object != null);
  253. }
  254. /**
  255. * Asserts that an object is null. If it isn't an {@link AssertionError} is
  256. * thrown.
  257. * Message contains: Expected: <null> but was: object
  258. *
  259. * @param object
  260. * Object to check or <code>null</code>
  261. */
  262. static public void assertNull(Object object) {
  263. String message = "Expected: <null> but was: " + String.valueOf(object);
  264. assertNull(message, object);
  265. }
  266. /**
  267. * Asserts that an object is null. If it is not
  268. * an AssertionFailedError is thrown with the given message.
  269. */
  270. static public void assertNull(String message, Object object) {
  271. assertTrue(message, object == null);
  272. }
  273. /**
  274. * Asserts that two objects refer to the same object. If they are not
  275. * an AssertionFailedError is thrown with the given message.
  276. */
  277. static public void assertSame(String message, Object expected, Object actual) {
  278. if (expected == actual)
  279. return;
  280. failNotSame(message, expected, actual);
  281. }
  282. /**
  283. * Asserts that two objects refer to the same object. If they are not
  284. * the same an AssertionFailedError is thrown.
  285. */
  286. static public void assertSame(Object expected, Object actual) {
  287. assertSame(null, expected, actual);
  288. }
  289. /**
  290. * Asserts that two objects do not refer to the same object. If they do
  291. * refer to the same object an AssertionFailedError is thrown with the
  292. * given message.
  293. */
  294. static public void assertNotSame(String message, Object expected, Object actual) {
  295. if (expected == actual)
  296. failSame(message);
  297. }
  298. /**
  299. * Asserts that two objects do not refer to the same object. If they do
  300. * refer to the same object an AssertionFailedError is thrown.
  301. */
  302. static public void assertNotSame(Object expected, Object actual) {
  303. assertNotSame(null, expected, actual);
  304. }
  305. static public void failSame(String message) {
  306. String formatted= "";
  307. if (message != null)
  308. formatted= message+" ";
  309. fail(formatted+"expected not same");
  310. }
  311. static public void failNotSame(String message, Object expected, Object actual) {
  312. String formatted= "";
  313. if (message != null)
  314. formatted= message+" ";
  315. fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
  316. }
  317. static public void failNotEquals(String message, Object expected, Object actual) {
  318. fail(format(message, expected, actual));
  319. }
  320. public static String format(String message, Object expected, Object actual) {
  321. String formatted= "";
  322. if (message != null && message.length() > 0)
  323. formatted= message+" ";
  324. return formatted+"expected:<"+expected+"> but was:<"+actual+">";
  325. }
  326. }