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