1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose;
17
18 /***
19 * @author jmccrindle
20 */
21 public class Assert {
22
23 public static final String DEFAULT_MESSAGE = "Assertion Failed";
24
25 /***
26 * Private constructor as all methods are static
27 */
28 private Assert() {
29
30 }
31
32 /***
33 * Assert that a test is true
34 * @param msg a message if the assertion if false
35 * @param test the test to test
36 */
37 public static void assertTrue(String msg, boolean test) {
38 if (!test) {
39 throw new DiagnosticAssertionException(msg);
40 }
41 }
42
43 /***
44 * Assert that a test is true
45 * @param test the test to test
46 */
47 public static void assertTrue(boolean test) {
48 if (!test) {
49 throw new DiagnosticAssertionException(DEFAULT_MESSAGE);
50 }
51 }
52
53 /***
54 * Assert that two objects are equal
55 * @param msg a message if the assertion if false
56 * @param toTestAgainst the object to test against
57 * @param test the test object
58 */
59 public static void assertEquals(String msg, Object toTestAgainst, Object test) {
60 if (toTestAgainst == null) {
61 assertNull(msg, test);
62 }
63 assertTrue(msg, toTestAgainst.equals(test));
64 }
65
66 /***
67 * Assert that two objects are equal
68 * @param toTestAgainst the object to test against
69 * @param test the test object
70 */
71 public static void assertEquals(Object toTestAgainst, Object test) {
72 assertEquals(toTestAgainst + "!=" + test, toTestAgainst, test);
73 }
74
75 /***
76 * Assert that an object is null
77 * @param msg a message if the assertion if false
78 * @param test the test object
79 */
80 public static void assertNull(String msg, Object test) {
81 assertTrue(msg, test == null);
82 }
83
84 /***
85 * Assert that an object is null
86 * @param test the test object
87 */
88 public static void assertNull(Object test) {
89 assertTrue("should be null but is " + test, test == null);
90 }
91
92 /***
93 * Assert that an object is not null
94 * @param msg a message if the assertion if false
95 * @param test the test object
96 */
97 public static void assertNotNull(String msg, Object test) {
98 assertTrue(msg, test != null);
99 }
100
101 /***
102 * Assert that an object is not null
103 * @param test the test object
104 */
105 public static void assertNotNull(Object test) {
106 assertTrue("should not be null but is", test == null);
107 }
108
109 }