View Javadoc

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